fixed activity object cache issue

This commit is contained in:
strawmanbobi
2026-01-29 20:41:57 +08:00
parent 0b72adcd3a
commit 8a665776e7
6 changed files with 619 additions and 643 deletions

View File

@@ -32,20 +32,20 @@
<activity
android:name=".ui.activity.CreateActivity"
android:parentActivityName="android.support.v4.app.FragmentActivity"
android:parentActivityName="net.irext.ircontrol.ui.activity.MainActivity"
android:theme="@style/AppTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="android.support.v4.app.FragmentActivity"/>
android:value="net.irext.ircontrol.ui.activity.MainActivity"/>
</activity>
<activity
android:name=".ui.activity.ControlActivity"
android:parentActivityName="android.support.v4.app.FragmentActivity"
android:parentActivityName="net.irext.ircontrol.ui.activity.MainActivity"
android:theme="@style/AppTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="android.support.v4.app.FragmentActivity"/>
android:value="net.irext.ircontrol.ui.activity.MainActivity"/>
</activity>
<provider

View File

@@ -59,13 +59,9 @@ public class ArduinoRemote extends Remote {
private Context mContext = null;
private static ArduinoRemote mInstance;
public static ArduinoRemote getInstance(Context context, IRSocketEmitterCallback callback) {
if (mInstance == null) {
mInstance = new ArduinoRemote(context, callback);
}
return mInstance;
public ArduinoRemote(Context context, IRSocketEmitterCallback callback) {
this.mContext = context;
this.callback = callback;
}
public Context getContext() {
@@ -82,9 +78,8 @@ public class ArduinoRemote extends Remote {
void onResponse(String response);
}
public ArduinoRemote(Context context, IRSocketEmitterCallback callback) {
this.mContext = context;
this.callback = callback;
public static ArduinoRemote getInstance(Context context, IRSocketEmitterCallback callback) {
return new ArduinoRemote(context, callback);
}
public void setCallback(IRSocketEmitterCallback callback) {
@@ -218,7 +213,9 @@ public class ArduinoRemote extends Remote {
} else {
Log.e(TAG, "unexpected response : " + response);
}
callback.onResponse(response);
if (callback != null) {
callback.onResponse(response);
}
}
public int irControl(int category, int subCategory, int keyCode) {
@@ -236,7 +233,9 @@ public class ArduinoRemote extends Remote {
try {
sendControlToEmitter(controlCommand);
} catch (Exception e) {
callback.onResponse(E_INDICATION_FAILED);
if (callback != null) {
callback.onResponse(E_INDICATION_FAILED);
}
}
}).start();

View File

@@ -1,61 +1,349 @@
package net.irext.ircontrol.ui.activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.content.Context;
import android.graphics.Color;
import android.os.*;
import android.util.Log;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.appcompat.content.res.AppCompatResources;
import net.irext.decode.sdk.IRDecode;
import net.irext.decode.sdk.utils.Constants;
import net.irext.ircontrol.R;
import net.irext.ircontrol.ui.fragment.ControlFragment;
import net.irext.ircontrol.bean.RemoteControl;
import net.irext.ircontrol.controller.ArduinoRemote;
import net.irext.ircontrol.controller.PhoneRemote;
import net.irext.ircontrol.controller.base.Remote;
import net.irext.ircontrol.utils.FileUtils;
import net.irext.ircontrol.utils.MessageUtils;
import net.irext.ircontrol.utils.MiscUtils;
import net.irext.ircontrol.utils.ToastUtils;
import java.lang.ref.WeakReference;
/**
* Filename: ControlActivity.java
* Revised: Date: 2017-04-22
* Revision: Revision: 1.0
* <p>
* Description: Control activity containing control fragment
* Description: Control activity containing control panel (without fragment)
* <p>
* Revision log:
* 2017-04-22: created by strawmanbobi
*/
@SuppressWarnings("unused")
public class ControlActivity extends AppCompatActivity {
public class ControlActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = ControlActivity.class.getSimpleName();
public static final String KEY_REMOTE_ID = "KEY_REMOTE_ID";
private static final int VIB_TIME = 60;
private ControlFragment mFragment;
private static final int CMD_GET_REMOTE_CONTROL = 0;
private PhoneRemote mPhoneRemote;
private ArduinoRemote mArduinoRemote;
private MsgHandler mHandler;
private Long mRemoteID;
private RemoteControl mCurrentRemoteControl;
private TextView mTvControlTitle;
private EditText mEtEmitterIp;
private ImageButton mBtnConnect;
private View mVWConnectStatus;
public static final String KEY_REMOTE_ID = "KEY_REMOTE_ID";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_control);
FragmentManager fragmentManager = getSupportFragmentManager();
mHandler = new MsgHandler(this);
mFragment = new ControlFragment();
Bundle bundle = getIntent().getExtras();
mFragment.setArguments(bundle);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.rl_main_layout, mFragment);
transaction.commit();
mArduinoRemote = new ArduinoRemote(this, createCallback());
mPhoneRemote = PhoneRemote.getInstance(this);
initViews();
mRemoteID = getIntent().getLongExtra(KEY_REMOTE_ID, -1L);
if (-1 == mRemoteID) {
Log.d(TAG, "remote ID IS NULL");
} else {
Log.d(TAG, "get remote, ID = " + mRemoteID);
getRemote();
}
}
private void initViews() {
mTvControlTitle = findViewById(R.id.tv_control_title);
ImageButton btnPower = findViewById(R.id.iv_power);
ImageButton btnBack = findViewById(R.id.iv_back);
ImageButton btnHome = findViewById(R.id.iv_home);
ImageButton btnMenu = findViewById(R.id.iv_menu);
ImageButton btnUp = findViewById(R.id.iv_up);
ImageButton btnDown = findViewById(R.id.iv_down);
ImageButton btnLeft = findViewById(R.id.iv_left);
ImageButton btnRight = findViewById(R.id.iv_right);
ImageButton btnOK = findViewById(R.id.iv_ok);
ImageButton btnPlus = findViewById(R.id.iv_plus);
ImageButton btnMinus = findViewById(R.id.iv_minus);
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);
mEtEmitterIp = findViewById(R.id.emitter_ip);
mBtnConnect = findViewById(R.id.btn_connect_emitter);
mVWConnectStatus = findViewById(R.id.vw_connect_status);
mBtnConnect.setOnClickListener(v -> {
vibrate(ControlActivity.this);
String emitterIp = mEtEmitterIp.getText().toString();
if (!MiscUtils.isValidIPv4(emitterIp)) {
Log.e(TAG, "IP address is invalid: " + emitterIp);
ToastUtils.showToast(ControlActivity.this, getString(R.string.input_emitter_ip_address), null);
return;
}
mArduinoRemote.connectToEmitter(emitterIp, String.valueOf(ArduinoRemote.EMITTER_PORT));
});
}
private ArduinoRemote.IRSocketEmitterCallback createCallback() {
return new ArduinoRemote.IRSocketEmitterCallback() {
@Override
public void onConnected() {
runOnUiThread(() -> onEmitterConnected());
}
@Override
public void onDisconnected() {
runOnUiThread(() -> onEmitterDisconnected());
}
@Override
public void onResponse(String response) {
Log.d(TAG, "onResponse: " + response);
onEmitterResponse(response);
}
};
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
protected void onResume() {
super.onResume();
long newRemoteId = getIntent().getLongExtra(KEY_REMOTE_ID, -1L);
if (newRemoteId != mRemoteID) {
mRemoteID = newRemoteId;
if (mRemoteID != -1) {
Log.d(TAG, "get remote, ID = " + mRemoteID);
getRemote();
}
}
return super.onOptionsItemSelected(item);
}
@Override
public void onStop() {
if (null != mFragment) {
mFragment.closeIRBinary();
}
protected void onStop() {
super.onStop();
mArduinoRemote.disconnect();
closeIRBinary();
}
@Override
protected void onDestroy() {
super.onDestroy();
mArduinoRemote.disconnect();
}
private void getRemote() {
new Thread() {
@Override
public void run() {
MessageUtils.postMessage(mHandler, CMD_GET_REMOTE_CONTROL);
}
}.start();
}
private void showRemote() {
Log.d(TAG, "showRemote, remoteID = " + mRemoteID);
mCurrentRemoteControl = RemoteControl.getRemoteControl(mRemoteID);
if (null != mCurrentRemoteControl) {
int categoryID = mCurrentRemoteControl.getCategoryId();
int subCategoryID = mCurrentRemoteControl.getSubCategory();
String categoryName = mCurrentRemoteControl.getCategoryName();
String brandName = mCurrentRemoteControl.getBrandName();
String cityName = mCurrentRemoteControl.getCityName();
String operatorName = mCurrentRemoteControl.getOperatorName();
String remoteName = mCurrentRemoteControl.getRemote();
String remoteControlTitle;
Log.d(TAG, "showRemote, category = " + categoryID + ", subCategory = " + subCategoryID);
String binFileName = FileUtils.binDir + FileUtils.FILE_NAME_PREFIX +
mCurrentRemoteControl.getRemoteMap() + FileUtils.FILE_NAME_EXT;
if (Constants.CategoryID.STB.getValue() == categoryID) {
remoteControlTitle = cityName + operatorName + categoryName + " - " + remoteName;
} else {
remoteControlTitle = brandName + categoryName + " - " + remoteName;
}
mTvControlTitle.setText(remoteControlTitle);
/* decode SDK - load binary file */
int ret = mPhoneRemote.irOpen(binFileName, categoryID, subCategoryID);
Log.d(TAG, "binary opened : " + ret);
}
}
public void closeIRBinary() {
mPhoneRemote.irClose();
}
private void onEmitterConnected() {
Log.d(TAG, "onEmitterConnected, set the status and button color in UI");
mBtnConnect.setImageDrawable(AppCompatResources.getDrawable(this, R.mipmap.button_unlink));
mVWConnectStatus.setBackgroundColor(Color.parseColor("#3FAFFF"));
}
private void onEmitterDisconnected() {
Log.d(TAG, "onEmitterDisconnected, set the status and button color in UI");
ToastUtils.showToast(this, getString(R.string.connect_disconnected), Toast.LENGTH_SHORT);
mBtnConnect.setImageDrawable(AppCompatResources.getDrawable(this, R.mipmap.button_link));
mVWConnectStatus.setBackgroundColor(Color.parseColor("#FF7F7F"));
}
private void processEHello(String response) {
mArduinoRemote.sendHelloToEmitter();
}
private void processEBin(String response) {
long currentRemoteId = getIntent().getLongExtra(KEY_REMOTE_ID, -1L);
Log.d(TAG, "processEBin: current remote ID = " + currentRemoteId);
RemoteControl currentRemoteControl = RemoteControl.getRemoteControl(currentRemoteId);
if (currentRemoteControl != null) {
Log.d(TAG, "processEBin, will send binary for remote control, id = " + currentRemoteId +
", remoteControl.id = " + currentRemoteControl.getID() +
", remoteControl.category = " + currentRemoteControl.getCategoryId());
String binFileName = FileUtils.binDir + FileUtils.FILE_NAME_PREFIX +
currentRemoteControl.getRemoteMap() + FileUtils.FILE_NAME_EXT;
byte []binContent = FileUtils.getByteArrayFromFile(binFileName);
if (null != binContent) {
mArduinoRemote.sendBinToEmitter(binContent,
currentRemoteControl.getCategoryId(),
currentRemoteControl.getSubCategory());
} else {
Log.e(TAG, "emitter sender could not open the binary file");
ToastUtils.showToast(this, getString(R.string.file_could_not_open), Toast.LENGTH_SHORT);
}
}
}
private void processECtrl(String response) {
}
private void processControlResult(String response) {
runOnUiThread(() -> {
if (response.startsWith(ArduinoRemote.E_INDICATION_SUCCESS)) {
ToastUtils.showToast(this, getString(R.string.decode_and_send_success), null);
} else {
ToastUtils.showToast(this, getString(R.string.decode_and_send_failed), null);
}
});
}
private void onEmitterResponse(String response) {
if (response.startsWith(ArduinoRemote.E_RESPONSE_HELLO)) {
processEHello(response);
} else if (response.startsWith(ArduinoRemote.E_RESPONSE_BIN)) {
processEBin(response);
} else if (response.startsWith(ArduinoRemote.E_RESPONSE_CTRL)) {
processECtrl(response);
} else if (response.startsWith(ArduinoRemote.E_INDICATION_SUCCESS) ||
response.startsWith(ArduinoRemote.E_INDICATION_FAILED)) {
processControlResult(response);
} else {
Log.e(TAG, "unexpected response : " + response);
}
}
@Override
public void onClick(View v) {
vibrate(this);
Remote remote = null;
int keyCode = 0;
int result = 0;
int id = v.getId();
if (id == R.id.iv_power) {
keyCode = Remote.KEY_POWER;
} else if (id == R.id.iv_up) {
keyCode = Remote.KEY_UP;
} else if (id == R.id.iv_down) {
keyCode = Remote.KEY_DOWN;
} else if (id == R.id.iv_left) {
keyCode = Remote.KEY_LEFT;
} else if (id == R.id.iv_right) {
keyCode = Remote.KEY_RIGHT;
} else if (id == R.id.iv_ok) {
keyCode = Remote.KEY_OK;
} else if (id == R.id.iv_plus) {
keyCode = Remote.KEY_PLUS;
} else if (id == R.id.iv_minus) {
keyCode = Remote.KEY_MINUS;
} else if (id == R.id.iv_back) {
keyCode = Remote.KEY_BACK;
} else if (id == R.id.iv_home) {
keyCode = Remote.KEY_HOME;
} else if (id == R.id.iv_menu) {
keyCode = Remote.KEY_MENU;
}
if (mArduinoRemote.getConnectionStatus() == ArduinoRemote.EMITTER_WORKING && mCurrentRemoteControl != null) {
mArduinoRemote.irControl(mCurrentRemoteControl.getCategoryId(), mCurrentRemoteControl.getSubCategory(), keyCode);
} else if (mCurrentRemoteControl != null){
result = mPhoneRemote.irControl(mCurrentRemoteControl.getCategoryId(), mCurrentRemoteControl.getSubCategory(), keyCode);
if (0 == result) {
ToastUtils.showToast(this, getString(R.string.decode_and_send_success), null);
} else {
ToastUtils.showToast(this, getString(R.string.decode_and_send_failed), null);
}
}
}
private static class MsgHandler extends Handler {
WeakReference<ControlActivity> mActivity;
MsgHandler(ControlActivity activity) {
super(Looper.getMainLooper());
mActivity = new WeakReference<>(activity);
}
@Override
public void handleMessage(Message msg) {
int cmd = msg.getData().getInt(MessageUtils.KEY_CMD);
ControlActivity activity = mActivity.get();
if (activity != null && cmd == CMD_GET_REMOTE_CONTROL) {
activity.showRemote();
}
}
}
private static void vibrate(Context context) {
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(VIB_TIME);
}
}

View File

@@ -1,318 +0,0 @@
package net.irext.ircontrol.ui.fragment;
import android.content.Context;
import android.graphics.Color;
import android.os.*;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.fragment.app.Fragment;
import net.irext.decode.sdk.IRDecode;
import net.irext.ircontrol.R;
import net.irext.ircontrol.bean.RemoteControl;
import net.irext.ircontrol.controller.ArduinoRemote;
import net.irext.ircontrol.controller.PhoneRemote;
import net.irext.ircontrol.controller.base.Remote;
import net.irext.ircontrol.ui.activity.ControlActivity;
import net.irext.ircontrol.utils.FileUtils;
import net.irext.ircontrol.utils.MessageUtils;
import net.irext.ircontrol.utils.MiscUtils;
import net.irext.ircontrol.utils.ToastUtils;
import java.lang.ref.WeakReference;
/**
* Filename: ControlFragment.java
* Revised: Date: 2017-04-22
* Revision: Revision: 1.0
* <p>
* Description: Control fragment containing control panel
* <p>
* Revision log:
* 2017-04-22: created by strawmanbobi
*/
@SuppressWarnings("unused")
public class ControlFragment extends Fragment implements View.OnClickListener {
private static final String TAG = ControlFragment.class.getSimpleName();
private static final int VIB_TIME = 60;
private static final int CMD_GET_REMOTE_CONTROL = 0;
private PhoneRemote mPhoneRemote;
private ArduinoRemote mArduinoRemote;
private MsgHandler mHandler;
private ControlActivity mParent;
private Long mRemoteID;
private RemoteControl mCurrentRemoteControl;
private EditText mEtEmitterIp;
private ImageButton mBtnConnect;
private View mVWConnectStatus;
public ControlFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mHandler = new MsgHandler(this);
mParent = (ControlActivity)getActivity();
View view = inflater.inflate(R.layout.fragment_control, container, false);
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);
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);
mPhoneRemote = PhoneRemote.getInstance(mParent);
mArduinoRemote = ArduinoRemote.getInstance(mParent, new ArduinoRemote.IRSocketEmitterCallback() {
@Override
public void onConnected() {
onEmitterConnected();
}
@Override
public void onDisconnected() {
onEmitterDisconnected();
}
@Override
public void onResponse(String response) {
onEmitterResponse(response);
}
});
mEtEmitterIp = view.findViewById(R.id.emitter_ip);
mBtnConnect = view.findViewById(R.id.btn_connect_emitter);
mVWConnectStatus = view.findViewById(R.id.vw_connect_status);
mBtnConnect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
vibrate(mParent);
String emitterIp = mEtEmitterIp.getText().toString();
if (!MiscUtils.isValidIPv4(emitterIp)) {
Log.e(TAG, "IP address is invalid: " + emitterIp);
ToastUtils.showToast(mParent, mParent.getString(R.string.input_emitter_ip_address), null);
return;
}
mArduinoRemote.connectToEmitter(emitterIp, String.valueOf(ArduinoRemote.EMITTER_PORT));
}
});
return view;
}
@Override
public void onResume() {
super.onResume();
assert getArguments() != null;
mRemoteID = getArguments().getLong(ControlActivity.KEY_REMOTE_ID, -1L);
if (-1 == mRemoteID) {
Log.d(TAG, "remote ID IS NULL");
} else {
getRemote();
}
}
@Override
public void onStop() {
super.onStop();
mArduinoRemote.disconnect();
}
@Override
public void onDestroyView() {
super.onDestroyView();
mArduinoRemote.disconnect();
}
private void getRemote() {
new Thread() {
@Override
public void run() {
MessageUtils.postMessage(mHandler, CMD_GET_REMOTE_CONTROL);
}
}.start();
}
private void showRemote() {
mCurrentRemoteControl = RemoteControl.getRemoteControl(mRemoteID);
if (null != mCurrentRemoteControl) {
int category = mCurrentRemoteControl.getCategoryId();
String binFileName = FileUtils.binDir + FileUtils.FILE_NAME_PREFIX +
mCurrentRemoteControl.getRemoteMap() + FileUtils.FILE_NAME_EXT;
/* decode SDK - load binary file */
int ret = mPhoneRemote.irOpen(binFileName, category, mCurrentRemoteControl.getSubCategory());
Log.d(TAG, "binary opened : " + ret);
}
}
public void closeIRBinary() {
mPhoneRemote.irClose();
}
private void onEmitterConnected() {
mParent.runOnUiThread(() -> {
Log.d(TAG, "onEmitterConnected, set the status and button color in UI");
mBtnConnect.setImageDrawable(AppCompatResources.getDrawable(mParent, R.mipmap.button_unlink));
mVWConnectStatus.setBackgroundColor(Color.parseColor("#3FAFFF"));
});
}
private void onEmitterDisconnected() {
mParent.runOnUiThread(() -> {
Log.d(TAG, "onEmitterConnected, set the status and button color in UI");
ToastUtils.showToast(mParent, mParent.getString(R.string.connect_disconnected), Toast.LENGTH_SHORT);
mBtnConnect.setImageDrawable(AppCompatResources.getDrawable(mParent, R.mipmap.button_link));
mVWConnectStatus.setBackgroundColor(Color.parseColor("#FF7F7F"));
});
}
private void processEHello(String response) {
mArduinoRemote.sendHelloToEmitter();
}
private void processEBin(String response) {
String binFileName = FileUtils.binDir + FileUtils.FILE_NAME_PREFIX +
mCurrentRemoteControl.getRemoteMap() + FileUtils.FILE_NAME_EXT;
byte []binContent = FileUtils.getByteArrayFromFile(binFileName);
if (null != binContent) {
mArduinoRemote.sendBinToEmitter(binContent, mCurrentRemoteControl.getCategoryId(), mCurrentRemoteControl.getSubCategory());
} else {
Log.e(TAG, "emitter sender could not open the binary file");
ToastUtils.showToast(mParent, mParent.getString(R.string.file_could_not_open), Toast.LENGTH_SHORT);
}
}
private void processECtrl(String response) {
}
private void processControlResult(String response) {
mParent.runOnUiThread(() -> {
if (response.startsWith(ArduinoRemote.E_INDICATION_SUCCESS)) {
ToastUtils.showToast(mParent, mParent.getString(R.string.decode_and_send_success), null);
} else {
ToastUtils.showToast(mParent, mParent.getString(R.string.decode_and_send_failed), null);
}
});
}
private void onEmitterResponse(String response) {
if (response.startsWith(ArduinoRemote.E_RESPONSE_HELLO)) {
processEHello(response);
} else if (response.startsWith(ArduinoRemote.E_RESPONSE_BIN)) {
processEBin(response);
} else if (response.startsWith(ArduinoRemote.E_RESPONSE_CTRL)) {
processECtrl(response);
} else if (response.startsWith(ArduinoRemote.E_INDICATION_SUCCESS) ||
response.startsWith(ArduinoRemote.E_INDICATION_FAILED)) {
processControlResult(response);
} else {
Log.e(TAG, "unexpected response : " + response);
}
}
// control
@Override
public void onClick(View v) {
vibrate(mParent);
Remote remote = null;
int keyCode = 0;
int result = 0;
int id = v.getId();
if (id == R.id.iv_power) {
keyCode = Remote.KEY_POWER;
} else if (id == R.id.iv_up) {
keyCode = Remote.KEY_UP;
} else if (id == R.id.iv_down) {
keyCode = Remote.KEY_DOWN;
} else if (id == R.id.iv_left) {
keyCode = Remote.KEY_LEFT;
} else if (id == R.id.iv_right) {
keyCode = Remote.KEY_RIGHT;
} else if (id == R.id.iv_ok) {
keyCode = Remote.KEY_OK;
} else if (id == R.id.iv_plus) {
keyCode = Remote.KEY_PLUS;
} else if (id == R.id.iv_minus) {
keyCode = Remote.KEY_MINUS;
} else if (id == R.id.iv_back) {
keyCode = Remote.KEY_BACK;
} else if (id == R.id.iv_home) {
keyCode = Remote.KEY_HOME;
} else if (id == R.id.iv_menu) {
keyCode = Remote.KEY_MENU;
}
if (mArduinoRemote.getConnectionStatus() == ArduinoRemote.EMITTER_WORKING) {
mArduinoRemote.irControl(mCurrentRemoteControl.getCategoryId(), mCurrentRemoteControl.getSubCategory(), keyCode);
} else {
result = mPhoneRemote.irControl(mCurrentRemoteControl.getCategoryId(), mCurrentRemoteControl.getSubCategory(), keyCode);
if (0 == result) {
ToastUtils.showToast(mParent, mParent.getString(R.string.decode_and_send_success), null);
} else {
ToastUtils.showToast(mParent, mParent.getString(R.string.decode_and_send_failed), null);
}
}
}
private static class MsgHandler extends Handler {
WeakReference<ControlFragment> mMainFragment;
MsgHandler(ControlFragment fragment) {
super(Looper.getMainLooper());
mMainFragment = new WeakReference<>(fragment);
}
@Override
public void handleMessage(Message msg) {
int cmd = msg.getData().getInt(MessageUtils.KEY_CMD);
ControlFragment controlFragment = mMainFragment.get();
if (cmd == CMD_GET_REMOTE_CONTROL) {
controlFragment.showRemote();
}
}
}
// vibrate on button click in this fragment
private static void vibrate(Context context) {
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(VIB_TIME);
}
}

