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.hardware.usb.IUsbManager; 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.widget.CheckBox; 36 37 import com.android.internal.app.AlertActivity; 38 import com.android.internal.app.AlertController; 39 import com.android.systemui.R; 40 41 public class UsbDebuggingActivity extends AlertActivity 42 implements DialogInterface.OnClickListener { 43 private static final String TAG = "UsbDebuggingActivity"; 44 45 private CheckBox mAlwaysAllow; 46 private UsbDisconnectedReceiver mDisconnectedReceiver; 47 private String mKey; 48 49 @Override onCreate(Bundle icicle)50 public void onCreate(Bundle icicle) { 51 super.onCreate(icicle); 52 53 if (SystemProperties.getInt("service.adb.tcp.port", 0) == 0) { 54 mDisconnectedReceiver = new UsbDisconnectedReceiver(this); 55 } 56 57 Intent intent = getIntent(); 58 String fingerprints = intent.getStringExtra("fingerprints"); 59 mKey = intent.getStringExtra("key"); 60 61 if (fingerprints == null || mKey == null) { 62 finish(); 63 return; 64 } 65 66 final AlertController.AlertParams ap = mAlertParams; 67 ap.mTitle = getString(R.string.usb_debugging_title); 68 ap.mIconId = com.android.internal.R.drawable.ic_dialog_usb; 69 ap.mMessage = getString(R.string.usb_debugging_message, fingerprints); 70 ap.mPositiveButtonText = getString(android.R.string.ok); 71 ap.mNegativeButtonText = getString(android.R.string.cancel); 72 ap.mPositiveButtonListener = this; 73 ap.mNegativeButtonListener = this; 74 75 // add "always allow" checkbox 76 LayoutInflater inflater = LayoutInflater.from(ap.mContext); 77 View checkbox = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null); 78 mAlwaysAllow = (CheckBox)checkbox.findViewById(com.android.internal.R.id.alwaysUse); 79 mAlwaysAllow.setText(getString(R.string.usb_debugging_always)); 80 ap.mView = checkbox; 81 82 setupAlert(); 83 } 84 85 private class UsbDisconnectedReceiver extends BroadcastReceiver { 86 private final Activity mActivity; UsbDisconnectedReceiver(Activity activity)87 public UsbDisconnectedReceiver(Activity activity) { 88 mActivity = activity; 89 } 90 91 @Override onReceive(Context content, Intent intent)92 public void onReceive(Context content, Intent intent) { 93 String action = intent.getAction(); 94 if (!UsbManager.ACTION_USB_STATE.equals(action)) { 95 return; 96 } 97 boolean connected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false); 98 if (!connected) { 99 mActivity.finish(); 100 } 101 } 102 } 103 104 @Override onStart()105 public void onStart() { 106 super.onStart(); 107 IntentFilter filter = new IntentFilter(UsbManager.ACTION_USB_STATE); 108 registerReceiver(mDisconnectedReceiver, filter); 109 } 110 111 @Override onStop()112 protected void onStop() { 113 if (mDisconnectedReceiver != null) { 114 unregisterReceiver(mDisconnectedReceiver); 115 } 116 super.onStop(); 117 } 118 119 @Override onClick(DialogInterface dialog, int which)120 public void onClick(DialogInterface dialog, int which) { 121 boolean allow = (which == AlertDialog.BUTTON_POSITIVE); 122 boolean alwaysAllow = allow && mAlwaysAllow.isChecked(); 123 try { 124 IBinder b = ServiceManager.getService(USB_SERVICE); 125 IUsbManager service = IUsbManager.Stub.asInterface(b); 126 if (allow) { 127 service.allowUsbDebugging(alwaysAllow, mKey); 128 } else { 129 service.denyUsbDebugging(); 130 } 131 } catch (Exception e) { 132 Log.e(TAG, "Unable to notify Usb service", e); 133 } 134 finish(); 135 } 136 } 137