1 package android.provider.cts.contacts.testapp;
2 
3 import android.content.BroadcastReceiver;
4 import android.content.Context;
5 import android.content.Intent;
6 import android.content.SharedPreferences;
7 import android.util.Log;
8 
9 import java.util.concurrent.CompletableFuture;
10 import java.util.concurrent.TimeUnit;
11 import java.util.concurrent.TimeoutException;
12 
13 public class BootReceiver extends BroadcastReceiver {
14     private static final String LOG_TAG = "CallLogTestBootReceiver";
15     public static final String BOOT_COMPLETE = "boot_complete";
16     public static final String LOCKED_BOOT_COMPLETE = "locked_boot_complete";
17     public static final String SHARED_PREFS_NAME = "boot_complete_info";
18 
19     @Override
onReceive(Context context, Intent intent)20     public void onReceive(Context context, Intent intent) {
21         if (Intent.ACTION_LOCKED_BOOT_COMPLETED.equals(intent.getAction())) {
22             SharedPreferences prefs = context.createDeviceProtectedStorageContext()
23                     .getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE);
24             prefs.edit().putBoolean(LOCKED_BOOT_COMPLETE, true).commit();
25             Log.i(LOG_TAG, "Received locked boot complete");
26         } else if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
27             SharedPreferences dePrefs = context.createDeviceProtectedStorageContext()
28                     .getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE);
29             SharedPreferences cePrefs = context.createCredentialProtectedStorageContext()
30                     .getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE);
31             dePrefs.edit().putBoolean(BOOT_COMPLETE, true).commit();
32             cePrefs.edit().putBoolean(BOOT_COMPLETE, true)
33                     .putBoolean(LOCKED_BOOT_COMPLETE, true).commit();
34             Log.i(LOG_TAG, "Received boot complete");
35         }
36     }
37 
waitForBootComplete(Context context, String action, long timeoutMillis)38     public static boolean waitForBootComplete(Context context, String action, long timeoutMillis)
39             throws Exception {
40         SharedPreferences prefs =
41                 context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE);
42         CompletableFuture<Void> onBootCompleteChanged = new CompletableFuture<>();
43         prefs.registerOnSharedPreferenceChangeListener(
44                 (sharedPreferences, key) -> {
45                     if (action.equals(key)) {
46                         onBootCompleteChanged.complete(null);
47                     }
48                 });
49         if (prefs.getBoolean(action, false)) return true;
50         try {
51             onBootCompleteChanged.get(timeoutMillis, TimeUnit.MILLISECONDS);
52         } catch (TimeoutException e) {
53             Log.w(LOG_TAG, "Timed out waiting for " + action);
54             return false;
55         }
56         return true;
57     }
58 }
59