Skip to content
This repository has been archived by the owner on Sep 17, 2023. It is now read-only.

Commit

Permalink
Fixing bugs where USB serial connections are not initiated when the a…
Browse files Browse the repository at this point in the history
…pp is started by plugging in the Chameleon device
  • Loading branch information
maxieds committed Oct 20, 2020
1 parent cfb1a12 commit 43693aa
Show file tree
Hide file tree
Showing 26 changed files with 480 additions and 448 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ android {
appRoundIcon : "@mipmap/chameleon_app_icon_round",
appTheme : "@style/AppThemeGreen",
appDebug : "false",
installLocation : "internalOnly"
installLocation : "preferExternal" // "internalOnly"
]
}
paid {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,24 +409,24 @@ public void run() {
LiveLoggerActivity.getLiveLoggerInstance().runOnUiThread(new Runnable() {
public void run() {
try {
((TextView) LiveLoggerActivity.getLiveLoggerInstance().findViewById(R.id.deviceConfigText)).setText(CONFIG);
((TextView) LiveLoggerActivity.getContentView(R.id.deviceConfigText)).setText(CONFIG);
String formattedUID = Utils.formatUIDString(UID, ":");
((TextView) LiveLoggerActivity.getLiveLoggerInstance().findViewById(R.id.deviceConfigUID)).setText(formattedUID);
((TextView) LiveLoggerActivity.getContentView(R.id.deviceConfigUID)).setText(formattedUID);
String subStats1 = String.format(Locale.ENGLISH, "REV%s|MEM-%dK|LOG-%s-%dK", ChameleonIO.REVE_BOARD ? "E" : "G", round(MEMSIZE / 1024), LOGMODE, round(LOGSIZE / 1024));
((TextView) LiveLoggerActivity.getLiveLoggerInstance().findViewById(R.id.deviceStats1)).setText(subStats1);
((TextView) LiveLoggerActivity.getContentView(R.id.deviceStats1)).setText(subStats1);
String subStats2 = String.format(Locale.ENGLISH, "SLOT-%d|%s|FLD-%s|CHRG-%s", DIP_SETTING, READONLY ? "RO" : "RW", FIELD ? "1" : "0", CHARGING ? "1" : "0");
((TextView) LiveLoggerActivity.getLiveLoggerInstance().findViewById(R.id.deviceStats2)).setText(subStats2);
((TextView) LiveLoggerActivity.getContentView(R.id.deviceStats2)).setText(subStats2);
String subStats3 = String.format(Locale.ENGLISH, "THRS-%dmv|TMT-%s", THRESHOLD, TIMEOUT.replace(" ", ""));
((TextView) LiveLoggerActivity.getLiveLoggerInstance().findViewById(R.id.deviceStats3)).setText(subStats3);
SeekBar thresholdSeekbar = (SeekBar) LiveLoggerActivity.getLiveLoggerInstance().findViewById(R.id.thresholdSeekbar);
((TextView) LiveLoggerActivity.getContentView(R.id.deviceStats3)).setText(subStats3);
SeekBar thresholdSeekbar = (SeekBar) LiveLoggerActivity.getContentView(R.id.thresholdSeekbar);
if (thresholdSeekbar != null) {
thresholdSeekbar.setProgress(THRESHOLD);
((TextView) LiveLoggerActivity.getLiveLoggerInstance().findViewById(R.id.thresholdSeekbarValueText)).setText(String.format(Locale.ENGLISH, "% 5d mV", THRESHOLD));
((TextView) LiveLoggerActivity.getContentView(R.id.thresholdSeekbarValueText)).setText(String.format(Locale.ENGLISH, "% 5d mV", THRESHOLD));
}
SeekBar timeoutSeekbar = (SeekBar) LiveLoggerActivity.getLiveLoggerInstance().findViewById(R.id.cmdTimeoutSeekbar);
SeekBar timeoutSeekbar = (SeekBar) LiveLoggerActivity.getContentView(R.id.cmdTimeoutSeekbar);
if (thresholdSeekbar != null) {
thresholdSeekbar.setProgress(THRESHOLD);
((TextView) LiveLoggerActivity.getLiveLoggerInstance().findViewById(R.id.cmdTimeoutSeekbarValueText)).setText(String.format(Locale.ENGLISH, "% 4d (x128) ms", TIMEOUT));
((TextView) LiveLoggerActivity.getContentView(R.id.cmdTimeoutSeekbarValueText)).setText(String.format(Locale.ENGLISH, "% 4d (x128) ms", TIMEOUT));
}
} catch (Exception ex) {
ex.printStackTrace();
Expand Down Expand Up @@ -470,7 +470,6 @@ public static SerialRespCode executeChameleonMiniCommand(String rawCmd, int time
Log.i(TAG, "serial port is null executing command");
return null;
}
Log.i(TAG, "sending data buffer");
serialPort.sendDataBuffer(sendBuf);
return OK;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ This program (The Chameleon Mini Live Debugger) is free software written by
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
Expand All @@ -45,6 +46,7 @@ This program (The Chameleon Mini Live Debugger) is free software written by
import android.widget.TextView;
import android.widget.Toolbar;

import androidx.annotation.IdRes;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
Expand Down Expand Up @@ -81,6 +83,46 @@ public static LiveLoggerActivity getLiveLoggerInstance() {
return (LiveLoggerActivity) runningActivity;
}

private static View liveLoggerActivityMainContentView = null;

public static View getContentView(@IdRes int viewResId) {
View mainContentView = LiveLoggerActivity.getLiveLoggerInstance().findViewById(android.R.id.content);
if(mainContentView == null || mainContentView.findViewById(viewResId) == null) {
mainContentView = LiveLoggerActivity.getLiveLoggerInstance().findViewById(android.R.id.content).getRootView();
if (mainContentView == null || mainContentView.findViewById(viewResId) == null) {
mainContentView = LiveLoggerActivity.getLiveLoggerInstance().getWindow().getDecorView().findViewById(android.R.id.content);
if (mainContentView == null || mainContentView.findViewById(viewResId) == null) {
mainContentView = LiveLoggerActivity.getLiveLoggerInstance().getWindow().getDecorView().getRootView();
if (mainContentView == null || mainContentView.findViewById(viewResId) == null) {
mainContentView = LiveLoggerActivity.getLiveLoggerInstance().getWindow().getDecorView();
if (mainContentView == null || mainContentView.findViewById(viewResId) == null) {
mainContentView = LiveLoggerActivity.getInstance().findViewById(viewResId);
if (mainContentView == null) {
mainContentView = liveLoggerActivityMainContentView;
if (mainContentView != null) {
Log.i(TAG, "liveLoggerActivityMainContentView");
return mainContentView.findViewById(viewResId);
} else {
return null;
}
}
Log.i(TAG, "NONE -> findViewById(viewResId)");
return mainContentView;
}
Log.i(TAG, "getWindow().getDecorView()");
return mainContentView.findViewById(viewResId);
}
Log.i(TAG, "findViewById(android.R.id.content).getRootView()");
return mainContentView.findViewById(viewResId);
}
Log.i(TAG, "getWindow().getDecorView().findViewById(android.R.id.content)");
return mainContentView.findViewById(viewResId);
}
}
Log.i(TAG, "findViewById(android.R.id.content)");
return mainContentView.findViewById(viewResId);
}

public static int getSelectedTab() { return selectedTab; }
public static void setSelectedTab(int tabIdx) {
if (selectedTab < 0) {
Expand Down Expand Up @@ -154,6 +196,7 @@ public void uncaughtException(Thread paramThread, Throwable paramExcpt) {
private static BroadcastReceiver serialIOActionReceiver = null;
private static IntentFilter serialIOActionFilter = null;
private static boolean serialIOReceiversRegistered = false;
private static boolean serialUSBDeviceSettingsNeedUpdate = true;

protected void reconfigureSerialIODevices() {
if(ChameleonSettings.serialIOPorts == null) {
Expand Down Expand Up @@ -213,15 +256,19 @@ public void onReceive(Context context, Intent intent) {
Runnable configDeviceRunnable = new Runnable() {
public void run() {
ChameleonIO.detectChameleonType();
ChameleonPeripherals.actionButtonRestorePeripheralDefaults(null);
TabFragment.UITAB_DATA[TabFragment.TAB_TOOLS].changeMenuItemDisplay(TAB_TOOLS_MITEM_SLOTS, true);
TabFragment.UITAB_DATA[TabFragment.TAB_CONFIG].changeMenuItemDisplay(TabFragment.TAB_CONFIG_MITEM_LOGGING, true);
ChameleonPeripherals.actionButtonRestorePeripheralDefaults(null);
ChameleonIO.DeviceStatusSettings.updateAllStatusAndPost(false);
ChameleonIO.DeviceStatusSettings.startPostingStats(400);
ChameleonIO.DeviceStatusSettings.updateAllStatusAndPost(false); /* Make sure the device returned the correct data to display */
ChameleonIO.DeviceStatusSettings.startPostingStats(0);
serialUSBDeviceSettingsNeedUpdate = false;
}
};
ChameleonIO.DeviceStatusSettings.stopPostingStats();
configDeviceHandler.postDelayed(configDeviceRunnable, 500);
if(serialUSBDeviceSettingsNeedUpdate) {
ChameleonIO.DeviceStatusSettings.stopPostingStats();
configDeviceHandler.postDelayed(configDeviceRunnable, 0);
}
}
}

Expand All @@ -242,6 +289,9 @@ else if(!isTaskRoot()) {
final Intent intent = getIntent();
final String intentAction = intent.getAction();
if (intentAction != null && (intentAction.equals(UsbManager.ACTION_USB_DEVICE_DETACHED) || intentAction.equals(UsbManager.ACTION_USB_DEVICE_ATTACHED))) {
if(LiveLoggerActivity.getLiveLoggerInstance() != null) {
LiveLoggerActivity.getLiveLoggerInstance().onNewIntent(intent);
}
Log.i(TAG, "onCreate(): Main Activity is not the root. Finishing Main Activity instead of re-launching.");
finish();
return;
Expand All @@ -250,7 +300,8 @@ else if(!isTaskRoot()) {

setUnhandledExceptionHandler();

boolean completeRestart = (getInstance() == null);
boolean completeRestart = (getLiveLoggerInstance() == null);
serialUSBDeviceSettingsNeedUpdate = true;

AndroidSettingsStorage.loadPreviousSettings(AndroidSettingsStorage.DEFAULT_CMLDAPP_PROFILE);
if(ChameleonLogUtils.CONFIG_CLEAR_LOGS_NEW_DEVICE_CONNNECT) {
Expand Down Expand Up @@ -289,13 +340,6 @@ else if(!isTaskRoot()) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); // keep app from crashing when the screen rotates
}

final Intent intent = getIntent();
final String intentAction = intent.getAction();
if (intentAction == null || (intentAction != null && !intentAction.equals(UsbManager.ACTION_USB_DEVICE_DETACHED) &&
!intentAction.equals(UsbManager.ACTION_USB_DEVICE_ATTACHED))) {
reconfigureSerialIODevices();
}

clearStatusIcon(R.id.statusIconNewMsg);
clearStatusIcon(R.id.statusIconNewXFer);
clearStatusIcon(R.id.signalStrength);
Expand All @@ -314,6 +358,15 @@ else if(!isTaskRoot()) {
Utils.displayToastMessageLong("Chameleon Mini Live Debugger recovered from crash.");
}

Handler runAfterGUIInitDeviceHandler = new Handler();
Runnable configDeviceRunnable = new Runnable() {
public void run() {
liveLoggerActivityMainContentView = findViewById(android.R.id.content);
reconfigureSerialIODevices();
}
};
runAfterGUIInitDeviceHandler.postDelayed(configDeviceRunnable, 300);

}

private static String INTENT_RESTART_ACTIVITY = "LiveLoggerActivity.Intent.Category.RESTART_ACTIVITY";
Expand Down Expand Up @@ -432,7 +485,7 @@ public void run() {
if(ChameleonLogUtils.CONFIG_CLEAR_LOGS_NEW_DEVICE_CONNNECT) {
MainActivityLogUtils.clearAllLogs();
}
configDeviceHandler.postDelayed(configDeviceRunnable, 400);
configDeviceRunnable.run();
setStatusIcon(R.id.statusIconUSB, R.drawable.usbconnected16);
}
}
Expand All @@ -447,6 +500,7 @@ else if(intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_DETACHED) ||
serialIOPort.shutdownSerial();
}
ChameleonSettings.SERIALIO_IFACE_ACTIVE_INDEX = -1;
serialUSBDeviceSettingsNeedUpdate = true;
try {
int lastActiveSlotNumber = ChameleonIO.DeviceStatusSettings.DIP_SETTING;
ChameleonConfigSlot.CHAMELEON_DEVICE_CONFIG_SLOTS[lastActiveSlotNumber - 1].disableLayout();
Expand Down Expand Up @@ -482,6 +536,8 @@ public void run() {
ChameleonPeripherals.actionButtonRestorePeripheralDefaults(null);
TabFragment.UITAB_DATA[TabFragment.TAB_TOOLS].changeMenuItemDisplay(TAB_TOOLS_MITEM_SLOTS, true);
TabFragment.UITAB_DATA[TabFragment.TAB_CONFIG].changeMenuItemDisplay(TabFragment.TAB_CONFIG_MITEM_LOGGING, true);
ChameleonIO.DeviceStatusSettings.updateAllStatusAndPost(false);
ChameleonIO.DeviceStatusSettings.updateAllStatusAndPost(false); /* Make sure the device returned the correct data to display */
ChameleonIO.DeviceStatusSettings.startPostingStats(0);
setStatusIcon(R.id.statusIconBT, R.drawable.bluetooth16);
}
Expand Down
Loading

0 comments on commit 43693aa

Please sign in to comment.