Skip to content

Commit 0ecef54

Browse files
committed
Merge remote-tracking branch 'origin/dev'
2 parents a364a41 + 75b594a commit 0ecef54

35 files changed

+72
-3789
lines changed

app/build.gradle

-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ android {
3737
dependencies {
3838
implementation 'no.nordicsemi.android.kotlin.ble:scanner:1.1.0'
3939
implementation 'no.nordicsemi.android:dfu:2.5.0'
40-
implementation project(':partial-flashing-lib')
4140

4241
implementation 'commons-io:commons-io:2.16.1'
4342
implementation 'org.apache.commons:commons-lang3:3.15.0'

app/src/main/AndroidManifest.xml

+4-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
xmlns:android="http://schemas.android.com/apk/res/android"
44
xmlns:tools="http://schemas.android.com/tools" >
55

6+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE" />
7+
68
<!--
79
Notification
810
-->
@@ -107,13 +109,10 @@
107109
<activity
108110
android:name=".activity.SettingsActivity"
109111
android:exported="false" />
110-
111112
<service
112113
android:name=".core.service.DfuService"
113-
android:enabled="true" />
114-
<service
115-
android:name=".core.service.PartialFlashingService"
116-
android:enabled="true" />
114+
android:enabled="true"
115+
android:foregroundServiceType="connectedDevice" />
117116
<service
118117
android:name=".core.service.FlashingService"
119118
android:enabled="true" />
@@ -123,9 +122,6 @@
123122
<service
124123
android:name=".core.service.LegacyDfuService"
125124
android:enabled="true" />
126-
<service
127-
android:name=".pf.Test"
128-
android:enabled="true" />
129125

130126
</application>
131127
</manifest>

app/src/main/java/cc/calliope/mini/ProgressCollector.java

-37
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
import android.content.Intent;
88
import android.content.IntentFilter;
99

10-
11-
import org.microbit.android.partialflashing.PartialFlashingBaseService;
12-
1310
import androidx.annotation.NonNull;
1411
import androidx.lifecycle.DefaultLifecycleObserver;
1512
import androidx.lifecycle.LifecycleOwner;
@@ -96,30 +93,6 @@ public void onReceive(Context context, Intent intent) {
9693
}
9794
};
9895

99-
private final BroadcastReceiver partialFlashingServiceReceiver = new BroadcastReceiver() {
100-
@Override
101-
public void onReceive(Context context, Intent intent) {
102-
final String action = intent.getAction();
103-
if (action == null) {
104-
return;
105-
}
106-
107-
switch (action) {
108-
case PartialFlashingBaseService.BROADCAST_PROGRESS -> {
109-
int percent = intent.getIntExtra(PartialFlashingBaseService.EXTRA_PROGRESS, 0);
110-
listener.onProgressUpdate(percent);
111-
}
112-
case PartialFlashingBaseService.BROADCAST_START ->
113-
listener.onProgressUpdate(DfuService.PROGRESS_STARTING);
114-
case PartialFlashingBaseService.BROADCAST_COMPLETE ->
115-
listener.onProgressUpdate(DfuService.PROGRESS_COMPLETED);
116-
case PartialFlashingBaseService.BROADCAST_PF_FAILED ->
117-
listener.onError(-1, "Partial Flashing FAILED");
118-
case PartialFlashingBaseService.BROADCAST_PF_ATTEMPT_DFU -> listener.onDfuAttempt();
119-
}
120-
}
121-
};
122-
12396
public ProgressCollector(Context context) {
12497
super(context);
12598
this.context = context;
@@ -161,21 +134,11 @@ public void registerReceivers() {
161134
IntentFilter dfuControlServiceFilter = new IntentFilter();
162135
dfuControlServiceFilter.addAction(LegacyDfuService.BROADCAST_COMPLETED);
163136
LocalBroadcastManager.getInstance(context).registerReceiver(dfuControlServiceReceiver, dfuControlServiceFilter);
164-
165-
//PartialFlashingService
166-
IntentFilter partialFlashingServiceFilter = new IntentFilter();
167-
partialFlashingServiceFilter.addAction(PartialFlashingBaseService.BROADCAST_PROGRESS);
168-
partialFlashingServiceFilter.addAction(PartialFlashingBaseService.BROADCAST_START);
169-
partialFlashingServiceFilter.addAction(PartialFlashingBaseService.BROADCAST_COMPLETE);
170-
partialFlashingServiceFilter.addAction(PartialFlashingBaseService.BROADCAST_PF_FAILED);
171-
partialFlashingServiceFilter.addAction(PartialFlashingBaseService.BROADCAST_PF_ATTEMPT_DFU);
172-
LocalBroadcastManager.getInstance(context).registerReceiver(partialFlashingServiceReceiver, partialFlashingServiceFilter);
173137
}
174138

175139
public void unregisterReceivers() {
176140
unregisterReceiver(bondStateReceiver);
177141
LocalBroadcastManager.getInstance(context).unregisterReceiver(dfuServiceReceiver);
178142
LocalBroadcastManager.getInstance(context).unregisterReceiver(dfuControlServiceReceiver);
179-
LocalBroadcastManager.getInstance(context).unregisterReceiver(partialFlashingServiceReceiver);
180143
}
181144
}

app/src/main/java/cc/calliope/mini/Aggregator.kt app/src/main/java/cc/calliope/mini/core/bluetooth/Aggregator.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
package cc.calliope.mini
1+
package cc.calliope.mini.core.bluetooth
22

3-
import android.util.Log
4-
import cc.calliope.mini.utils.Utils
53
import no.nordicsemi.android.kotlin.ble.core.ServerDevice
64
import no.nordicsemi.android.kotlin.ble.core.scanner.BleScanResult
75
import no.nordicsemi.android.kotlin.ble.core.scanner.BleScanResultData

app/src/main/java/cc/calliope/mini/CurrentDevice.kt app/src/main/java/cc/calliope/mini/core/bluetooth/CurrentDevice.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cc.calliope.mini
1+
package cc.calliope.mini.core.bluetooth
22

33
import android.os.Parcel
44
import android.os.Parcelable

app/src/main/java/cc/calliope/mini/DeviceKt.kt app/src/main/java/cc/calliope/mini/core/bluetooth/DeviceKt.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cc.calliope.mini
1+
package cc.calliope.mini.core.bluetooth
22

33
import android.os.Build
44
import android.os.SystemClock

app/src/main/java/cc/calliope/mini/ScanViewModelKt.kt app/src/main/java/cc/calliope/mini/core/bluetooth/ScanViewModelKt.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cc.calliope.mini
1+
package cc.calliope.mini.core.bluetooth
22

33
import android.app.Application
44
import android.util.Log
@@ -18,7 +18,6 @@ import no.nordicsemi.android.kotlin.ble.core.scanner.BleScannerCallbackType
1818
import no.nordicsemi.android.kotlin.ble.core.scanner.BleScannerMatchMode
1919
import no.nordicsemi.android.kotlin.ble.core.scanner.BleScannerSettings
2020
import no.nordicsemi.android.kotlin.ble.scanner.BleScanner
21-
import no.nordicsemi.android.kotlin.ble.scanner.aggregator.BleScanResultAggregator
2221

2322

2423
class ScanViewModelKt(application: Application) : AndroidViewModel(application) {

app/src/main/java/cc/calliope/mini/core/service/FlashingService.java

+49-60
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
import android.app.ActivityManager;
1010
import android.bluetooth.BluetoothAdapter;
1111
import android.bluetooth.BluetoothDevice;
12-
import android.content.ComponentName;
1312
import android.content.Context;
1413
import android.content.Intent;
1514
import android.content.SharedPreferences;
16-
import android.os.IBinder;
15+
import android.os.Handler;
16+
import android.os.Looper;
1717
import android.util.Log;
1818

19-
import android.content.ServiceConnection;
2019
import androidx.lifecycle.LifecycleService;
20+
import androidx.lifecycle.Observer;
2121
import androidx.preference.PreferenceManager;
2222

2323
import java.io.ByteArrayOutputStream;
@@ -34,8 +34,8 @@
3434
import cc.calliope.mini.ProgressCollector;
3535
import cc.calliope.mini.ProgressListener;
3636
import cc.calliope.mini.R;
37-
import cc.calliope.mini.pf.Test;
3837
import cc.calliope.mini.core.state.ApplicationStateHandler;
38+
import cc.calliope.mini.core.state.Notification;
3939
import cc.calliope.mini.core.state.State;
4040
import cc.calliope.mini.utils.FileUtils;
4141
import cc.calliope.mini.utils.Preference;
@@ -57,26 +57,46 @@ public class FlashingService extends LifecycleService implements ProgressListene
5757
private int currentVersion;
5858
private String currentPath;
5959
private int progress = -10;
60-
private Test testService;
61-
private boolean isBound = false;
60+
61+
private static final int CONNECTION_TIMEOUT = 10000; // 10 seconds
62+
private Handler handler;
63+
private Runnable timeoutRunnable;
6264

6365
private record HexToDfu(String path, int size) {
6466
}
6567

68+
private final Observer<State> stateObserver = new Observer<>() {
69+
@Override
70+
public void onChanged(State state) {
71+
if (state == null) {
72+
return;
73+
}
74+
75+
int type = state.getType();
76+
77+
if (type == State.STATE_READY ||
78+
type == State.STATE_FLASHING ||
79+
type == State.STATE_ERROR ||
80+
type == State.STATE_IDLE) {
81+
handler.removeCallbacks(timeoutRunnable); // Remove the timeout callback
82+
}
83+
}
84+
};
85+
6686
@Override
6787
public void onCreate() {
6888
super.onCreate();
6989

90+
ApplicationStateHandler.getStateLiveData().observe(this, stateObserver);
91+
7092
ProgressCollector progressCollector = new ProgressCollector(this);
7193
getLifecycle().addObserver(progressCollector);
72-
73-
//bindToTestService();
7494
}
7595

7696
@Override
7797
public void onDestroy() {
78-
//unbindFromTestService();
7998
super.onDestroy();
99+
ApplicationStateHandler.getStateLiveData().removeObserver(stateObserver);
80100
}
81101

82102
@Override
@@ -136,6 +156,7 @@ public void onBluetoothBondingStateChanged(BluetoothDevice device, int bondState
136156
}
137157

138158

159+
@SuppressWarnings("MissingPermission")
139160
@Override
140161
public void onError(int code, String message) {
141162
if (code == 4110) {
@@ -172,8 +193,7 @@ private boolean isServiceRunning() {
172193
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
173194
for (ActivityManager.RunningServiceInfo service : activityManager.getRunningServices(Integer.MAX_VALUE)) {
174195
if (LegacyDfuService.class.getName().equals(service.service.getClassName()) ||
175-
DfuService.class.getName().equals(service.service.getClassName()) ||
176-
PartialFlashingService.class.getName().equals(service.service.getClassName())) {
196+
DfuService.class.getName().equals(service.service.getClassName())) {
177197
Utils.log(Log.ERROR, TAG, service.service.getClassName() + " is already running.");
178198
return true;
179199
}
@@ -224,8 +244,25 @@ private void initFlashing(){
224244
return;
225245
}
226246

247+
// Initialize the handler and timeout runnable
248+
handler = new Handler(Looper.getMainLooper());
249+
timeoutRunnable = new Runnable() {
250+
@Override
251+
public void run() {
252+
if(currentVersion == MINI_V1) {
253+
ApplicationStateHandler.updateNotification(Notification.WARNING, getString(R.string.flashing_timeout_v1));
254+
} else {
255+
ApplicationStateHandler.updateNotification(Notification.WARNING, getString(R.string.flashing_timeout_v2));
256+
}
257+
}
258+
};
259+
260+
// Start the 10 second timeout countdown
261+
handler.postDelayed(timeoutRunnable, CONNECTION_TIMEOUT);
262+
263+
227264
if (Settings.isPartialFlashingEnable(this)) {
228-
startPartialFlashing();
265+
// TODO: Implement partial flashing
229266
} else {
230267
if(currentVersion == MINI_V1) {
231268
startDfuControlService();
@@ -235,56 +272,8 @@ private void initFlashing(){
235272
}
236273
}
237274

238-
private void startPartialFlashing() {
239-
Utils.log(TAG, "Starting PartialFlashing Service...");
240-
241-
Intent service = new Intent(this, Test.class);
242-
service.putExtra("deviceAddress", currentAddress);
243-
service.putExtra("filePath", currentPath); // a path or URI must be provided.
244-
service.putExtra("hardwareType", currentVersion);
245-
startService(service);
246-
}
247-
248-
private final ServiceConnection connection = new ServiceConnection() {
249-
@Override
250-
public void onServiceConnected(ComponentName className, IBinder service) {
251-
Test.LocalBinder binder = (Test.LocalBinder) service;
252-
testService = binder.getService();
253-
isBound = true;
254-
255-
testService.getProgressData().observeForever(progress -> {
256-
Utils.log(Log.ASSERT, TAG, "Progress: " + progress);
257-
});
258-
259-
testService.getServiceState().observeForever(state -> {
260-
Utils.log(Log.ASSERT, TAG, "State: " + state);
261-
});
262-
}
263-
264-
@Override
265-
public void onServiceDisconnected(ComponentName arg0) {
266-
isBound = false;
267-
}
268-
};
269-
270-
public void bindToTestService() {
271-
Intent intent = new Intent(this, Test.class);
272-
bindService(intent, connection, Context.BIND_AUTO_CREATE);
273-
}
274-
275-
public void unbindFromTestService() {
276-
if (isBound) {
277-
unbindService(connection);
278-
isBound = false;
279-
}
280-
}
281-
282275
private void startDfuControlService() {
283276
Utils.log(TAG, "Starting DfuControl Service...");
284-
285-
// Intent service = new Intent(this, DfuControlService.class);
286-
// service.putExtra(StaticExtras.CURRENT_DEVICE_ADDRESS, currentAddress);
287-
// startService(service);
288277
Intent service = new Intent(this, LegacyDfuService.class);
289278
service.putExtra(Constants.CURRENT_DEVICE_ADDRESS, currentAddress);
290279
startService(service);

app/src/main/java/cc/calliope/mini/core/service/PartialFlashingService.java

-29
This file was deleted.

app/src/main/java/cc/calliope/mini/dialog/pattern/PatternDialogFragment.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
import androidx.fragment.app.DialogFragment;
3232
import androidx.lifecycle.ViewModelProvider;
3333

34-
import cc.calliope.mini.DeviceKt;
34+
import cc.calliope.mini.core.bluetooth.DeviceKt;
3535
import cc.calliope.mini.core.service.BondingService;
3636
import cc.calliope.mini.views.PatternMatrixView;
37-
import cc.calliope.mini.ScanViewModelKt;
37+
import cc.calliope.mini.core.bluetooth.ScanViewModelKt;
3838
import cc.calliope.mini.core.state.Notification;
3939
import cc.calliope.mini.core.state.State;
4040
import cc.calliope.mini.core.state.ApplicationStateHandler;

app/src/main/java/cc/calliope/mini/dialog/scripts/ScriptsFragment.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ private void renameFile(FileWrapper file) {
219219
}
220220

221221
private void removeFile(FileWrapper file) {
222-
String title = getResources().getString(R.string.title_dialog_rename);
222+
String title = getResources().getString(R.string.title_dialog_delete);
223223
String message = String.format(getString(R.string.info_dialog_delete), FilenameUtils.removeExtension(file.getName()));
224224

225225
DialogUtils.showWarningDialog(activity, title, message, () -> {

app/src/main/java/cc/calliope/mini/WebInfoFragment.kt app/src/main/java/cc/calliope/mini/fragment/WebInfoFragment.kt

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
package cc.calliope.mini
1+
package cc.calliope.mini.fragment
22

3-
import android.app.UiModeManager
4-
import android.content.Context
5-
import android.content.res.Configuration
63
import android.os.Bundle
74
import android.view.LayoutInflater
85
import android.view.View
96
import android.view.ViewGroup
107
import android.webkit.WebView
118
import androidx.fragment.app.Fragment
9+
import cc.calliope.mini.R
1210
import java.util.Locale
1311

1412
class WebInfoFragment : Fragment() {

0 commit comments

Comments
 (0)