1 /*
2  * Copyright 2024 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.example.androidx.javascriptengine;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.util.AttributeSet;
22 import android.widget.ArrayAdapter;
23 import android.widget.ListView;
24 
25 import org.jspecify.annotations.NonNull;
26 import org.jspecify.annotations.Nullable;
27 
28 /**
29  * A {@link ListView} which serves as a menu of elements firing {@link Intent}s to other Activities.
30  */
31 public class MenuListView extends ListView {
MenuListView(@onNull Context context)32     public MenuListView(@NonNull Context context) {
33         super(context);
34     }
MenuListView(@onNull Context context, @Nullable AttributeSet attrs)35     public MenuListView(@NonNull Context context, @Nullable AttributeSet attrs) {
36         super(context, attrs);
37     }
MenuListView(@onNull Context context, @Nullable AttributeSet attrs, int defStyleAttr)38     public MenuListView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
39         super(context, attrs, defStyleAttr);
40     }
41 
MenuListView(@onNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes)42     public MenuListView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr,
43             int defStyleRes) {
44         super(context, attrs, defStyleAttr, defStyleRes);
45     }
46 
47     /**
48      * An item in the {@link MenuListView}.
49      */
50     public static class MenuItem {
51         private String mName;
52         private Intent mIntent;
53 
MenuItem(@onNull String name, @NonNull Intent intentToLaunch)54         public MenuItem(@NonNull String name, @NonNull Intent intentToLaunch) {
55             mName = name;
56             mIntent = intentToLaunch;
57         }
58 
59         @Override
toString()60         public @NonNull String toString() {
61             return mName;
62         }
63 
64         /**
65          * Starts the {@link Intent} for this MenuItem. This accepts the {@link Context} for the
66          * current Activity on the stack, which will be used as the {@code this} argument to call
67          * {@link Context#startActivity(Intent)}.
68          *
69          * @param activityContext the Activity Context of the current Activity on the stack.
70          */
start(@onNull Context activityContext)71         public void start(@NonNull Context activityContext) {
72             activityContext.startActivity(mIntent);
73         }
74     }
75 
76     /**
77      * Sets the menu items for this {@link MenuListView}.
78      */
setItems(MenuItem @onNull [] items)79     public void setItems(MenuItem @NonNull [] items) {
80         final Context context = getContext();
81         ArrayAdapter<MenuItem> featureArrayAdapter =
82                 new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, items);
83         setAdapter(featureArrayAdapter);
84         setOnItemClickListener((parent, view, position, id) ->
85                 featureArrayAdapter.getItem(position).start(context));
86     }
87 }
88