1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.android.server;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.HashMap;
import com.android.internal.os.BackgroundThread;
import com.android.server.location.ComprehensiveCountryDetector;
import android.content.Context;
import android.location.Country;
import android.location.CountryListener;
import android.location.ICountryDetector;
import android.location.ICountryListener;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.PrintWriterPrinter;
import android.util.Printer;
import android.util.Slog;
/**
* This class detects the country that the user is in through
* {@link ComprehensiveCountryDetector}.
*
* @hide
*/
public class CountryDetectorService extends ICountryDetector.Stub implements Runnable {
/**
* The class represents the remote listener, it will also removes itself
* from listener list when the remote process was died.
*/
private final class Receiver implements IBinder.DeathRecipient {
private final ICountryListener mListener;
private final IBinder mKey;
public Receiver(ICountryListener listener) {
mListener = listener;
mKey = listener.asBinder();
}
public void binderDied() {
removeListener(mKey);
}
@Override
public boolean equals(Object otherObj) {
if (otherObj instanceof Receiver) {
return mKey.equals(((Receiver) otherObj).mKey);
}
return false;
}
@Override
public int hashCode() {
return mKey.hashCode();
}
public ICountryListener getListener() {
return mListener;
}
}
private final static String TAG = "CountryDetector";
/** Whether to dump the state of the country detector service to bugreports */
private static final boolean DEBUG = false;
private final HashMap<IBinder, Receiver> mReceivers;
private final Context mContext;
private ComprehensiveCountryDetector mCountryDetector;
private boolean mSystemReady;
private Handler mHandler;
private CountryListener mLocationBasedDetectorListener;
public CountryDetectorService(Context context) {
super();
mReceivers = new HashMap<IBinder, Receiver>();
mContext = context;
}
@Override
public Country detectCountry() {
if (!mSystemReady) {
return null; // server not yet active
} else {
return mCountryDetector.detectCountry();
}
}
/**
* Add the ICountryListener into the listener list.
*/
@Override
public void addCountryListener(ICountryListener listener) throws RemoteException {
if (!mSystemReady) {
throw new RemoteException();
}
addListener(listener);
}
/**
* Remove the ICountryListener from the listener list.
*/
@Override
public void removeCountryListener(ICountryListener listener) throws RemoteException {
if (!mSystemReady) {
throw new RemoteException();
}
removeListener(listener.asBinder());
}
private void addListener(ICountryListener listener) {
synchronized (mReceivers) {
Receiver r = new Receiver(listener);
try {
listener.asBinder().linkToDeath(r, 0);
mReceivers.put(listener.asBinder(), r);
if (mReceivers.size() == 1) {
Slog.d(TAG, "The first listener is added");
setCountryListener(mLocationBasedDetectorListener);
}
} catch (RemoteException e) {
Slog.e(TAG, "linkToDeath failed:", e);
}
}
}
private void removeListener(IBinder key) {
synchronized (mReceivers) {
mReceivers.remove(key);
if (mReceivers.isEmpty()) {
setCountryListener(null);
Slog.d(TAG, "No listener is left");
}
}
}
protected void notifyReceivers(Country country) {
synchronized(mReceivers) {
for (Receiver receiver : mReceivers.values()) {
try {
receiver.getListener().onCountryDetected(country);
} catch (RemoteException e) {
// TODO: Shall we remove the receiver?
Slog.e(TAG, "notifyReceivers failed:", e);
}
}
}
}
void systemRunning() {
// Shall we wait for the initialization finish.
BackgroundThread.getHandler().post(this);
}
private void initialize() {
mCountryDetector = new ComprehensiveCountryDetector(mContext);
mLocationBasedDetectorListener = new CountryListener() {
public void onCountryDetected(final Country country) {
mHandler.post(new Runnable() {
public void run() {
notifyReceivers(country);
}
});
}
};
}
public void run() {
mHandler = new Handler();
initialize();
mSystemReady = true;
}
protected void setCountryListener(final CountryListener listener) {
mHandler.post(new Runnable() {
@Override
public void run() {
mCountryDetector.setCountryListener(listener);
}
});
}
// For testing
boolean isSystemReady() {
return mSystemReady;
}
@SuppressWarnings("unused")
@Override
protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) {
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
if (!DEBUG) return;
try {
final Printer p = new PrintWriterPrinter(fout);
p.println("CountryDetectorService state:");
p.println(" Number of listeners=" + mReceivers.keySet().size());
if (mCountryDetector == null) {
p.println(" ComprehensiveCountryDetector not initialized");
} else {
p.println(" " + mCountryDetector.toString());
}
} catch (Exception e) {
Slog.e(TAG, "Failed to dump CountryDetectorService: ", e);
}
}
}
|