diff --git a/android-example/app/build.gradle b/android-example/app/build.gradle index 21930da..cd8247c 100644 --- a/android-example/app/build.gradle +++ b/android-example/app/build.gradle @@ -3,6 +3,7 @@ apply plugin: 'com.android.application' android { defaultConfig { applicationId "net.irext.ircontrol" + compileSdk 36 targetSdkVersion 36 minSdkVersion 26 versionCode 7 diff --git a/android-example/app/src/main/java/net/irext/ircontrol/ui/fragment/ControlFragment.java b/android-example/app/src/main/java/net/irext/ircontrol/ui/fragment/ControlFragment.java index 3c05038..ac9ccf4 100644 --- a/android-example/app/src/main/java/net/irext/ircontrol/ui/fragment/ControlFragment.java +++ b/android-example/app/src/main/java/net/irext/ircontrol/ui/fragment/ControlFragment.java @@ -1,11 +1,10 @@ package net.irext.ircontrol.ui.fragment; import android.content.Context; +import android.graphics.Color; import android.hardware.ConsumerIrManager; -import android.os.Bundle; -import android.os.Handler; -import android.os.Message; -import android.os.Vibrator; +import android.net.InetAddresses; +import android.os.*; import android.util.Log; import android.view.LayoutInflater; import android.view.View; @@ -29,7 +28,6 @@ import java.io.InputStreamReader; import java.io.PrintWriter; import java.lang.ref.WeakReference; import java.net.Socket; -import java.net.UnknownHostException; /** * Filename: ControlFragment.java @@ -76,6 +74,11 @@ public class ControlFragment extends Fragment implements View.OnClickListener { // define the single instance of IRDecode private IRDecode mIRDecode; + private CheckBox mCbUseEmitter; + private EditText mEtEmitterIp; + private ImageButton mBtnConnect; + private TextView mTvConnectionStatus; + public ControlFragment() { } @@ -89,44 +92,48 @@ public class ControlFragment extends Fragment implements View.OnClickListener { mParent = (ControlActivity)getActivity(); View view = inflater.inflate(R.layout.fragment_control, container, false); - ImageButton mBtnPower = view.findViewById(R.id.iv_power); - ImageButton mBtnBack = view.findViewById(R.id.iv_back); - ImageButton mBtnHome = view.findViewById(R.id.iv_home); - ImageButton mBtnMenu = view.findViewById(R.id.iv_menu); - ImageButton mBtnUp = view.findViewById(R.id.iv_up); - ImageButton mBtnDown = view.findViewById(R.id.iv_down); - ImageButton mBtnLeft = view.findViewById(R.id.iv_left); - ImageButton mBtnRight = view.findViewById(R.id.iv_right); - ImageButton mBtnOK = view.findViewById(R.id.iv_ok); - ImageButton mBtnPlus = view.findViewById(R.id.iv_plus); - ImageButton mBtnMinus = view.findViewById(R.id.iv_minus); + ImageButton btnPower = view.findViewById(R.id.iv_power); + ImageButton btnBack = view.findViewById(R.id.iv_back); + ImageButton btnHome = view.findViewById(R.id.iv_home); + ImageButton btnMenu = view.findViewById(R.id.iv_menu); + ImageButton btnUp = view.findViewById(R.id.iv_up); + ImageButton btnDown = view.findViewById(R.id.iv_down); + ImageButton btnLeft = view.findViewById(R.id.iv_left); + ImageButton btnRight = view.findViewById(R.id.iv_right); + ImageButton btnOK = view.findViewById(R.id.iv_ok); + ImageButton btnPlus = view.findViewById(R.id.iv_plus); + ImageButton btnMinus = view.findViewById(R.id.iv_minus); - mBtnPower.setOnClickListener(this); - mBtnBack.setOnClickListener(this); - mBtnHome.setOnClickListener(this); - mBtnMenu.setOnClickListener(this); - mBtnUp.setOnClickListener(this); - mBtnDown.setOnClickListener(this); - mBtnLeft.setOnClickListener(this); - mBtnRight.setOnClickListener(this); - mBtnOK.setOnClickListener(this); - mBtnPlus.setOnClickListener(this); - mBtnMinus.setOnClickListener(this); + btnPower.setOnClickListener(this); + btnBack.setOnClickListener(this); + btnHome.setOnClickListener(this); + btnMenu.setOnClickListener(this); + btnUp.setOnClickListener(this); + btnDown.setOnClickListener(this); + btnLeft.setOnClickListener(this); + btnRight.setOnClickListener(this); + btnOK.setOnClickListener(this); + btnPlus.setOnClickListener(this); + btnMinus.setOnClickListener(this); - CheckBox mCbUseEmitter = view.findViewById(R.id.cb_use_emitter); - EditText mEtEmitterIp = view.findViewById(R.id.emitter_ip); - ImageButton mBtnConnect = view.findViewById(R.id.btn_connection_status); + mCbUseEmitter = view.findViewById(R.id.cb_use_emitter); + mEtEmitterIp = view.findViewById(R.id.emitter_ip); + mBtnConnect = view.findViewById(R.id.btn_connect_emitter); + mTvConnectionStatus = view.findViewById(R.id.tv_connection_status); mBtnConnect.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + vibrate(mParent); String emitterIp = mEtEmitterIp.getText().toString(); - if (android.net.InetAddresses.isNumericAddress(emitterIp)) { - connectToEmitter(emitterIp, String.valueOf(EMITTER_PORT)); - } else { - ToastUtils.showToast(mParent, mParent.getString(R.string.input_emitter_ip_address), null); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + if (!InetAddresses.isNumericAddress(emitterIp)) { + ToastUtils.showToast(mParent, mParent.getString(R.string.input_emitter_ip_address), null); + return; + } } + connectToEmitter(emitterIp, String.valueOf(EMITTER_PORT)); } }); @@ -232,6 +239,14 @@ public class ControlFragment extends Fragment implements View.OnClickListener { return mIRDecode.decodeBinary(inputKeyCode, acStatus, 0); } + private void onEmitterConnected() { + emitterConnected = 1; + mParent.runOnUiThread(() -> { + mBtnConnect.setImageDrawable(mParent.getDrawable(R.mipmap.button_unlink)); + mTvConnectionStatus.setText(mParent.getString(R.string.status_connected)); + mTvConnectionStatus.setTextColor(Color.parseColor("#7F7FFF")); + }); + } private void onEmitterDisconnected() { if (1 == emitterConnected) { Log.d(TAG, "emitter disconnected"); @@ -245,6 +260,9 @@ public class ControlFragment extends Fragment implements View.OnClickListener { } else { ToastUtils.showToast(mParent, mParent.getString(R.string.connect_disconnected), Toast.LENGTH_SHORT); } + mBtnConnect.setImageDrawable(mParent.getDrawable(R.mipmap.button_link)); + mTvConnectionStatus.setText(mParent.getString(R.string.status_not_connected)); + mTvConnectionStatus.setTextColor(Color.parseColor("#FF7F7F")); }); emitterConnected = 0; @@ -274,7 +292,7 @@ public class ControlFragment extends Fragment implements View.OnClickListener { try { emitterConn = new Socket(ipAddress, Integer.parseInt(port)); emitterConn.setKeepAlive(true); - emitterConnected = 1; + onEmitterConnected(); BufferedReader in = new BufferedReader(new InputStreamReader(emitterConn.getInputStream())); String line; while ((line = in.readLine()) != null) { @@ -332,7 +350,9 @@ public class ControlFragment extends Fragment implements View.OnClickListener { decodedValue += ","; } Log.d(TAG, "decodedValue : " + decodedValue); - sendDecodedToEmitter(decodedValue); + if (1 == emitterConnected) { + sendDecodedToEmitter(decodedValue); + } // send decoded integer array to IR emitter ConsumerIrManager irEmitter = (ConsumerIrManager) mParent.getSystemService(Context.CONSUMER_IR_SERVICE); diff --git a/android-example/app/src/main/res/animator/button_elevation_anim.xml b/android-example/app/src/main/res/animator/button_elevation_anim.xml new file mode 100644 index 0000000..2971252 --- /dev/null +++ b/android-example/app/src/main/res/animator/button_elevation_anim.xml @@ -0,0 +1,16 @@ + + + + + + + + \ No newline at end of file diff --git a/android-example/app/src/main/res/drawable/button_background.xml b/android-example/app/src/main/res/drawable/button_background.xml new file mode 100644 index 0000000..acd450a --- /dev/null +++ b/android-example/app/src/main/res/drawable/button_background.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/android-example/app/src/main/res/layout/fragment_control.xml b/android-example/app/src/main/res/layout/fragment_control.xml index 9af7034..bfa9dd4 100644 --- a/android-example/app/src/main/res/layout/fragment_control.xml +++ b/android-example/app/src/main/res/layout/fragment_control.xml @@ -26,6 +26,7 @@ android:paddingTop="16dp" android:paddingBottom="16dp" android:contentDescription="@string/button_power" + android:stateListAnimator="@animator/button_elevation_anim" android:scaleType="fitCenter" style="?android:buttonBarButtonStyle"/> @@ -49,6 +50,7 @@ android:layout_weight="1" android:scaleType="fitCenter" android:contentDescription="@string/button_back" + android:stateListAnimator="@animator/button_elevation_anim" style="?android:buttonBarButtonStyle"/> @@ -94,6 +98,7 @@ android:layout_weight="1" android:scaleType="fitCenter" android:contentDescription="@string/button_up" + android:stateListAnimator="@animator/button_elevation_anim" style="?android:buttonBarButtonStyle"/> @@ -162,6 +170,7 @@ android:layout_weight="1" android:scaleType="fitCenter" android:contentDescription="@string/button_down" + android:stateListAnimator="@animator/button_elevation_anim" style="?android:buttonBarButtonStyle"/> @@ -220,15 +231,30 @@ android:layout_height="1dp" android:layout_margin="4dp" android:background="#7F7F7F"/> - + android:layout_height="wrap_content" + android:orientation="horizontal"> + + + 该设备不支持红外发射, 请连接外部模块进行发射 发射端 IP - 192.168.1.100 + 192.168.2.85 连接 使用发射器 请输入接收器的 IP 地址 连接失败 - 断开连接 + 连接已断开 + 未连接 + 已连接 diff --git a/android-example/app/src/main/res/values/strings.xml b/android-example/app/src/main/res/values/strings.xml index af712f4..5933e21 100644 --- a/android-example/app/src/main/res/values/strings.xml +++ b/android-example/app/src/main/res/values/strings.xml @@ -33,11 +33,13 @@ IR emitter is not supported in this device, please try with external emitters Emitter IP - 192.168.1.100 + 192.168.2.85 connect Use Emitter Please input IP address of emitter Connect failed - Disconnected + The connection has lost + Disconnected + Connected