• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 android.support.v17.leanback.system;
18 
19 import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP;
20 
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.pm.ApplicationInfo;
24 import android.content.pm.PackageManager;
25 import android.content.pm.ResolveInfo;
26 import android.content.res.Resources;
27 import android.os.Build;
28 import android.support.annotation.RestrictTo;
29 import android.support.v17.leanback.widget.ShadowOverlayContainer;
30 import android.util.Log;
31 
32 /**
33  * Provides various preferences affecting Leanback runtime behavior.
34  * <p>Note this class is not thread safe and its methods should only
35  * be invoked from the UI thread
36  * </p>
37  */
38 public class Settings {
39     static private final String TAG = "Settings";
40     static private final boolean DEBUG = false;
41 
42     // The intent action that must be provided by a broadcast receiver
43     // in a customization package.
44     private static final String ACTION_PARTNER_CUSTOMIZATION =
45             "android.support.v17.leanback.action.PARTNER_CUSTOMIZATION";
46 
47     public static final String PREFER_STATIC_SHADOWS = "PREFER_STATIC_SHADOWS";
48 
49     public static final String OUTLINE_CLIPPING_DISABLED = "OUTLINE_CLIPPING_DISABLED";
50 
51     static private Settings sInstance;
52 
53     private boolean mPreferStaticShadows;
54     private boolean mOutlineClippingDisabled;
55 
56     /**
57      * Returns the singleton Settings instance.
58      */
getInstance(Context context)59     static public Settings getInstance(Context context) {
60         if (sInstance == null) {
61             sInstance = new Settings(context);
62         }
63         return sInstance;
64     }
65 
Settings(Context context)66     private Settings(Context context) {
67         if (DEBUG) Log.v(TAG, "generating preferences");
68         Customizations customizations = getCustomizations(context);
69         generateSetting(customizations);
70     }
71 
72     /**
73      * Returns true if static shadows are recommended.
74      * @hide
75      */
76     @RestrictTo(LIBRARY_GROUP)
preferStaticShadows()77     public boolean preferStaticShadows() {
78         return mPreferStaticShadows;
79     }
80 
81     /**
82      * Returns true if view outline is disabled on low power chipset.
83      * @hide
84      */
85     @RestrictTo(LIBRARY_GROUP)
isOutlineClippingDisabled()86     public boolean isOutlineClippingDisabled() {
87         return mOutlineClippingDisabled;
88     }
89 
90     /**
91      * Returns the boolean preference for the given key.
92      */
getBoolean(String key)93     public boolean getBoolean(String key) {
94         return getOrSetBoolean(key, false, false);
95     }
96 
97     /**
98      * Sets the boolean preference for the given key.  If an app uses this api to override
99      * a default preference, it must do so on every activity create.
100      */
setBoolean(String key, boolean value)101     public void setBoolean(String key, boolean value) {
102         getOrSetBoolean(key, true, value);
103     }
104 
getOrSetBoolean(String key, boolean set, boolean value)105     boolean getOrSetBoolean(String key, boolean set, boolean value) {
106         if (key.compareTo(PREFER_STATIC_SHADOWS) == 0) {
107             return set ? (mPreferStaticShadows = value) : mPreferStaticShadows;
108         } else if (key.compareTo(OUTLINE_CLIPPING_DISABLED) == 0) {
109             return set ? (mOutlineClippingDisabled = value) : mOutlineClippingDisabled;
110         }
111         throw new IllegalArgumentException("Invalid key");
112     }
113 
generateSetting(Customizations customizations)114     private void generateSetting(Customizations customizations) {
115         if (ShadowOverlayContainer.supportsDynamicShadow()) {
116             mPreferStaticShadows = false;
117             if (customizations != null) {
118                 mPreferStaticShadows = customizations.getBoolean(
119                         "leanback_prefer_static_shadows", mPreferStaticShadows);
120             }
121         } else {
122             mPreferStaticShadows = true;
123         }
124 
125         if (Build.VERSION.SDK_INT >= 21) {
126             mOutlineClippingDisabled = false;
127             if (customizations != null) {
128                 mOutlineClippingDisabled = customizations.getBoolean(
129                         "leanback_outline_clipping_disabled", mOutlineClippingDisabled);
130             }
131         } else {
132             mOutlineClippingDisabled = true;
133         }
134         if (DEBUG) Log.v(TAG, "generated preference " + PREFER_STATIC_SHADOWS + ": "
135                 + mPreferStaticShadows + " "
136                 + OUTLINE_CLIPPING_DISABLED + " : " + mOutlineClippingDisabled);
137     }
138 
139     static class Customizations {
140         Resources mResources;
141         String mPackageName;
142 
Customizations(Resources resources, String packageName)143         public Customizations(Resources resources, String packageName) {
144             mResources = resources;
145             mPackageName = packageName;
146         }
147 
getBoolean(String resourceName, boolean defaultValue)148         public boolean getBoolean(String resourceName, boolean defaultValue) {
149             int resId = mResources.getIdentifier(resourceName, "bool", mPackageName);
150             return resId > 0 ? mResources.getBoolean(resId) : defaultValue;
151         }
152     };
153 
getCustomizations(Context context)154     private Customizations getCustomizations(Context context) {
155         final PackageManager pm = context.getPackageManager();
156         final Intent intent = new Intent(ACTION_PARTNER_CUSTOMIZATION);
157         if (DEBUG) {
158             Log.v(TAG, "getting oem customizations by intent: " + ACTION_PARTNER_CUSTOMIZATION);
159         }
160 
161         Resources resources = null;
162         String packageName = null;
163         for (ResolveInfo info : pm.queryBroadcastReceivers(intent, 0)) {
164             packageName = info.activityInfo.packageName;
165             if (DEBUG) Log.v(TAG, "got package " + packageName);
166             if (packageName != null && isSystemApp(info)) try {
167                 resources = pm.getResourcesForApplication(packageName);
168             } catch (PackageManager.NameNotFoundException ex) {
169                 // Do nothing
170             }
171             if (resources != null) {
172                 if (DEBUG) Log.v(TAG, "found customization package: " + packageName);
173                 break;
174             }
175         }
176         return resources == null ? null : new Customizations(resources, packageName);
177     }
178 
isSystemApp(ResolveInfo info)179     private static boolean isSystemApp(ResolveInfo info) {
180         return (info.activityInfo != null
181                 && (info.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
182     }
183 }
184