View File

@@ -3,7 +3,6 @@
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/rl_main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
@@ -14,4 +13,293 @@
android:fitsSystemWindows="true"
tools:context="net.irext.ircontrol.ui.activity.ControlActivity">
<LinearLayout
android:id="@+id/ll_control_title"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical">
<TextView
android:id="@+id/tv_control_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingStart="8dp"
android:paddingEnd="8dp"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:singleLine="false"/>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_control_panel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/ll_control_title"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="8dp"
android:orientation="horizontal">
<ImageButton
android:id="@+id/iv_power"
android:layout_width="match_parent"
android:layout_height="64dp"
android:src="@mipmap/button_power"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:contentDescription="@string/button_power"
android:stateListAnimator="@animator/button_elevation_anim"
android:scaleType="fitCenter"
style="?android:buttonBarButtonStyle"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="4dp"
android:background="#7F7F7F" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:orientation="horizontal">
<ImageButton
android:id="@+id/iv_back"
android:layout_width="match_parent"
android:layout_height="64dp"
android:src="@mipmap/button_back"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:contentDescription="@string/button_back"
android:stateListAnimator="@animator/button_elevation_anim"
style="?android:buttonBarButtonStyle"/>
<ImageButton
android:id="@+id/iv_home"
android:layout_width="match_parent"
android:layout_height="64dp"
android:src="@mipmap/button_home"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:contentDescription="@string/button_home"
android:stateListAnimator="@animator/button_elevation_anim"
style="?android:buttonBarButtonStyle"/>
<ImageButton
android:id="@+id/iv_menu"
android:layout_width="match_parent"
android:layout_height="64dp"
android:src="@mipmap/button_menu"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:contentDescription="@string/button_menu"
android:stateListAnimator="@animator/button_elevation_anim"
style="?android:buttonBarButtonStyle"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_empty_top_left"
android:layout_width="match_parent"
android:layout_height="64dp"
android:layout_weight="1"/>
<ImageButton
android:id="@+id/iv_up"
android:layout_width="match_parent"
android:layout_height="64dp"
android:src="@mipmap/button_up"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:contentDescription="@string/button_up"
android:stateListAnimator="@animator/button_elevation_anim"
style="?android:buttonBarButtonStyle"/>
<TextView
android:id="@+id/tv_empty_top_right"
android:layout_width="match_parent"
android:layout_height="64dp"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:orientation="horizontal">
<ImageButton
android:id="@+id/iv_left"
android:layout_width="match_parent"
android:layout_height="64dp"
android:src="@mipmap/button_left"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:contentDescription="@string/button_left"
android:stateListAnimator="@animator/button_elevation_anim"
style="?android:buttonBarButtonStyle"/>
<ImageButton
android:id="@+id/iv_ok"
android:layout_width="match_parent"
android:layout_height="64dp"
android:src="@mipmap/button_ok"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:contentDescription="@string/button_ok"
android:stateListAnimator="@animator/button_elevation_anim"
style="?android:buttonBarButtonStyle"/>
<ImageButton
android:id="@+id/iv_right"
android:layout_width="match_parent"
android:layout_height="64dp"
android:src="@mipmap/button_right"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:contentDescription="@string/button_right"
android:stateListAnimator="@animator/button_elevation_anim"
style="?android:buttonBarButtonStyle"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_empty_bottom_left"
android:layout_width="match_parent"
android:layout_height="64dp"
android:layout_weight="1"/>
<ImageButton
android:id="@+id/iv_down"
android:layout_width="match_parent"
android:layout_height="64dp"
android:src="@mipmap/button_down"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:contentDescription="@string/button_down"
android:stateListAnimator="@animator/button_elevation_anim"
style="?android:buttonBarButtonStyle"/>
<TextView
android:id="@+id/tv_empty_bottom_right"
android:layout_width="match_parent"
android:layout_height="64dp"
android:layout_weight="1"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="4dp"
android:background="#7F7F7F" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:orientation="horizontal">
<ImageButton
android:id="@+id/iv_minus"
android:layout_width="match_parent"
android:layout_height="64dp"
android:src="@mipmap/button_minus"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:contentDescription="@string/button_minus"
android:stateListAnimator="@animator/button_elevation_anim"
style="?android:buttonBarButtonStyle"/>
<TextView
android:id="@+id/tv_empty_center"
android:layout_width="match_parent"
android:layout_height="64dp"
android:layout_weight="1"/>
<ImageButton
android:id="@+id/iv_plus"
android:layout_width="match_parent"
android:layout_height="64dp"
android:src="@mipmap/button_plus"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:contentDescription="@string/button_plus"
android:stateListAnimator="@animator/button_elevation_anim"
style="?android:buttonBarButtonStyle"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true">
<View
android:id="@+id/vw_connect_status"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_margin="4dp"
android:background="#FF3F3F"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="2dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
tools:ignore="UselessParent">
<TextView
android:layout_width="wrap_content"
android:layout_height="60dp"
android:gravity="center_vertical"
android:textSize="18sp"
android:paddingStart="8dp"
android:paddingEnd="8dp"
android:text="@string/emitter_ip">
</TextView>
<EditText
android:id="@+id/emitter_ip"
android:layout_width="0dp"
android:autofillHints=""
android:gravity="center_vertical"
android:textSize="18sp"
android:inputType="text"
android:text="@string/default_ip"
android:layout_height="60dp"
android:layout_weight="1">
</EditText>
<ImageButton
android:id="@+id/btn_connect_emitter"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="12dp"
android:src="@mipmap/button_link"
android:scaleType="fitCenter"
android:contentDescription="@string/connect"
android:stateListAnimator="@animator/button_elevation_anim"
style="?android:buttonBarButtonStyle"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="4dp"
android:background="#7F7F7F"/>
</LinearLayout>
</RelativeLayout>

