blob: b0132d35fa3b78cbe7654ff47152386e05775e61 [file] [log] [blame]
Bai Taoa58a8752010-07-13 15:32:16 +08001/*
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
17package com.android.server;
18
Bai Taoa58a8752010-07-13 15:32:16 +080019import android.content.Context;
20import android.location.Country;
21import android.location.CountryListener;
22import android.location.ICountryDetector;
23import android.location.ICountryListener;
24import android.os.Handler;
25import android.os.IBinder;
Bai Taoa58a8752010-07-13 15:32:16 +080026import android.os.RemoteException;
Christophe Koessler34147602019-12-20 14:30:35 -080027import android.text.TextUtils;
Makoto Onukid73b79b2011-12-13 14:02:32 -080028import android.util.PrintWriterPrinter;
29import android.util.Printer;
Bai Taoa58a8752010-07-13 15:32:16 +080030import android.util.Slog;
31
Christophe Koessler34147602019-12-20 14:30:35 -080032import com.android.internal.R;
Christophe Koessler50361682019-12-17 16:40:47 -080033import com.android.internal.annotations.VisibleForTesting;
34import com.android.internal.os.BackgroundThread;
35import com.android.internal.util.DumpUtils;
36import com.android.server.location.ComprehensiveCountryDetector;
Christophe Koessler34147602019-12-20 14:30:35 -080037import com.android.server.location.CountryDetectorBase;
Christophe Koessler50361682019-12-17 16:40:47 -080038
39import java.io.FileDescriptor;
40import java.io.PrintWriter;
Christophe Koessler34147602019-12-20 14:30:35 -080041import java.lang.reflect.InvocationTargetException;
Christophe Koessler50361682019-12-17 16:40:47 -080042import java.util.HashMap;
43
Bai Taoa58a8752010-07-13 15:32:16 +080044/**
Christophe Koessler34147602019-12-20 14:30:35 -080045 * 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 Taoa58a8752010-07-13 15:32:16 +080050 *
51 * @hide
52 */
Christophe Koessler50361682019-12-17 16:40:47 -080053public class CountryDetectorService extends ICountryDetector.Stub {
Bai Taoa58a8752010-07-13 15:32:16 +080054
55 /**
Christophe Koessler50361682019-12-17 16:40:47 -080056 * The class represents the remote listener, it will also removes itself from listener list when
57 * the remote process was died.
Bai Taoa58a8752010-07-13 15:32:16 +080058 */
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 Koessler50361682019-12-17 16:40:47 -080090 private static final String TAG = "CountryDetector";
Bai Taoa58a8752010-07-13 15:32:16 +080091
Christophe Koessler50361682019-12-17 16:40:47 -080092 /**
93 * Whether to dump the state of the country detector service to bugreports
94 */
Daniel Lehmann0e873702012-03-12 17:04:17 -070095 private static final boolean DEBUG = false;
96
Bai Taoa58a8752010-07-13 15:32:16 +080097 private final HashMap<IBinder, Receiver> mReceivers;
98 private final Context mContext;
Christophe Koessler34147602019-12-20 14:30:35 -080099 private CountryDetectorBase mCountryDetector;
Bai Taoa58a8752010-07-13 15:32:16 +0800100 private boolean mSystemReady;
101 private Handler mHandler;
102 private CountryListener mLocationBasedDetectorListener;
103
104 public CountryDetectorService(Context context) {
Christophe Koessler50361682019-12-17 16:40:47 -0800105 this(context, BackgroundThread.getHandler());
106 }
107
108 @VisibleForTesting
109 CountryDetectorService(Context context, Handler handler) {
Bai Taoa58a8752010-07-13 15:32:16 +0800110 super();
Christophe Koessler50361682019-12-17 16:40:47 -0800111 mReceivers = new HashMap<>();
Bai Taoa58a8752010-07-13 15:32:16 +0800112 mContext = context;
Christophe Koessler50361682019-12-17 16:40:47 -0800113 mHandler = handler;
Bai Taoa58a8752010-07-13 15:32:16 +0800114 }
115
116 @Override
Stan Chesnutt8183aac2013-09-06 13:57:16 -0700117 public Country detectCountry() {
Bai Taoa58a8752010-07-13 15:32:16 +0800118 if (!mSystemReady) {
Christophe Koessler50361682019-12-17 16:40:47 -0800119 return null; // server not yet active
Stan Chesnutt8183aac2013-09-06 13:57:16 -0700120 } else {
121 return mCountryDetector.detectCountry();
Bai Taoa58a8752010-07-13 15:32:16 +0800122 }
Bai Taoa58a8752010-07-13 15:32:16 +0800123 }
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 Taoa58a8752010-07-13 15:32:16 +0800173 protected void notifyReceivers(Country country) {
Christophe Koessler50361682019-12-17 16:40:47 -0800174 synchronized (mReceivers) {
Bai Taoa58a8752010-07-13 15:32:16 +0800175 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 Ganova0027152013-06-25 14:59:53 -0700186 void systemRunning() {
Bai Taoa58a8752010-07-13 15:32:16 +0800187 // Shall we wait for the initialization finish.
Christophe Koessler50361682019-12-17 16:40:47 -0800188 mHandler.post(
189 () -> {
190 initialize();
191 mSystemReady = true;
192 });
Bai Taoa58a8752010-07-13 15:32:16 +0800193 }
194
Christophe Koessler34147602019-12-20 14:30:35 -0800195 @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 Koessler50361682019-12-17 16:40:47 -0800206 mLocationBasedDetectorListener = country -> mHandler.post(() -> notifyReceivers(country));
Bai Taoa58a8752010-07-13 15:32:16 +0800207 }
208
209 protected void setCountryListener(final CountryListener listener) {
Christophe Koessler50361682019-12-17 16:40:47 -0800210 mHandler.post(() -> mCountryDetector.setCountryListener(listener));
Bai Taoa58a8752010-07-13 15:32:16 +0800211 }
212
Christophe Koessler50361682019-12-17 16:40:47 -0800213 @VisibleForTesting
Christophe Koessler34147602019-12-20 14:30:35 -0800214 CountryDetectorBase getCountryDetector() {
215 return mCountryDetector;
216 }
217
218 @VisibleForTesting
Bai Taoa58a8752010-07-13 15:32:16 +0800219 boolean isSystemReady() {
220 return mSystemReady;
221 }
Makoto Onukid73b79b2011-12-13 14:02:32 -0800222
Christophe Koessler34147602019-12-20 14:30:35 -0800223 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 Lehmann0e873702012-03-12 17:04:17 -0700240 @SuppressWarnings("unused")
Makoto Onukid73b79b2011-12-13 14:02:32 -0800241 @Override
242 protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -0600243 if (!DumpUtils.checkDumpPermission(mContext, TAG, fout)) return;
Daniel Lehmann0e873702012-03-12 17:04:17 -0700244 if (!DEBUG) return;
Makoto Onukid73b79b2011-12-13 14:02:32 -0800245 try {
246 final Printer p = new PrintWriterPrinter(fout);
247 p.println("CountryDetectorService state:");
Christophe Koessler34147602019-12-20 14:30:35 -0800248 p.println("Country detector class=" + mCountryDetector.getClass().getName());
Makoto Onukid73b79b2011-12-13 14:02:32 -0800249 p.println(" Number of listeners=" + mReceivers.keySet().size());
250 if (mCountryDetector == null) {
Christophe Koessler34147602019-12-20 14:30:35 -0800251 p.println(" CountryDetector not initialized");
Makoto Onukid73b79b2011-12-13 14:02:32 -0800252 } else {
253 p.println(" " + mCountryDetector.toString());
254 }
255 } catch (Exception e) {
256 Slog.e(TAG, "Failed to dump CountryDetectorService: ", e);
257 }
258 }
Bai Taoa58a8752010-07-13 15:32:16 +0800259}