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.os.Bundle;
21import android.text.TextUtils;
22import android.widget.CheckBox;
23import android.widget.EditText;
24
25import androidx.annotation.NonNull;
26import androidx.car.app.CarListDialog;
27import androidx.fragment.app.DialogFragment;
28import androidx.fragment.app.FragmentActivity;
29
30/**
31 * A demo activity that will display a {@link CarListDialog} with configurable options for what is
32 * in the contents of that resulting dialog.
33 */
34public class CarListDialogDemo extends FragmentActivity {
35    private static final String DIALOG_TAG = "list_dialog_tag";
36
37    private static final int DEFAULT_NUM_OF_SECTIONS = 0;
38    private static final int DEFAULT_NUM_OF_ITEMS = 4;
39    private static final int DEFAULT_INITIAL_POSITION = 0;
40
41    @Override
42    protected void onCreate(Bundle savedInstanceState) {
43        super.onCreate(savedInstanceState);
44        setContentView(R.layout.list_dialog_activity);
45
46        EditText numOfSectionsEdit = findViewById(R.id.num_of_sections_edit);
47        EditText numOfItemsEdit = findViewById(R.id.num_of_items_edit);
48        EditText initialPositionEdit = findViewById(R.id.initial_position_edit);
49
50        findViewById(R.id.create_dialog).setOnClickListener(v -> {
51            CharSequence numOfSectionsText = numOfSectionsEdit.getText();
52            int numOfSections = TextUtils.isEmpty(numOfSectionsText)
53                    ? DEFAULT_NUM_OF_SECTIONS
54                    : Integer.parseInt(numOfSectionsText.toString());
55
56            CharSequence numOfItemsText = numOfItemsEdit.getText();
57            int numOfItems = TextUtils.isEmpty(numOfItemsText)
58                    ? DEFAULT_NUM_OF_ITEMS
59                    : Integer.parseInt(numOfItemsText.toString());
60
61            CharSequence initialPositionText = initialPositionEdit.getText();
62            int initialPosition = TextUtils.isEmpty(initialPositionText)
63                    ? DEFAULT_INITIAL_POSITION
64                    : Integer.parseInt(initialPositionText.toString());
65
66            ListDialogFragment alertDialog = ListDialogFragment.newInstance(
67                    ((CheckBox) findViewById(R.id.has_title)).isChecked(),
68                    numOfSections,
69                    numOfItems,
70                    initialPosition);
71
72            alertDialog.show(getSupportFragmentManager(), DIALOG_TAG);
73        });
74    }
75
76    /** A {@link DialogFragment} that will inflate a {@link CarListDialog}. */
77    public static class ListDialogFragment extends DialogFragment {
78        private static final String HAS_TITLE_KEY = "has_title_key";
79        private static final String NUM_OF_SECTIONS_KEY = "num_of_sections_key";
80        private static final String NUM_OF_ITEMS_KEY = "num_of_items_key";
81        private static final String INITIAL_POSITION_KEY = "initial_position_key";
82
83        static ListDialogFragment newInstance(boolean hasTitle, int numOfSections,
84                int numOfItems, int initialPosition) {
85            Bundle args = new Bundle();
86            args.putBoolean(HAS_TITLE_KEY, hasTitle);
87            args.putInt(NUM_OF_SECTIONS_KEY, numOfSections);
88            args.putInt(NUM_OF_ITEMS_KEY, numOfItems);
89            args.putInt(INITIAL_POSITION_KEY, initialPosition);
90
91            ListDialogFragment fragment = new ListDialogFragment();
92            fragment.setArguments(args);
93            return fragment;
94        }
95
96        @NonNull
97        @Override
98        public Dialog onCreateDialog(Bundle savedInstanceState) {
99            CarListDialog.Builder builder = new CarListDialog.Builder(getContext())
100                    .setInitialPosition(getArguments().getInt(INITIAL_POSITION_KEY));
101
102            int numOfSections = getArguments().getInt(NUM_OF_SECTIONS_KEY);
103            int numOfItems = getArguments().getInt(NUM_OF_ITEMS_KEY);
104
105            if (numOfSections != 0) {
106                builder.setItems(createSections(numOfSections, numOfItems), null);
107            } else {
108                builder.setItems(createItems(numOfItems), null);
109            }
110
111            if (getArguments().getBoolean(HAS_TITLE_KEY)) {
112                builder.setTitle(getContext().getString(R.string.list_dialog_title));
113            }
114
115            return builder.create();
116        }
117
118        private CarListDialog.DialogSubSection[] createSections(int numOfSections, int numOfItems) {
119            CarListDialog.DialogSubSection[] items =
120                    new CarListDialog.DialogSubSection[numOfSections];
121
122            for (int i = 0; i < numOfSections; i++) {
123                items[i] = new CarListDialog.DialogSubSection(
124                        /* title= */ "Section " + (i + 1),
125                        createItems(numOfItems));
126            }
127
128            return items;
129        }
130
131        private String[] createItems(int numOfItems) {
132            String[] items = new String[numOfItems];
133            for (int i = 0; i < numOfItems; i++) {
134                items[i] = "Item " + (i + 1);
135            }
136            return items;
137        }
138    }
139}
140