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