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.customization.model.grid;
17 
18 import android.net.Uri;
19 import android.os.Parcel;
20 import android.os.Parcelable;
21 import android.text.TextUtils;
22 import android.view.View;
23 
24 import androidx.annotation.Nullable;
25 
26 import com.android.customization.model.CustomizationManager;
27 import com.android.customization.model.CustomizationOption;
28 import com.android.customization.widget.GridTileDrawable;
29 import com.android.themepicker.R;
30 
31 /**
32  * Represents a grid layout option available in the current launcher.
33  */
34 public class GridOption implements CustomizationOption<GridOption>, Parcelable {
35     public static final Creator<GridOption> CREATOR = new Creator<GridOption>() {
36         @Override
37         public GridOption createFromParcel(Parcel in) {
38             return new GridOption(in);
39         }
40 
41         @Override
42         public GridOption[] newArray(int size) {
43             return new GridOption[size];
44         }
45     };
46 
47     private final String mIconShapePath;
48     private final GridTileDrawable mTileDrawable;
49     public final String title;
50     public final String name;
51     public final int rows;
52     public final int cols;
53     public final Uri previewImageUri;
54     public final int previewPagesCount;
55     private boolean mIsCurrent;
56 
GridOption(String title, String name, boolean isCurrent, int rows, int cols, Uri previewImageUri, int previewPagesCount, String iconShapePath)57     public GridOption(String title, String name, boolean isCurrent, int rows, int cols,
58             Uri previewImageUri, int previewPagesCount, String iconShapePath) {
59         this.title = title;
60         mIsCurrent = isCurrent;
61         mIconShapePath = iconShapePath;
62         mTileDrawable = new GridTileDrawable(rows, cols, mIconShapePath);
63         this.name = name;
64         this.rows = rows;
65         this.cols = cols;
66         this.previewImageUri = previewImageUri;
67         this.previewPagesCount = previewPagesCount;
68     }
69 
setIsCurrent(boolean isCurrent)70     public void setIsCurrent(boolean isCurrent) {
71         mIsCurrent = isCurrent;
72     }
73 
GridOption(Parcel in)74     protected GridOption(Parcel in) {
75         title = in.readString();
76         mIsCurrent = in.readByte() != 0;
77         mIconShapePath = in.readString();
78         name = in.readString();
79         rows = in.readInt();
80         cols = in.readInt();
81         previewImageUri = in.readParcelable(Uri.class.getClassLoader());
82         previewPagesCount = in.readInt();
83         mTileDrawable = new GridTileDrawable(rows, cols, mIconShapePath);
84     }
85 
86     @Override
getTitle()87     public String getTitle() {
88         return title;
89     }
90 
91     @Override
bindThumbnailTile(View view)92     public void bindThumbnailTile(View view) {
93         // Do nothing. This function will no longer be used in the Revamped UI
94     }
95 
96     @Override
isActive(CustomizationManager<GridOption> manager)97     public boolean isActive(CustomizationManager<GridOption> manager) {
98         return mIsCurrent;
99     }
100 
101     @Override
equals(@ullable Object obj)102     public boolean equals(@Nullable Object obj) {
103         if (this == obj) {
104             return true;
105         }
106 
107         if (obj instanceof GridOption) {
108             GridOption other = (GridOption) obj;
109             return TextUtils.equals(this.name, other.name)
110                     && this.cols == other.cols
111                     && this.rows == other.rows;
112         }
113         return false;
114     }
115 
116     @Override
getLayoutResId()117     public int getLayoutResId() {
118         return R.layout.grid_option;
119     }
120 
121     @Override
describeContents()122     public int describeContents() {
123         return 0;
124     }
125 
126     @Override
writeToParcel(Parcel parcel, int i)127     public void writeToParcel(Parcel parcel, int i) {
128         parcel.writeString(title);
129         parcel.writeByte((byte) (mIsCurrent ? 1 : 0));
130         parcel.writeString(mIconShapePath);
131         parcel.writeString(name);
132         parcel.writeInt(rows);
133         parcel.writeInt(cols);
134         parcel.writeParcelable(previewImageUri, i);
135         parcel.writeInt(previewPagesCount);
136     }
137 
138     @Override
toString()139     public String toString() {
140         return String.format(
141                 "GridOption{mTitle='%s', mIsCurrent=%s, mTileDrawable=%s, name='%s', rows=%d, "
142                         + "cols=%d, previewImageUri=%s, previewPagesCount=%d}\n",
143                 title, mIsCurrent, mTileDrawable, name, rows, cols, previewImageUri,
144                 previewPagesCount);
145     }
146 }
147