1/*
2 * Copyright (C) 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 */
16package com.android.internal.globalactions;
17
18import android.app.Dialog;
19import android.content.Context;
20import android.content.DialogInterface;
21import android.os.Bundle;
22import android.util.TypedValue;
23import android.view.accessibility.AccessibilityEvent;
24import android.view.KeyEvent;
25import android.widget.ListView;
26import com.android.internal.app.AlertController;
27
28/** A dialog that lists the given Action items to be user selectable. */
29public final class ActionsDialog extends Dialog implements DialogInterface {
30    private final Context mContext;
31    private final AlertController mAlert;
32    private final ActionsAdapter mAdapter;
33
34    public ActionsDialog(Context context, AlertController.AlertParams params) {
35        super(context, getDialogTheme(context));
36        mContext = getContext();
37        mAlert = AlertController.create(mContext, this, getWindow());
38        mAdapter = (ActionsAdapter) params.mAdapter;
39        params.apply(mAlert);
40    }
41
42    private static int getDialogTheme(Context context) {
43        TypedValue outValue = new TypedValue();
44        context.getTheme().resolveAttribute(com.android.internal.R.attr.alertDialogTheme,
45                outValue, true);
46        return outValue.resourceId;
47    }
48
49    @Override
50    protected void onStart() {
51        super.setCanceledOnTouchOutside(true);
52        super.onStart();
53    }
54
55    public ListView getListView() {
56        return mAlert.getListView();
57    }
58
59    @Override
60    protected void onCreate(Bundle savedInstanceState) {
61        super.onCreate(savedInstanceState);
62        mAlert.installContent();
63    }
64
65    @Override
66    public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
67        if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
68            for (int i = 0; i < mAdapter.getCount(); ++i) {
69                CharSequence label =
70                        mAdapter.getItem(i).getLabelForAccessibility(getContext());
71                if (label != null) {
72                    event.getText().add(label);
73                }
74            }
75        }
76        return super.dispatchPopulateAccessibilityEvent(event);
77    }
78
79    @Override
80    public boolean onKeyDown(int keyCode, KeyEvent event) {
81        if (mAlert.onKeyDown(keyCode, event)) {
82            return true;
83        }
84        return super.onKeyDown(keyCode, event);
85    }
86
87    @Override
88    public boolean onKeyUp(int keyCode, KeyEvent event) {
89        if (mAlert.onKeyUp(keyCode, event)) {
90            return true;
91        }
92        return super.onKeyUp(keyCode, event);
93    }
94}
95