Skip to content

Commit 5d48a99

Browse files
committed
Flashing ends in initiating Activity
1 parent 64c0f0b commit 5d48a99

File tree

1 file changed

+57
-28
lines changed

1 file changed

+57
-28
lines changed

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

Lines changed: 57 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ public class ProjectActivity extends Activity implements View.OnClickListener, B
146146

147147
private boolean previousPartialFlashFailed = false;
148148

149+
private boolean finishWhenFlashComplete = true;
150+
149151
private int FLASH_TYPE_DFU = 0;
150152
private int FLASH_TYPE_PF = 1;
151153

@@ -154,6 +156,25 @@ public class ProjectActivity extends Activity implements View.OnClickListener, B
154156

155157
BLEService bleService;
156158

159+
private void onFlashComplete() {
160+
if ( finishWhenFlashComplete) {
161+
finish();
162+
}
163+
}
164+
165+
/**
166+
* Handler for popup button that hides a popup window
167+
* and calls onFlashComplete()
168+
*/
169+
View.OnClickListener popupClickFlashComplete = new View.OnClickListener() {
170+
@Override
171+
public void onClick(View v) {
172+
logi("popupClickFlashComplete");
173+
PopUp.hide();
174+
onFlashComplete();
175+
}
176+
};
177+
157178
private final Runnable tryToConnectAgain = new Runnable() {
158179

159180
@Override
@@ -417,6 +438,7 @@ protected void onCreate(Bundle savedInstanceState) {
417438
setConnectedDeviceText();
418439

419440
if(savedInstanceState == null && getIntent() != null) {
441+
finishWhenFlashComplete = getIntent().getData() != null;
420442
handleIncomingIntent(getIntent());
421443
}
422444
}
@@ -508,19 +530,11 @@ private void handleIncomingIntent(Intent intent) {
508530
}
509531

510532
if ( mProgramToSend != null) {
511-
if ( startBluetoothForFlashing())
512-
finish();
513-
//TODO call finish()?
514-
// if(!BluetoothChecker.getInstance().isBluetoothON()) {
515-
// startBluetooth();
516-
// } else {
517-
// flashingChecks();
518-
// finish();
519-
// }
533+
startBluetoothForFlashing();
520534
} else {
521535
if (isOpenByOtherApp) {
522536
Toast.makeText(this, "Not a micro:bit HEX file", Toast.LENGTH_LONG).show();
523-
finish();
537+
onFlashComplete();
524538
}
525539
}
526540
}
@@ -888,25 +902,30 @@ private void setupListAdapter() {
888902

889903
@Override
890904
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
905+
boolean flash = mActivityState == FlashActivityState.STATE_ENABLE_BT_INTERNAL_FLASH_REQUEST ||
906+
mActivityState == FlashActivityState.STATE_ENABLE_BT_EXTERNAL_FLASH_REQUEST;
907+
boolean connect = mActivityState == FlashActivityState.STATE_ENABLE_BT_FOR_CONNECT;
891908

892-
if(requestCode == RequestCodes.REQUEST_ENABLE_BT) {
893-
if(resultCode == Activity.RESULT_OK) {
894-
if(mActivityState == FlashActivityState.STATE_ENABLE_BT_INTERNAL_FLASH_REQUEST ||
895-
mActivityState == FlashActivityState.STATE_ENABLE_BT_EXTERNAL_FLASH_REQUEST) {
909+
if (requestCode == RequestCodes.REQUEST_ENABLE_BT) {
910+
if (resultCode == Activity.RESULT_OK) {
911+
if (flash) {
896912
proceedAfterBlePermissionGrantedAndBleEnabled();
897-
} else if(mActivityState == FlashActivityState.STATE_ENABLE_BT_FOR_CONNECT) {
913+
} else if (connect) {
898914
setActivityState(FlashActivityState.STATE_IDLE);
899915
toggleConnection();
900916
}
901917
}
902-
if(resultCode == Activity.RESULT_CANCELED) {
918+
else if (resultCode == Activity.RESULT_CANCELED) {
903919
setActivityState(FlashActivityState.STATE_IDLE);
904920
PopUp.show(getString(R.string.bluetooth_off_cannot_continue), //message
905921
"",
906922
R.drawable.error_face, R.drawable.red_btn,
907923
PopUp.GIFF_ANIMATION_ERROR,
908924
TYPE_ALERT,
909925
null, null);
926+
if (flash) {
927+
onFlashComplete();
928+
}
910929
}
911930
}
912931
super.onActivityResult(requestCode, resultCode, data);
@@ -999,6 +1018,7 @@ public void requestPermissionsFlashingResult(int requestCode,
9991018
switch(requestCode) {
10001019
case PermissionCodes.BLUETOOTH_PERMISSIONS_REQUESTED_FLASHING_API31: {
10011020
popupPermissionFlashingError();
1021+
onFlashComplete();
10021022
break;
10031023
}
10041024
}
@@ -1032,6 +1052,7 @@ public void onClick(View v) {
10321052
logi("bluetoothPermissionCancelHandler");
10331053
PopUp.hide();
10341054
popupPermissionFlashingError();
1055+
onFlashComplete();
10351056
}
10361057
});
10371058
}
@@ -1157,7 +1178,8 @@ public void onClick(View v) {
11571178
}
11581179
}
11591180
);
1160-
return;
1181+
onFlashComplete();
1182+
return;
11611183
}
11621184
//
11631185
// if(mProgramToSend == null || mProgramToSend.filePath == null) {
@@ -1184,6 +1206,7 @@ public void onClick(View v) {
11841206
PopUp.GIFF_ANIMATION_FLASH,
11851207
TYPE_ALERT,
11861208
null, null);
1209+
onFlashComplete();
11871210
return;
11881211
}
11891212

