1 /*
2  * Copyright (C) 2018 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 
17 package com.android.settings.homepage.contextualcards.conditional;
18 
19 import android.app.settings.SettingsEnums;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.net.ConnectivityManager;
25 import android.net.wifi.SoftApConfiguration;
26 import android.net.wifi.WifiManager;
27 import android.os.UserHandle;
28 import android.os.UserManager;
29 
30 import com.android.settings.R;
31 import com.android.settings.TetherSettings;
32 import com.android.settings.core.SubSettingLauncher;
33 import com.android.settings.homepage.contextualcards.ContextualCard;
34 import com.android.settingslib.RestrictedLockUtils;
35 import com.android.settingslib.RestrictedLockUtilsInternal;
36 
37 import java.util.Objects;
38 
39 public class HotspotConditionController implements ConditionalCardController {
40     static final int ID = Objects.hash("HotspotConditionController");
41 
42     private static final IntentFilter WIFI_AP_STATE_FILTER =
43             new IntentFilter(WifiManager.WIFI_AP_STATE_CHANGED_ACTION);
44 
45     private final Context mAppContext;
46     private final ConditionManager mConditionManager;
47     private final WifiManager mWifiManager;
48     private final Receiver mReceiver;
49 
50 
HotspotConditionController(Context appContext, ConditionManager conditionManager)51     public HotspotConditionController(Context appContext, ConditionManager conditionManager) {
52         mAppContext = appContext;
53         mConditionManager = conditionManager;
54         mWifiManager = appContext.getSystemService(WifiManager.class);
55         mReceiver = new Receiver();
56     }
57 
58     @Override
getId()59     public long getId() {
60         return ID;
61     }
62 
63     @Override
isDisplayable()64     public boolean isDisplayable() {
65         return mWifiManager.isWifiApEnabled();
66     }
67 
68     @Override
onPrimaryClick(Context context)69     public void onPrimaryClick(Context context) {
70         new SubSettingLauncher(context)
71                 .setDestination(TetherSettings.class.getName())
72                 .setSourceMetricsCategory(SettingsEnums.DASHBOARD_SUMMARY)
73                 .setTitleRes(R.string.tether_settings_title_all)
74                 .launch();
75     }
76 
77     @Override
onActionClick()78     public void onActionClick() {
79         final RestrictedLockUtils.EnforcedAdmin admin =
80                 RestrictedLockUtilsInternal.checkIfRestrictionEnforced(
81                         mAppContext, UserManager.DISALLOW_CONFIG_TETHERING, UserHandle.myUserId());
82         if (admin != null) {
83             RestrictedLockUtils.sendShowAdminSupportDetailsIntent(mAppContext, admin);
84         } else {
85             ConnectivityManager cm = (ConnectivityManager) mAppContext.getSystemService(
86                     Context.CONNECTIVITY_SERVICE);
87             cm.stopTethering(ConnectivityManager.TETHERING_WIFI);
88         }
89     }
90 
91     @Override
buildContextualCard()92     public ContextualCard buildContextualCard() {
93         return new ConditionalContextualCard.Builder()
94                 .setConditionId(ID)
95                 .setMetricsConstant(SettingsEnums.SETTINGS_CONDITION_HOTSPOT)
96                 .setActionText(mAppContext.getText(R.string.condition_turn_off))
97                 .setName(mAppContext.getPackageName() + "/"
98                         + mAppContext.getText(R.string.condition_hotspot_title))
99                 .setTitleText(mAppContext.getText(R.string.condition_hotspot_title).toString())
100                 .setSummaryText(getSsid().toString())
101                 .setIconDrawable(mAppContext.getDrawable(R.drawable.ic_hotspot))
102                 .setViewType(ConditionContextualCardRenderer.VIEW_TYPE_HALF_WIDTH)
103                 .build();
104     }
105 
106     @Override
startMonitoringStateChange()107     public void startMonitoringStateChange() {
108         mAppContext.registerReceiver(mReceiver, WIFI_AP_STATE_FILTER);
109     }
110 
111     @Override
stopMonitoringStateChange()112     public void stopMonitoringStateChange() {
113         mAppContext.unregisterReceiver(mReceiver);
114     }
115 
getSsid()116     private CharSequence getSsid() {
117         final SoftApConfiguration softApConfig = mWifiManager.getSoftApConfiguration();
118         if (softApConfig == null) {
119             // Should never happen.
120             return "";
121         }
122         return softApConfig.getSsid();
123     }
124 
125     public class Receiver extends BroadcastReceiver {
126         @Override
onReceive(Context context, Intent intent)127         public void onReceive(Context context, Intent intent) {
128             if (WifiManager.WIFI_AP_STATE_CHANGED_ACTION.equals(intent.getAction())) {
129                 mConditionManager.onConditionChanged();
130             }
131         }
132     }
133 }
134