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.hardware.usb.UsbManager; 26 import android.os.Bundle; 27 import android.os.SystemProperties; 28 29 import com.android.internal.app.AlertActivity; 30 import com.android.internal.app.AlertController; 31 import com.android.systemui.R; 32 33 public class UsbDebuggingSecondaryUserActivity extends AlertActivity 34 implements DialogInterface.OnClickListener { 35 private UsbDisconnectedReceiver mDisconnectedReceiver; 36 37 @Override onCreate(Bundle icicle)38 public void onCreate(Bundle icicle) { 39 super.onCreate(icicle); 40 41 if (SystemProperties.getInt("service.adb.tcp.port", 0) == 0) { 42 mDisconnectedReceiver = new UsbDisconnectedReceiver(this); 43 } 44 45 final AlertController.AlertParams ap = mAlertParams; 46 ap.mTitle = getString(R.string.usb_debugging_secondary_user_title); 47 ap.mMessage = getString(R.string.usb_debugging_secondary_user_message); 48 ap.mPositiveButtonText = getString(android.R.string.ok); 49 ap.mPositiveButtonListener = this; 50 51 setupAlert(); 52 } 53 54 private class UsbDisconnectedReceiver extends BroadcastReceiver { 55 private final Activity mActivity; UsbDisconnectedReceiver(Activity activity)56 public UsbDisconnectedReceiver(Activity activity) { 57 mActivity = activity; 58 } 59 60 @Override onReceive(Context content, Intent intent)61 public void onReceive(Context content, Intent intent) { 62 String action = intent.getAction(); 63 if (UsbManager.ACTION_USB_STATE.equals(action)) { 64 boolean connected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false); 65 if (!connected) { 66 mActivity.finish(); 67 } 68 } 69 } 70 } 71 72 @Override onStart()73 public void onStart() { 74 super.onStart(); 75 76 IntentFilter filter = new IntentFilter(UsbManager.ACTION_USB_STATE); 77 registerReceiver(mDisconnectedReceiver, filter); 78 } 79 80 @Override onStop()81 protected void onStop() { 82 if (mDisconnectedReceiver != null) { 83 unregisterReceiver(mDisconnectedReceiver); 84 } 85 super.onStop(); 86 } 87 88 @Override onClick(DialogInterface dialog, int which)89 public void onClick(DialogInterface dialog, int which) { 90 finish(); 91 } 92 } 93