Skip to content

Commit d189b83

Browse files
authored
Merge pull request #41 from martinwork/from300
Projects - flash calls Pairing for Reset to BLE mode UI
2 parents 18328fc + d5caa32 commit d189b83

File tree

3 files changed

+177
-47
lines changed

3 files changed

+177
-47
lines changed

app/src/main/java/com/samsung/microbit/ui/activity/PairingActivity.java

Lines changed: 120 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,53 @@ private enum PAIRING_STATE {
152152

153153
private BLEPair blePair = null;
154154

155+
public final static String ACTION_RESET_TO_BLE = "com.samsung.microbit.ACTION_RESET_TO_BLE";
156+
public final static String ACTION_PAIR_BEFORE_FLASH = "com.samsung.microbit.ACTION_PAIR_BEFORE_FLASH";
157+
158+
private String inAction = null;
159+
160+
@Override
161+
protected void onNewIntent(Intent intent) {
162+
super.onNewIntent(intent);
163+
if(intent != null) {
164+
handleIncomingIntent(intent);
165+
}
166+
}
167+
168+
private void handleIncomingIntent(Intent intent) {
169+
inAction = intent.getAction();
170+
if ( inAction != null) {
171+
if ( inAction.equals(ACTION_RESET_TO_BLE)) {
172+
resetToBLEStart();
173+
return;
174+
}
175+
if ( inAction.equals(ACTION_PAIR_BEFORE_FLASH)) {
176+
pairBeforeFlashStart();
177+
return;
178+
}
179+
}
180+
}
181+
182+
private void resetToBLEStart() {
183+
displayScreen(PAIRING_STATE.PAIRING_STATE_TRIPLE);
184+
}
185+
186+
private void resetToBLEFinish( int resultCode) {
187+
inAction = null;
188+
setResult( resultCode);
189+
finish();
190+
}
191+
192+
private void pairBeforeFlashStart() {
193+
displayScreen(PAIRING_STATE.PAIRING_STATE_TRIPLE);
194+
}
195+
196+
private void pairBeforeFlashFinish( int resultCode) {
197+
inAction = null;
198+
setResult( resultCode);
199+
finish();
200+
}
201+
155202
private BLEPair getBLEPair() {
156203
if ( blePair == null) {
157204
blePair = new BLEPair( this);
@@ -427,6 +474,10 @@ private void popupPermissionPairing() {
427474
public void onClick(View v) {
428475
logi("======successfulPairingHandler======");
429476
PopUp.hide();
477+
if ( inAction.equals(ACTION_PAIR_BEFORE_FLASH)) {
478+
pairBeforeFlashFinish( RESULT_OK);
479+
return;
480+
}
430481
displayScreen(PAIRING_STATE.PAIRING_STATE_CONNECT_BUTTON);
431482
}
432483
};
@@ -440,6 +491,10 @@ public void onClick(View v) {
440491
public void onClick(View v) {
441492
logi("======failedPairingHandler======");
442493
PopUp.hide();
494+
if ( inAction.equals(ACTION_PAIR_BEFORE_FLASH)) {
495+
pairBeforeFlashFinish( RESULT_CANCELED);
496+
return;
497+
}
443498
displayScreen(PAIRING_STATE.PAIRING_STATE_STEP_2);
444499
}
445500
};
@@ -782,8 +837,12 @@ protected void onCreate(Bundle savedInstanceState) {
782837

783838
currentOrientation = getResources().getConfiguration().orientation;
784839

785-
// pin view
786-
displayScreen(pairingState);
840+
if (savedInstanceState == null && getIntent() != null) {
841+
handleIncomingIntent(getIntent());
842+
} else {
843+
// pin view
844+
displayScreen(pairingState);
845+
}
787846
}
788847

789848
@Override
@@ -1102,26 +1161,18 @@ private void displayScreen(PAIRING_STATE gotoState) {
11021161
newDeviceCode = "";
11031162
break;
11041163

1105-
case PAIRING_STATE_TRIPLE: {
1106-
GifImageView view = (GifImageView) findViewById(R.id.pair_tip_step_1_giff);
1107-
view.setImageResource(R.drawable.reset_triple);
1108-
TextView prompt = (TextView) findViewById(R.id.pair_tip_step_1_instructions);
1109-
prompt.setText(R.string.viewPairTriplePromptText);
1110-
prompt.setContentDescription(prompt.getText());
1111-
pairTipView.setVisibility(View.VISIBLE);
1112-
view.animate();
1164+
case PAIRING_STATE_TRIPLE:
1165+
displayScreenTripleOrStep1(
1166+
R.drawable.reset_triple,
1167+
R.string.viewPairTriplePromptText);
11131168
break;
1114-
}
1115-
case PAIRING_STATE_STEP_1: {
1116-
GifImageView view = (GifImageView) findViewById(R.id.pair_tip_step_1_giff);
1117-
view.setImageResource(R.drawable.how_to_pair_microbit);
1118-
TextView prompt = (TextView) findViewById(R.id.pair_tip_step_1_instructions);
1119-
prompt.setText(R.string.connect_tip_text);
1120-
prompt.setContentDescription(prompt.getText());
1121-
pairTipView.setVisibility(View.VISIBLE);
1122-
view.animate();
1169+
1170+
case PAIRING_STATE_STEP_1:
1171+
displayScreenTripleOrStep1(
1172+
R.drawable.how_to_pair_microbit,
1173+
R.string.connect_tip_text);
11231174
break;
1124-
}
1175+
11251176
case PAIRING_STATE_STEP_2:
11261177
newDeviceView.setVisibility(View.VISIBLE);
11271178
findViewById(R.id.cancel_enter_pattern_step_2_btn).setVisibility(View.VISIBLE);
@@ -1159,6 +1210,27 @@ private void displayScreen(PAIRING_STATE gotoState) {
11591210
logi("displayScreen End");
11601211
}
11611212

1213+
private void displayScreenTripleOrStep1( int resIdGif, int resIdPrompt)
1214+
{
1215+
if ( inAction.equals(ACTION_RESET_TO_BLE)) {
1216+
TextView title = (TextView) findViewById(R.id.pairTipTitle);
1217+
title.setText(R.string.connect_tip_title_resetToBLE);
1218+
TextView step = (TextView) findViewById(R.id.pair_tip_step_1_step);
1219+
step.setText("");
1220+
step.setContentDescription("");
1221+
}
1222+
1223+
GifImageView gif = (GifImageView) findViewById(R.id.pair_tip_step_1_giff);
1224+
gif.setImageResource( resIdGif);
1225+
1226+
TextView prompt = (TextView) findViewById(R.id.pair_tip_step_1_instructions);
1227+
prompt.setText( resIdPrompt);
1228+
prompt.setContentDescription(prompt.getText());
1229+
1230+
pairTipView.setVisibility(View.VISIBLE);
1231+
gif.animate();
1232+
}
1233+
11621234
/**
11631235
* Starts activity to enable bluetooth.
11641236
*/
@@ -1342,6 +1414,10 @@ public void onClick(final View v) {
13421414
// Proceed to Enter Pattern
13431415
case R.id.ok_tip_step_1_btn:
13441416
logi("onClick() :: ok_tip_screen_one_button");
1417+
if ( inAction.equals(ACTION_RESET_TO_BLE)) {
1418+
resetToBLEFinish(Activity.RESULT_OK);
1419+
return;
1420+
}
13451421
displayScreen(PAIRING_STATE.PAIRING_STATE_STEP_2);
13461422
break;
13471423

@@ -1358,18 +1434,34 @@ public void onClick(final View v) {
13581434

13591435
case R.id.cancel_tip_step_1_btn:
13601436
logi("onClick() :: cancel_tip_button");
1437+
if ( inAction.equals(ACTION_RESET_TO_BLE)) {
1438+
resetToBLEFinish(Activity.RESULT_CANCELED);
1439+
return;
1440+
}
1441+
if ( inAction.equals(ACTION_PAIR_BEFORE_FLASH)) {
1442+
pairBeforeFlashFinish( RESULT_CANCELED);
1443+
return;
1444+
}
13611445
displayScreen(PAIRING_STATE.PAIRING_STATE_CONNECT_BUTTON);
13621446
break;
13631447

13641448
case R.id.cancel_enter_pattern_step_2_btn:
13651449
logi("onClick() :: cancel_name_button");
13661450
stopScanning();
1451+
if ( inAction.equals(ACTION_PAIR_BEFORE_FLASH)) {
1452+
pairBeforeFlashFinish( RESULT_CANCELED);
1453+
return;
1454+
}
13671455
displayScreen(PAIRING_STATE.PAIRING_STATE_CONNECT_BUTTON);
13681456
break;
13691457

13701458
case R.id.cancel_search_microbit_step_3_btn:
13711459
logi("onClick() :: cancel_search_button");
13721460
stopScanning();
1461+
if ( inAction.equals(ACTION_PAIR_BEFORE_FLASH)) {
1462+
pairBeforeFlashFinish( RESULT_CANCELED);
1463+
return;
1464+
}
13731465
displayScreen(PAIRING_STATE.PAIRING_STATE_CONNECT_BUTTON);
13741466
break;
13751467

@@ -1457,7 +1549,10 @@ private void unPairDevice() {
14571549
private void handleResetAll() {
14581550
Arrays.fill(DEVICE_CODE_ARRAY, 0);
14591551
stopScanning();
1460-
1552+
if ( inAction.equals(ACTION_PAIR_BEFORE_FLASH)) {
1553+
pairBeforeFlashFinish( RESULT_CANCELED);
1554+
return;
1555+
}
14611556
if(pairingState == PAIRING_STATE.PAIRING_STATE_CONNECT_BUTTON) {
14621557
finish();
14631558
} else {
@@ -1516,6 +1611,10 @@ public void onClick(View v) {
15161611
@Override
15171612
public void onClick(View v) {
15181613
PopUp.hide();
1614+
if ( inAction.equals(ACTION_PAIR_BEFORE_FLASH)) {
1615+
pairBeforeFlashFinish( RESULT_CANCELED);
1616+
return;
1617+
}
15191618
displayScreen(PAIRING_STATE.PAIRING_STATE_CONNECT_BUTTON);
15201619
}
15211620
});
@@ -1557,9 +1656,6 @@ private void handlePairingSuccessful() {
15571656
}
15581657
}
15591658

1560-
1561-
1562-
15631659
@Override
15641660
protected void onDestroy() {
15651661
super.onDestroy();

app/src/main/java/com/samsung/microbit/ui/activity/ProjectActivity.java

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,47 @@ public class ProjectActivity extends Activity implements View.OnClickListener, B
166166

167167
BLEService bleService;
168168

169+
private static final int REQUEST_CODE_EXPORT = 1;
170+
private static final int REQUEST_CODE_IMPORT = 2;
171+
private static final int REQUEST_CODE_RESET_TO_BLE = 3;
172+
private static final int REQUEST_CODE_PAIR_BEFORE_FLASH = 4;
173+
174+
private void goToPairingFromAppBarDeviceName() {
175+
Intent intent = new Intent(this, PairingActivity.class);
176+
startActivity(intent);
177+
}
178+
179+
private void goToPairingToPairBeforeFlash() {
180+
Intent i = new Intent(this, PairingActivity.class);
181+
i.setAction( PairingActivity.ACTION_PAIR_BEFORE_FLASH);
182+
startActivityForResult( i, REQUEST_CODE_PAIR_BEFORE_FLASH);
183+
}
184+
185+
private void goToPairingResetToBLE() {
186+
Intent i = new Intent(this, PairingActivity.class);
187+
i.setAction( PairingActivity.ACTION_RESET_TO_BLE);
188+
startActivityForResult( i, REQUEST_CODE_RESET_TO_BLE);
189+
}
190+
191+
protected void onActivityResultPairing(int requestCode, int resultCode, Intent data) {
192+
switch ( requestCode) {
193+
case REQUEST_CODE_RESET_TO_BLE:
194+
if (resultCode == RESULT_OK) {
195+
startFlashing();
196+
} else {
197+
onFlashComplete();
198+
}
199+
break;
200+
case REQUEST_CODE_PAIR_BEFORE_FLASH:
201+
if (resultCode == RESULT_OK) {
202+
flashingChecks();
203+
} else {
204+
onFlashComplete();
205+
}
206+
break;
207+
}
208+
}
209+
169210
private void goToMakeCode( String hex, String name) {
170211
if ( inMakeCodeActionFlash) {
171212
inMakeCodeActionFlash = false;
@@ -842,6 +883,11 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
842883
onActivityResultScriptsExport( requestCode, resultCode, data);
843884
super.onActivityResult(requestCode, resultCode, data);
844885
return;
886+
case REQUEST_CODE_RESET_TO_BLE:
887+
case REQUEST_CODE_PAIR_BEFORE_FLASH:
888+
onActivityResultPairing( requestCode, resultCode, data);
889+
super.onActivityResult(requestCode, resultCode, data);
890+
return;
845891
}
846892

847893
boolean flash = mActivityState == FlashActivityState.STATE_ENABLE_BT_INTERNAL_FLASH_REQUEST ||
@@ -914,8 +960,15 @@ private void proceedAfterBlePermissionGranted() {
914960
* start the flashing steps.
915961
*/
916962
private void proceedAfterBlePermissionGrantedAndBleEnabled() {
917-
if (launchPairingIfNoCurrentMicrobit())
963+
/**
964+
* Checks for requisite state of a micro:bit board. If all is good then
965+
* initiates flashing.
966+
*/
967+
ConnectedDevice currentMicrobit = BluetoothUtils.getPairedMicrobit(this);
968+
if ( currentMicrobit.mPattern == null) {
969+
goToPairingToPairBeforeFlash();
918970
return;
971+
}
919972

920973
flashingChecks();
921974
}
@@ -1074,29 +1127,12 @@ public void onClick(final View v) {
10741127
// }
10751128
// break;
10761129
case R.id.deviceName:
1077-
// Toast.makeText(this, "Back to connectMaybeInit screen", Toast.LENGTH_SHORT).show();
1078-
Intent intent = new Intent(this, PairingActivity.class);
1079-
startActivity(intent);
1130+
goToPairingFromAppBarDeviceName();
10801131
break;
10811132

10821133
}
10831134
}
10841135

1085-
/**
1086-
* Checks for requisite state of a micro:bit board. If all is good then
1087-
* initiates flashing.
1088-
*/
1089-
private boolean launchPairingIfNoCurrentMicrobit() {
1090-
ConnectedDevice currentMicrobit = BluetoothUtils.getPairedMicrobit(this);
1091-
1092-
if(currentMicrobit.mPattern != null)
1093-
return false;
1094-
1095-
Intent intent = new Intent(this, PairingActivity.class);
1096-
startActivity(intent);
1097-
return true;
1098-
}
1099-
11001136
private void flashingChecks() {
11011137
ConnectedDevice currentMicrobit = BluetoothUtils.getPairedMicrobit(MBApp.getApp());
11021138

@@ -1169,7 +1205,7 @@ public void onClick(View v) {
11691205
public void onClick(View v) {
11701206
ConnectedDevice currentMicrobit = BluetoothUtils.getPairedMicrobit(MBApp.getApp());
11711207
PopUp.hide();
1172-
startFlashing();
1208+
goToPairingResetToBLE();
11731209
}
11741210
},//override click listener for ok button
11751211
popupClickFlashComplete);
@@ -2284,9 +2320,6 @@ private void scriptsCreateCode() {
22842320
goToMakeCode( null, null);
22852321
}
22862322

2287-
private static final int REQUEST_CODE_EXPORT = 1;
2288-
private static final int REQUEST_CODE_IMPORT = 2;
2289-
22902323

22912324
private void scriptsImport() {
22922325
String messageTitle = "Import";

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@
154154
<string name="gamepad">Gamepad</string>
155155
<string name="connectNew">Pair a new micro:bit</string>
156156
<string name="connect_tip_title">How to pair your micro:bit</string>
157+
<string name="connect_tip_title_resetToBLE">Reset to Bluetooth mode</string>
157158
<string name="step_connect_tip_text_step_three">Step 3</string>
158159
<string name="step_connect_tip_text_step_four_text">When you see the success message, \npress RESET and you\'re done!</string>
159160
<string name="connect_tip_text_step">Step 1</string>

0 commit comments

Comments
 (0)