1 /* 2 * Copyright (C) 2019 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.ui.paintbooth.dialogs; 18 19 import android.app.Activity; 20 import android.os.Bundle; 21 import android.util.Pair; 22 import android.view.LayoutInflater; 23 import android.view.View; 24 import android.view.ViewGroup; 25 import android.widget.Button; 26 import android.widget.Toast; 27 28 import androidx.annotation.NonNull; 29 30 import com.android.car.ui.AlertDialogBuilder; 31 import com.android.car.ui.baselayout.Insets; 32 import com.android.car.ui.baselayout.InsetsChangedListener; 33 import com.android.car.ui.core.CarUi; 34 import com.android.car.ui.paintbooth.R; 35 import com.android.car.ui.recyclerview.CarUiRadioButtonListItem; 36 import com.android.car.ui.recyclerview.CarUiRadioButtonListItemAdapter; 37 import com.android.car.ui.recyclerview.CarUiRecyclerView; 38 import com.android.car.ui.toolbar.Toolbar; 39 import com.android.car.ui.toolbar.ToolbarController; 40 41 import java.util.ArrayList; 42 import java.util.List; 43 44 /** 45 * Activity that shows different dialogs from the device default theme. 46 */ 47 public class DialogsActivity extends Activity implements InsetsChangedListener { 48 49 private final List<Pair<Integer, View.OnClickListener>> mButtons = new ArrayList<>(); 50 51 @Override onCreate(Bundle savedInstanceState)52 protected void onCreate(Bundle savedInstanceState) { 53 super.onCreate(savedInstanceState); 54 setContentView(R.layout.car_ui_recycler_view_activity); 55 ToolbarController toolbar = CarUi.requireToolbar(this); 56 toolbar.setTitle(getTitle()); 57 toolbar.setState(Toolbar.State.SUBPAGE); 58 59 mButtons.add(Pair.create(R.string.dialog_show_dialog, 60 v -> showDialog())); 61 mButtons.add(Pair.create(R.string.dialog_show_dialog_icon, 62 v -> showDialogWithIcon())); 63 mButtons.add(Pair.create(R.string.dialog_show_dialog_edit, 64 v -> showDialogWithTextBox())); 65 mButtons.add(Pair.create(R.string.dialog_show_dialog_only_positive, 66 v -> showDialogWithOnlyPositiveButton())); 67 mButtons.add(Pair.create(R.string.dialog_show_dialog_no_button, 68 v -> showDialogWithNoButtonProvided())); 69 mButtons.add(Pair.create(R.string.dialog_show_dialog_checkbox, 70 v -> showDialogWithCheckbox())); 71 mButtons.add(Pair.create(R.string.dialog_show_dialog_no_title, 72 v -> showDialogWithoutTitle())); 73 mButtons.add(Pair.create(R.string.dialog_show_toast, 74 v -> showToast())); 75 mButtons.add(Pair.create(R.string.dialog_show_subtitle, 76 v -> showDialogWithSubtitle())); 77 mButtons.add(Pair.create(R.string.dialog_show_subtitle_and_icon, 78 v -> showDialogWithSubtitleAndIcon())); 79 mButtons.add(Pair.create(R.string.dialog_show_single_choice, 80 v -> showDialogWithSingleChoiceItems())); 81 82 83 CarUiRecyclerView recyclerView = requireViewById(R.id.list); 84 recyclerView.setAdapter(mAdapter); 85 } 86 showDialog()87 private void showDialog() { 88 new AlertDialogBuilder(this) 89 .setTitle("Standard Alert Dialog") 90 .setMessage("With a message to show.") 91 .setNeutralButton("NEUTRAL", (dialogInterface, which) -> { 92 }) 93 .setPositiveButton("OK", (dialogInterface, which) -> { 94 }) 95 .setNegativeButton("CANCEL", (dialogInterface, which) -> { 96 }) 97 .show(); 98 } 99 showDialogWithIcon()100 private void showDialogWithIcon() { 101 new AlertDialogBuilder(this) 102 .setTitle("Alert dialog with icon") 103 .setMessage("The message body of the alert") 104 .setIcon(R.drawable.ic_tracklist) 105 .show(); 106 } 107 showDialogWithNoButtonProvided()108 private void showDialogWithNoButtonProvided() { 109 new AlertDialogBuilder(this) 110 .setTitle("Standard Alert Dialog") 111 .show(); 112 } 113 showDialogWithCheckbox()114 private void showDialogWithCheckbox() { 115 new AlertDialogBuilder(this) 116 .setTitle("Custom Dialog Box") 117 .setMultiChoiceItems( 118 new CharSequence[]{"I am a checkbox"}, 119 new boolean[]{false}, 120 (dialog, which, isChecked) -> { 121 }) 122 .setPositiveButton("OK", (dialogInterface, which) -> { 123 }) 124 .setNegativeButton("CANCEL", (dialogInterface, which) -> { 125 }) 126 .show(); 127 } 128 showDialogWithTextBox()129 private void showDialogWithTextBox() { 130 new AlertDialogBuilder(this) 131 .setTitle("Standard Alert Dialog") 132 .setEditBox("Edit me please", null, null) 133 .setPositiveButton("OK", (dialogInterface, i) -> { 134 }) 135 .show(); 136 } 137 showDialogWithOnlyPositiveButton()138 private void showDialogWithOnlyPositiveButton() { 139 new AlertDialogBuilder(this) 140 .setTitle("Standard Alert Dialog").setMessage("With a message to show.") 141 .setPositiveButton("OK", (dialogInterface, i) -> { 142 }) 143 .show(); 144 } 145 showDialogWithoutTitle()146 private void showDialogWithoutTitle() { 147 new AlertDialogBuilder(this) 148 .setMessage("I dont have a title.") 149 .setPositiveButton("OK", (dialogInterface, i) -> { 150 }) 151 .setNegativeButton("CANCEL", (dialogInterface, which) -> { 152 }) 153 .show(); 154 } 155 showToast()156 private void showToast() { 157 Toast.makeText(this, "Toast message looks like this", Toast.LENGTH_LONG).show(); 158 } 159 showDialogWithSubtitle()160 private void showDialogWithSubtitle() { 161 new AlertDialogBuilder(this) 162 .setTitle("My Title!") 163 .setSubtitle("My Subtitle!") 164 .setMessage("My Message!") 165 .show(); 166 } 167 showDialogWithSingleChoiceItems()168 private void showDialogWithSingleChoiceItems() { 169 ArrayList<CarUiRadioButtonListItem> data = new ArrayList<>(); 170 171 CarUiRadioButtonListItem item = new CarUiRadioButtonListItem(); 172 item.setTitle("First item"); 173 data.add(item); 174 175 item = new CarUiRadioButtonListItem(); 176 item.setTitle("Second item"); 177 data.add(item); 178 179 item = new CarUiRadioButtonListItem(); 180 item.setTitle("Third item"); 181 data.add(item); 182 183 new AlertDialogBuilder(this) 184 .setTitle("Select one option.") 185 .setSubtitle("Ony one option may be selected at a time") 186 .setSingleChoiceItems(new CarUiRadioButtonListItemAdapter(data), null) 187 .show(); 188 } 189 showDialogWithSubtitleAndIcon()190 private void showDialogWithSubtitleAndIcon() { 191 new AlertDialogBuilder(this) 192 .setTitle("My Title!") 193 .setSubtitle("My Subtitle!") 194 .setMessage("My Message!") 195 .setIcon(R.drawable.ic_tracklist) 196 .show(); 197 } 198 199 private static class ViewHolder extends CarUiRecyclerView.ViewHolder { 200 201 private final Button mButton; 202 ViewHolder(View itemView)203 ViewHolder(View itemView) { 204 super(itemView); 205 mButton = itemView.requireViewById(R.id.button); 206 } 207 bind(Integer title, View.OnClickListener listener)208 public void bind(Integer title, View.OnClickListener listener) { 209 mButton.setText(title); 210 mButton.setOnClickListener(listener); 211 } 212 } 213 214 private final CarUiRecyclerView.Adapter<ViewHolder> mAdapter = 215 new CarUiRecyclerView.Adapter<ViewHolder>() { 216 @Override 217 public int getItemCount() { 218 return mButtons.size(); 219 } 220 221 @Override 222 public ViewHolder onCreateViewHolder(ViewGroup parent, int position) { 223 View item = 224 LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, 225 parent, false); 226 return new ViewHolder(item); 227 } 228 229 @Override 230 public void onBindViewHolder(@NonNull ViewHolder holder, int position) { 231 Pair<Integer, View.OnClickListener> pair = mButtons.get(position); 232 holder.bind(pair.first, pair.second); 233 } 234 }; 235 236 @Override onCarUiInsetsChanged(Insets insets)237 public void onCarUiInsetsChanged(Insets insets) { 238 requireViewById(R.id.list) 239 .setPadding(0, insets.getTop(), 0, insets.getBottom()); 240 requireViewById(android.R.id.content) 241 .setPadding(insets.getLeft(), 0, insets.getRight(), 0); 242 } 243 } 244