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 17 package com.android.car.settings.common; 18 19 import android.app.Dialog; 20 import android.os.Bundle; 21 22 import androidx.annotation.StringRes; 23 import androidx.fragment.app.Fragment; 24 25 import com.android.car.ui.AlertDialogBuilder; 26 import com.android.car.ui.preference.CarUiDialogFragment; 27 28 /** 29 * Dialog to inform that an action failed. 30 */ 31 public class ErrorDialog extends CarUiDialogFragment { 32 private static final String ERROR_DIALOG_TITLE_KEY = "error_dialog_title"; 33 private static final String DIALOG_TAG = "ErrorDialogTag"; 34 35 /** 36 * Shows the error dialog. 37 * 38 * @param parent Fragment associated with the dialog. 39 * @param title Title for the error dialog. 40 */ show(Fragment parent, @StringRes int title)41 public static ErrorDialog show(Fragment parent, @StringRes int title) { 42 ErrorDialog dialog = newInstance(title); 43 dialog.setTargetFragment(parent, 0); 44 dialog.show(parent.getFragmentManager(), DIALOG_TAG); 45 return dialog; 46 } 47 48 /** 49 * Creates an instance of error dialog with the appropriate title. This constructor should be 50 * used when starting an error dialog when we don't have a reference to the parent fragment. 51 */ newInstance(@tringRes int title)52 public static ErrorDialog newInstance(@StringRes int title) { 53 ErrorDialog dialog = new ErrorDialog(); 54 Bundle bundle = new Bundle(); 55 bundle.putInt(ERROR_DIALOG_TITLE_KEY, title); 56 dialog.setArguments(bundle); 57 return dialog; 58 } 59 60 @Override onCreateDialog(Bundle savedInstanceState)61 public Dialog onCreateDialog(Bundle savedInstanceState) { 62 return new AlertDialogBuilder(getContext()) 63 .setTitle(getArguments().getInt(ERROR_DIALOG_TITLE_KEY)) 64 .setPositiveButton(android.R.string.ok, /* listener= */ null) 65 .create(); 66 } 67 68 @Override onDialogClosed(boolean positiveResult)69 protected void onDialogClosed(boolean positiveResult) { 70 } 71 } 72