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
17package com.android.systemui.usb;
18
19import android.app.Activity;
20import android.content.BroadcastReceiver;
21import android.content.Context;
22import android.content.DialogInterface;
23import android.content.Intent;
24import android.content.IntentFilter;
25import android.hardware.usb.UsbManager;
26import android.os.Bundle;
27import android.os.SystemProperties;
28
29import com.android.internal.app.AlertActivity;
30import com.android.internal.app.AlertController;
31import com.android.systemui.R;
32
33public class UsbDebuggingSecondaryUserActivity extends AlertActivity
34        implements DialogInterface.OnClickListener {
35    private UsbDisconnectedReceiver mDisconnectedReceiver;
36
37    @Override
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;
56        public UsbDisconnectedReceiver(Activity activity) {
57            mActivity = activity;
58        }
59
60        @Override
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
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
81    protected void onStop() {
82        if (mDisconnectedReceiver != null) {
83            unregisterReceiver(mDisconnectedReceiver);
84        }
85        super.onStop();
86    }
87
88    @Override
89    public void onClick(DialogInterface dialog, int which) {
90        finish();
91    }
92}
93