1/* 2 * Copyright 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 17package com.example.androidx.car; 18 19import android.app.Dialog; 20import android.content.Context; 21import android.os.Bundle; 22import android.widget.CheckBox; 23 24import androidx.car.app.CarAlertDialog; 25import androidx.fragment.app.DialogFragment; 26import androidx.fragment.app.FragmentActivity; 27 28/** 29 * A demo activity that will display a {@link CarAlertDialog} with configurable options for what is 30 * in the contents of that resulting dialog. 31 */ 32public class CarAlertDialogDemo extends FragmentActivity { 33 private static final String DIALOG_TAG = "alert_dialog_tag"; 34 35 @Override 36 protected void onCreate(Bundle savedInstanceState) { 37 super.onCreate(savedInstanceState); 38 setContentView(R.layout.alert_dialog_activity); 39 40 CheckBox hasTitleView = findViewById(R.id.has_title); 41 CheckBox hasBodyText = findViewById(R.id.has_body_text); 42 CheckBox hasSingleLineBody = findViewById(R.id.has_single_line_body); 43 CheckBox hasAction1 = findViewById(R.id.has_action_1); 44 CheckBox hasAction2 = findViewById(R.id.has_action_2); 45 46 findViewById(R.id.create_dialog).setOnClickListener(v -> { 47 AlertDialogFragment alertDialog = AlertDialogFragment.newInstance( 48 hasTitleView.isChecked(), 49 hasBodyText.isChecked(), 50 hasSingleLineBody.isChecked(), 51 hasAction1.isChecked(), 52 hasAction2.isChecked()); 53 54 alertDialog.show(getSupportFragmentManager(), DIALOG_TAG); 55 }); 56 } 57 58 /** A {@link DialogFragment} that will inflate a {@link CarAlertDialog}. */ 59 public static class AlertDialogFragment extends DialogFragment { 60 private static final String HAS_TITLE_KEY = "has_title_key"; 61 private static final String HAS_BODY_KEY = "has_body_key"; 62 private static final String HAS_SINGLE_LINE_BODY_KEY = "has_single_line_body_key"; 63 private static final String HAS_ACTION_1_KEY = "has_action_1_key"; 64 private static final String HAS_ACTION_2_KEY = "has_action_2_key"; 65 66 static AlertDialogFragment newInstance(boolean hasTitle, 67 boolean hasBody, boolean hasSingleLineBody, boolean hasAction1, 68 boolean hasAction2) { 69 Bundle args = new Bundle(); 70 args.putBoolean(HAS_TITLE_KEY, hasTitle); 71 args.putBoolean(HAS_BODY_KEY, hasBody); 72 args.putBoolean(HAS_SINGLE_LINE_BODY_KEY, hasSingleLineBody); 73 args.putBoolean(HAS_ACTION_1_KEY, hasAction1); 74 args.putBoolean(HAS_ACTION_2_KEY, hasAction2); 75 76 AlertDialogFragment fragment = new AlertDialogFragment(); 77 fragment.setArguments(args); 78 return fragment; 79 } 80 81 @Override 82 public Dialog onCreateDialog(Bundle savedInstanceState) { 83 Context context = getContext(); 84 Bundle args = getArguments(); 85 CarAlertDialog.Builder builder = new CarAlertDialog.Builder(context); 86 87 if (args.getBoolean(HAS_TITLE_KEY)) { 88 builder.setTitle(context.getString(R.string.alert_dialog_title)); 89 } 90 91 if (args.getBoolean(HAS_BODY_KEY)) { 92 builder.setBody(context.getString(R.string.alert_dialog_body)); 93 } 94 95 if (args.getBoolean(HAS_SINGLE_LINE_BODY_KEY)) { 96 builder.setBody(context.getString(R.string.alert_dialog_body_single_line)); 97 } 98 99 if (args.getBoolean(HAS_ACTION_1_KEY)) { 100 builder.setPositiveButton(context.getString(R.string.alert_dialog_action1), 101 /* listener= */ null); 102 } 103 104 if (args.getBoolean(HAS_ACTION_2_KEY)) { 105 builder.setNegativeButton(context.getString(R.string.alert_dialog_action2), 106 /* listener= */ null); 107 } 108 109 return builder.create(); 110 } 111 } 112} 113