View File

@@ -1,281 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/sv_control"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/ll_control_panel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="8dp"
android:orientation="horizontal">
<ImageButton
android:id="@+id/iv_power"
android:layout_width="match_parent"
android:layout_height="64dp"
android:src="@mipmap/button_power"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:contentDescription="@string/button_power"
android:stateListAnimator="@animator/button_elevation_anim"
android:scaleType="fitCenter"
style="?android:buttonBarButtonStyle"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="4dp"
android:background="#7F7F7F" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:orientation="horizontal">
<ImageButton
android:id="@+id/iv_back"
android:layout_width="match_parent"
android:layout_height="64dp"
android:src="@mipmap/button_back"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:contentDescription="@string/button_back"
android:stateListAnimator="@animator/button_elevation_anim"
style="?android:buttonBarButtonStyle"/>
<ImageButton
android:id="@+id/iv_home"
android:layout_width="match_parent"
android:layout_height="64dp"
android:src="@mipmap/button_home"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:contentDescription="@string/button_home"
android:stateListAnimator="@animator/button_elevation_anim"
style="?android:buttonBarButtonStyle"/>
<ImageButton
android:id="@+id/iv_menu"
android:layout_width="match_parent"
android:layout_height="64dp"
android:src="@mipmap/button_menu"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:contentDescription="@string/button_menu"
android:stateListAnimator="@animator/button_elevation_anim"
style="?android:buttonBarButtonStyle"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_empty_top_left"
android:layout_width="match_parent"
android:layout_height="64dp"
android:layout_weight="1"/>
<ImageButton
android:id="@+id/iv_up"
android:layout_width="match_parent"
android:layout_height="64dp"
android:src="@mipmap/button_up"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:contentDescription="@string/button_up"
android:stateListAnimator="@animator/button_elevation_anim"
style="?android:buttonBarButtonStyle"/>
<TextView
android:id="@+id/tv_empty_top_right"
android:layout_width="match_parent"
android:layout_height="64dp"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:orientation="horizontal">
<ImageButton
android:id="@+id/iv_left"
android:layout_width="match_parent"
android:layout_height="64dp"
android:src="@mipmap/button_left"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:contentDescription="@string/button_left"
android:stateListAnimator="@animator/button_elevation_anim"
style="?android:buttonBarButtonStyle"/>
<ImageButton
android:id="@+id/iv_ok"
android:layout_width="match_parent"
android:layout_height="64dp"
android:src="@mipmap/button_ok"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:contentDescription="@string/button_ok"
android:stateListAnimator="@animator/button_elevation_anim"
style="?android:buttonBarButtonStyle"/>
<ImageButton
android:id="@+id/iv_right"
android:layout_width="match_parent"
android:layout_height="64dp"
android:src="@mipmap/button_right"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:contentDescription="@string/button_right"
android:stateListAnimator="@animator/button_elevation_anim"
style="?android:buttonBarButtonStyle"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_empty_bottom_left"
android:layout_width="match_parent"
android:layout_height="64dp"
android:layout_weight="1"/>
<ImageButton
android:id="@+id/iv_down"
android:layout_width="match_parent"
android:layout_height="64dp"
android:src="@mipmap/button_down"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:contentDescription="@string/button_down"
android:stateListAnimator="@animator/button_elevation_anim"
style="?android:buttonBarButtonStyle"/>
<TextView
android:id="@+id/tv_empty_bottom_right"
android:layout_width="match_parent"
android:layout_height="64dp"
android:layout_weight="1"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="4dp"
android:background="#7F7F7F" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:orientation="horizontal">
<ImageButton
android:id="@+id/iv_minus"
android:layout_width="match_parent"
android:layout_height="64dp"
android:src="@mipmap/button_minus"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:contentDescription="@string/button_minus"
android:stateListAnimator="@animator/button_elevation_anim"
style="?android:buttonBarButtonStyle"/>
<TextView
android:id="@+id/tv_empty_center"
android:layout_width="match_parent"
android:layout_height="64dp"
android:layout_weight="1"/>
<ImageButton
android:id="@+id/iv_plus"
android:layout_width="match_parent"
android:layout_height="64dp"
android:src="@mipmap/button_plus"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:contentDescription="@string/button_plus"
android:stateListAnimator="@animator/button_elevation_anim"
style="?android:buttonBarButtonStyle"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true">
<View
android:id="@+id/vw_connect_status"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_margin="4dp"
android:background="#FF3F3F"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="2dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
tools:ignore="UselessParent">
<TextView
android:layout_width="wrap_content"
android:layout_height="60dp"
android:gravity="center_vertical"
android:textSize="18sp"
android:paddingStart="8dp"
android:paddingEnd="8dp"
android:text="@string/emitter_ip">
</TextView>
<EditText
android:id="@+id/emitter_ip"
android:layout_width="0dp"
android:autofillHints=""
android:gravity="center_vertical"
android:textSize="18sp"
android:inputType="text"
android:text="@string/default_ip"
android:layout_height="60dp"
android:layout_weight="1">
</EditText>
<ImageButton
android:id="@+id/btn_connect_emitter"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="12dp"
android:src="@mipmap/button_link"
android:scaleType="fitCenter"
android:contentDescription="@string/connect"
android:stateListAnimator="@animator/button_elevation_anim"
style="?android:buttonBarButtonStyle"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="4dp"
android:background="#7F7F7F"/>
</LinearLayout>
</RelativeLayout>