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 17 package com.android.settings.development; 18 19 import android.app.AlertDialog; 20 import android.app.Dialog; 21 import android.content.Context; 22 import android.provider.Settings; 23 import android.support.v14.preference.SwitchPreference; 24 25 import com.android.settings.R; 26 import com.android.settings.core.PreferenceControllerMixin; 27 import com.android.settingslib.development.AbstractEnableAdbPreferenceController; 28 29 public class EnableAdbPreferenceController extends AbstractEnableAdbPreferenceController 30 implements PreferenceControllerMixin { 31 32 private Dialog mAdbDialog; 33 private boolean mDialogClicked; 34 EnableAdbPreferenceController(Context context)35 public EnableAdbPreferenceController(Context context) { 36 super(context); 37 } 38 39 @Override showConfirmationDialog(SwitchPreference preference)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 dismissDialogs()63 public void dismissDialogs() { 64 if (mAdbDialog != null) { 65 mAdbDialog.dismiss(); 66 mAdbDialog = null; 67 } 68 } 69 } 70