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.usb; 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.debug.IAdbManager; 26 import android.hardware.usb.UsbManager; 27 import android.os.Bundle; 28 import android.os.IBinder; 29 import android.os.RemoteException; 30 import android.os.ServiceManager; 31 import android.os.SystemProperties; 32 import android.util.Log; 33 34 import com.android.internal.app.AlertActivity; 35 import com.android.internal.app.AlertController; 36 import com.android.systemui.R; 37 import com.android.systemui.broadcast.BroadcastDispatcher; 38 39 import javax.inject.Inject; 40 41 public class UsbDebuggingSecondaryUserActivity extends AlertActivity 42 implements DialogInterface.OnClickListener { 43 private static final String TAG = "UsbDebuggingSecondaryUserActivity"; 44 private UsbDisconnectedReceiver mDisconnectedReceiver; 45 private final BroadcastDispatcher mBroadcastDispatcher; 46 47 @Inject UsbDebuggingSecondaryUserActivity(BroadcastDispatcher broadcastDispatcher)48 public UsbDebuggingSecondaryUserActivity(BroadcastDispatcher broadcastDispatcher) { 49 mBroadcastDispatcher = broadcastDispatcher; 50 } 51 52 @Override onCreate(Bundle icicle)53 public void onCreate(Bundle icicle) { 54 super.onCreate(icicle); 55 56 if (SystemProperties.getInt("service.adb.tcp.port", 0) == 0) { 57 mDisconnectedReceiver = new UsbDisconnectedReceiver(this); 58 } 59 60 final AlertController.AlertParams ap = mAlertParams; 61 ap.mTitle = getString(R.string.usb_debugging_secondary_user_title); 62 ap.mMessage = getString(R.string.usb_debugging_secondary_user_message); 63 ap.mPositiveButtonText = getString(android.R.string.ok); 64 ap.mPositiveButtonListener = this; 65 66 setupAlert(); 67 } 68 69 private class UsbDisconnectedReceiver extends BroadcastReceiver { 70 private final Activity mActivity; UsbDisconnectedReceiver(Activity activity)71 UsbDisconnectedReceiver(Activity activity) { 72 mActivity = activity; 73 } 74 75 @Override onReceive(Context content, Intent intent)76 public void onReceive(Context content, Intent intent) { 77 String action = intent.getAction(); 78 if (UsbManager.ACTION_USB_STATE.equals(action)) { 79 boolean connected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false); 80 if (!connected) { 81 mActivity.finish(); 82 } 83 } 84 } 85 } 86 87 @Override onStart()88 public void onStart() { 89 super.onStart(); 90 if (mDisconnectedReceiver != null) { 91 IntentFilter filter = new IntentFilter(UsbManager.ACTION_USB_STATE); 92 mBroadcastDispatcher.registerReceiver(mDisconnectedReceiver, filter); 93 } 94 } 95 96 @Override onStop()97 protected void onStop() { 98 if (mDisconnectedReceiver != null) { 99 mBroadcastDispatcher.unregisterReceiver(mDisconnectedReceiver); 100 } 101 try { 102 IBinder b = ServiceManager.getService(ADB_SERVICE); 103 IAdbManager service = IAdbManager.Stub.asInterface(b); 104 service.denyDebugging(); 105 } catch (RemoteException e) { 106 Log.e(TAG, "Unable to notify Usb service", e); 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