• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.documentsui;
18 
19 import static com.android.documentsui.base.SharedMinimal.DEBUG;
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.om.OverlayInfo;
26 import android.content.om.OverlayManager;
27 import android.content.pm.PackageManager;
28 import android.content.res.Resources;
29 import android.util.Log;
30 
31 import com.android.documentsui.theme.ThemeOverlayManager;
32 
33 /**
34  * A receiver listening action.PRE_BOOT_COMPLETED event for setting component enable or disable.
35  * Since there's limitation of overlay AndroidManifest.xml attrs at boot stage.
36  * The workaround to retrieve config from DocumentsUI RRO package at boot time in Q.
37  */
38 public class PreBootReceiver extends BroadcastReceiver {
39 
40     private static final String TAG = "PreBootReceiver";
41     private static final String CONFIG_IS_LAUNCHER_ENABLED = "is_launcher_enabled";
42     private static final String CONFIG_HANDLE_VIEW_DOWNLOADS = "handle_view_downloads_intent";
43     private static final String LAUNCHER_TARGET_CLASS = "com.android.documentsui.LauncherActivity";
44     private static final String DOWNLOADS_TARGET_CLASS =
45             "com.android.documentsui.ViewDownloadsActivity";
46 
47     @Override
onReceive(Context context, Intent intent)48     public void onReceive(Context context, Intent intent) {
49         final PackageManager pm = context.getPackageManager();
50         if (pm == null) {
51             Log.w(TAG, "Can't obtain PackageManager from System Service!");
52             return;
53         }
54 
55         final OverlayManager om = context.getSystemService(OverlayManager.class);
56         if (om == null) {
57             Log.w(TAG, "Can't obtain OverlayManager from System Service!");
58             return;
59         }
60 
61         final OverlayInfo info = new ThemeOverlayManager(om,
62                 context.getPackageName()).getValidOverlay(pm);
63 
64         if (info == null) {
65             Log.w(TAG, "Can't get valid overlay info");
66             return;
67         }
68 
69         final String overlayPkg = info.getPackageName();
70         final String packageName = context.getPackageName();
71 
72         Resources overlayRes;
73         try {
74             overlayRes = pm.getResourcesForApplication(overlayPkg);
75         } catch (PackageManager.NameNotFoundException e) {
76             Log.w(TAG, "Failed while parse package res.");
77             overlayRes = null;
78         }
79         if (overlayRes == null) {
80             return;
81         }
82 
83         setComponentEnabledByConfigResources(pm, packageName, LAUNCHER_TARGET_CLASS,
84                 overlayPkg, overlayRes, CONFIG_IS_LAUNCHER_ENABLED);
85         setComponentEnabledByConfigResources(pm, packageName, DOWNLOADS_TARGET_CLASS,
86                 overlayPkg, overlayRes, CONFIG_HANDLE_VIEW_DOWNLOADS);
87     }
88 
setComponentEnabledByConfigResources(PackageManager pm, String packageName, String className, String overlayPkg, Resources overlayRes, String config)89     private static void setComponentEnabledByConfigResources(PackageManager pm, String packageName,
90             String className, String overlayPkg, Resources overlayRes, String config) {
91         int resId = overlayRes.getIdentifier(config, "bool", overlayPkg);
92         if (resId != 0) {
93             final ComponentName component = new ComponentName(packageName, className);
94             final boolean value = overlayRes.getBoolean(resId);
95             if (DEBUG) {
96                 Log.i(TAG, "Overlay package:" + overlayPkg + ", customize " + config + ":" + value);
97             }
98             pm.setComponentEnabledSetting(component, value
99                             ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
100                             : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
101                     PackageManager.DONT_KILL_APP);
102         }
103     }
104 }
105