1 /* 2 * Copyright (C) 2018 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 package com.android.car.settings.common; 17 18 import android.app.Dialog; 19 import android.content.Context; 20 import android.content.DialogInterface; 21 import android.os.Bundle; 22 import android.support.v4.app.DialogFragment; 23 24 import androidx.car.app.CarAlertDialog; 25 26 import com.android.car.settings.R; 27 import com.android.car.settings.common.BaseFragment.FragmentController; 28 import com.android.car.settings.quicksettings.QuickSettingFragment; 29 30 /** 31 * A dialog to block non-distraction optimized view when restriction is applied. 32 */ 33 public class DOBlockingDialogFragment extends DialogFragment implements 34 DialogInterface.OnClickListener { 35 public static final String DIALOG_TAG = "block_dialog_tag"; 36 private static final String MESSAGE_ARG_KEY = "message"; 37 private boolean mShowQuickSettingsMainScreen = true; 38 39 /** 40 * Creates a DOBlockingDialogFragment with a specified message 41 * 42 * @param message 43 * @return an instance of DOBlockingDialogFragment 44 */ newInstance(String message)45 public static DOBlockingDialogFragment newInstance(String message) { 46 DOBlockingDialogFragment fragment = new DOBlockingDialogFragment(); 47 48 Bundle args = new Bundle(); 49 args.putString(MESSAGE_ARG_KEY, message); 50 fragment.setArguments(args); 51 52 return fragment; 53 } 54 55 @Override onCreateDialog(Bundle savedInstanceState)56 public Dialog onCreateDialog(Bundle savedInstanceState) { 57 Context context = getContext(); 58 // If a message is not set, use the default message. 59 String message = getArguments().getString(MESSAGE_ARG_KEY); 60 if (message == null) { 61 message = getContext().getString(R.string.restricted_while_driving); 62 } 63 Dialog dialog = new CarAlertDialog.Builder(context) 64 .setBody(message) 65 .setPositiveButton(context.getString(R.string.okay), /* listener= */ this) 66 .setCancelable(false) 67 .create(); 68 return dialog; 69 } 70 71 /** 72 * Return to the quick settings main screen after the dialog is dismissed. 73 * @param showQuickSettingsMainScreen whether to return to the quick settings main screen, the 74 * default value is true 75 */ goBackToQuickSettingsMainScreen(boolean showQuickSettingsMainScreen)76 public void goBackToQuickSettingsMainScreen(boolean showQuickSettingsMainScreen) { 77 mShowQuickSettingsMainScreen = showQuickSettingsMainScreen; 78 } 79 80 // only one button, no need to check on negative. 81 @Override onClick(DialogInterface dialog, int which)82 public void onClick(DialogInterface dialog, int which) { 83 if (mShowQuickSettingsMainScreen) { 84 ((FragmentController) getActivity()).launchFragment( 85 QuickSettingFragment.newInstance()); 86 } 87 dismiss(); 88 } 89 } 90