• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 com.android.settingslib.drawer;
17 
18 import static java.lang.String.CASE_INSENSITIVE_ORDER;
19 
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 import android.text.TextUtils;
23 
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27 
28 /**
29  * The category for handle {@link Tile}
30  */
31 public class DashboardCategory implements Parcelable {
32 
33     /**
34      * Key used for placing external tiles.
35      */
36     public final String key;
37 
38     /**
39      * List of the category's children
40      */
41     private List<Tile> mTiles = new ArrayList<>();
42 
DashboardCategory(String key)43     public DashboardCategory(String key) {
44         this.key = key;
45     }
46 
DashboardCategory(Parcel in)47     DashboardCategory(Parcel in) {
48         key = in.readString();
49 
50         final int count = in.readInt();
51 
52         for (int n = 0; n < count; n++) {
53             Tile tile = Tile.CREATOR.createFromParcel(in);
54             mTiles.add(tile);
55         }
56     }
57 
58     /**
59      * Get a copy of the list of the category's children.
60      *
61      * Note: the returned list serves as a read-only list. If tiles needs to be added or removed
62      * from the actual tiles list, it should be done through {@link #addTile}, {@link #removeTile}.
63      */
getTiles()64     public synchronized List<Tile> getTiles() {
65         final List<Tile> result = new ArrayList<>(mTiles.size());
66         for (Tile tile : mTiles) {
67             result.add(tile);
68         }
69         return result;
70     }
71 
72     /**
73      * Add tile
74      */
addTile(Tile tile)75     public synchronized void addTile(Tile tile) {
76         mTiles.add(tile);
77     }
78 
79     /**
80      * Remove tile
81      */
removeTile(int n)82     public synchronized void removeTile(int n) {
83         mTiles.remove(n);
84     }
85 
86     /**
87      * Get size of tile
88      */
getTilesCount()89     public int getTilesCount() {
90         return mTiles.size();
91     }
92 
93     /**
94      * Get tile
95      */
getTile(int n)96     public Tile getTile(int n) {
97         return mTiles.get(n);
98     }
99 
100     /**
101      * Sort priority value for tiles in this category.
102      */
sortTiles()103     public void sortTiles() {
104         Collections.sort(mTiles, Tile.TILE_COMPARATOR);
105     }
106 
107     /**
108      * Sort priority value and package name for tiles in this category.
109      */
sortTiles(String skipPackageName)110     public synchronized void sortTiles(String skipPackageName) {
111         // Sort mTiles based on [order, package within order]
112         Collections.sort(mTiles, (tile1, tile2) -> {
113             // First sort by order
114             final int orderCompare = tile2.getOrder() - tile1.getOrder();
115             if (orderCompare != 0) {
116                 return orderCompare;
117             }
118 
119             // Then sort by package name, skip package take precedence
120             final String package1 = tile1.getPackageName();
121             final String package2 = tile2.getPackageName();
122             final int packageCompare = CASE_INSENSITIVE_ORDER.compare(package1, package2);
123             if (packageCompare != 0) {
124                 if (TextUtils.equals(package1, skipPackageName)) {
125                     return -1;
126                 }
127                 if (TextUtils.equals(package2, skipPackageName)) {
128                     return 1;
129                 }
130             }
131             return packageCompare;
132         });
133     }
134 
135     @Override
describeContents()136     public int describeContents() {
137         return 0;
138     }
139 
140     @Override
writeToParcel(Parcel dest, int flags)141     public void writeToParcel(Parcel dest, int flags) {
142         dest.writeString(key);
143 
144         final int count = mTiles.size();
145         dest.writeInt(count);
146 
147         for (int n = 0; n < count; n++) {
148             Tile tile = mTiles.get(n);
149             tile.writeToParcel(dest, flags);
150         }
151     }
152 
153     public static final Creator<DashboardCategory> CREATOR = new Creator<DashboardCategory>() {
154         public DashboardCategory createFromParcel(Parcel source) {
155             return new DashboardCategory(source);
156         }
157 
158         public DashboardCategory[] newArray(int size) {
159             return new DashboardCategory[size];
160         }
161     };
162 
163 }
164