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