1 /* 2 * Copyright (C) 2015 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 package com.android.settings.dashboard.conditional; 17 18 import android.content.BroadcastReceiver; 19 import android.content.Context; 20 import android.content.Intent; 21 import android.graphics.drawable.Icon; 22 import android.net.ConnectivityManager; 23 import android.net.wifi.WifiConfiguration; 24 import android.net.wifi.WifiManager; 25 import android.os.UserHandle; 26 import android.os.UserManager; 27 28 import com.android.internal.logging.MetricsProto.MetricsEvent; 29 import com.android.settings.R; 30 import com.android.settings.TetherSettings; 31 import com.android.settings.Utils; 32 import com.android.settingslib.RestrictedLockUtils; 33 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; 34 import com.android.settingslib.TetherUtil; 35 36 public class HotspotCondition extends Condition { 37 38 private final WifiManager mWifiManager; 39 40 public HotspotCondition(ConditionManager manager) { 41 super(manager); 42 mWifiManager = mManager.getContext().getSystemService(WifiManager.class); 43 } 44 45 @Override 46 public void refreshState() { 47 boolean wifiTetherEnabled = mWifiManager.isWifiApEnabled(); 48 setActive(wifiTetherEnabled); 49 } 50 51 @Override 52 protected Class<?> getReceiverClass() { 53 return Receiver.class; 54 } 55 56 @Override 57 public Icon getIcon() { 58 return Icon.createWithResource(mManager.getContext(), R.drawable.ic_hotspot); 59 } 60 61 private String getSsid() { 62 WifiConfiguration wifiConfig = mWifiManager.getWifiApConfiguration(); 63 if (wifiConfig == null) { 64 return mManager.getContext().getString( 65 com.android.internal.R.string.wifi_tether_configure_ssid_default); 66 } else { 67 return wifiConfig.SSID; 68 } 69 } 70 71 @Override 72 public CharSequence getTitle() { 73 return mManager.getContext().getString(R.string.condition_hotspot_title); 74 } 75 76 @Override 77 public CharSequence getSummary() { 78 return mManager.getContext().getString(R.string.condition_hotspot_summary, getSsid()); 79 } 80 81 @Override 82 public CharSequence[] getActions() { 83 final Context context = mManager.getContext(); 84 if (RestrictedLockUtils.hasBaseUserRestriction(context, 85 UserManager.DISALLOW_CONFIG_TETHERING, UserHandle.myUserId())) { 86 return new CharSequence[0]; 87 } 88 return new CharSequence[] { context.getString(R.string.condition_turn_off) }; 89 } 90 91 @Override 92 public void onPrimaryClick() { 93 Utils.startWithFragment(mManager.getContext(), TetherSettings.class.getName(), null, null, 94 0, R.string.tether_settings_title_all, null); 95 } 96 97 @Override 98 public void onActionClick(int index) { 99 if (index == 0) { 100 final Context context = mManager.getContext(); 101 final EnforcedAdmin admin = RestrictedLockUtils.checkIfRestrictionEnforced(context, 102 UserManager.DISALLOW_CONFIG_TETHERING, UserHandle.myUserId()); 103 if (admin != null) { 104 RestrictedLockUtils.sendShowAdminSupportDetailsIntent(context, admin); 105 } else { 106 ConnectivityManager cm = (ConnectivityManager) context.getSystemService( 107 Context.CONNECTIVITY_SERVICE); 108 cm.stopTethering(ConnectivityManager.TETHERING_WIFI); 109 setActive(false); 110 } 111 } else { 112 throw new IllegalArgumentException("Unexpected index " + index); 113 } 114 } 115 116 @Override 117 public int getMetricsConstant() { 118 return MetricsEvent.SETTINGS_CONDITION_HOTSPOT; 119 } 120 121 public static class Receiver extends BroadcastReceiver { 122 @Override 123 public void onReceive(Context context, Intent intent) { 124 if (WifiManager.WIFI_AP_STATE_CHANGED_ACTION.equals(intent.getAction())) { 125 ConditionManager.get(context).getCondition(HotspotCondition.class) 126 .refreshState(); 127 } 128 } 129 } 130 } 131