FileDocCategorySizeDatePackage
CountryDetectorService.javaAPI DocAndroid 5.1 API7011Thu Mar 12 22:22:42 GMT 2015com.android.server

CountryDetectorService

public class CountryDetectorService extends ICountryDetector.Stub implements Runnable
This class detects the country that the user is in through {@link ComprehensiveCountryDetector}.
hide

Fields Summary
private static final String
TAG
private static final boolean
DEBUG
Whether to dump the state of the country detector service to bugreports
private final HashMap
mReceivers
private final android.content.Context
mContext
private com.android.server.location.ComprehensiveCountryDetector
mCountryDetector
private boolean
mSystemReady
private android.os.Handler
mHandler
private android.location.CountryListener
mLocationBasedDetectorListener
Constructors Summary
public CountryDetectorService(android.content.Context context)


       
        super();
        mReceivers = new HashMap<IBinder, Receiver>();
        mContext = context;
    
Methods Summary
public voidaddCountryListener(android.location.ICountryListener listener)
Add the ICountryListener into the listener list.

        if (!mSystemReady) {
            throw new RemoteException();
        }
        addListener(listener);
    
private voidaddListener(android.location.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);
            }
        }
    
public android.location.CountrydetectCountry()

        if (!mSystemReady) {
            return null;   // server not yet active
        } else {
            return mCountryDetector.detectCountry();
        }
    
protected voiddump(java.io.FileDescriptor fd, java.io.PrintWriter fout, java.lang.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);
        }
    
private voidinitialize()

        mCountryDetector = new ComprehensiveCountryDetector(mContext);
        mLocationBasedDetectorListener = new CountryListener() {
            public void onCountryDetected(final Country country) {
                mHandler.post(new Runnable() {
                    public void run() {
                        notifyReceivers(country);
                    }
                });
            }
        };
    
booleanisSystemReady()

        return mSystemReady;
    
protected voidnotifyReceivers(android.location.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);
                }
            }
        }
    
public voidremoveCountryListener(android.location.ICountryListener listener)
Remove the ICountryListener from the listener list.

        if (!mSystemReady) {
            throw new RemoteException();
        }
        removeListener(listener.asBinder());
    
private voidremoveListener(android.os.IBinder key)

        synchronized (mReceivers) {
            mReceivers.remove(key);
            if (mReceivers.isEmpty()) {
                setCountryListener(null);
                Slog.d(TAG, "No listener is left");
            }
        }
    
public voidrun()

        mHandler = new Handler();
        initialize();
        mSystemReady = true;
    
protected voidsetCountryListener(android.location.CountryListener listener)

        mHandler.post(new Runnable() {
            @Override
            public void run() {
                mCountryDetector.setCountryListener(listener);
            }
        });
    
voidsystemRunning()

        // Shall we wait for the initialization finish.
        BackgroundThread.getHandler().post(this);