Skip to content

Commit

Permalink
Merge pull request #41 from martinwork/from300
Browse files Browse the repository at this point in the history
Projects - flash calls Pairing for Reset to BLE mode UI
  • Loading branch information
martinwork authored Mar 26, 2024
2 parents 18328fc + d5caa32 commit d189b83
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 47 deletions.
144 changes: 120 additions & 24 deletions app/src/main/java/com/samsung/microbit/ui/activity/PairingActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
};
Expand All @@ -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);
}
};
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
});
Expand Down Expand Up @@ -1557,9 +1656,6 @@ private void handlePairingSuccessful() {
}
}




@Override
protected void onDestroy() {
super.onDestroy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 ||
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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";
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
<string name="gamepad">Gamepad</string>
<string name="connectNew">Pair a new micro:bit</string>
<string name="connect_tip_title">How to pair your micro:bit</string>
<string name="connect_tip_title_resetToBLE">Reset to Bluetooth mode</string>
<string name="step_connect_tip_text_step_three">Step 3</string>
<string name="step_connect_tip_text_step_four_text">When you see the success message, \npress RESET and you\'re done!</string>
<string name="connect_tip_text_step">Step 1</string>
Expand Down

0 comments on commit d189b83

Please sign in to comment.