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