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