1/*
2 * Copyright (C) 2016 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.android.tv.ui;
18
19import android.app.AlertDialog;
20import android.content.Context;
21import android.content.DialogInterface;
22import android.content.res.Resources;
23
24import com.android.tv.common.SoftPreconditions;
25
26public final class DialogUtils {
27
28    /**
29     * Shows a list in a Dialog.
30     *
31     * @param itemResIds String resource id for each item
32     * @param runnables Runnable for each item
33     */
34    public static void showListDialog(Context context, int[] itemResIds,
35            final Runnable[] runnables) {
36        int size = itemResIds.length;
37        SoftPreconditions.checkState(size == runnables.length);
38        DialogInterface.OnClickListener onClickListener
39                = new DialogInterface.OnClickListener() {
40            @Override
41            public void onClick(final DialogInterface dialog, int which) {
42                Runnable runnable = runnables[which];
43                if (runnable != null) {
44                    runnable.run();
45                }
46                dialog.dismiss();
47            }
48        };
49        CharSequence[] items = new CharSequence[itemResIds.length];
50        Resources res = context.getResources();
51        for (int i = 0; i < size; ++i) {
52            items[i] = res.getString(itemResIds[i]);
53        }
54        new AlertDialog.Builder(context)
55                .setItems(items, onClickListener)
56                .create()
57                .show();
58    }
59
60    private DialogUtils() { }
61}
62