1 /*
2  * Copyright 2017 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 package androidx.work.impl.utils;
17 
18 import android.content.ComponentName;
19 import android.content.Context;
20 import android.content.pm.PackageManager;
21 import android.support.annotation.NonNull;
22 import android.util.Log;
23 
24 /**
25  * Helper class for common {@link PackageManager} functions
26  */
27 
28 public class PackageManagerHelper {
29     private static final String TAG = "PackageManagerHelper";
30 
PackageManagerHelper()31     private PackageManagerHelper() {
32     }
33 
34     /**
35      * Uses {@link PackageManager} to enable/disable a manifest-defined component
36      *
37      * @param context {@link Context}
38      * @param klazz The class of component
39      * @param enabled {@code true} if component should be enabled
40      */
setComponentEnabled( @onNull Context context, @NonNull Class klazz, boolean enabled)41     public static void setComponentEnabled(
42             @NonNull Context context,
43             @NonNull Class klazz,
44             boolean enabled) {
45         try {
46             PackageManager packageManager = context.getPackageManager();
47             ComponentName componentName = new ComponentName(context, klazz.getName());
48             packageManager.setComponentEnabledSetting(componentName,
49                     enabled
50                             ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
51                             : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
52                     PackageManager.DONT_KILL_APP);
53 
54             Log.d(TAG, String.format("%s %s", klazz.getName(), (enabled ? "enabled" : "disabled")));
55         } catch (Exception exception) {
56             Log.d(TAG, String.format("%s could not be %s", klazz.getName(),
57                     (enabled ? "enabled" : "disabled")), exception);
58         }
59     }
60 
61     /**
62      * Convenience method for {@link #isComponentExplicitlyEnabled(Context, String)}
63      */
isComponentExplicitlyEnabled(Context context, Class klazz)64     public static boolean isComponentExplicitlyEnabled(Context context, Class klazz) {
65         return isComponentExplicitlyEnabled(context, klazz.getName());
66     }
67 
68     /**
69      * Checks if a manifest-defined component is explicitly enabled
70      *
71      * @param context {@link Context}
72      * @param className {@link Class#getName()} name of component
73      * @return {@code true} if component is explicitly enabled
74      */
isComponentExplicitlyEnabled(Context context, String className)75     public static boolean isComponentExplicitlyEnabled(Context context, String className) {
76         PackageManager packageManager = context.getPackageManager();
77         ComponentName componentName = new ComponentName(context, className);
78         int state = packageManager.getComponentEnabledSetting(componentName);
79         return state == PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
80     }
81 }
82