1 /*
2  * Copyright (C) 2015 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 package android.view;
17 
18 import static com.android.internal.util.Preconditions.checkNotNull;
19 
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.annotation.TestApi;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.List;
29 
30 /**
31  * A group of {@link KeyboardShortcutInfo}.
32  */
33 public final class KeyboardShortcutGroup implements Parcelable {
34     private final CharSequence mLabel;
35     private final List<KeyboardShortcutInfo> mItems;
36     // The system group looks different UI wise.
37     private boolean mSystemGroup;
38 
39     /**
40      * @param label The title to be used for this group, or null if there is none.
41      * @param items The set of items to be included.
42      */
KeyboardShortcutGroup(@ullable CharSequence label, @NonNull List<KeyboardShortcutInfo> items)43     public KeyboardShortcutGroup(@Nullable CharSequence label,
44             @NonNull List<KeyboardShortcutInfo> items) {
45         mLabel = label;
46         mItems = new ArrayList<>(checkNotNull(items));
47     }
48 
49     /**
50      * @param label The title to be used for this group, or null if there is none.
51      */
KeyboardShortcutGroup(@ullable CharSequence label)52     public KeyboardShortcutGroup(@Nullable CharSequence label) {
53         this(label, Collections.<KeyboardShortcutInfo>emptyList());
54     }
55 
56     /**
57      * @param label The title to be used for this group, or null if there is none.
58      * @param items The set of items to be included.
59      * @param isSystemGroup Set this to {@code true} if this is s system group.
60      * @hide
61      */
62     @TestApi
KeyboardShortcutGroup(@ullable CharSequence label, @NonNull List<KeyboardShortcutInfo> items, boolean isSystemGroup)63     public KeyboardShortcutGroup(@Nullable CharSequence label,
64             @NonNull List<KeyboardShortcutInfo> items, boolean isSystemGroup) {
65         mLabel = label;
66         mItems = new ArrayList<>(checkNotNull(items));
67         mSystemGroup = isSystemGroup;
68     }
69 
70     /**
71      * @param label The title to be used for this group, or null if there is none.
72      * @param isSystemGroup Set this to {@code true} if this is s system group.
73      * @hide
74      */
75     @TestApi
KeyboardShortcutGroup(@ullable CharSequence label, boolean isSystemGroup)76     public KeyboardShortcutGroup(@Nullable CharSequence label, boolean isSystemGroup) {
77         this(label, Collections.<KeyboardShortcutInfo>emptyList(), isSystemGroup);
78     }
79 
KeyboardShortcutGroup(Parcel source)80     private KeyboardShortcutGroup(Parcel source) {
81         mItems = new ArrayList<>();
82         mLabel = source.readCharSequence();
83         source.readTypedList(mItems, KeyboardShortcutInfo.CREATOR);
84         mSystemGroup = source.readInt() == 1;
85     }
86 
87     /**
88      * Returns the label to be used to describe this group.
89      */
getLabel()90     public CharSequence getLabel() {
91         return mLabel;
92     }
93 
94     /**
95      * Returns the list of items included in this group.
96      */
getItems()97     public List<KeyboardShortcutInfo> getItems() {
98         return mItems;
99     }
100 
101     /** @hide **/
102     @TestApi
isSystemGroup()103     public boolean isSystemGroup() {
104         return mSystemGroup;
105     }
106 
107     /**
108      * Adds an item to the existing list.
109      *
110      * @param item The item to be added.
111      */
addItem(KeyboardShortcutInfo item)112     public void addItem(KeyboardShortcutInfo item) {
113         mItems.add(item);
114     }
115 
116     @Override
describeContents()117     public int describeContents() {
118         return 0;
119     }
120 
121     @Override
writeToParcel(Parcel dest, int flags)122     public void writeToParcel(Parcel dest, int flags) {
123         dest.writeCharSequence(mLabel);
124         dest.writeTypedList(mItems);
125         dest.writeInt(mSystemGroup ? 1 : 0);
126     }
127 
128     public static final @android.annotation.NonNull Creator<KeyboardShortcutGroup> CREATOR =
129             new Creator<KeyboardShortcutGroup>() {
130                 public KeyboardShortcutGroup createFromParcel(Parcel source) {
131                     return new KeyboardShortcutGroup(source);
132                 }
133                 public KeyboardShortcutGroup[] newArray(int size) {
134                     return new KeyboardShortcutGroup[size];
135                 }
136             };
137 }
138