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.usb; 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.hardware.usb.UsbManager; 28 import android.os.Bundle; 29 import android.os.IBinder; 30 import android.os.ServiceManager; 31 import android.os.SystemProperties; 32 import android.util.Log; 33 import android.view.LayoutInflater; 34 import android.view.View; 35 import android.view.Window; 36 import android.view.WindowManager; 37 import android.widget.CheckBox; 38 39 import com.android.internal.app.AlertActivity; 40 import com.android.internal.app.AlertController; 41 import com.android.systemui.R; 42 import com.android.systemui.broadcast.BroadcastDispatcher; 43 44 import javax.inject.Inject; 45 46 public class UsbDebuggingActivity extends AlertActivity 47 implements DialogInterface.OnClickListener { 48 private static final String TAG = "UsbDebuggingActivity"; 49 50 private CheckBox mAlwaysAllow; 51 private UsbDisconnectedReceiver mDisconnectedReceiver; 52 private final BroadcastDispatcher mBroadcastDispatcher; 53 private String mKey; 54 private boolean mServiceNotified; 55 56 @Inject UsbDebuggingActivity(BroadcastDispatcher broadcastDispatcher)57 public UsbDebuggingActivity(BroadcastDispatcher broadcastDispatcher) { 58 super(); 59 mBroadcastDispatcher = broadcastDispatcher; 60 } 61 62 @Override onCreate(Bundle icicle)63 public void onCreate(Bundle icicle) { 64 Window window = getWindow(); 65 window.addSystemFlags( 66 WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS); 67 window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG); 68 69 super.onCreate(icicle); 70 71 if (SystemProperties.getInt("service.adb.tcp.port", 0) == 0) { 72 mDisconnectedReceiver = new UsbDisconnectedReceiver(this); 73 IntentFilter filter = new IntentFilter(UsbManager.ACTION_USB_STATE); 74 mBroadcastDispatcher.registerReceiver(mDisconnectedReceiver, filter); 75 } 76 77 Intent intent = getIntent(); 78 String fingerprints = intent.getStringExtra("fingerprints"); 79 mKey = intent.getStringExtra("key"); 80 81 if (fingerprints == null || mKey == null) { 82 finish(); 83 return; 84 } 85 86 final AlertController.AlertParams ap = mAlertParams; 87 ap.mTitle = getString(R.string.usb_debugging_title); 88 ap.mMessage = getString(R.string.usb_debugging_message, fingerprints); 89 ap.mPositiveButtonText = getString(R.string.usb_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.usb_debugging_always)); 99 ap.mView = checkbox; 100 window.setCloseOnTouchOutside(false); 101 102 setupAlert(); 103 } 104 105 @Override onWindowAttributesChanged(WindowManager.LayoutParams params)106 public void onWindowAttributesChanged(WindowManager.LayoutParams params) { 107 super.onWindowAttributesChanged(params); 108 } 109 110 private class UsbDisconnectedReceiver extends BroadcastReceiver { 111 private final Activity mActivity; UsbDisconnectedReceiver(Activity activity)112 UsbDisconnectedReceiver(Activity activity) { 113 mActivity = activity; 114 } 115 116 @Override onReceive(Context content, Intent intent)117 public void onReceive(Context content, Intent intent) { 118 String action = intent.getAction(); 119 if (!UsbManager.ACTION_USB_STATE.equals(action)) { 120 return; 121 } 122 boolean connected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false); 123 if (!connected) { 124 Log.d(TAG, "USB disconnected, notifying service"); 125 notifyService(false); 126 mActivity.finish(); 127 } 128 } 129 } 130 131 @Override onDestroy()132 protected void onDestroy() { 133 if (mDisconnectedReceiver != null) { 134 mBroadcastDispatcher.unregisterReceiver(mDisconnectedReceiver); 135 } 136 // Only notify the service if the activity is finishing; if onDestroy has been called due to 137 // a configuration change then allow the user to still authorize the connection the next 138 // time the activity is in the foreground. 139 if (isFinishing()) { 140 // If the ADB service has not yet been notified due to this dialog being closed in some 141 // other way then notify the service to deny the connection to ensure system_server 142 // sends a response to adbd. 143 if (!mServiceNotified) { 144 notifyService(false); 145 } 146 } 147 super.onDestroy(); 148 } 149 150 @Override onClick(DialogInterface dialog, int which)151 public void onClick(DialogInterface dialog, int which) { 152 boolean allow = (which == AlertDialog.BUTTON_POSITIVE); 153 boolean alwaysAllow = allow && mAlwaysAllow.isChecked(); 154 notifyService(allow, alwaysAllow); 155 finish(); 156 } 157 158 /** 159 * Notifies the ADB service as to whether the current ADB request should be allowed; if the 160 * request is allowed it is only allowed for this session, and the user should be prompted again 161 * on subsequent requests from this key. 162 * 163 * @param allow whether the connection should be allowed for this session 164 */ notifyService(boolean allow)165 private void notifyService(boolean allow) { 166 notifyService(allow, false); 167 } 168 169 /** 170 * Notifies the ADB service as to whether the current ADB request should be allowed, and if 171 * subsequent requests from this key should be allowed without user consent. 172 * 173 * @param allow whether the connection should be allowed 174 * @param alwaysAllow whether subsequent requests from this key should be allowed without user 175 * consent 176 */ notifyService(boolean allow, boolean alwaysAllow)177 private void notifyService(boolean allow, boolean alwaysAllow) { 178 try { 179 IBinder b = ServiceManager.getService(ADB_SERVICE); 180 IAdbManager service = IAdbManager.Stub.asInterface(b); 181 if (allow) { 182 service.allowDebugging(alwaysAllow, mKey); 183 } else { 184 service.denyDebugging(); 185 } 186 mServiceNotified = true; 187 } catch (Exception e) { 188 Log.e(TAG, "Unable to notify Usb service", e); 189 } 190 } 191 } 192