HotspotControllerImpl.java revision 256a22663dbeb31cf4f4a6e295e0c74d80e4fecb
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.app.ActivityManager;
20import android.content.BroadcastReceiver;
21import android.content.ContentResolver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.IntentFilter;
25import android.net.ConnectivityManager;
26import android.net.wifi.WifiManager;
27import android.os.UserHandle;
28import android.provider.Settings;
29import android.util.Log;
30
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 Context mContext;
41    private final WifiManager mWifiManager;
42    private final ConnectivityManager mConnectivityManager;
43
44    public HotspotControllerImpl(Context context) {
45        mContext = context;
46        mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
47        mConnectivityManager = (ConnectivityManager)
48                mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
49    }
50
51    public void addCallback(Callback callback) {
52        if (callback == null || mCallbacks.contains(callback)) return;
53        if (DEBUG) Log.d(TAG, "addCallback " + callback);
54        mCallbacks.add(callback);
55        mReceiver.setListening(!mCallbacks.isEmpty());
56    }
57
58    public void removeCallback(Callback callback) {
59        if (callback == null) return;
60        if (DEBUG) Log.d(TAG, "removeCallback " + callback);
61        mCallbacks.remove(callback);
62        mReceiver.setListening(!mCallbacks.isEmpty());
63    }
64
65    @Override
66    public boolean isHotspotEnabled() {
67        return mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED;
68    }
69
70    @Override
71    public boolean isHotspotSupported() {
72        final boolean isSecondaryUser = ActivityManager.getCurrentUser() != UserHandle.USER_OWNER;
73        return !isSecondaryUser && mConnectivityManager.isTetheringSupported();
74    }
75
76    @Override
77    public void setHotspotEnabled(boolean enabled) {
78        final ContentResolver cr = mContext.getContentResolver();
79        // This needs to be kept up to date with Settings (WifiApEnabler.setSoftapEnabled)
80        // in case it is turned on in settings and off in qs (or vice versa).
81        // Disable Wifi if enabling tethering.
82        int wifiState = mWifiManager.getWifiState();
83        if (enabled && ((wifiState == WifiManager.WIFI_STATE_ENABLING) ||
84                    (wifiState == WifiManager.WIFI_STATE_ENABLED))) {
85            mWifiManager.setWifiEnabled(false);
86            Settings.Global.putInt(cr, Settings.Global.WIFI_SAVED_STATE, 1);
87        }
88
89        mWifiManager.setWifiApEnabled(null, enabled);
90
91        // If needed, restore Wifi on tether disable.
92        if (!enabled) {
93            if (Settings.Global.getInt(cr, Settings.Global.WIFI_SAVED_STATE, 0) == 1) {
94                mWifiManager.setWifiEnabled(true);
95                Settings.Global.putInt(cr, Settings.Global.WIFI_SAVED_STATE, 0);
96            }
97        }
98    }
99
100    private void fireCallback(boolean isEnabled) {
101        for (Callback callback : mCallbacks) {
102            callback.onHotspotChanged(isEnabled);
103        }
104    }
105
106    private final class Receiver extends BroadcastReceiver {
107        private boolean mRegistered;
108
109        public void setListening(boolean listening) {
110            if (listening && !mRegistered) {
111                if (DEBUG) Log.d(TAG, "Registering receiver");
112                final IntentFilter filter = new IntentFilter();
113                filter.addAction(WifiManager.WIFI_AP_STATE_CHANGED_ACTION);
114                mContext.registerReceiver(this, filter);
115                mRegistered = true;
116            } else if (!listening && mRegistered) {
117                if (DEBUG) Log.d(TAG, "Unregistering receiver");
118                mContext.unregisterReceiver(this);
119                mRegistered = false;
120            }
121        }
122
123        @Override
124        public void onReceive(Context context, Intent intent) {
125            if (DEBUG) Log.d(TAG, "onReceive " + intent.getAction());
126            fireCallback(isHotspotEnabled());
127        }
128    }
129}
130