HotspotControllerImpl.java revision b6fc931204a3e7a40ac2dfda67f4b61aef9e4b5b
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.Context; 22import android.content.Intent; 23import android.content.IntentFilter; 24import android.net.ConnectivityManager; 25import android.net.wifi.WifiManager; 26import android.os.UserHandle; 27import android.os.UserManager; 28import android.util.Log; 29 30import com.android.settingslib.TetherUtil; 31 32import java.io.FileDescriptor; 33import java.io.PrintWriter; 34import java.util.ArrayList; 35 36public class HotspotControllerImpl implements HotspotController { 37 38 private static final String TAG = "HotspotController"; 39 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); 40 41 private final ArrayList<Callback> mCallbacks = new ArrayList<Callback>(); 42 private final Receiver mReceiver = new Receiver(); 43 private final ConnectivityManager mConnectivityManager; 44 private final Context mContext; 45 private final UserManager mUserManager; 46 private final int mCurrentUser; 47 48 private int mHotspotState; 49 50 public HotspotControllerImpl(Context context) { 51 mContext = context; 52 mConnectivityManager = (ConnectivityManager) context.getSystemService( 53 Context.CONNECTIVITY_SERVICE); 54 mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); 55 mCurrentUser = ActivityManager.getCurrentUser(); 56 } 57 58 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 59 pw.println("HotspotController state:"); 60 pw.print(" mHotspotEnabled="); pw.println(stateToString(mHotspotState)); 61 } 62 63 private static String stateToString(int hotspotState) { 64 switch (hotspotState) { 65 case WifiManager.WIFI_AP_STATE_DISABLED: 66 return "DISABLED"; 67 case WifiManager.WIFI_AP_STATE_DISABLING: 68 return "DISABLING"; 69 case WifiManager.WIFI_AP_STATE_ENABLED: 70 return "ENABLED"; 71 case WifiManager.WIFI_AP_STATE_ENABLING: 72 return "ENABLING"; 73 case WifiManager.WIFI_AP_STATE_FAILED: 74 return "FAILED"; 75 } 76 return null; 77 } 78 79 @Override 80 public void addCallback(Callback callback) { 81 if (callback == null || mCallbacks.contains(callback)) return; 82 if (DEBUG) Log.d(TAG, "addCallback " + callback); 83 mCallbacks.add(callback); 84 mReceiver.setListening(!mCallbacks.isEmpty()); 85 } 86 87 @Override 88 public void removeCallback(Callback callback) { 89 if (callback == null) return; 90 if (DEBUG) Log.d(TAG, "removeCallback " + callback); 91 mCallbacks.remove(callback); 92 mReceiver.setListening(!mCallbacks.isEmpty()); 93 } 94 95 @Override 96 public boolean isHotspotEnabled() { 97 return mHotspotState == WifiManager.WIFI_AP_STATE_ENABLED; 98 } 99 100 @Override 101 public boolean isHotspotSupported() { 102 return TetherUtil.isTetheringSupported(mContext); 103 } 104 105 @Override 106 public boolean isTetheringAllowed() { 107 return !mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING, 108 UserHandle.of(mCurrentUser)); 109 } 110 111 static final class OnStartTetheringCallback extends 112 ConnectivityManager.OnStartTetheringCallback { 113 @Override 114 public void onTetheringStarted() {} 115 @Override 116 public void onTetheringFailed() { 117 // TODO: Show error. 118 } 119 } 120 121 @Override 122 public void setHotspotEnabled(boolean enabled) { 123 if (enabled) { 124 OnStartTetheringCallback callback = new OnStartTetheringCallback(); 125 mConnectivityManager.startTethering( 126 ConnectivityManager.TETHERING_WIFI, false, callback); 127 } else { 128 mConnectivityManager.stopTethering(ConnectivityManager.TETHERING_WIFI); 129 } 130 } 131 132 private void fireCallback(boolean isEnabled) { 133 for (Callback callback : mCallbacks) { 134 callback.onHotspotChanged(isEnabled); 135 } 136 } 137 138 private final class Receiver extends BroadcastReceiver { 139 private boolean mRegistered; 140 141 public void setListening(boolean listening) { 142 if (listening && !mRegistered) { 143 if (DEBUG) Log.d(TAG, "Registering receiver"); 144 final IntentFilter filter = new IntentFilter(); 145 filter.addAction(WifiManager.WIFI_AP_STATE_CHANGED_ACTION); 146 mContext.registerReceiver(this, filter); 147 mRegistered = true; 148 } else if (!listening && mRegistered) { 149 if (DEBUG) Log.d(TAG, "Unregistering receiver"); 150 mContext.unregisterReceiver(this); 151 mRegistered = false; 152 } 153 } 154 155 @Override 156 public void onReceive(Context context, Intent intent) { 157 if (DEBUG) Log.d(TAG, "onReceive " + intent.getAction()); 158 int state = intent.getIntExtra( 159 WifiManager.EXTRA_WIFI_AP_STATE, WifiManager.WIFI_AP_STATE_FAILED); 160 mHotspotState = state; 161 fireCallback(mHotspotState == WifiManager.WIFI_AP_STATE_ENABLED); 162 } 163 } 164} 165