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