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