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 
17 package com.android.systemui.wifi;
18 
19 import android.app.Activity;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.DialogInterface;
23 import android.content.Intent;
24 import android.content.IntentFilter;
25 import android.net.ConnectivityManager;
26 import android.net.NetworkInfo;
27 import android.net.wifi.WifiInfo;
28 import android.net.wifi.WifiManager;
29 import android.os.Bundle;
30 
31 import com.android.internal.app.AlertActivity;
32 import com.android.internal.app.AlertController;
33 import com.android.systemui.R;
34 
35 /**
36  * Alerts the user that wireless debugging cannot be enabled by a secondary user.
37  */
38 public class WifiDebuggingSecondaryUserActivity extends AlertActivity
39         implements DialogInterface.OnClickListener {
40     private WifiChangeReceiver mWifiChangeReceiver;
41     private WifiManager mWifiManager;
42 
43     @Override
onCreate(Bundle icicle)44     public void onCreate(Bundle icicle) {
45         super.onCreate(icicle);
46 
47         mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
48         mWifiChangeReceiver = new WifiChangeReceiver(this);
49 
50         final AlertController.AlertParams ap = mAlertParams;
51         ap.mTitle = getString(R.string.wifi_debugging_secondary_user_title);
52         ap.mMessage = getString(R.string.wifi_debugging_secondary_user_message);
53         ap.mPositiveButtonText = getString(android.R.string.ok);
54         ap.mPositiveButtonListener = this;
55 
56         setupAlert();
57     }
58 
59     private class WifiChangeReceiver extends BroadcastReceiver {
60         private final Activity mActivity;
WifiChangeReceiver(Activity activity)61         WifiChangeReceiver(Activity activity) {
62             mActivity = activity;
63         }
64 
65         @Override
onReceive(Context content, Intent intent)66         public void onReceive(Context content, Intent intent) {
67             String action = intent.getAction();
68             if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
69                 int state = intent.getIntExtra(
70                         WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED);
71                 if (state == WifiManager.WIFI_STATE_DISABLED) {
72                     mActivity.finish();
73                 }
74             } else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
75                 NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(
76                         WifiManager.EXTRA_NETWORK_INFO);
77                 if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
78                     if (!networkInfo.isConnected()) {
79                         mActivity.finish();
80                         return;
81                     }
82                     WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
83                     if (wifiInfo == null || wifiInfo.getNetworkId() == -1) {
84                         mActivity.finish();
85                         return;
86                     }
87                 }
88             }
89         }
90     }
91 
92     @Override
onStart()93     public void onStart() {
94         super.onStart();
95 
96         IntentFilter filter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
97         filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
98         registerReceiver(mWifiChangeReceiver, filter);
99         // Close quick shade
100         sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
101     }
102 
103     @Override
onStop()104     protected void onStop() {
105         if (mWifiChangeReceiver != null) {
106             unregisterReceiver(mWifiChangeReceiver);
107         }
108         super.onStop();
109     }
110 
111     @Override
onClick(DialogInterface dialog, int which)112     public void onClick(DialogInterface dialog, int which) {
113         finish();
114     }
115 }
116