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.Manifest;
20 import android.app.Activity;
21 import android.content.pm.PackageManager;
22 import android.os.Bundle;
23 import android.util.Pair;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.widget.Button;
28 import android.widget.Toast;
29 
30 import androidx.annotation.NonNull;
31 import androidx.recyclerview.widget.RecyclerView;
32 
33 import com.android.car.ui.AlertDialogBuilder;
34 import com.android.car.ui.baselayout.Insets;
35 import com.android.car.ui.baselayout.InsetsChangedListener;
36 import com.android.car.ui.core.CarUi;
37 import com.android.car.ui.paintbooth.R;
38 import com.android.car.ui.recyclerview.CarUiRadioButtonListItem;
39 import com.android.car.ui.recyclerview.CarUiRadioButtonListItemAdapter;
40 import com.android.car.ui.recyclerview.CarUiRecyclerView;
41 import com.android.car.ui.toolbar.Toolbar;
42 import com.android.car.ui.toolbar.ToolbarController;
43 
44 import java.util.ArrayList;
45 import java.util.List;
46 
47 /**
48  * Activity that shows different dialogs from the device default theme.
49  */
50 public class DialogsActivity extends Activity implements InsetsChangedListener {
51 
52     private final List<Pair<Integer, View.OnClickListener>> mButtons = new ArrayList<>();
53 
54     @Override
onCreate(Bundle savedInstanceState)55     protected void onCreate(Bundle savedInstanceState) {
56         super.onCreate(savedInstanceState);
57         setContentView(R.layout.car_ui_recycler_view_activity);
58         ToolbarController toolbar = CarUi.requireToolbar(this);
59         toolbar.setTitle(getTitle());
60         toolbar.setState(Toolbar.State.SUBPAGE);
61 
62         mButtons.add(Pair.create(R.string.dialog_show_dialog,
63                 v -> showDialog()));
64         mButtons.add(Pair.create(R.string.dialog_show_dialog_icon,
65                 v -> showDialogWithIcon()));
66         mButtons.add(Pair.create(R.string.dialog_show_dialog_edit,
67                 v -> showDialogWithTextBox()));
68         mButtons.add(Pair.create(R.string.dialog_show_dialog_only_positive,
69                 v -> showDialogWithOnlyPositiveButton()));
70         mButtons.add(Pair.create(R.string.dialog_show_dialog_no_button,
71                 v -> showDialogWithNoButtonProvided()));
72         mButtons.add(Pair.create(R.string.dialog_show_dialog_checkbox,
73                 v -> showDialogWithCheckbox()));
74         mButtons.add(Pair.create(R.string.dialog_show_dialog_no_title,
75                 v -> showDialogWithoutTitle()));
76         mButtons.add(Pair.create(R.string.dialog_show_toast,
77                 v -> showToast()));
78         mButtons.add(Pair.create(R.string.dialog_show_subtitle,
79                 v -> showDialogWithSubtitle()));
80         mButtons.add(Pair.create(R.string.dialog_show_subtitle_and_icon,
81                 v -> showDialogWithSubtitleAndIcon()));
82         mButtons.add(Pair.create(R.string.dialog_show_long_subtitle_and_icon,
83                 v -> showDialogWithLongSubtitleAndIcon()));
84         mButtons.add(Pair.create(R.string.dialog_show_single_choice,
85                 v -> showDialogWithSingleChoiceItems()));
86         mButtons.add(Pair.create(R.string.dialog_show_permission_dialog,
87                 v -> showPermissionDialog()));
88         mButtons.add(Pair.create(R.string.dialog_show_multi_permission_dialog,
89                 v -> showMultiPermissionDialog()));
90         mButtons.add(Pair.create(R.string.dialog_show_foreground_permission_dialog,
91                 v -> showForegroundPermissionDialog()));
92         mButtons.add(Pair.create(R.string.dialog_show_background_permission_dialog,
93                 v -> showBackgroundPermissionDialog()));
94 
95         CarUiRecyclerView recyclerView = requireViewById(R.id.list);
96         recyclerView.setAdapter(mAdapter);
97     }
98 
showDialog()99     private void showDialog() {
100         new AlertDialogBuilder(this)
101                 .setTitle("Standard Alert Dialog")
102                 .setMessage("With a message to show.")
103                 .setNeutralButton("NEUTRAL", (dialogInterface, which) -> {
104                 })
105                 .setPositiveButton("OK", (dialogInterface, which) -> {
106                 })
107                 .setNegativeButton("CANCEL", (dialogInterface, which) -> {
108                 })
109                 .show();
110     }
111 
showDialogWithIcon()112     private void showDialogWithIcon() {
113         new AlertDialogBuilder(this)
114                 .setTitle("Alert dialog with icon")
115                 .setMessage("The message body of the alert")
116                 .setIcon(R.drawable.ic_tracklist)
117                 .show();
118     }
119 
showDialogWithNoButtonProvided()120     private void showDialogWithNoButtonProvided() {
121         new AlertDialogBuilder(this)
122                 .setTitle("Standard Alert Dialog")
123                 .show();
124     }
125 
showDialogWithCheckbox()126     private void showDialogWithCheckbox() {
127         new AlertDialogBuilder(this)
128                 .setTitle("Custom Dialog Box")
129                 .setMultiChoiceItems(
130                         new CharSequence[]{"I am a checkbox"},
131                         new boolean[]{false},
132                         (dialog, which, isChecked) -> {
133                         })
134                 .setPositiveButton("OK", (dialogInterface, which) -> {
135                 })
136                 .setNegativeButton("CANCEL", (dialogInterface, which) -> {
137                 })
138                 .show();
139     }
140 
showDialogWithTextBox()141     private void showDialogWithTextBox() {
142         new AlertDialogBuilder(this)
143                 .setTitle("Standard Alert Dialog")
144                 .setEditBox("Edit me please", null, null)
145                 .setPositiveButton("OK", (dialogInterface, i) -> {
146                 })
147                 .show();
148     }
149 
showDialogWithOnlyPositiveButton()150     private void showDialogWithOnlyPositiveButton() {
151         new AlertDialogBuilder(this)
152                 .setTitle("Standard Alert Dialog").setMessage("With a message to show.")
153                 .setPositiveButton("OK", (dialogInterface, i) -> {
154                 })
155                 .show();
156     }
157 
showDialogWithoutTitle()158     private void showDialogWithoutTitle() {
159         new AlertDialogBuilder(this)
160                 .setMessage("I dont have a title.")
161                 .setPositiveButton("OK", (dialogInterface, i) -> {
162                 })
163                 .setNegativeButton("CANCEL", (dialogInterface, which) -> {
164                 })
165                 .show();
166     }
167 
showToast()168     private void showToast() {
169         Toast.makeText(this, "Toast message looks like this", Toast.LENGTH_LONG).show();
170     }
171 
showDialogWithSubtitle()172     private void showDialogWithSubtitle() {
173         new AlertDialogBuilder(this)
174                 .setTitle("My Title!")
175                 .setSubtitle("My Subtitle!")
176                 .setMessage("My Message!")
177                 .show();
178     }
179 
showDialogWithSingleChoiceItems()180     private void showDialogWithSingleChoiceItems() {
181         ArrayList<CarUiRadioButtonListItem> data = new ArrayList<>();
182 
183         CarUiRadioButtonListItem item = new CarUiRadioButtonListItem();
184         item.setTitle("First item");
185         data.add(item);
186 
187         item = new CarUiRadioButtonListItem();
188         item.setTitle("Second item");
189         data.add(item);
190 
191         item = new CarUiRadioButtonListItem();
192         item.setTitle("Third item");
193         data.add(item);
194 
195         new AlertDialogBuilder(this)
196                 .setTitle("Select one option.")
197                 .setSubtitle("Ony one option may be selected at a time")
198                 .setSingleChoiceItems(new CarUiRadioButtonListItemAdapter(data), null)
199                 .show();
200     }
201 
showDialogWithSubtitleAndIcon()202     private void showDialogWithSubtitleAndIcon() {
203         new AlertDialogBuilder(this)
204                 .setTitle("My Title!")
205                 .setSubtitle("My Subtitle!")
206                 .setMessage("My Message!")
207                 .setIcon(R.drawable.ic_tracklist)
208                 .show();
209     }
210 
showDialogWithLongSubtitleAndIcon()211     private void showDialogWithLongSubtitleAndIcon() {
212         new AlertDialogBuilder(this)
213                 .setTitle("This is a very long title. It should likely span across "
214                             + "multiple lines or something. It shouldn't get cut off.")
215                 .setSubtitle("This is a very long subtitle. It should likely span across "
216                         + "multiple lines or something. It shouldn't get cut off.")
217                 .setMessage("My Message!")
218                 .setIcon(R.drawable.ic_tracklist)
219                 .show();
220     }
221 
showPermissionDialog()222     private void showPermissionDialog() {
223         if (checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
224             Toast.makeText(this, "Permission already granted. Remove CAMERA permission from "
225                     + "Settings > All apps > PaintBooth", Toast.LENGTH_SHORT).show();
226             return;
227         }
228         requestPermissions(new String[]{Manifest.permission.CAMERA}, 1);
229     }
230 
showMultiPermissionDialog()231     private void showMultiPermissionDialog() {
232         if (checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED
233                 && checkSelfPermission(Manifest.permission.SEND_SMS)
234                     == PackageManager.PERMISSION_GRANTED
235                 && checkSelfPermission(Manifest.permission.READ_CONTACTS)
236                     == PackageManager.PERMISSION_GRANTED) {
237             Toast.makeText(this, "Permissions are already granted. Remove CAMERA, SEND_SMS or "
238                     + "READ_CONTACTS permission from Settings > All apps > PaintBooth",
239                     Toast.LENGTH_SHORT).show();
240             return;
241         }
242         requestPermissions(new String[]{Manifest.permission.CAMERA,
243                 Manifest.permission.READ_CONTACTS, Manifest.permission.SEND_SMS}, 1);
244     }
245 
showForegroundPermissionDialog()246     private void showForegroundPermissionDialog() {
247         requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
248     }
249 
showBackgroundPermissionDialog()250     private void showBackgroundPermissionDialog() {
251         requestPermissions(new String[]{Manifest.permission.ACCESS_BACKGROUND_LOCATION}, 1);
252     }
253 
254     @Override
onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)255     public void onRequestPermissionsResult(int requestCode, String[] permissions,
256             int[] grantResults) {
257         StringBuilder sb = new StringBuilder();
258         sb.append("Permission ");
259         for (int i = 0; i < permissions.length; i++) {
260             sb.append(permissions[i]);
261             sb.append("=");
262             sb.append(grantResults[i] == PackageManager.PERMISSION_GRANTED ? "granted" : "denied");
263             sb.append("\n");
264         }
265         Toast.makeText(this, sb.toString(), Toast.LENGTH_SHORT).show();
266     }
267 
268     private static class ViewHolder extends RecyclerView.ViewHolder {
269 
270         private final Button mButton;
271 
ViewHolder(View itemView)272         ViewHolder(View itemView) {
273             super(itemView);
274             mButton = itemView.requireViewById(R.id.button);
275         }
276 
bind(Integer title, View.OnClickListener listener)277         public void bind(Integer title, View.OnClickListener listener) {
278             mButton.setText(title);
279             mButton.setOnClickListener(listener);
280         }
281     }
282 
283     private final RecyclerView.Adapter<ViewHolder> mAdapter =
284             new RecyclerView.Adapter<ViewHolder>() {
285                 @Override
286                 public int getItemCount() {
287                     return mButtons.size();
288                 }
289 
290                 @Override
291                 public ViewHolder onCreateViewHolder(ViewGroup parent, int position) {
292                     View item =
293                             LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,
294                                     parent, false);
295                     return new ViewHolder(item);
296                 }
297 
298                 @Override
299                 public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
300                     Pair<Integer, View.OnClickListener> pair = mButtons.get(position);
301                     holder.bind(pair.first, pair.second);
302                 }
303             };
304 
305     @Override
onCarUiInsetsChanged(@onNull Insets insets)306     public void onCarUiInsetsChanged(@NonNull Insets insets) {
307         requireViewById(R.id.list)
308                 .setPadding(0, insets.getTop(), 0, insets.getBottom());
309         requireViewById(android.R.id.content)
310                 .setPadding(insets.getLeft(), 0, insets.getRight(), 0);
311     }
312 }
313