1a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao/*
2a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * Copyright (C) 2010 The Android Open Source Project
3a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao *
4a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * Licensed under the Apache License, Version 2.0 (the "License");
5a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * you may not use this file except in compliance with the License.
6a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * You may obtain a copy of the License at
7a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao *
8a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao *      http://www.apache.org/licenses/LICENSE-2.0
9a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao *
10a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * Unless required by applicable law or agreed to in writing, software
11a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * distributed under the License is distributed on an "AS IS" BASIS,
12a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * See the License for the specific language governing permissions and
14a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * limitations under the License
15a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao */
16a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao
17a58a8751b4c2ce457f0082a0baaee61312d56195Bai Taopackage com.android.server.location;
18a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao
19a58a8751b4c2ce457f0082a0baaee61312d56195Bai Taoimport android.content.Context;
20a58a8751b4c2ce457f0082a0baaee61312d56195Bai Taoimport android.location.Country;
21a58a8751b4c2ce457f0082a0baaee61312d56195Bai Taoimport android.location.CountryListener;
22a58a8751b4c2ce457f0082a0baaee61312d56195Bai Taoimport android.os.Handler;
23a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao
24a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao/**
25a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * This class defines the methods need to be implemented by the country
26a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * detector.
27a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * <p>
28a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * Calling {@link #detectCountry} to start detecting the country. The country
29a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * could be returned immediately if it is available.
30a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao *
31a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * @hide
32a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao */
33a58a8751b4c2ce457f0082a0baaee61312d56195Bai Taopublic abstract class CountryDetectorBase {
34a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    protected final Handler mHandler;
35a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    protected final Context mContext;
36a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    protected CountryListener mListener;
37a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    protected Country mDetectedCountry;
38a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao
39a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    public CountryDetectorBase(Context ctx) {
40a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        mContext = ctx;
41a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        mHandler = new Handler();
42a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    }
43a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao
44a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    /**
45a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     * Start detecting the country that the user is in.
46a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     *
47a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     * @return the country if it is available immediately, otherwise null should
48a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     *         be returned.
49a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     */
50a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    public abstract Country detectCountry();
51a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao
52a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    /**
53a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     * Register a listener to receive the notification when the country is detected or changed.
54a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     * <p>
55a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     * The previous listener will be replaced if it exists.
56a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     */
57a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    public void setCountryListener(CountryListener listener) {
58a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        mListener = listener;
59a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    }
60a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao
61a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    /**
62a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     * Stop detecting the country. The detector should release all system services and be ready to
63a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     * be freed
64a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     */
65a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    public abstract void stop();
66a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao
67a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    protected void notifyListener(Country country) {
68a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        if (mListener != null) {
69a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            mListener.onCountryDetected(country);
70a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        }
71a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    }
72a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao}
73