• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Icon;
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.Utils;
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 
HotspotCondition(ConditionManager manager)44     public HotspotCondition(ConditionManager manager) {
45         super(manager);
46         mWifiManager = mManager.getContext().getSystemService(WifiManager.class);
47         mReceiver = new Receiver();
48     }
49 
50     @Override
refreshState()51     public void refreshState() {
52         boolean wifiTetherEnabled = mWifiManager.isWifiApEnabled();
53         setActive(wifiTetherEnabled);
54     }
55 
56     @Override
getReceiver()57     protected BroadcastReceiver getReceiver() {
58         return mReceiver;
59     }
60 
61     @Override
getIntentFilter()62     protected IntentFilter getIntentFilter() {
63         return WIFI_AP_STATE_FILTER;
64     }
65 
66     @Override
getIcon()67     public Icon getIcon() {
68         return Icon.createWithResource(mManager.getContext(), R.drawable.ic_hotspot);
69     }
70 
getSsid()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
getTitle()82     public CharSequence getTitle() {
83         return mManager.getContext().getString(R.string.condition_hotspot_title);
84     }
85 
86     @Override
getSummary()87     public CharSequence getSummary() {
88         return mManager.getContext().getString(R.string.condition_hotspot_summary, getSsid());
89     }
90 
91     @Override
getActions()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
onPrimaryClick()102     public void onPrimaryClick() {
103         Utils.startWithFragment(mManager.getContext(), TetherSettings.class.getName(), null, null,
104                 0, R.string.tether_settings_title_all, null, MetricsEvent.DASHBOARD_SUMMARY);
105     }
106 
107     @Override
onActionClick(int index)108     public void onActionClick(int index) {
109         if (index == 0) {
110             final Context context = mManager.getContext();
111             final EnforcedAdmin admin = RestrictedLockUtils.checkIfRestrictionEnforced(context,
112                     UserManager.DISALLOW_CONFIG_TETHERING, UserHandle.myUserId());
113             if (admin != null) {
114                 RestrictedLockUtils.sendShowAdminSupportDetailsIntent(context, admin);
115             } else {
116                 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(
117                         Context.CONNECTIVITY_SERVICE);
118                 cm.stopTethering(ConnectivityManager.TETHERING_WIFI);
119                 setActive(false);
120             }
121         } else {
122             throw new IllegalArgumentException("Unexpected index " + index);
123         }
124     }
125 
126     @Override
getMetricsConstant()127     public int getMetricsConstant() {
128         return MetricsEvent.SETTINGS_CONDITION_HOTSPOT;
129     }
130 
131     public static class Receiver extends BroadcastReceiver {
132         @Override
onReceive(Context context, Intent intent)133         public void onReceive(Context context, Intent intent) {
134             if (WifiManager.WIFI_AP_STATE_CHANGED_ACTION.equals(intent.getAction())) {
135                 ConditionManager.get(context).getCondition(HotspotCondition.class)
136                         .refreshState();
137             }
138         }
139     }
140 }
141