Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 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 | * |
Bill Peckham | a74879d | 2018-09-08 10:06:40 -0700 | [diff] [blame^] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 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.android.settingslib.drawer; |
| 18 | |
Doris Ling | b8d2cd4 | 2017-11-27 12:24:09 -0800 | [diff] [blame] | 19 | import static java.lang.String.CASE_INSENSITIVE_ORDER; |
| 20 | |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 21 | import android.os.Parcel; |
| 22 | import android.os.Parcelable; |
| 23 | import android.text.TextUtils; |
| 24 | |
| 25 | import java.util.ArrayList; |
Doris Ling | b8d2cd4 | 2017-11-27 12:24:09 -0800 | [diff] [blame] | 26 | import java.util.Collections; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 27 | import java.util.List; |
| 28 | |
| 29 | public class DashboardCategory implements Parcelable { |
| 30 | |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 31 | /** |
| 32 | * Key used for placing external tiles. |
| 33 | */ |
Bill Peckham | a74879d | 2018-09-08 10:06:40 -0700 | [diff] [blame^] | 34 | public final String key; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * List of the category's children |
| 38 | */ |
Doris Ling | b8d2cd4 | 2017-11-27 12:24:09 -0800 | [diff] [blame] | 39 | private List<Tile> mTiles = new ArrayList<>(); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 40 | |
Bill Peckham | a74879d | 2018-09-08 10:06:40 -0700 | [diff] [blame^] | 41 | public DashboardCategory(String key) { |
| 42 | this.key = key; |
Doris Ling | b8d2cd4 | 2017-11-27 12:24:09 -0800 | [diff] [blame] | 43 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 44 | |
Bill Peckham | a74879d | 2018-09-08 10:06:40 -0700 | [diff] [blame^] | 45 | 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 Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 54 | } |
| 55 | |
Doris Ling | b8d2cd4 | 2017-11-27 12:24:09 -0800 | [diff] [blame] | 56 | /** |
| 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 Ling | 6ba8770 | 2017-12-04 11:12:26 -0800 | [diff] [blame] | 62 | 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 Ling | b8d2cd4 | 2017-11-27 12:24:09 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Doris Ling | 6ba8770 | 2017-12-04 11:12:26 -0800 | [diff] [blame] | 70 | public synchronized void addTile(Tile tile) { |
Doris Ling | b8d2cd4 | 2017-11-27 12:24:09 -0800 | [diff] [blame] | 71 | mTiles.add(tile); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 72 | } |
| 73 | |
Doris Ling | 6ba8770 | 2017-12-04 11:12:26 -0800 | [diff] [blame] | 74 | public synchronized void removeTile(int n) { |
Doris Ling | b8d2cd4 | 2017-11-27 12:24:09 -0800 | [diff] [blame] | 75 | mTiles.remove(n); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | public int getTilesCount() { |
Doris Ling | b8d2cd4 | 2017-11-27 12:24:09 -0800 | [diff] [blame] | 79 | return mTiles.size(); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 80 | } |
| 81 | |
Jason Monk | f509d7e | 2016-01-07 16:22:53 -0500 | [diff] [blame] | 82 | public Tile getTile(int n) { |
Doris Ling | b8d2cd4 | 2017-11-27 12:24:09 -0800 | [diff] [blame] | 83 | return mTiles.get(n); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 84 | } |
| 85 | |
Doris Ling | b8d2cd4 | 2017-11-27 12:24:09 -0800 | [diff] [blame] | 86 | /** |
| 87 | * Sort priority value for tiles in this category. |
| 88 | */ |
| 89 | public void sortTiles() { |
Bill Peckham | a74879d | 2018-09-08 10:06:40 -0700 | [diff] [blame^] | 90 | Collections.sort(mTiles, Tile.TILE_COMPARATOR); |
Doris Ling | b8d2cd4 | 2017-11-27 12:24:09 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Sort priority value and package name for tiles in this category. |
| 95 | */ |
Doris Ling | 6ba8770 | 2017-12-04 11:12:26 -0800 | [diff] [blame] | 96 | public synchronized void sortTiles(String skipPackageName) { |
Bill Peckham | a74879d | 2018-09-08 10:06:40 -0700 | [diff] [blame^] | 97 | // Sort mTiles based on [order, package within order] |
Doris Ling | b8d2cd4 | 2017-11-27 12:24:09 -0800 | [diff] [blame] | 98 | Collections.sort(mTiles, (tile1, tile2) -> { |
Bill Peckham | a74879d | 2018-09-08 10:06:40 -0700 | [diff] [blame^] | 99 | // First sort by order |
| 100 | final int orderCompare = tile2.getOrder() - tile1.getOrder(); |
| 101 | if (orderCompare != 0) { |
| 102 | return orderCompare; |
Doris Ling | b8d2cd4 | 2017-11-27 12:24:09 -0800 | [diff] [blame] | 103 | } |
Bill Peckham | a74879d | 2018-09-08 10:06:40 -0700 | [diff] [blame^] | 104 | |
Doris Ling | b8d2cd4 | 2017-11-27 12:24:09 -0800 | [diff] [blame] | 105 | // Then sort by package name, skip package take precedence |
Bill Peckham | a74879d | 2018-09-08 10:06:40 -0700 | [diff] [blame^] | 106 | final String package1 = tile1.getPackageName(); |
| 107 | final String package2 = tile2.getPackageName(); |
| 108 | final int packageCompare = CASE_INSENSITIVE_ORDER.compare(package1, package2); |
Doris Ling | b8d2cd4 | 2017-11-27 12:24:09 -0800 | [diff] [blame] | 109 | 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 Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 121 | @Override |
| 122 | public int describeContents() { |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public void writeToParcel(Parcel dest, int flags) { |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 128 | dest.writeString(key); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 129 | |
Doris Ling | b8d2cd4 | 2017-11-27 12:24:09 -0800 | [diff] [blame] | 130 | final int count = mTiles.size(); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 131 | dest.writeInt(count); |
| 132 | |
| 133 | for (int n = 0; n < count; n++) { |
Doris Ling | b8d2cd4 | 2017-11-27 12:24:09 -0800 | [diff] [blame] | 134 | Tile tile = mTiles.get(n); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 135 | tile.writeToParcel(dest, flags); |
| 136 | } |
| 137 | } |
| 138 | |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 139 | 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 Ling | b8d2cd4 | 2017-11-27 12:24:09 -0800 | [diff] [blame] | 148 | |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 149 | } |