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