9
9
import android .app .ActivityManager ;
10
10
import android .bluetooth .BluetoothAdapter ;
11
11
import android .bluetooth .BluetoothDevice ;
12
- import android .content .ComponentName ;
13
12
import android .content .Context ;
14
13
import android .content .Intent ;
15
14
import android .content .SharedPreferences ;
16
- import android .os .IBinder ;
15
+ import android .os .Handler ;
16
+ import android .os .Looper ;
17
17
import android .util .Log ;
18
18
19
- import android .content .ServiceConnection ;
20
19
import androidx .lifecycle .LifecycleService ;
20
+ import androidx .lifecycle .Observer ;
21
21
import androidx .preference .PreferenceManager ;
22
22
23
23
import java .io .ByteArrayOutputStream ;
34
34
import cc .calliope .mini .ProgressCollector ;
35
35
import cc .calliope .mini .ProgressListener ;
36
36
import cc .calliope .mini .R ;
37
- import cc .calliope .mini .pf .Test ;
38
37
import cc .calliope .mini .core .state .ApplicationStateHandler ;
38
+ import cc .calliope .mini .core .state .Notification ;
39
39
import cc .calliope .mini .core .state .State ;
40
40
import cc .calliope .mini .utils .FileUtils ;
41
41
import cc .calliope .mini .utils .Preference ;
@@ -57,26 +57,46 @@ public class FlashingService extends LifecycleService implements ProgressListene
57
57
private int currentVersion ;
58
58
private String currentPath ;
59
59
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 ;
62
64
63
65
private record HexToDfu (String path , int size ) {
64
66
}
65
67
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
+
66
86
@ Override
67
87
public void onCreate () {
68
88
super .onCreate ();
69
89
90
+ ApplicationStateHandler .getStateLiveData ().observe (this , stateObserver );
91
+
70
92
ProgressCollector progressCollector = new ProgressCollector (this );
71
93
getLifecycle ().addObserver (progressCollector );
72
-
73
- //bindToTestService();
74
94
}
75
95
76
96
@ Override
77
97
public void onDestroy () {
78
- //unbindFromTestService();
79
98
super .onDestroy ();
99
+ ApplicationStateHandler .getStateLiveData ().removeObserver (stateObserver );
80
100
}
81
101
82
102
@ Override
@@ -136,6 +156,7 @@ public void onBluetoothBondingStateChanged(BluetoothDevice device, int bondState
136
156
}
137
157
138
158
159
+ @ SuppressWarnings ("MissingPermission" )
139
160
@ Override
140
161
public void onError (int code , String message ) {
141
162
if (code == 4110 ) {
@@ -172,8 +193,7 @@ private boolean isServiceRunning() {
172
193
ActivityManager activityManager = (ActivityManager ) getSystemService (Context .ACTIVITY_SERVICE );
173
194
for (ActivityManager .RunningServiceInfo service : activityManager .getRunningServices (Integer .MAX_VALUE )) {
174
195
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 ())) {
177
197
Utils .log (Log .ERROR , TAG , service .service .getClassName () + " is already running." );
178
198
return true ;
179
199
}
@@ -224,8 +244,25 @@ private void initFlashing(){
224
244
return ;
225
245
}
226
246
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
+
227
264
if (Settings .isPartialFlashingEnable (this )) {
228
- startPartialFlashing ();
265
+ // TODO: Implement partial flashing
229
266
} else {
230
267
if (currentVersion == MINI_V1 ) {
231
268
startDfuControlService ();
@@ -235,56 +272,8 @@ private void initFlashing(){
235
272
}
236
273
}
237
274
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
-
282
275
private void startDfuControlService () {
283
276
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);
288
277
Intent service = new Intent (this , LegacyDfuService .class );
289
278
service .putExtra (Constants .CURRENT_DEVICE_ADDRESS , currentAddress );
290
279
startService (service );
0 commit comments