• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package com.android.angle.common;
17 
18 import android.content.ContentResolver;
19 import android.content.Context;
20 import android.content.SharedPreferences;
21 import android.content.pm.PackageInfo;
22 import android.provider.Settings;
23 import android.util.Log;
24 
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.List;
28 
29 import com.android.angle.R;
30 
31 class GlobalSettings
32 {
33     public static final String DRIVER_SELECTION_ANGLE = "angle";
34     public static final String DRIVER_SELECTION_DEFAULT = "default";
35     public static final String DRIVER_SELECTION_NATIVE = "native";
36 
37     private static final String TAG = "ANGLEGlobalSettings";
38     private static final boolean DEBUG = false;
39 
40     private static final String SHOW_ANGLE_IN_USE_DIALOG_BOX = "show_angle_in_use_dialog_box";
41     private static final String DRIVER_SELECTION_PACKAGES = "angle_gl_driver_selection_pkgs";
42     private static final String DRIVER_SELECTION_VALUES = "angle_gl_driver_selection_values";
43     private static final String ANGLE_DEBUG_PACKAGE = "angle_debug_package";
44 
45     private Context mContext;
46     private SharedPreferences mSharedPreferences;
47     private List<String> mDriverSelectionPackages = new ArrayList<>();
48     private List<String> mDriverSelectionValues = new ArrayList<>();
49 
GlobalSettings(Context context, SharedPreferences sharedPreferences, List<PackageInfo> installedPackages)50     GlobalSettings(Context context, SharedPreferences sharedPreferences,
51             List<PackageInfo> installedPackages)
52     {
53         mContext = context;
54         mSharedPreferences = sharedPreferences;
55 
56         initGlobalSettings(installedPackages);
57     }
58 
initGlobalSettings(List<PackageInfo> installedPackages)59     void initGlobalSettings(List<PackageInfo> installedPackages)
60     {
61         mDriverSelectionPackages.clear();
62         mDriverSelectionValues.clear();
63 
64         for (PackageInfo packageInfo : installedPackages)
65         {
66             final String packageName = packageInfo.packageName;
67             final String driverSelectionValue = mSharedPreferences.getString(packageName, DRIVER_SELECTION_DEFAULT);
68             if (driverSelectionValue.equals(DRIVER_SELECTION_DEFAULT))
69             {
70                 continue;
71             }
72             mDriverSelectionPackages.add(packageName);
73             mDriverSelectionValues.add(driverSelectionValue);
74         }
75         if (!mDriverSelectionPackages.isEmpty())
76         {
77             writeGlobalSettings();
78         }
79     }
80 
clearGlobalSettings(Context context)81     static void clearGlobalSettings(Context context)
82     {
83         // show_angle_in_use_dialog_box
84         updateShowAngleInUse(context, false);
85 
86         // clear angle_gl_driver_selection_pkgs, angle_gl_driver_selection_values
87         ContentResolver contentResolver = context.getContentResolver();
88         Settings.Global.putString(contentResolver, DRIVER_SELECTION_PACKAGES, "");
89         Settings.Global.putString(contentResolver, DRIVER_SELECTION_VALUES, "");
90 
91         // For completeness, we'll clear the angle_debug_package, but we don't allow setting
92         // it via the UI
93         Settings.Global.putString(contentResolver, ANGLE_DEBUG_PACKAGE, "");
94     }
95 
updateShowAngleInUse(Context context, Boolean showAngleInUse)96     static void updateShowAngleInUse(Context context, Boolean showAngleInUse)
97     {
98         if (DEBUG)
99         {
100             Log.v(TAG, "Show angle in use: " + showAngleInUse);
101         }
102         ContentResolver contentResolver = context.getContentResolver();
103         Settings.Global.putInt(contentResolver, SHOW_ANGLE_IN_USE_DIALOG_BOX,
104                 showAngleInUse ? 1 : 0);
105     }
106 
getShowAngleInUse()107     boolean getShowAngleInUse()
108     {
109         return Settings.Global.getInt(mContext.getContentResolver(),
110                 SHOW_ANGLE_IN_USE_DIALOG_BOX, 0) == 1;
111     }
112 
updatePackageDriverSelection(String packageName, String driverSelectionValue)113     void updatePackageDriverSelection(String packageName, String driverSelectionValue)
114     {
115         if (!isValidDriverSelectionValue(driverSelectionValue))
116         {
117             Log.v(TAG, "Attempting to update " + packageName
118                     + " with an invalid driver selection value: '" + driverSelectionValue + "'");
119             return;
120         }
121 
122         updateSharedPreferences(packageName, driverSelectionValue);
123         updatePackageDriverSelectionInternal(packageName, driverSelectionValue);
124         writeGlobalSettings();
125     }
126 
updateSharedPreferences(String packageName, String driverSelectionValue)127     private void updateSharedPreferences(String packageName, String driverSelectionValue)
128     {
129         final SharedPreferences.Editor editor = mSharedPreferences.edit();
130         if (driverSelectionValue.equals(DRIVER_SELECTION_DEFAULT))
131         {
132             editor.remove(packageName);
133             editor.apply();
134             return;
135         }
136         editor.putString(packageName, driverSelectionValue);
137         editor.apply();
138     }
139 
updatePackageDriverSelectionInternal(String packageName, String driverSelectionValue)140     private void updatePackageDriverSelectionInternal(String packageName, String driverSelectionValue)
141     {
142         final int packageIndex = getPackageIndex(packageName);
143         if (packageIndex < 0)
144         {
145             if (driverSelectionValue.equals(DRIVER_SELECTION_DEFAULT))
146             {
147                 return;
148             }
149             mDriverSelectionPackages.add(packageName);
150             mDriverSelectionValues.add(driverSelectionValue);
151             return;
152         }
153         if (driverSelectionValue.equals(DRIVER_SELECTION_DEFAULT))
154         {
155             mDriverSelectionPackages.remove(packageIndex);
156             mDriverSelectionValues.remove(packageIndex);
157             return;
158         }
159         mDriverSelectionValues.set(packageIndex, driverSelectionValue);
160     }
161 
getDriverSelectionValue(String packageName)162     String getDriverSelectionValue(String packageName)
163     {
164         final int packageIndex = getPackageIndex(packageName);
165 
166         return packageIndex >= 0
167                 ? mDriverSelectionValues.get(packageIndex) : DRIVER_SELECTION_DEFAULT;
168     }
169 
writeGlobalSettings()170     private void writeGlobalSettings()
171     {
172         final String driverSelectionPackages   = String.join(",", mDriverSelectionPackages);
173         final String driverSelectionValues = String.join(",", mDriverSelectionValues);
174 
175         final ContentResolver contentResolver = mContext.getContentResolver();
176         Settings.Global.putString(contentResolver,
177                 DRIVER_SELECTION_PACKAGES, driverSelectionPackages);
178         Settings.Global.putString(contentResolver, DRIVER_SELECTION_VALUES, driverSelectionValues);
179     }
180 
writeGlobalSettings(Context context, String driverSelectionPackages, String driverSelectionValues)181     static void writeGlobalSettings(Context context, String driverSelectionPackages,
182             String driverSelectionValues)
183     {
184         final ContentResolver contentResolver = context.getContentResolver();
185         Settings.Global.putString(contentResolver,
186                 DRIVER_SELECTION_PACKAGES, driverSelectionPackages);
187         Settings.Global.putString(contentResolver, DRIVER_SELECTION_VALUES, driverSelectionValues);
188     }
189 
getPackageIndex(String packageName)190     private int getPackageIndex(String packageName)
191     {
192         for (int i = 0; i < mDriverSelectionPackages.size(); i++)
193         {
194             if (mDriverSelectionPackages.get(i).equals(packageName))
195             {
196                 return i;
197             }
198         }
199 
200         return -1;
201     }
202 
isValidDriverSelectionValue(String driverSelectionValue)203     private boolean isValidDriverSelectionValue(String driverSelectionValue)
204     {
205         CharSequence[] drivers = mContext.getResources().getStringArray(R.array.driver_values);
206 
207         for (CharSequence driver : drivers)
208         {
209             if (driverSelectionValue.equals(driver.toString()))
210             {
211                 return true;
212             }
213         }
214 
215         return false;
216     }
217 }
218