1 /*
2  * Copyright (C) 2020 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.server.location.injector;
18 
19 import android.os.PackageTagsList;
20 import android.os.UserHandle;
21 import android.util.IndentingPrintWriter;
22 import android.util.SparseArray;
23 
24 import com.android.internal.util.Preconditions;
25 
26 import java.io.FileDescriptor;
27 import java.util.Collections;
28 import java.util.Set;
29 import java.util.concurrent.CopyOnWriteArrayList;
30 
31 /**
32  * Version of SettingsHelper for testing. Settings are initialized to reasonable defaults (location
33  * is enabled by default).
34  */
35 public class FakeSettingsHelper extends SettingsHelper {
36 
37     private static class Setting {
38 
39         private final SparseArray<Object> mValues;
40         private final Object mDefaultValue;
41 
42         private final CopyOnWriteArrayList<UserSettingChangedListener> mListeners;
43 
Setting(Object defaultValue)44         Setting(Object defaultValue) {
45             mValues = new SparseArray<>();
46             mDefaultValue = defaultValue;
47             mListeners = new CopyOnWriteArrayList<>();
48         }
49 
addListener(UserSettingChangedListener listener)50         public void addListener(UserSettingChangedListener listener) {
51             mListeners.add(listener);
52         }
53 
removeListener(UserSettingChangedListener listener)54         public void removeListener(UserSettingChangedListener listener) {
55             mListeners.remove(listener);
56         }
57 
setValue(Object newValue)58         public void setValue(Object newValue) {
59             setValue(newValue, UserHandle.USER_NULL);
60         }
61 
setValue(Object newValue, int userId)62         public void setValue(Object newValue, int userId) {
63             Preconditions.checkState(userId == UserHandle.USER_NULL || userId >= 0);
64 
65             mValues.put(userId, newValue);
66 
67             for (UserSettingChangedListener listener : mListeners) {
68                 listener.onSettingChanged(userId);
69             }
70         }
71 
getValue(Class<T> clazz)72         public <T> T getValue(Class<T> clazz) {
73             return getValue(UserHandle.USER_NULL, clazz);
74         }
75 
getValue(int userId, Class<T> clazz)76         public <T> T getValue(int userId, Class<T> clazz) {
77             return clazz.cast(mValues.get(userId, mDefaultValue));
78         }
79     }
80 
81     private final Setting mLocationEnabledSetting = new Setting(Boolean.TRUE);
82     private final Setting mBackgroundThrottleIntervalSetting = new Setting(30 * 60 * 1000L);
83     private final Setting mLocationPackageBlacklistSetting = new Setting(Collections.emptySet());
84     private final Setting mBackgroundThrottlePackageWhitelistSetting = new Setting(
85             Collections.emptySet());
86     private final Setting mGnssMeasurementsFullTrackingSetting = new Setting(Boolean.FALSE);
87     private final Setting mAdasPackageAllowlist = new Setting(
88             new PackageTagsList.Builder().build());
89     private final Setting mIgnoreSettingsAllowlist = new Setting(
90             new PackageTagsList.Builder().build());
91     private final Setting mBackgroundThrottleProximityAlertIntervalSetting = new Setting(
92             30 * 60 * 1000L);
93     private final Setting mCoarseLocationAccuracySetting = new Setting(2000.0f);
94 
95     @Override
isLocationEnabled(int userId)96     public boolean isLocationEnabled(int userId) {
97         return mLocationEnabledSetting.getValue(userId, Boolean.class);
98     }
99 
100     @Override
setLocationEnabled(boolean enabled, int userId)101     public void setLocationEnabled(boolean enabled, int userId) {
102         mLocationEnabledSetting.setValue(enabled, userId);
103     }
104 
105     @Override
addOnLocationEnabledChangedListener(UserSettingChangedListener listener)106     public void addOnLocationEnabledChangedListener(UserSettingChangedListener listener) {
107         mLocationEnabledSetting.addListener(listener);
108     }
109 
110     @Override
removeOnLocationEnabledChangedListener(UserSettingChangedListener listener)111     public void removeOnLocationEnabledChangedListener(UserSettingChangedListener listener) {
112         mLocationEnabledSetting.removeListener(listener);
113     }
114 
115     @Override
getBackgroundThrottleIntervalMs()116     public long getBackgroundThrottleIntervalMs() {
117         return mBackgroundThrottleIntervalSetting.getValue(Long.class);
118     }
119 
setBackgroundThrottleIntervalMs(long backgroundThrottleIntervalMs)120     public void setBackgroundThrottleIntervalMs(long backgroundThrottleIntervalMs) {
121         mBackgroundThrottleIntervalSetting.setValue(backgroundThrottleIntervalMs);
122     }
123 
124     @Override
addOnBackgroundThrottleIntervalChangedListener( GlobalSettingChangedListener listener)125     public void addOnBackgroundThrottleIntervalChangedListener(
126             GlobalSettingChangedListener listener) {
127         mBackgroundThrottleIntervalSetting.addListener(listener);
128     }
129 
130     @Override
removeOnBackgroundThrottleIntervalChangedListener( GlobalSettingChangedListener listener)131     public void removeOnBackgroundThrottleIntervalChangedListener(
132             GlobalSettingChangedListener listener) {
133         mBackgroundThrottleIntervalSetting.removeListener(listener);
134     }
135 
136     @Override
isLocationPackageBlacklisted(int userId, String packageName)137     public boolean isLocationPackageBlacklisted(int userId, String packageName) {
138         return mLocationPackageBlacklistSetting.getValue(userId, Set.class).contains(packageName);
139     }
140 
setLocationPackageBlacklisted(int userId, Set<String> newSet)141     public void setLocationPackageBlacklisted(int userId, Set<String> newSet) {
142         mLocationPackageBlacklistSetting.setValue(newSet, userId);
143     }
144 
145     @Override
addOnLocationPackageBlacklistChangedListener(UserSettingChangedListener listener)146     public void addOnLocationPackageBlacklistChangedListener(UserSettingChangedListener listener) {
147         mLocationPackageBlacklistSetting.addListener(listener);
148     }
149 
150     @Override
removeOnLocationPackageBlacklistChangedListener( UserSettingChangedListener listener)151     public void removeOnLocationPackageBlacklistChangedListener(
152             UserSettingChangedListener listener) {
153         mLocationPackageBlacklistSetting.removeListener(listener);
154     }
155 
156     @Override
getBackgroundThrottlePackageWhitelist()157     public Set<String> getBackgroundThrottlePackageWhitelist() {
158         return mBackgroundThrottlePackageWhitelistSetting.getValue(Set.class);
159     }
160 
setBackgroundThrottlePackageWhitelist(Set<String> newSet)161     public void setBackgroundThrottlePackageWhitelist(Set<String> newSet) {
162         mBackgroundThrottlePackageWhitelistSetting.setValue(newSet);
163     }
164 
165     @Override
addOnBackgroundThrottlePackageWhitelistChangedListener( GlobalSettingChangedListener listener)166     public void addOnBackgroundThrottlePackageWhitelistChangedListener(
167             GlobalSettingChangedListener listener) {
168         mBackgroundThrottlePackageWhitelistSetting.addListener(listener);
169     }
170 
171     @Override
removeOnBackgroundThrottlePackageWhitelistChangedListener( GlobalSettingChangedListener listener)172     public void removeOnBackgroundThrottlePackageWhitelistChangedListener(
173             GlobalSettingChangedListener listener) {
174         mBackgroundThrottlePackageWhitelistSetting.removeListener(listener);
175     }
176 
177     @Override
isGnssMeasurementsFullTrackingEnabled()178     public boolean isGnssMeasurementsFullTrackingEnabled() {
179         return mGnssMeasurementsFullTrackingSetting.getValue(Boolean.class);
180     }
181 
setGnssMeasurementsFullTrackingEnabled(boolean newValue)182     public void setGnssMeasurementsFullTrackingEnabled(boolean newValue) {
183         mGnssMeasurementsFullTrackingSetting.setValue(newValue);
184     }
185 
186     @Override
addOnGnssMeasurementsFullTrackingEnabledChangedListener( GlobalSettingChangedListener listener)187     public void addOnGnssMeasurementsFullTrackingEnabledChangedListener(
188             GlobalSettingChangedListener listener) {
189         mGnssMeasurementsFullTrackingSetting.addListener(listener);
190     }
191 
192     @Override
removeOnGnssMeasurementsFullTrackingEnabledChangedListener( GlobalSettingChangedListener listener)193     public void removeOnGnssMeasurementsFullTrackingEnabledChangedListener(
194             GlobalSettingChangedListener listener) {
195         mGnssMeasurementsFullTrackingSetting.removeListener(listener);
196     }
197 
198     @Override
getAdasAllowlist()199     public PackageTagsList getAdasAllowlist() {
200         return mAdasPackageAllowlist.getValue(PackageTagsList.class);
201     }
202 
203     @Override
addAdasAllowlistChangedListener(GlobalSettingChangedListener listener)204     public void addAdasAllowlistChangedListener(GlobalSettingChangedListener listener) {
205         mAdasPackageAllowlist.addListener(listener);
206     }
207 
208     @Override
removeAdasAllowlistChangedListener(GlobalSettingChangedListener listener)209     public void removeAdasAllowlistChangedListener(GlobalSettingChangedListener listener) {
210         mAdasPackageAllowlist.removeListener(listener);
211     }
212 
213     @Override
getIgnoreSettingsAllowlist()214     public PackageTagsList getIgnoreSettingsAllowlist() {
215         return mIgnoreSettingsAllowlist.getValue(PackageTagsList.class);
216     }
217 
setAdasSettingsAllowlist(PackageTagsList newValue)218     public void setAdasSettingsAllowlist(PackageTagsList newValue) {
219         mAdasPackageAllowlist.setValue(newValue);
220     }
221 
setIgnoreSettingsAllowlist(PackageTagsList newValue)222     public void setIgnoreSettingsAllowlist(PackageTagsList newValue) {
223         mIgnoreSettingsAllowlist.setValue(newValue);
224     }
225 
226     @Override
addIgnoreSettingsAllowlistChangedListener( GlobalSettingChangedListener listener)227     public void addIgnoreSettingsAllowlistChangedListener(
228             GlobalSettingChangedListener listener) {
229         mIgnoreSettingsAllowlist.addListener(listener);
230     }
231 
232     @Override
removeIgnoreSettingsAllowlistChangedListener( GlobalSettingChangedListener listener)233     public void removeIgnoreSettingsAllowlistChangedListener(
234             GlobalSettingChangedListener listener) {
235         mIgnoreSettingsAllowlist.removeListener(listener);
236     }
237 
238     @Override
getBackgroundThrottleProximityAlertIntervalMs()239     public long getBackgroundThrottleProximityAlertIntervalMs() {
240         return mBackgroundThrottleProximityAlertIntervalSetting.getValue(Long.class);
241     }
242 
getBackgroundThrottleProximityAlertIntervalMs(long newValue)243     public void getBackgroundThrottleProximityAlertIntervalMs(long newValue) {
244         mBackgroundThrottleProximityAlertIntervalSetting.setValue(newValue);
245     }
246 
247     @Override
getCoarseLocationAccuracyM()248     public float getCoarseLocationAccuracyM() {
249         return mCoarseLocationAccuracySetting.getValue(Float.class);
250     }
251 
setCoarseLocationAccuracyM(float newValue)252     public void setCoarseLocationAccuracyM(float newValue) {
253         mCoarseLocationAccuracySetting.setValue(newValue);
254     }
255 
256     @Override
dump(FileDescriptor fd, IndentingPrintWriter ipw, String[] args)257     public void dump(FileDescriptor fd, IndentingPrintWriter ipw, String[] args) {}
258 }
259