blob: a983adccadc4c022100d4444d5a58bc102fce5a8 [file] [log] [blame]
Tony Huangc39b0b22019-06-10 16:55:20 +08001/*
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
17package com.android.documentsui;
18
19import static com.android.documentsui.base.SharedMinimal.DEBUG;
20
21import android.content.BroadcastReceiver;
22import android.content.ComponentName;
23import android.content.Context;
24import android.content.Intent;
25import android.content.om.OverlayInfo;
26import android.content.om.OverlayManager;
27import android.content.pm.PackageManager;
28import android.content.res.Resources;
29import android.util.Log;
30
31import 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 */
38public 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
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
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}