1 /*
2  * Copyright 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.car.apps.common;
18 
19 
20 import android.annotation.NonNull;
21 import android.content.Context;
22 import android.content.res.Resources;
23 import android.os.SystemProperties;
24 
25 /** Singleton class regrouping common library feature flags. */
26 public class CommonFlags {
27 
28     private static final String FLAG_IMPROPER_IMAGE_REFS_KEY =
29             "com.android.car.apps.common.FlagNonLocalImages";
30 
31     @SuppressWarnings("StaticFieldLeak") // We store the application context, not an activity.
32     private static CommonFlags sInstance;
33 
34     /** Returns the singleton. */
getInstance(Context context)35     public static CommonFlags getInstance(Context context) {
36         if (sInstance == null) {
37             sInstance = new CommonFlags(context);
38         }
39         return sInstance;
40     }
41 
42     private final Context mApplicationContext;
43     private Boolean mFlagImproperImageRefs;
44 
CommonFlags(@onNull Context context)45     private CommonFlags(@NonNull Context context) {
46         mApplicationContext = context.getApplicationContext();
47     }
48 
49     /**
50      * Returns whether improper image references should be flagged (typically tinting the images
51      * in red with {@link R.color.improper_image_refs_tint_color}. This special mode is intended for
52      * third party app developers so they can notice quickly that they are sending improper image
53      * references. Such references include :
54      * <li>remote image instead of a local content uri</li>
55      * <li>bitmap sent over the binder instead of a local content uri</li>
56      * <li>bitmap icon instead of a vector drawable</li>
57      * <p/>
58      *
59      * To activate, either overlay R.bool.flag_improper_image_references to true, or use adb:
60      * <code>
61      *     adb root
62      *     adb shell setprop com.android.car.apps.common.FlagNonLocalImages 1
63      *     adb shell am force-stop APP_PACKAGE # eg: APP_PACKAGE= com.android.car.media
64      * </code>
65      */
shouldFlagImproperImageRefs()66     public boolean shouldFlagImproperImageRefs() {
67         if (mFlagImproperImageRefs == null) {
68             Resources res = mApplicationContext.getResources();
69             mFlagImproperImageRefs = res.getBoolean(R.bool.flag_improper_image_references)
70                     || "1".equals(SystemProperties.get(FLAG_IMPROPER_IMAGE_REFS_KEY, "0"));
71         }
72         return mFlagImproperImageRefs;
73     }
74 }
75