HotspotControllerImpl.java revision 36c7aa03255d91cfa0808323ac475ad02d161d7d
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.systemui.statusbar.policy;
18
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
23import android.net.ConnectivityManager;
24import android.net.wifi.WifiManager;
25import android.util.Log;
26
27import com.android.settingslib.TetherUtil;
28
29import java.io.FileDescriptor;
30import java.io.PrintWriter;
31import java.util.ArrayList;
32
33public class HotspotControllerImpl implements HotspotController {
34
35    private static final String TAG = "HotspotController";
36    private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
37
38    private final ArrayList<Callback> mCallbacks = new ArrayList<Callback>();
39    private final Receiver mReceiver = new Receiver();
40    private final ConnectivityManager mConnectivityManager;
41    private final Context mContext;
42
43    private int mHotspotState;
44
45    public HotspotControllerImpl(Context context) {
46        mContext = context;
47        mConnectivityManager = (ConnectivityManager)context.getSystemService(
48                Context.CONNECTIVITY_SERVICE);
49    }
50
51    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
52        pw.println("HotspotController state:");
53        pw.print("  mHotspotEnabled="); pw.println(stateToString(mHotspotState));
54    }
55
56    private static String stateToString(int hotspotState) {
57        switch (hotspotState) {
58            case WifiManager.WIFI_AP_STATE_DISABLED:
59                return "DISABLED";
60            case WifiManager.WIFI_AP_STATE_DISABLING:
61                return "DISABLING";
62            case WifiManager.WIFI_AP_STATE_ENABLED:
63                return "ENABLED";
64            case WifiManager.WIFI_AP_STATE_ENABLING:
65                return "ENABLING";
66            case WifiManager.WIFI_AP_STATE_FAILED:
67                return "FAILED";
68        }
69        return null;
70    }
71
72    @Override
73    public void addCallback(Callback callback) {
74        if (callback == null || mCallbacks.contains(callback)) return;
75        if (DEBUG) Log.d(TAG, "addCallback " + callback);
76        mCallbacks.add(callback);
77        mReceiver.setListening(!mCallbacks.isEmpty());
78    }
79
80    @Override
81    public void removeCallback(Callback callback) {
82        if (callback == null) return;
83        if (DEBUG) Log.d(TAG, "removeCallback " + callback);
84        mCallbacks.remove(callback);
85        mReceiver.setListening(!mCallbacks.isEmpty());
86    }
87
88    @Override
89    public boolean isHotspotEnabled() {
90        return mHotspotState == WifiManager.WIFI_AP_STATE_ENABLED;
91    }
92
93    @Override
94    public boolean isHotspotSupported() {
95        return TetherUtil.isTetheringSupported(mContext);
96    }
97
98    static final class OnStartTetheringCallback extends
99            ConnectivityManager.OnStartTetheringCallback {
100        @Override
101        public void onTetheringStarted() {}
102        @Override
103        public void onTetheringFailed() {
104          // TODO: Show error.
105        }
106    }
107
108    @Override
109    public void setHotspotEnabled(boolean enabled) {
110        if (enabled) {
111            OnStartTetheringCallback callback = new OnStartTetheringCallback();
112            mConnectivityManager.startTethering(
113                    ConnectivityManager.TETHERING_WIFI, false, callback);
114        } else {
115            mConnectivityManager.stopTethering(ConnectivityManager.TETHERING_WIFI);
116        }
117    }
118
119    private void fireCallback(boolean isEnabled) {
120        for (Callback callback : mCallbacks) {
121            callback.onHotspotChanged(isEnabled);
122        }
123    }
124
125    private final class Receiver extends BroadcastReceiver {
126        private boolean mRegistered;
127
128        public void setListening(boolean listening) {
129            if (listening && !mRegistered) {
130                if (DEBUG) Log.d(TAG, "Registering receiver");
131                final IntentFilter filter = new IntentFilter();
132                filter.addAction(WifiManager.WIFI_AP_STATE_CHANGED_ACTION);
133                mContext.registerReceiver(this, filter);
134                mRegistered = true;
135            } else if (!listening && mRegistered) {
136                if (DEBUG) Log.d(TAG, "Unregistering receiver");
137                mContext.unregisterReceiver(this);
138                mRegistered = false;
139            }
140        }
141
142        @Override
143        public void onReceive(Context context, Intent intent) {
144            if (DEBUG) Log.d(TAG, "onReceive " + intent.getAction());
145            int state = intent.getIntExtra(
146                    WifiManager.EXTRA_WIFI_AP_STATE, WifiManager.WIFI_AP_STATE_FAILED);
147            mHotspotState = state;
148            fireCallback(mHotspotState == WifiManager.WIFI_AP_STATE_ENABLED);
149        }
150    }
151}
152