1/*
2 * Copyright (C) 2017 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.settings.development;
18
19import android.app.AlertDialog;
20import android.app.Dialog;
21import android.content.Context;
22import android.provider.Settings;
23import android.support.v14.preference.SwitchPreference;
24
25import com.android.settings.R;
26import com.android.settings.core.PreferenceControllerMixin;
27import com.android.settingslib.development.AbstractEnableAdbPreferenceController;
28
29public class EnableAdbPreferenceController extends AbstractEnableAdbPreferenceController
30        implements PreferenceControllerMixin {
31
32    private Dialog mAdbDialog;
33    private boolean mDialogClicked;
34
35    public EnableAdbPreferenceController(Context context) {
36        super(context);
37    }
38
39    @Override
40    public void showConfirmationDialog(SwitchPreference preference) {
41        mDialogClicked = false;
42        dismissDialogs();
43        mAdbDialog = new AlertDialog.Builder(mContext).setMessage(
44                mContext.getString(R.string.adb_warning_message))
45                .setTitle(R.string.adb_warning_title)
46                .setPositiveButton(android.R.string.yes, (dialog, which) -> {
47                    mDialogClicked = true;
48                    writeAdbSetting(true);
49                })
50                .setNegativeButton(android.R.string.no, (dialog, which) -> {
51                    preference.setChecked(false);
52                })
53                .show();
54        mAdbDialog.setOnDismissListener(dialog -> {
55            // Assuming that onClick gets called first
56            if (!mDialogClicked) {
57                preference.setChecked(false);
58            }
59            mAdbDialog = null;
60        });
61    }
62
63    public void dismissDialogs() {
64        if (mAdbDialog != null) {
65            mAdbDialog.dismiss();
66            mAdbDialog = null;
67        }
68    }
69}
70