From afdb08e11e18ba1830c1b4665bc74b3cc46556b6 Mon Sep 17 00:00:00 2001 From: Martin Williams Date: Tue, 26 Mar 2024 21:18:31 +0000 Subject: [PATCH] Projects - flash calls Pairing for Reset to BLE mode UI --- .../microbit/ui/activity/PairingActivity.java | 144 +++++++++++++++--- .../microbit/ui/activity/ProjectActivity.java | 79 +++++++--- app/src/main/res/values/strings.xml | 1 + 3 files changed, 177 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/com/samsung/microbit/ui/activity/PairingActivity.java b/app/src/main/java/com/samsung/microbit/ui/activity/PairingActivity.java index 657387b3..f5265821 100644 --- a/app/src/main/java/com/samsung/microbit/ui/activity/PairingActivity.java +++ b/app/src/main/java/com/samsung/microbit/ui/activity/PairingActivity.java @@ -152,6 +152,53 @@ private enum PAIRING_STATE { private BLEPair blePair = null; + public final static String ACTION_RESET_TO_BLE = "com.samsung.microbit.ACTION_RESET_TO_BLE"; + public final static String ACTION_PAIR_BEFORE_FLASH = "com.samsung.microbit.ACTION_PAIR_BEFORE_FLASH"; + + private String inAction = null; + + @Override + protected void onNewIntent(Intent intent) { + super.onNewIntent(intent); + if(intent != null) { + handleIncomingIntent(intent); + } + } + + private void handleIncomingIntent(Intent intent) { + inAction = intent.getAction(); + if ( inAction != null) { + if ( inAction.equals(ACTION_RESET_TO_BLE)) { + resetToBLEStart(); + return; + } + if ( inAction.equals(ACTION_PAIR_BEFORE_FLASH)) { + pairBeforeFlashStart(); + return; + } + } + } + + private void resetToBLEStart() { + displayScreen(PAIRING_STATE.PAIRING_STATE_TRIPLE); + } + + private void resetToBLEFinish( int resultCode) { + inAction = null; + setResult( resultCode); + finish(); + } + + private void pairBeforeFlashStart() { + displayScreen(PAIRING_STATE.PAIRING_STATE_TRIPLE); + } + + private void pairBeforeFlashFinish( int resultCode) { + inAction = null; + setResult( resultCode); + finish(); + } + private BLEPair getBLEPair() { if ( blePair == null) { blePair = new BLEPair( this); @@ -427,6 +474,10 @@ private void popupPermissionPairing() { public void onClick(View v) { logi("======successfulPairingHandler======"); PopUp.hide(); + if ( inAction.equals(ACTION_PAIR_BEFORE_FLASH)) { + pairBeforeFlashFinish( RESULT_OK); + return; + } displayScreen(PAIRING_STATE.PAIRING_STATE_CONNECT_BUTTON); } }; @@ -440,6 +491,10 @@ public void onClick(View v) { public void onClick(View v) { logi("======failedPairingHandler======"); PopUp.hide(); + if ( inAction.equals(ACTION_PAIR_BEFORE_FLASH)) { + pairBeforeFlashFinish( RESULT_CANCELED); + return; + } displayScreen(PAIRING_STATE.PAIRING_STATE_STEP_2); } }; @@ -782,8 +837,12 @@ protected void onCreate(Bundle savedInstanceState) { currentOrientation = getResources().getConfiguration().orientation; - // pin view - displayScreen(pairingState); + if (savedInstanceState == null && getIntent() != null) { + handleIncomingIntent(getIntent()); + } else { + // pin view + displayScreen(pairingState); + } } @Override @@ -1102,26 +1161,18 @@ private void displayScreen(PAIRING_STATE gotoState) { newDeviceCode = ""; break; - case PAIRING_STATE_TRIPLE: { - GifImageView view = (GifImageView) findViewById(R.id.pair_tip_step_1_giff); - view.setImageResource(R.drawable.reset_triple); - TextView prompt = (TextView) findViewById(R.id.pair_tip_step_1_instructions); - prompt.setText(R.string.viewPairTriplePromptText); - prompt.setContentDescription(prompt.getText()); - pairTipView.setVisibility(View.VISIBLE); - view.animate(); + case PAIRING_STATE_TRIPLE: + displayScreenTripleOrStep1( + R.drawable.reset_triple, + R.string.viewPairTriplePromptText); break; - } - case PAIRING_STATE_STEP_1: { - GifImageView view = (GifImageView) findViewById(R.id.pair_tip_step_1_giff); - view.setImageResource(R.drawable.how_to_pair_microbit); - TextView prompt = (TextView) findViewById(R.id.pair_tip_step_1_instructions); - prompt.setText(R.string.connect_tip_text); - prompt.setContentDescription(prompt.getText()); - pairTipView.setVisibility(View.VISIBLE); - view.animate(); + + case PAIRING_STATE_STEP_1: + displayScreenTripleOrStep1( + R.drawable.how_to_pair_microbit, + R.string.connect_tip_text); break; - } + case PAIRING_STATE_STEP_2: newDeviceView.setVisibility(View.VISIBLE); findViewById(R.id.cancel_enter_pattern_step_2_btn).setVisibility(View.VISIBLE); @@ -1159,6 +1210,27 @@ private void displayScreen(PAIRING_STATE gotoState) { logi("displayScreen End"); } + private void displayScreenTripleOrStep1( int resIdGif, int resIdPrompt) + { + if ( inAction.equals(ACTION_RESET_TO_BLE)) { + TextView title = (TextView) findViewById(R.id.pairTipTitle); + title.setText(R.string.connect_tip_title_resetToBLE); + TextView step = (TextView) findViewById(R.id.pair_tip_step_1_step); + step.setText(""); + step.setContentDescription(""); + } + + GifImageView gif = (GifImageView) findViewById(R.id.pair_tip_step_1_giff); + gif.setImageResource( resIdGif); + + TextView prompt = (TextView) findViewById(R.id.pair_tip_step_1_instructions); + prompt.setText( resIdPrompt); + prompt.setContentDescription(prompt.getText()); + + pairTipView.setVisibility(View.VISIBLE); + gif.animate(); + } + /** * Starts activity to enable bluetooth. */ @@ -1342,6 +1414,10 @@ public void onClick(final View v) { // Proceed to Enter Pattern case R.id.ok_tip_step_1_btn: logi("onClick() :: ok_tip_screen_one_button"); + if ( inAction.equals(ACTION_RESET_TO_BLE)) { + resetToBLEFinish(Activity.RESULT_OK); + return; + } displayScreen(PAIRING_STATE.PAIRING_STATE_STEP_2); break; @@ -1358,18 +1434,34 @@ public void onClick(final View v) { case R.id.cancel_tip_step_1_btn: logi("onClick() :: cancel_tip_button"); + if ( inAction.equals(ACTION_RESET_TO_BLE)) { + resetToBLEFinish(Activity.RESULT_CANCELED); + return; + } + if ( inAction.equals(ACTION_PAIR_BEFORE_FLASH)) { + pairBeforeFlashFinish( RESULT_CANCELED); + return; + } displayScreen(PAIRING_STATE.PAIRING_STATE_CONNECT_BUTTON); break; case R.id.cancel_enter_pattern_step_2_btn: logi("onClick() :: cancel_name_button"); stopScanning(); + if ( inAction.equals(ACTION_PAIR_BEFORE_FLASH)) { + pairBeforeFlashFinish( RESULT_CANCELED); + return; + } displayScreen(PAIRING_STATE.PAIRING_STATE_CONNECT_BUTTON); break; case R.id.cancel_search_microbit_step_3_btn: logi("onClick() :: cancel_search_button"); stopScanning(); + if ( inAction.equals(ACTION_PAIR_BEFORE_FLASH)) { + pairBeforeFlashFinish( RESULT_CANCELED); + return; + } displayScreen(PAIRING_STATE.PAIRING_STATE_CONNECT_BUTTON); break; @@ -1457,7 +1549,10 @@ private void unPairDevice() { private void handleResetAll() { Arrays.fill(DEVICE_CODE_ARRAY, 0); stopScanning(); - + if ( inAction.equals(ACTION_PAIR_BEFORE_FLASH)) { + pairBeforeFlashFinish( RESULT_CANCELED); + return; + } if(pairingState == PAIRING_STATE.PAIRING_STATE_CONNECT_BUTTON) { finish(); } else { @@ -1516,6 +1611,10 @@ public void onClick(View v) { @Override public void onClick(View v) { PopUp.hide(); + if ( inAction.equals(ACTION_PAIR_BEFORE_FLASH)) { + pairBeforeFlashFinish( RESULT_CANCELED); + return; + } displayScreen(PAIRING_STATE.PAIRING_STATE_CONNECT_BUTTON); } }); @@ -1557,9 +1656,6 @@ private void handlePairingSuccessful() { } } - - - @Override protected void onDestroy() { super.onDestroy(); diff --git a/app/src/main/java/com/samsung/microbit/ui/activity/ProjectActivity.java b/app/src/main/java/com/samsung/microbit/ui/activity/ProjectActivity.java index 812ddd7d..0821a2da 100644 --- a/app/src/main/java/com/samsung/microbit/ui/activity/ProjectActivity.java +++ b/app/src/main/java/com/samsung/microbit/ui/activity/ProjectActivity.java @@ -166,6 +166,47 @@ public class ProjectActivity extends Activity implements View.OnClickListener, B BLEService bleService; + private static final int REQUEST_CODE_EXPORT = 1; + private static final int REQUEST_CODE_IMPORT = 2; + private static final int REQUEST_CODE_RESET_TO_BLE = 3; + private static final int REQUEST_CODE_PAIR_BEFORE_FLASH = 4; + + private void goToPairingFromAppBarDeviceName() { + Intent intent = new Intent(this, PairingActivity.class); + startActivity(intent); + } + + private void goToPairingToPairBeforeFlash() { + Intent i = new Intent(this, PairingActivity.class); + i.setAction( PairingActivity.ACTION_PAIR_BEFORE_FLASH); + startActivityForResult( i, REQUEST_CODE_PAIR_BEFORE_FLASH); + } + + private void goToPairingResetToBLE() { + Intent i = new Intent(this, PairingActivity.class); + i.setAction( PairingActivity.ACTION_RESET_TO_BLE); + startActivityForResult( i, REQUEST_CODE_RESET_TO_BLE); + } + + protected void onActivityResultPairing(int requestCode, int resultCode, Intent data) { + switch ( requestCode) { + case REQUEST_CODE_RESET_TO_BLE: + if (resultCode == RESULT_OK) { + startFlashing(); + } else { + onFlashComplete(); + } + break; + case REQUEST_CODE_PAIR_BEFORE_FLASH: + if (resultCode == RESULT_OK) { + flashingChecks(); + } else { + onFlashComplete(); + } + break; + } + } + private void goToMakeCode( String hex, String name) { if ( inMakeCodeActionFlash) { inMakeCodeActionFlash = false; @@ -842,6 +883,11 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { onActivityResultScriptsExport( requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data); return; + case REQUEST_CODE_RESET_TO_BLE: + case REQUEST_CODE_PAIR_BEFORE_FLASH: + onActivityResultPairing( requestCode, resultCode, data); + super.onActivityResult(requestCode, resultCode, data); + return; } boolean flash = mActivityState == FlashActivityState.STATE_ENABLE_BT_INTERNAL_FLASH_REQUEST || @@ -914,8 +960,15 @@ private void proceedAfterBlePermissionGranted() { * start the flashing steps. */ private void proceedAfterBlePermissionGrantedAndBleEnabled() { - if (launchPairingIfNoCurrentMicrobit()) + /** + * Checks for requisite state of a micro:bit board. If all is good then + * initiates flashing. + */ + ConnectedDevice currentMicrobit = BluetoothUtils.getPairedMicrobit(this); + if ( currentMicrobit.mPattern == null) { + goToPairingToPairBeforeFlash(); return; + } flashingChecks(); } @@ -1074,29 +1127,12 @@ public void onClick(final View v) { // } // break; case R.id.deviceName: - // Toast.makeText(this, "Back to connectMaybeInit screen", Toast.LENGTH_SHORT).show(); - Intent intent = new Intent(this, PairingActivity.class); - startActivity(intent); + goToPairingFromAppBarDeviceName(); break; } } - /** - * Checks for requisite state of a micro:bit board. If all is good then - * initiates flashing. - */ - private boolean launchPairingIfNoCurrentMicrobit() { - ConnectedDevice currentMicrobit = BluetoothUtils.getPairedMicrobit(this); - - if(currentMicrobit.mPattern != null) - return false; - - Intent intent = new Intent(this, PairingActivity.class); - startActivity(intent); - return true; - } - private void flashingChecks() { ConnectedDevice currentMicrobit = BluetoothUtils.getPairedMicrobit(MBApp.getApp()); @@ -1169,7 +1205,7 @@ public void onClick(View v) { public void onClick(View v) { ConnectedDevice currentMicrobit = BluetoothUtils.getPairedMicrobit(MBApp.getApp()); PopUp.hide(); - startFlashing(); + goToPairingResetToBLE(); } },//override click listener for ok button popupClickFlashComplete); @@ -2284,9 +2320,6 @@ private void scriptsCreateCode() { goToMakeCode( null, null); } - private static final int REQUEST_CODE_EXPORT = 1; - private static final int REQUEST_CODE_IMPORT = 2; - private void scriptsImport() { String messageTitle = "Import"; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 44852614..9ecc4f9c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -154,6 +154,7 @@ Gamepad Pair a new micro:bit How to pair your micro:bit + Reset to Bluetooth mode Step 3 When you see the success message, \npress RESET and you\'re done! Step 1