LocationBlacklist.java revision b319d5de196f7ff8fac39a567a449dd93db832c8
1/*
2 * Copyright (C) 2012 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
18package com.android.server.location;
19
20import android.content.Context;
21import android.database.ContentObserver;
22import android.os.Handler;
23import android.provider.Settings;
24import android.util.Log;
25import android.util.Slog;
26
27import com.android.server.LocationManagerService;
28
29import java.io.PrintWriter;
30import java.util.ArrayList;
31import java.util.Arrays;
32
33/**
34 * Allows applications to be blacklisted from location updates at run-time.
35 *
36 * This is a silent blacklist. Applications can still call Location Manager
37 * API's, but they just won't receive any locations.
38 */
39public final class LocationBlacklist extends ContentObserver {
40    private static final String TAG = "LocationBlacklist";
41    private static final boolean D = LocationManagerService.D;
42    private static final String BLACKLIST_CONFIG_NAME = "locationPackagePrefixBlacklist";
43    private static final String WHITELIST_CONFIG_NAME = "locationPackagePrefixWhitelist";
44
45    private final Context mContext;
46    private final Object mLock = new Object();
47
48    // all fields below synchronized on mLock
49    private String[] mWhitelist = new String[0];
50    private String[] mBlacklist = new String[0];
51
52    public LocationBlacklist(Context context, Handler handler) {
53        super(handler);
54        mContext = context;
55    }
56
57    public void init() {
58        mContext.getContentResolver().registerContentObserver(Settings.Secure.getUriFor(
59                BLACKLIST_CONFIG_NAME), false, this);
60//        mContext.getContentResolver().registerContentObserver(Settings.Secure.getUriFor(
61//                WHITELIST_CONFIG_NAME), false, this);
62        reloadBlacklist();
63    }
64
65    private void reloadBlacklist() {
66        String blacklist[] = getStringArray(BLACKLIST_CONFIG_NAME);
67        String whitelist[] = getStringArray(WHITELIST_CONFIG_NAME);
68        synchronized (mLock) {
69            mWhitelist = whitelist;
70            Slog.i(TAG, "whitelist: " + Arrays.toString(mWhitelist));
71            mBlacklist = blacklist;
72            Slog.i(TAG, "blacklist: " + Arrays.toString(mBlacklist));
73        }
74    }
75
76    /**
77     * Return true if in blacklist
78     * (package name matches blacklist, and does not match whitelist)
79     */
80    public boolean isBlacklisted(String packageName) {
81        /*
82        synchronized (mLock) {
83            for (String black : mBlacklist) {
84                if (packageName.startsWith(black)) {
85                    if (inWhitelist(packageName)) {
86                        continue;
87                    } else {
88                        if (D) Log.d(TAG, "dropping location (blacklisted): "
89                                + packageName + " matches " + black);
90                        return true;
91                    }
92                }
93            }
94        }
95        */
96        return false;
97    }
98
99    /**
100     * Return true if any of packages are in whitelist
101     */
102    private boolean inWhitelist(String pkg) {
103        synchronized (mLock) {
104            for (String white : mWhitelist) {
105                if (pkg.startsWith(white)) return true;
106            }
107        }
108        return false;
109    }
110
111    @Override
112    public void onChange(boolean selfChange) {
113        reloadBlacklist();
114    }
115
116    private String[] getStringArray(String key) {
117        String flatString = Settings.Secure.getString(mContext.getContentResolver(), key);
118        if (flatString == null) {
119            return new String[0];
120        }
121        String[] splitStrings = flatString.split(",");
122        ArrayList<String> result = new ArrayList<String>();
123        for (String pkg : splitStrings) {
124            pkg = pkg.trim();
125            if (pkg.isEmpty()) {
126                continue;
127            }
128            result.add(pkg);
129        }
130        return result.toArray(new String[result.size()]);
131    }
132
133    public void dump(PrintWriter pw) {
134        pw.println("mWhitelist=" + Arrays.toString(mWhitelist) + " mBlacklist=" +
135                Arrays.toString(mBlacklist));
136    }
137}
138