Yu-Han Yang | 66c7ea9 | 2018-03-11 17:17:15 -0700 | [diff] [blame] | 1 | package com.android.server.location; |
| 2 | |
| 3 | import android.content.ContentResolver; |
| 4 | import android.content.Context; |
| 5 | import android.database.ContentObserver; |
| 6 | import android.os.Handler; |
| 7 | import android.os.Looper; |
| 8 | import android.os.UserHandle; |
| 9 | import android.provider.Settings; |
| 10 | import android.util.Log; |
| 11 | |
| 12 | import com.android.internal.annotations.VisibleForTesting; |
| 13 | |
| 14 | import java.util.ArrayList; |
| 15 | import java.util.List; |
| 16 | |
| 17 | /** |
| 18 | * Detects blacklist change and updates the blacklist. |
| 19 | */ |
| 20 | class GnssSatelliteBlacklistHelper { |
| 21 | |
| 22 | private static final String TAG = "GnssBlacklistHelper"; |
| 23 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
| 24 | private static final String BLACKLIST_DELIMITER = ","; |
| 25 | |
| 26 | private final Context mContext; |
| 27 | private final GnssSatelliteBlacklistCallback mCallback; |
| 28 | |
| 29 | interface GnssSatelliteBlacklistCallback { |
| 30 | void onUpdateSatelliteBlacklist(int[] constellations, int[] svids); |
| 31 | } |
| 32 | |
| 33 | GnssSatelliteBlacklistHelper(Context context, Looper looper, |
| 34 | GnssSatelliteBlacklistCallback callback) { |
| 35 | mContext = context; |
| 36 | mCallback = callback; |
| 37 | ContentObserver contentObserver = new ContentObserver(new Handler(looper)) { |
| 38 | @Override |
| 39 | public void onChange(boolean selfChange) { |
| 40 | updateSatelliteBlacklist(); |
| 41 | } |
| 42 | }; |
| 43 | mContext.getContentResolver().registerContentObserver( |
| 44 | Settings.Global.getUriFor( |
| 45 | Settings.Global.GNSS_SATELLITE_BLACKLIST), |
| 46 | true, |
| 47 | contentObserver, UserHandle.USER_ALL); |
| 48 | } |
| 49 | |
| 50 | void updateSatelliteBlacklist() { |
| 51 | ContentResolver resolver = mContext.getContentResolver(); |
| 52 | String blacklist = Settings.Global.getString( |
| 53 | resolver, |
| 54 | Settings.Global.GNSS_SATELLITE_BLACKLIST); |
| 55 | if (blacklist == null) { |
| 56 | blacklist = ""; |
| 57 | } |
| 58 | if (DEBUG) { |
| 59 | Log.d(TAG, String.format("Update GNSS satellite blacklist: %s", blacklist)); |
| 60 | } |
| 61 | |
| 62 | List<Integer> blacklistValues; |
| 63 | try { |
| 64 | blacklistValues = parseSatelliteBlacklist(blacklist); |
| 65 | } catch (NumberFormatException e) { |
| 66 | Log.e(TAG, "Exception thrown when parsing blacklist string.", e); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | if (blacklistValues.size() % 2 != 0) { |
| 71 | Log.e(TAG, "blacklist string has odd number of values." |
| 72 | + "Aborting updateSatelliteBlacklist"); |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | int length = blacklistValues.size() / 2; |
| 77 | int[] constellations = new int[length]; |
| 78 | int[] svids = new int[length]; |
| 79 | for (int i = 0; i < length; i++) { |
| 80 | constellations[i] = blacklistValues.get(i * 2); |
| 81 | svids[i] = blacklistValues.get(i * 2 + 1); |
| 82 | } |
| 83 | mCallback.onUpdateSatelliteBlacklist(constellations, svids); |
| 84 | } |
| 85 | |
| 86 | @VisibleForTesting |
| 87 | static List<Integer> parseSatelliteBlacklist(String blacklist) throws NumberFormatException { |
| 88 | String[] strings = blacklist.split(BLACKLIST_DELIMITER); |
| 89 | List<Integer> parsed = new ArrayList<>(strings.length); |
| 90 | for (String string : strings) { |
| 91 | string = string.trim(); |
| 92 | if (!"".equals(string)) { |
| 93 | int value = Integer.parseInt(string); |
| 94 | if (value < 0) { |
| 95 | throw new NumberFormatException("Negative value is invalid."); |
| 96 | } |
| 97 | parsed.add(value); |
| 98 | } |
| 99 | } |
| 100 | return parsed; |
| 101 | } |
| 102 | } |