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