Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | * use this file except in compliance with the License. You may obtain a copy of |
| 6 | * 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, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations under |
| 14 | * the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server; |
| 18 | |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.location.Country; |
| 21 | import android.location.CountryListener; |
| 22 | import android.location.ICountryDetector; |
| 23 | import android.location.ICountryListener; |
| 24 | import android.os.Handler; |
| 25 | import android.os.IBinder; |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 26 | import android.os.RemoteException; |
Christophe Koessler | 3414760 | 2019-12-20 14:30:35 -0800 | [diff] [blame] | 27 | import android.text.TextUtils; |
Makoto Onuki | d73b79b | 2011-12-13 14:02:32 -0800 | [diff] [blame] | 28 | import android.util.PrintWriterPrinter; |
| 29 | import android.util.Printer; |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 30 | import android.util.Slog; |
| 31 | |
Christophe Koessler | 3414760 | 2019-12-20 14:30:35 -0800 | [diff] [blame] | 32 | import com.android.internal.R; |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 33 | import com.android.internal.annotations.VisibleForTesting; |
| 34 | import com.android.internal.os.BackgroundThread; |
| 35 | import com.android.internal.util.DumpUtils; |
| 36 | import com.android.server.location.ComprehensiveCountryDetector; |
Christophe Koessler | 3414760 | 2019-12-20 14:30:35 -0800 | [diff] [blame] | 37 | import com.android.server.location.CountryDetectorBase; |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 38 | |
| 39 | import java.io.FileDescriptor; |
| 40 | import java.io.PrintWriter; |
Christophe Koessler | 3414760 | 2019-12-20 14:30:35 -0800 | [diff] [blame] | 41 | import java.lang.reflect.InvocationTargetException; |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 42 | import java.util.HashMap; |
| 43 | |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 44 | /** |
Christophe Koessler | 3414760 | 2019-12-20 14:30:35 -0800 | [diff] [blame] | 45 | * This class detects the country that the user is in. The default country detection is made through |
| 46 | * {@link com.android.server.location.ComprehensiveCountryDetector}. It is possible to overlay the |
| 47 | * detection algorithm by overlaying the attribute R.string.config_customCountryDetector with the |
| 48 | * custom class name to use instead. The custom class must extend |
| 49 | * {@link com.android.server.location.CountryDetectorBase} |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 50 | * |
| 51 | * @hide |
| 52 | */ |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 53 | public class CountryDetectorService extends ICountryDetector.Stub { |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 54 | |
| 55 | /** |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 56 | * The class represents the remote listener, it will also removes itself from listener list when |
| 57 | * the remote process was died. |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 58 | */ |
| 59 | private final class Receiver implements IBinder.DeathRecipient { |
| 60 | private final ICountryListener mListener; |
| 61 | private final IBinder mKey; |
| 62 | |
| 63 | public Receiver(ICountryListener listener) { |
| 64 | mListener = listener; |
| 65 | mKey = listener.asBinder(); |
| 66 | } |
| 67 | |
| 68 | public void binderDied() { |
| 69 | removeListener(mKey); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public boolean equals(Object otherObj) { |
| 74 | if (otherObj instanceof Receiver) { |
| 75 | return mKey.equals(((Receiver) otherObj).mKey); |
| 76 | } |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public int hashCode() { |
| 82 | return mKey.hashCode(); |
| 83 | } |
| 84 | |
| 85 | public ICountryListener getListener() { |
| 86 | return mListener; |
| 87 | } |
| 88 | } |
| 89 | |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 90 | private static final String TAG = "CountryDetector"; |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 91 | |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 92 | /** |
| 93 | * Whether to dump the state of the country detector service to bugreports |
| 94 | */ |
Daniel Lehmann | 0e87370 | 2012-03-12 17:04:17 -0700 | [diff] [blame] | 95 | private static final boolean DEBUG = false; |
| 96 | |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 97 | private final HashMap<IBinder, Receiver> mReceivers; |
| 98 | private final Context mContext; |
Christophe Koessler | 3414760 | 2019-12-20 14:30:35 -0800 | [diff] [blame] | 99 | private CountryDetectorBase mCountryDetector; |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 100 | private boolean mSystemReady; |
| 101 | private Handler mHandler; |
| 102 | private CountryListener mLocationBasedDetectorListener; |
| 103 | |
| 104 | public CountryDetectorService(Context context) { |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 105 | this(context, BackgroundThread.getHandler()); |
| 106 | } |
| 107 | |
| 108 | @VisibleForTesting |
| 109 | CountryDetectorService(Context context, Handler handler) { |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 110 | super(); |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 111 | mReceivers = new HashMap<>(); |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 112 | mContext = context; |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 113 | mHandler = handler; |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Override |
Stan Chesnutt | 8183aac | 2013-09-06 13:57:16 -0700 | [diff] [blame] | 117 | public Country detectCountry() { |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 118 | if (!mSystemReady) { |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 119 | return null; // server not yet active |
Stan Chesnutt | 8183aac | 2013-09-06 13:57:16 -0700 | [diff] [blame] | 120 | } else { |
| 121 | return mCountryDetector.detectCountry(); |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 122 | } |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Add the ICountryListener into the listener list. |
| 127 | */ |
| 128 | @Override |
| 129 | public void addCountryListener(ICountryListener listener) throws RemoteException { |
| 130 | if (!mSystemReady) { |
| 131 | throw new RemoteException(); |
| 132 | } |
| 133 | addListener(listener); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Remove the ICountryListener from the listener list. |
| 138 | */ |
| 139 | @Override |
| 140 | public void removeCountryListener(ICountryListener listener) throws RemoteException { |
| 141 | if (!mSystemReady) { |
| 142 | throw new RemoteException(); |
| 143 | } |
| 144 | removeListener(listener.asBinder()); |
| 145 | } |
| 146 | |
| 147 | private void addListener(ICountryListener listener) { |
| 148 | synchronized (mReceivers) { |
| 149 | Receiver r = new Receiver(listener); |
| 150 | try { |
| 151 | listener.asBinder().linkToDeath(r, 0); |
| 152 | mReceivers.put(listener.asBinder(), r); |
| 153 | if (mReceivers.size() == 1) { |
| 154 | Slog.d(TAG, "The first listener is added"); |
| 155 | setCountryListener(mLocationBasedDetectorListener); |
| 156 | } |
| 157 | } catch (RemoteException e) { |
| 158 | Slog.e(TAG, "linkToDeath failed:", e); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | private void removeListener(IBinder key) { |
| 164 | synchronized (mReceivers) { |
| 165 | mReceivers.remove(key); |
| 166 | if (mReceivers.isEmpty()) { |
| 167 | setCountryListener(null); |
| 168 | Slog.d(TAG, "No listener is left"); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 173 | protected void notifyReceivers(Country country) { |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 174 | synchronized (mReceivers) { |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 175 | for (Receiver receiver : mReceivers.values()) { |
| 176 | try { |
| 177 | receiver.getListener().onCountryDetected(country); |
| 178 | } catch (RemoteException e) { |
| 179 | // TODO: Shall we remove the receiver? |
| 180 | Slog.e(TAG, "notifyReceivers failed:", e); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 186 | void systemRunning() { |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 187 | // Shall we wait for the initialization finish. |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 188 | mHandler.post( |
| 189 | () -> { |
| 190 | initialize(); |
| 191 | mSystemReady = true; |
| 192 | }); |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 193 | } |
| 194 | |
Christophe Koessler | 3414760 | 2019-12-20 14:30:35 -0800 | [diff] [blame] | 195 | @VisibleForTesting |
| 196 | void initialize() { |
| 197 | final String customCountryClass = mContext.getString(R.string.config_customCountryDetector); |
| 198 | if (!TextUtils.isEmpty(customCountryClass)) { |
| 199 | mCountryDetector = loadCustomCountryDetectorIfAvailable(customCountryClass); |
| 200 | } |
| 201 | |
| 202 | if (mCountryDetector == null) { |
| 203 | Slog.d(TAG, "Using default country detector"); |
| 204 | mCountryDetector = new ComprehensiveCountryDetector(mContext); |
| 205 | } |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 206 | mLocationBasedDetectorListener = country -> mHandler.post(() -> notifyReceivers(country)); |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | protected void setCountryListener(final CountryListener listener) { |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 210 | mHandler.post(() -> mCountryDetector.setCountryListener(listener)); |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 211 | } |
| 212 | |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 213 | @VisibleForTesting |
Christophe Koessler | 3414760 | 2019-12-20 14:30:35 -0800 | [diff] [blame] | 214 | CountryDetectorBase getCountryDetector() { |
| 215 | return mCountryDetector; |
| 216 | } |
| 217 | |
| 218 | @VisibleForTesting |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 219 | boolean isSystemReady() { |
| 220 | return mSystemReady; |
| 221 | } |
Makoto Onuki | d73b79b | 2011-12-13 14:02:32 -0800 | [diff] [blame] | 222 | |
Christophe Koessler | 3414760 | 2019-12-20 14:30:35 -0800 | [diff] [blame] | 223 | private CountryDetectorBase loadCustomCountryDetectorIfAvailable( |
| 224 | final String customCountryClass) { |
| 225 | CountryDetectorBase customCountryDetector = null; |
| 226 | |
| 227 | Slog.d(TAG, "Using custom country detector class: " + customCountryClass); |
| 228 | try { |
| 229 | customCountryDetector = Class.forName(customCountryClass).asSubclass( |
| 230 | CountryDetectorBase.class).getConstructor(Context.class).newInstance( |
| 231 | mContext); |
| 232 | } catch (ClassNotFoundException | InstantiationException | IllegalAccessException |
| 233 | | NoSuchMethodException | InvocationTargetException e) { |
| 234 | Slog.e(TAG, "Could not instantiate the custom country detector class"); |
| 235 | } |
| 236 | |
| 237 | return customCountryDetector; |
| 238 | } |
| 239 | |
Daniel Lehmann | 0e87370 | 2012-03-12 17:04:17 -0700 | [diff] [blame] | 240 | @SuppressWarnings("unused") |
Makoto Onuki | d73b79b | 2011-12-13 14:02:32 -0800 | [diff] [blame] | 241 | @Override |
| 242 | protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 243 | if (!DumpUtils.checkDumpPermission(mContext, TAG, fout)) return; |
Daniel Lehmann | 0e87370 | 2012-03-12 17:04:17 -0700 | [diff] [blame] | 244 | if (!DEBUG) return; |
Makoto Onuki | d73b79b | 2011-12-13 14:02:32 -0800 | [diff] [blame] | 245 | try { |
| 246 | final Printer p = new PrintWriterPrinter(fout); |
| 247 | p.println("CountryDetectorService state:"); |
Christophe Koessler | 3414760 | 2019-12-20 14:30:35 -0800 | [diff] [blame] | 248 | p.println("Country detector class=" + mCountryDetector.getClass().getName()); |
Makoto Onuki | d73b79b | 2011-12-13 14:02:32 -0800 | [diff] [blame] | 249 | p.println(" Number of listeners=" + mReceivers.keySet().size()); |
| 250 | if (mCountryDetector == null) { |
Christophe Koessler | 3414760 | 2019-12-20 14:30:35 -0800 | [diff] [blame] | 251 | p.println(" CountryDetector not initialized"); |
Makoto Onuki | d73b79b | 2011-12-13 14:02:32 -0800 | [diff] [blame] | 252 | } else { |
| 253 | p.println(" " + mCountryDetector.toString()); |
| 254 | } |
| 255 | } catch (Exception e) { |
| 256 | Slog.e(TAG, "Failed to dump CountryDetectorService: ", e); |
| 257 | } |
| 258 | } |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 259 | } |