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