|
4 | 4 | import android.content.Context; |
5 | 5 | import android.content.Intent; |
6 | 6 | import android.content.SharedPreferences; |
| 7 | +import android.util.Log; |
7 | 8 |
|
| 9 | +import java.util.Arrays; |
| 10 | +import java.util.HashSet; |
| 11 | +import java.util.Set; |
| 12 | + |
| 13 | +/** |
| 14 | + * Restarts uploading when the head unit powers up. |
| 15 | + * |
| 16 | + * BOOT_COMPLETED alone was not enough. An MG4's head unit rarely cold-boots: switching the |
| 17 | + * car on usually resumes it, and the resume is announced with QUICKBOOT_POWERON instead. |
| 18 | + * Users reported uploading never starting with the car, and this is why — the app was |
| 19 | + * waiting for a broadcast their car does not always send. The other MG4Suite apps already |
| 20 | + * listen to the whole family; this one now does too. |
| 21 | + * |
| 22 | + * MY_PACKAGE_REPLACED is in the list for a different reason: an update stops the service, |
| 23 | + * and nothing else would bring it back until the next power cycle. |
| 24 | + */ |
8 | 25 | public class BootReceiver extends BroadcastReceiver { |
9 | 26 |
|
| 27 | + private static final String TAG = "MG4ABRP.Boot"; |
| 28 | + |
| 29 | + private static final Set<String> START_ACTIONS = new HashSet<>(Arrays.asList( |
| 30 | + Intent.ACTION_BOOT_COMPLETED, |
| 31 | + Intent.ACTION_MY_PACKAGE_REPLACED, |
| 32 | + "android.intent.action.LOCKED_BOOT_COMPLETED", |
| 33 | + "android.intent.action.QUICKBOOT_POWERON", |
| 34 | + "com.htc.intent.action.QUICKBOOT_POWERON" |
| 35 | + )); |
| 36 | + |
10 | 37 | @Override |
11 | 38 | public void onReceive(Context context, Intent intent) { |
12 | | - if (!Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) return; |
| 39 | + String action = intent.getAction(); |
| 40 | + if (action == null || !START_ACTIONS.contains(action)) return; |
13 | 41 |
|
14 | 42 | SharedPreferences prefs = context.getSharedPreferences("abrp_prefs", Context.MODE_PRIVATE); |
15 | 43 | boolean enabled = prefs.getBoolean("service_enabled", false); |
16 | | - String token = prefs.getString("token", "").trim(); |
| 44 | + boolean autostart = prefs.getBoolean(UploadSettings.KEY_AUTOSTART, UploadSettings.DEFAULT_AUTOSTART); |
| 45 | + |
| 46 | + // Credentials come from SecurePrefs, not from the plaintext file. This receiver used |
| 47 | + // to read "token" out of abrp_prefs — where it has not lived since the credentials |
| 48 | + // were encrypted, because the migration deletes the plaintext copy. The check |
| 49 | + // therefore always failed, and uploading never started by itself on any car. |
| 50 | + SharedPreferences secure = SecurePrefs.get(context); |
| 51 | + boolean haveCreds = !secure.getString(SecurePrefs.KEY_TOKEN, "").trim().isEmpty() |
| 52 | + && !secure.getString(SecurePrefs.KEY_API_KEY, "").trim().isEmpty(); |
| 53 | + |
| 54 | + if (!enabled || !autostart || !haveCreds) { |
| 55 | + Log.i(TAG, action + " ignored (enabled=" + enabled |
| 56 | + + " autostart=" + autostart + " credentials=" + haveCreds + ")"); |
| 57 | + return; |
| 58 | + } |
17 | 59 |
|
18 | | - if (enabled && !token.isEmpty()) { |
| 60 | + Log.i(TAG, action + " — starting upload service"); |
| 61 | + try { |
19 | 62 | context.startForegroundService(new Intent(context, AbrpUploadService.class)); |
| 63 | + } catch (Exception e) { |
| 64 | + // A start refused this early is not worth crashing the boot broadcast over: the |
| 65 | + // next power cycle, or opening the app, starts it just the same. |
| 66 | + Log.w(TAG, "startForegroundService failed: " + e.getMessage()); |
20 | 67 | } |
21 | 68 | } |
22 | 69 | } |
0 commit comments