• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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.app.AlertDialog;
21 import android.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.DialogInterface;
24 import android.content.Intent;
25 import android.content.IntentFilter;
26 import android.debug.IAdbManager;
27 import android.net.ConnectivityManager;
28 import android.net.NetworkInfo;
29 import android.net.wifi.WifiInfo;
30 import android.net.wifi.WifiManager;
31 import android.os.Bundle;
32 import android.os.IBinder;
33 import android.os.ServiceManager;
34 import android.util.EventLog;
35 import android.util.Log;
36 import android.view.LayoutInflater;
37 import android.view.MotionEvent;
38 import android.view.View;
39 import android.view.Window;
40 import android.view.WindowManager;
41 import android.widget.CheckBox;
42 import android.widget.Toast;
43 
44 import com.android.internal.app.AlertActivity;
45 import com.android.internal.app.AlertController;
46 import com.android.systemui.R;
47 
48 /**
49  * Alerts the user of an untrusted network when enabling wireless debugging.
50  * The user can either deny, allow, or allow with the "always allow on this
51  * network" checked.
52  */
53 public class WifiDebuggingActivity extends AlertActivity
54                                   implements DialogInterface.OnClickListener {
55     private static final String TAG = "WifiDebuggingActivity";
56 
57     private CheckBox mAlwaysAllow;
58     // Notifies when wifi is disabled, or the network changed
59     private WifiChangeReceiver mWifiChangeReceiver;
60     private WifiManager mWifiManager;
61     private String mBssid;
62     private boolean mClicked = false;
63 
64     @Override
onCreate(Bundle icicle)65     public void onCreate(Bundle icicle) {
66         Window window = getWindow();
67         window.addSystemFlags(
68                 WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
69         window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
70 
71         super.onCreate(icicle);
72 
73 
74         mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
75         mWifiChangeReceiver = new WifiChangeReceiver(this);
76 
77         Intent intent = getIntent();
78         String ssid = intent.getStringExtra("ssid");
79         mBssid = intent.getStringExtra("bssid");
80 
81         if (ssid == null || mBssid == null) {
82             finish();
83             return;
84         }
85 
86         final AlertController.AlertParams ap = mAlertParams;
87         ap.mTitle = getString(R.string.wifi_debugging_title);
88         ap.mMessage = getString(R.string.wifi_debugging_message, ssid, mBssid);
89         ap.mPositiveButtonText = getString(R.string.wifi_debugging_allow);
90         ap.mNegativeButtonText = getString(android.R.string.cancel);
91         ap.mPositiveButtonListener = this;
92         ap.mNegativeButtonListener = this;
93 
94         // add "always allow" checkbox
95         LayoutInflater inflater = LayoutInflater.from(ap.mContext);
96         View checkbox = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null);
97         mAlwaysAllow = (CheckBox) checkbox.findViewById(com.android.internal.R.id.alwaysUse);
98         mAlwaysAllow.setText(getString(R.string.wifi_debugging_always));
99         ap.mView = checkbox;
100         window.setCloseOnTouchOutside(false);
101 
102         setupAlert();
103 
104         // adding touch listener on affirmative button - checks if window is obscured
105         // if obscured, do not let user give permissions (could be tapjacking involved)
106         final View.OnTouchListener filterTouchListener = (View v, MotionEvent event) -> {
107             // Filter obscured touches by consuming them.
108             if (((event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0)
109                     || ((event.getFlags() & MotionEvent.FLAG_WINDOW_IS_PARTIALLY_OBSCURED) != 0)) {
110                 if (event.getAction() == MotionEvent.ACTION_UP) {
111                     // TODO: need a different value for safety net?
112                     EventLog.writeEvent(0x534e4554, "62187985"); // safety net logging
113                     Toast.makeText(v.getContext(),
114                             R.string.touch_filtered_warning,
115                             Toast.LENGTH_SHORT).show();
116                 }
117                 return true;
118             }
119             return false;
120         };
121         mAlert.getButton(BUTTON_POSITIVE).setOnTouchListener(filterTouchListener);
122 
123     }
124 
125     @Override
onWindowAttributesChanged(WindowManager.LayoutParams params)126     public void onWindowAttributesChanged(WindowManager.LayoutParams params) {
127         super.onWindowAttributesChanged(params);
128     }
129 
130     private class WifiChangeReceiver extends BroadcastReceiver {
131         private final Activity mActivity;
WifiChangeReceiver(Activity activity)132         WifiChangeReceiver(Activity activity) {
133             mActivity = activity;
134         }
135 
136         @Override
onReceive(Context content, Intent intent)137         public void onReceive(Context content, Intent intent) {
138             String action = intent.getAction();
139             if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
140                 int state = intent.getIntExtra(
141                         WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED);
142                 if (state == WifiManager.WIFI_STATE_DISABLED) {
143                     mActivity.finish();
144                 }
145             } else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
146                 NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(
147                         WifiManager.EXTRA_NETWORK_INFO);
148                 if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
149                     if (!networkInfo.isConnected()) {
150                         mActivity.finish();
151                         return;
152                     }
153                     WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
154                     if (wifiInfo == null || wifiInfo.getNetworkId() == -1) {
155                         mActivity.finish();
156                         return;
157                     }
158                     String bssid = wifiInfo.getBSSID();
159                     if (bssid == null || bssid.isEmpty()) {
160                         mActivity.finish();
161                         return;
162                     }
163                     if (!bssid.equals(mBssid)) {
164                         mActivity.finish();
165                         return;
166                     }
167                 }
168             }
169         }
170     }
171 
172     @Override
onStart()173     public void onStart() {
174         super.onStart();
175         IntentFilter filter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
176         filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
177         registerReceiver(mWifiChangeReceiver, filter);
178         // Close quick shade
179         sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
180 
181     }
182 
183     @Override
onStop()184     protected void onStop() {
185         if (mWifiChangeReceiver != null) {
186             unregisterReceiver(mWifiChangeReceiver);
187         }
188         super.onStop();
189     }
190 
191     @Override
onDestroy()192     protected void onDestroy() {
193         super.onDestroy();
194 
195         // In the case where user dismissed the dialog, we don't get an onClick event.
196         // In that case, tell adb to deny the network connection.
197         if (!mClicked) {
198             try {
199                 IBinder b = ServiceManager.getService(ADB_SERVICE);
200                 IAdbManager service = IAdbManager.Stub.asInterface(b);
201                 service.denyWirelessDebugging();
202             } catch (Exception e) {
203                 Log.e(TAG, "Unable to notify Adb service", e);
204             }
205         }
206     }
207 
208     @Override
onClick(DialogInterface dialog, int which)209     public void onClick(DialogInterface dialog, int which) {
210         mClicked = true;
211         boolean allow = (which == AlertDialog.BUTTON_POSITIVE);
212         boolean alwaysAllow = allow && mAlwaysAllow.isChecked();
213         try {
214             IBinder b = ServiceManager.getService(ADB_SERVICE);
215             IAdbManager service = IAdbManager.Stub.asInterface(b);
216             if (allow) {
217                 service.allowWirelessDebugging(alwaysAllow, mBssid);
218             } else {
219                 service.denyWirelessDebugging();
220             }
221         } catch (Exception e) {
222             Log.e(TAG, "Unable to notify Adb service", e);
223         }
224         finish();
225     }
226 }
227