blob: a478b2f47637ec3ab63296859858bb9f1e6bd9ec [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
Makoto Onukid73b79b2011-12-13 14:02:32 -080019import java.io.FileDescriptor;
20import java.io.PrintWriter;
Bai Taoa58a8752010-07-13 15:32:16 +080021import java.util.HashMap;
22
Dianne Hackborn8d044e82013-04-30 17:24:15 -070023import com.android.internal.os.BackgroundThread;
Bai Taoa58a8752010-07-13 15:32:16 +080024import com.android.server.location.ComprehensiveCountryDetector;
25
26import android.content.Context;
27import android.location.Country;
28import android.location.CountryListener;
29import android.location.ICountryDetector;
30import android.location.ICountryListener;
31import android.os.Handler;
32import android.os.IBinder;
Bai Taoa58a8752010-07-13 15:32:16 +080033import android.os.RemoteException;
Makoto Onukid73b79b2011-12-13 14:02:32 -080034import android.util.PrintWriterPrinter;
35import android.util.Printer;
Bai Taoa58a8752010-07-13 15:32:16 +080036import android.util.Slog;
37
38/**
39 * This class detects the country that the user is in through
40 * {@link ComprehensiveCountryDetector}.
41 *
42 * @hide
43 */
44public class CountryDetectorService extends ICountryDetector.Stub implements Runnable {
45
46 /**
47 * The class represents the remote listener, it will also removes itself
48 * from listener list when the remote process was died.
49 */
50 private final class Receiver implements IBinder.DeathRecipient {
51 private final ICountryListener mListener;
52 private final IBinder mKey;
53
54 public Receiver(ICountryListener listener) {
55 mListener = listener;
56 mKey = listener.asBinder();
57 }
58
59 public void binderDied() {
60 removeListener(mKey);
61 }
62
63 @Override
64 public boolean equals(Object otherObj) {
65 if (otherObj instanceof Receiver) {
66 return mKey.equals(((Receiver) otherObj).mKey);
67 }
68 return false;
69 }
70
71 @Override
72 public int hashCode() {
73 return mKey.hashCode();
74 }
75
76 public ICountryListener getListener() {
77 return mListener;
78 }
79 }
80
Makoto Onukid73b79b2011-12-13 14:02:32 -080081 private final static String TAG = "CountryDetector";
Bai Taoa58a8752010-07-13 15:32:16 +080082
Daniel Lehmann0e873702012-03-12 17:04:17 -070083 /** Whether to dump the state of the country detector service to bugreports */
84 private static final boolean DEBUG = false;
85
Bai Taoa58a8752010-07-13 15:32:16 +080086 private final HashMap<IBinder, Receiver> mReceivers;
87 private final Context mContext;
88 private ComprehensiveCountryDetector mCountryDetector;
89 private boolean mSystemReady;
90 private Handler mHandler;
91 private CountryListener mLocationBasedDetectorListener;
92
93 public CountryDetectorService(Context context) {
94 super();
95 mReceivers = new HashMap<IBinder, Receiver>();
96 mContext = context;
97 }
98
99 @Override
Stan Chesnutt8183aac2013-09-06 13:57:16 -0700100 public Country detectCountry() {
Bai Taoa58a8752010-07-13 15:32:16 +0800101 if (!mSystemReady) {
Stan Chesnutt8183aac2013-09-06 13:57:16 -0700102 return null; // server not yet active
103 } else {
104 return mCountryDetector.detectCountry();
Bai Taoa58a8752010-07-13 15:32:16 +0800105 }
Bai Taoa58a8752010-07-13 15:32:16 +0800106 }
107
108 /**
109 * Add the ICountryListener into the listener list.
110 */
111 @Override
112 public void addCountryListener(ICountryListener listener) throws RemoteException {
113 if (!mSystemReady) {
114 throw new RemoteException();
115 }
116 addListener(listener);
117 }
118
119 /**
120 * Remove the ICountryListener from the listener list.
121 */
122 @Override
123 public void removeCountryListener(ICountryListener listener) throws RemoteException {
124 if (!mSystemReady) {
125 throw new RemoteException();
126 }
127 removeListener(listener.asBinder());
128 }
129
130 private void addListener(ICountryListener listener) {
131 synchronized (mReceivers) {
132 Receiver r = new Receiver(listener);
133 try {
134 listener.asBinder().linkToDeath(r, 0);
135 mReceivers.put(listener.asBinder(), r);
136 if (mReceivers.size() == 1) {
137 Slog.d(TAG, "The first listener is added");
138 setCountryListener(mLocationBasedDetectorListener);
139 }
140 } catch (RemoteException e) {
141 Slog.e(TAG, "linkToDeath failed:", e);
142 }
143 }
144 }
145
146 private void removeListener(IBinder key) {
147 synchronized (mReceivers) {
148 mReceivers.remove(key);
149 if (mReceivers.isEmpty()) {
150 setCountryListener(null);
151 Slog.d(TAG, "No listener is left");
152 }
153 }
154 }
155
156
157 protected void notifyReceivers(Country country) {
158 synchronized(mReceivers) {
159 for (Receiver receiver : mReceivers.values()) {
160 try {
161 receiver.getListener().onCountryDetected(country);
162 } catch (RemoteException e) {
163 // TODO: Shall we remove the receiver?
164 Slog.e(TAG, "notifyReceivers failed:", e);
165 }
166 }
167 }
168 }
169
Svetoslav Ganova0027152013-06-25 14:59:53 -0700170 void systemRunning() {
Bai Taoa58a8752010-07-13 15:32:16 +0800171 // Shall we wait for the initialization finish.
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700172 BackgroundThread.getHandler().post(this);
Bai Taoa58a8752010-07-13 15:32:16 +0800173 }
174
175 private void initialize() {
176 mCountryDetector = new ComprehensiveCountryDetector(mContext);
177 mLocationBasedDetectorListener = new CountryListener() {
178 public void onCountryDetected(final Country country) {
179 mHandler.post(new Runnable() {
180 public void run() {
181 notifyReceivers(country);
182 }
183 });
184 }
185 };
186 }
187
188 public void run() {
Bai Taoa58a8752010-07-13 15:32:16 +0800189 mHandler = new Handler();
190 initialize();
191 mSystemReady = true;
Bai Taoa58a8752010-07-13 15:32:16 +0800192 }
193
194 protected void setCountryListener(final CountryListener listener) {
195 mHandler.post(new Runnable() {
196 @Override
197 public void run() {
198 mCountryDetector.setCountryListener(listener);
199 }
200 });
201 }
202
203 // For testing
204 boolean isSystemReady() {
205 return mSystemReady;
206 }
Makoto Onukid73b79b2011-12-13 14:02:32 -0800207
Daniel Lehmann0e873702012-03-12 17:04:17 -0700208 @SuppressWarnings("unused")
Makoto Onukid73b79b2011-12-13 14:02:32 -0800209 @Override
210 protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) {
Jeff Sharkeyeb4cc4922012-04-26 18:17:29 -0700211 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
212
Daniel Lehmann0e873702012-03-12 17:04:17 -0700213 if (!DEBUG) return;
Makoto Onukid73b79b2011-12-13 14:02:32 -0800214 try {
215 final Printer p = new PrintWriterPrinter(fout);
216 p.println("CountryDetectorService state:");
217 p.println(" Number of listeners=" + mReceivers.keySet().size());
218 if (mCountryDetector == null) {
219 p.println(" ComprehensiveCountryDetector not initialized");
220 } else {
221 p.println(" " + mCountryDetector.toString());
222 }
223 } catch (Exception e) {
224 Slog.e(TAG, "Failed to dump CountryDetectorService: ", e);
225 }
226 }
Bai Taoa58a8752010-07-13 15:32:16 +0800227}