@@ -1203,13 +1226,7 @@ public void onClick(View v) {
12031226
initiateFlashing();
12041227
}
12051228
},//override click listener for ok button
1206-
new View.OnClickListener() {
1207-
@Override
1208-
public void onClick(View v) {
1209-
PopUp.hide();
1210-
finish();
1211-
}
1212-
});//pass null to use default listeneronClick
1229+
popupClickFlashComplete);
12131230
} else {
12141231
initiateFlashing();
12151232
}
@@ -1246,7 +1263,7 @@ int charToInt(char in) {
12461263
* Creates and starts service to flash a program to a micro:bit board.
12471264
* int FLASH_TYPE_DFU
12481265
*/
1249-
@RequiresApi(api = Build.VERSION_CODES.O)
1266+
//@RequiresApi(api = Build.VERSION_CODES.O)
12501267
protected void startFlashing(int flashingType) {
12511268

12521269
logi(">>>>>>>>>>>>>>>>>>> startFlashing called >>>>>>>>>>>>>>>>>>> ");
@@ -1282,6 +1299,7 @@ protected void startFlashing(int flashingType) {
12821299
PopUp.GIFF_ANIMATION_ERROR,
12831300
TYPE_ALERT,
12841301
null, null);
1302+
onFlashComplete();
12851303
return;
12861304
}
12871305

@@ -1763,6 +1781,7 @@ public void onReceive(Context context, Intent intent) {
17631781
flashSuccess.putExtra(INTENT_GIFF_ANIMATION_CODE, 1);
17641782
flashSuccess.putExtra(INTENT_EXTRA_TYPE, TYPE_ALERT);
17651783
localBroadcastManager.sendBroadcast( flashSuccess );
1784+
onFlashComplete();
17661785
} else if(intent.getAction().equals(PartialFlashingService.BROADCAST_START)) {
17671786
// Display progress
17681787
PopUp.show("",
@@ -1796,6 +1815,7 @@ public void onClick(View v) {
17961815
TYPE_ALERT, //type of popup.
17971816
popupOkHandler,//override click listener for ok button
17981817
popupOkHandler);//pass null to use default listener
1818+
onFlashComplete();
17991819
}
18001820

18011821
}
@@ -1823,7 +1843,7 @@ public void reset() {
18231843
private View.OnClickListener okFinishFlashingHandler = new View.OnClickListener() {
18241844
@Override
18251845
public void onClick(View v) {
1826-
logi("popupOkHandler");
1846+
logi("okFinishFlashingHandler");
18271847
PopUp.hide();
18281848
}
18291849
};
@@ -1879,6 +1899,7 @@ public void onClick(View v) {
18791899
TYPE_ALERT, //type of popup.
18801900
okFinishFlashingHandler,//override click listener for ok button
18811901
okFinishFlashingHandler);//pass null to use default listener
1902+
onFlashComplete();
18821903
}
18831904

18841905
isCompleted = true;
@@ -2001,6 +2022,7 @@ public void onClick(View v) {
20012022

20022023
dfuUnregister();
20032024
removeReconnectionRunnable();
2025+
onFlashComplete();
20042026
break;
20052027
/*
20062028
case DfuService.PROGRESS_SERVICE_NOT_FOUND:
@@ -2075,6 +2097,7 @@ public void onClick(View v) {
20752097
MBApp application = MBApp.getApp();
20762098

20772099
dfuUnregister();
2100+
removeReconnectionRunnable();
20782101
//Update Stats
20792102
/*
20802103
GoogleAnalyticsManager.getInstance().sendFlashStats(
@@ -2097,7 +2120,13 @@ public void onClick(View v) {
20972120
flashingChecks();
20982121
}
20992122
},
2100-
popupOkHandler);
2123+
new View.OnClickListener() {
2124+
@Override
2125+
public void onClick(View v) {
2126+
PopUp.hide();
2127+
onFlashComplete();
2128+
}
2129+
});
21012130
} else {
21022131
PopUp.show(error_message + "\n\n" + getString(R.string.connect_tip_text), //message
21032132
getString(R.string.flashing_failed_title), //title
@@ -2106,8 +2135,8 @@ public void onClick(View v) {
21062135
TYPE_ALERT, //type of popup.
21072136
popupOkHandler,//override click listener for ok button
21082137
popupOkHandler);//pass null to use default listener
2138+
onFlashComplete();
21092139
}
2110-
removeReconnectionRunnable();
21112140
} else if(intent.getAction().equals(DfuService.BROADCAST_LOG)) {
21122141
//Only used for Stats at the moment
21132142
String data;

0 commit comments

Comments
 (0)