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 | |
Makoto Onuki | d73b79b | 2011-12-13 14:02:32 -0800 | [diff] [blame] | 19 | import java.io.FileDescriptor; |
| 20 | import java.io.PrintWriter; |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 21 | import java.util.HashMap; |
| 22 | |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 23 | import com.android.internal.os.BackgroundThread; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 24 | import com.android.internal.util.DumpUtils; |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 25 | import com.android.server.location.ComprehensiveCountryDetector; |
| 26 | |
| 27 | import android.content.Context; |
| 28 | import android.location.Country; |
| 29 | import android.location.CountryListener; |
| 30 | import android.location.ICountryDetector; |
| 31 | import android.location.ICountryListener; |
| 32 | import android.os.Handler; |
| 33 | import android.os.IBinder; |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 34 | import android.os.RemoteException; |
Makoto Onuki | d73b79b | 2011-12-13 14:02:32 -0800 | [diff] [blame] | 35 | import android.util.PrintWriterPrinter; |
| 36 | import android.util.Printer; |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 37 | import android.util.Slog; |
| 38 | |
| 39 | /** |
| 40 | * This class detects the country that the user is in through |
| 41 | * {@link ComprehensiveCountryDetector}. |
| 42 | * |
| 43 | * @hide |
| 44 | */ |
| 45 | public class CountryDetectorService extends ICountryDetector.Stub implements Runnable { |
| 46 | |
| 47 | /** |
| 48 | * The class represents the remote listener, it will also removes itself |
| 49 | * from listener list when the remote process was died. |
| 50 | */ |
| 51 | private final class Receiver implements IBinder.DeathRecipient { |
| 52 | private final ICountryListener mListener; |
| 53 | private final IBinder mKey; |
| 54 | |
| 55 | public Receiver(ICountryListener listener) { |
| 56 | mListener = listener; |
| 57 | mKey = listener.asBinder(); |
| 58 | } |
| 59 | |
| 60 | public void binderDied() { |
| 61 | removeListener(mKey); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public boolean equals(Object otherObj) { |
| 66 | if (otherObj instanceof Receiver) { |
| 67 | return mKey.equals(((Receiver) otherObj).mKey); |
| 68 | } |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public int hashCode() { |
| 74 | return mKey.hashCode(); |
| 75 | } |
| 76 | |
| 77 | public ICountryListener getListener() { |
| 78 | return mListener; |
| 79 | } |
| 80 | } |
| 81 | |
Makoto Onuki | d73b79b | 2011-12-13 14:02:32 -0800 | [diff] [blame] | 82 | private final static String TAG = "CountryDetector"; |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 83 | |
Daniel Lehmann | 0e87370 | 2012-03-12 17:04:17 -0700 | [diff] [blame] | 84 | /** Whether to dump the state of the country detector service to bugreports */ |
| 85 | private static final boolean DEBUG = false; |
| 86 | |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 87 | private final HashMap<IBinder, Receiver> mReceivers; |
| 88 | private final Context mContext; |
| 89 | private ComprehensiveCountryDetector mCountryDetector; |
| 90 | private boolean mSystemReady; |
| 91 | private Handler mHandler; |
| 92 | private CountryListener mLocationBasedDetectorListener; |
| 93 | |
| 94 | public CountryDetectorService(Context context) { |
| 95 | super(); |
| 96 | mReceivers = new HashMap<IBinder, Receiver>(); |
| 97 | mContext = context; |
| 98 | } |
| 99 | |
| 100 | @Override |
Stan Chesnutt | 8183aac | 2013-09-06 13:57:16 -0700 | [diff] [blame] | 101 | public Country detectCountry() { |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 102 | if (!mSystemReady) { |
Stan Chesnutt | 8183aac | 2013-09-06 13:57:16 -0700 | [diff] [blame] | 103 | return null; // server not yet active |
| 104 | } else { |
| 105 | return mCountryDetector.detectCountry(); |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 106 | } |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Add the ICountryListener into the listener list. |
| 111 | */ |
| 112 | @Override |
| 113 | public void addCountryListener(ICountryListener listener) throws RemoteException { |
| 114 | if (!mSystemReady) { |
| 115 | throw new RemoteException(); |
| 116 | } |
| 117 | addListener(listener); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Remove the ICountryListener from the listener list. |
| 122 | */ |
| 123 | @Override |
| 124 | public void removeCountryListener(ICountryListener listener) throws RemoteException { |
| 125 | if (!mSystemReady) { |
| 126 | throw new RemoteException(); |
| 127 | } |
| 128 | removeListener(listener.asBinder()); |
| 129 | } |
| 130 | |
| 131 | private void addListener(ICountryListener listener) { |
| 132 | synchronized (mReceivers) { |
| 133 | Receiver r = new Receiver(listener); |
| 134 | try { |
| 135 | listener.asBinder().linkToDeath(r, 0); |
| 136 | mReceivers.put(listener.asBinder(), r); |
| 137 | if (mReceivers.size() == 1) { |
| 138 | Slog.d(TAG, "The first listener is added"); |
| 139 | setCountryListener(mLocationBasedDetectorListener); |
| 140 | } |
| 141 | } catch (RemoteException e) { |
| 142 | Slog.e(TAG, "linkToDeath failed:", e); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | private void removeListener(IBinder key) { |
| 148 | synchronized (mReceivers) { |
| 149 | mReceivers.remove(key); |
| 150 | if (mReceivers.isEmpty()) { |
| 151 | setCountryListener(null); |
| 152 | Slog.d(TAG, "No listener is left"); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | |
| 158 | protected void notifyReceivers(Country country) { |
| 159 | synchronized(mReceivers) { |
| 160 | for (Receiver receiver : mReceivers.values()) { |
| 161 | try { |
| 162 | receiver.getListener().onCountryDetected(country); |
| 163 | } catch (RemoteException e) { |
| 164 | // TODO: Shall we remove the receiver? |
| 165 | Slog.e(TAG, "notifyReceivers failed:", e); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 171 | void systemRunning() { |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 172 | // Shall we wait for the initialization finish. |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 173 | BackgroundThread.getHandler().post(this); |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | private void initialize() { |
| 177 | mCountryDetector = new ComprehensiveCountryDetector(mContext); |
| 178 | mLocationBasedDetectorListener = new CountryListener() { |
| 179 | public void onCountryDetected(final Country country) { |
| 180 | mHandler.post(new Runnable() { |
| 181 | public void run() { |
| 182 | notifyReceivers(country); |
| 183 | } |
| 184 | }); |
| 185 | } |
| 186 | }; |
| 187 | } |
| 188 | |
| 189 | public void run() { |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 190 | mHandler = new Handler(); |
| 191 | initialize(); |
| 192 | mSystemReady = true; |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | protected void setCountryListener(final CountryListener listener) { |
| 196 | mHandler.post(new Runnable() { |
| 197 | @Override |
| 198 | public void run() { |
| 199 | mCountryDetector.setCountryListener(listener); |
| 200 | } |
| 201 | }); |
| 202 | } |
| 203 | |
| 204 | // For testing |
| 205 | boolean isSystemReady() { |
| 206 | return mSystemReady; |
| 207 | } |
Makoto Onuki | d73b79b | 2011-12-13 14:02:32 -0800 | [diff] [blame] | 208 | |
Daniel Lehmann | 0e87370 | 2012-03-12 17:04:17 -0700 | [diff] [blame] | 209 | @SuppressWarnings("unused") |
Makoto Onuki | d73b79b | 2011-12-13 14:02:32 -0800 | [diff] [blame] | 210 | @Override |
| 211 | protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 212 | if (!DumpUtils.checkDumpPermission(mContext, TAG, fout)) return; |
Daniel Lehmann | 0e87370 | 2012-03-12 17:04:17 -0700 | [diff] [blame] | 213 | if (!DEBUG) return; |
Makoto Onuki | d73b79b | 2011-12-13 14:02:32 -0800 | [diff] [blame] | 214 | 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 Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 227 | } |