blob: a3dda658bec721267296218235dda66b83ea7a8f [file] [log] [blame]
Jason Monk744b6362015-11-03 18:24:29 -05001/**
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 *
Bill Peckhama74879d2018-09-08 10:06:40 -07008 * http://www.apache.org/licenses/LICENSE-2.0
Jason Monk744b6362015-11-03 18:24:29 -05009 *
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
17package com.android.settingslib.drawer;
18
Doris Lingb8d2cd42017-11-27 12:24:09 -080019import static java.lang.String.CASE_INSENSITIVE_ORDER;
20
Jason Monk744b6362015-11-03 18:24:29 -050021import android.os.Parcel;
22import android.os.Parcelable;
23import android.text.TextUtils;
24
25import java.util.ArrayList;
Doris Lingb8d2cd42017-11-27 12:24:09 -080026import java.util.Collections;
Jason Monk744b6362015-11-03 18:24:29 -050027import java.util.List;
28
29public class DashboardCategory implements Parcelable {
30
Jason Monk744b6362015-11-03 18:24:29 -050031 /**
32 * Key used for placing external tiles.
33 */
Bill Peckhama74879d2018-09-08 10:06:40 -070034 public final String key;
Jason Monk744b6362015-11-03 18:24:29 -050035
36 /**
37 * List of the category's children
38 */
Doris Lingb8d2cd42017-11-27 12:24:09 -080039 private List<Tile> mTiles = new ArrayList<>();
Jason Monk744b6362015-11-03 18:24:29 -050040
Bill Peckhama74879d2018-09-08 10:06:40 -070041 public DashboardCategory(String key) {
42 this.key = key;
Doris Lingb8d2cd42017-11-27 12:24:09 -080043 }
Jason Monk744b6362015-11-03 18:24:29 -050044
Bill Peckhama74879d2018-09-08 10:06:40 -070045 DashboardCategory(Parcel in) {
46 key = in.readString();
47
48 final int count = in.readInt();
49
50 for (int n = 0; n < count; n++) {
51 Tile tile = Tile.CREATOR.createFromParcel(in);
52 mTiles.add(tile);
53 }
Jason Monk744b6362015-11-03 18:24:29 -050054 }
55
Doris Lingb8d2cd42017-11-27 12:24:09 -080056 /**
57 * Get a copy of the list of the category's children.
58 *
59 * Note: the returned list serves as a read-only list. If tiles needs to be added or removed
60 * from the actual tiles list, it should be done through {@link #addTile}, {@link #removeTile}.
61 */
Doris Ling6ba87702017-12-04 11:12:26 -080062 public synchronized List<Tile> getTiles() {
63 final List<Tile> result = new ArrayList<>(mTiles.size());
64 for (Tile tile : mTiles) {
65 result.add(tile);
66 }
67 return result;
Doris Lingb8d2cd42017-11-27 12:24:09 -080068 }
69
Doris Ling6ba87702017-12-04 11:12:26 -080070 public synchronized void addTile(Tile tile) {
Doris Lingb8d2cd42017-11-27 12:24:09 -080071 mTiles.add(tile);
Jason Monk744b6362015-11-03 18:24:29 -050072 }
73
Doris Ling6ba87702017-12-04 11:12:26 -080074 public synchronized void removeTile(int n) {
Doris Lingb8d2cd42017-11-27 12:24:09 -080075 mTiles.remove(n);
Jason Monk744b6362015-11-03 18:24:29 -050076 }
77
78 public int getTilesCount() {
Doris Lingb8d2cd42017-11-27 12:24:09 -080079 return mTiles.size();
Jason Monk744b6362015-11-03 18:24:29 -050080 }
81
Jason Monkf509d7e2016-01-07 16:22:53 -050082 public Tile getTile(int n) {
Doris Lingb8d2cd42017-11-27 12:24:09 -080083 return mTiles.get(n);
Jason Monk744b6362015-11-03 18:24:29 -050084 }
85
Doris Lingb8d2cd42017-11-27 12:24:09 -080086 /**
87 * Sort priority value for tiles in this category.
88 */
89 public void sortTiles() {
Bill Peckhama74879d2018-09-08 10:06:40 -070090 Collections.sort(mTiles, Tile.TILE_COMPARATOR);
Doris Lingb8d2cd42017-11-27 12:24:09 -080091 }
92
93 /**
94 * Sort priority value and package name for tiles in this category.
95 */
Doris Ling6ba87702017-12-04 11:12:26 -080096 public synchronized void sortTiles(String skipPackageName) {
Bill Peckhama74879d2018-09-08 10:06:40 -070097 // Sort mTiles based on [order, package within order]
Doris Lingb8d2cd42017-11-27 12:24:09 -080098 Collections.sort(mTiles, (tile1, tile2) -> {
Bill Peckhama74879d2018-09-08 10:06:40 -070099 // First sort by order
100 final int orderCompare = tile2.getOrder() - tile1.getOrder();
101 if (orderCompare != 0) {
102 return orderCompare;
Doris Lingb8d2cd42017-11-27 12:24:09 -0800103 }
Bill Peckhama74879d2018-09-08 10:06:40 -0700104
Doris Lingb8d2cd42017-11-27 12:24:09 -0800105 // Then sort by package name, skip package take precedence
Bill Peckhama74879d2018-09-08 10:06:40 -0700106 final String package1 = tile1.getPackageName();
107 final String package2 = tile2.getPackageName();
108 final int packageCompare = CASE_INSENSITIVE_ORDER.compare(package1, package2);
Doris Lingb8d2cd42017-11-27 12:24:09 -0800109 if (packageCompare != 0) {
110 if (TextUtils.equals(package1, skipPackageName)) {
111 return -1;
112 }
113 if (TextUtils.equals(package2, skipPackageName)) {
114 return 1;
115 }
116 }
117 return packageCompare;
118 });
119 }
120
Jason Monk744b6362015-11-03 18:24:29 -0500121 @Override
122 public int describeContents() {
123 return 0;
124 }
125
126 @Override
127 public void writeToParcel(Parcel dest, int flags) {
Jason Monk744b6362015-11-03 18:24:29 -0500128 dest.writeString(key);
Jason Monk744b6362015-11-03 18:24:29 -0500129
Doris Lingb8d2cd42017-11-27 12:24:09 -0800130 final int count = mTiles.size();
Jason Monk744b6362015-11-03 18:24:29 -0500131 dest.writeInt(count);
132
133 for (int n = 0; n < count; n++) {
Doris Lingb8d2cd42017-11-27 12:24:09 -0800134 Tile tile = mTiles.get(n);
Jason Monk744b6362015-11-03 18:24:29 -0500135 tile.writeToParcel(dest, flags);
136 }
137 }
138
Jason Monk744b6362015-11-03 18:24:29 -0500139 public static final Creator<DashboardCategory> CREATOR = new Creator<DashboardCategory>() {
140 public DashboardCategory createFromParcel(Parcel source) {
141 return new DashboardCategory(source);
142 }
143
144 public DashboardCategory[] newArray(int size) {
145 return new DashboardCategory[size];
146 }
147 };
Doris Lingb8d2cd42017-11-27 12:24:09 -0800148
Jason Monk744b6362015-11-03 18:24:29 -0500149}