1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.ondevicepersonalization.services; 18 19 import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED; 20 21 import android.content.BroadcastReceiver; 22 import android.content.ComponentName; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.pm.PackageManager; 26 import android.util.Log; 27 28 import com.android.internal.annotations.VisibleForTesting; 29 import com.android.ondevicepersonalization.services.data.user.UserDataCollectionJobService; 30 import com.android.ondevicepersonalization.services.download.mdd.MobileDataDownloadFactory; 31 import com.android.ondevicepersonalization.services.maintenance.OnDevicePersonalizationMaintenanceJobService; 32 import com.android.ondevicepersonalization.services.policyengine.api.ChronicleManager; 33 import com.android.ondevicepersonalization.services.policyengine.data.impl.UserDataConnectionProvider; 34 import com.android.ondevicepersonalization.services.policyengine.policy.DataIngressPolicy; 35 36 import com.google.common.util.concurrent.FutureCallback; 37 import com.google.common.util.concurrent.Futures; 38 39 import java.util.Arrays; 40 import java.util.HashSet; 41 import java.util.concurrent.Executor; 42 43 /** 44 * BroadcastReceiver used to schedule OnDevicePersonalization jobs/workers. 45 */ 46 public class OnDevicePersonalizationBroadcastReceiver extends BroadcastReceiver { 47 private static final String TAG = "OnDevicePersonalizationBroadcastReceiver"; 48 private final Executor mExecutor; 49 OnDevicePersonalizationBroadcastReceiver()50 public OnDevicePersonalizationBroadcastReceiver() { 51 this.mExecutor = OnDevicePersonalizationExecutors.getLightweightExecutor(); 52 } 53 54 @VisibleForTesting OnDevicePersonalizationBroadcastReceiver(Executor executor)55 public OnDevicePersonalizationBroadcastReceiver(Executor executor) { 56 this.mExecutor = executor; 57 } 58 59 /** Enable the OnDevicePersonalizationBroadcastReceiver */ enableReceiver(Context context)60 public static boolean enableReceiver(Context context) { 61 try { 62 context.getPackageManager() 63 .setComponentEnabledSetting( 64 new ComponentName(context, 65 OnDevicePersonalizationBroadcastReceiver.class), 66 COMPONENT_ENABLED_STATE_ENABLED, 67 PackageManager.DONT_KILL_APP); 68 } catch (IllegalArgumentException e) { 69 Log.e(TAG, "enableService failed for " + context.getPackageName(), e); 70 return false; 71 } 72 return true; 73 } 74 75 /** 76 * Called when the broadcast is received. OnDevicePersonalization jobs will be started here. 77 */ onReceive(Context context, Intent intent)78 public void onReceive(Context context, Intent intent) { 79 Log.d(TAG, "onReceive() with intent + " + intent.getAction()); 80 if (!Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) { 81 Log.d(TAG, "Received unexpected intent " + intent.getAction()); 82 return; 83 } 84 85 // Initialize policy engine instance 86 ChronicleManager.getInstance( 87 new HashSet<>(Arrays.asList(new UserDataConnectionProvider())), 88 new HashSet<>(Arrays.asList(DataIngressPolicy.NPA_DATA_POLICY))); 89 90 // Schedule maintenance task 91 OnDevicePersonalizationMaintenanceJobService.schedule(context); 92 93 // Schedule user data collection task 94 UserDataCollectionJobService.schedule(context); 95 96 final PendingResult pendingResult = goAsync(); 97 // Schedule MDD to download scripts periodically. 98 Futures.addCallback( 99 MobileDataDownloadFactory.getMdd(context).schedulePeriodicBackgroundTasks(), 100 new FutureCallback<Void>() { 101 @Override 102 public void onSuccess(Void result) { 103 Log.d(TAG, "Successfully scheduled MDD tasks."); 104 pendingResult.finish(); 105 } 106 107 @Override 108 public void onFailure(Throwable t) { 109 Log.e(TAG, "Failed to schedule MDD tasks.", t); 110 pendingResult.finish(); 111 } 112 }, 113 mExecutor); 114 } 115 } 116