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 34 /** 35 * Represents a grid layout option available in the current launcher. 36 */ 37 public class GridOption implements CustomizationOption<GridOption>, Parcelable { 38 public static final Creator<GridOption> CREATOR = new Creator<GridOption>() { 39 @Override 40 public GridOption createFromParcel(Parcel in) { 41 return new GridOption(in); 42 } 43 44 @Override 45 public GridOption[] newArray(int size) { 46 return new GridOption[size]; 47 } 48 }; 49 50 private final String mTitle; 51 private final boolean mIsCurrent; 52 private final String mIconShapePath; 53 private final GridTileDrawable mTileDrawable; 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 GridOption(String title, String name, boolean isCurrent, int rows, int cols, Uri previewImageUri, int previewPagesCount, String iconShapePath)60 public GridOption(String title, String name, boolean isCurrent, int rows, int cols, 61 Uri previewImageUri, int previewPagesCount, String iconShapePath) { 62 mTitle = title; 63 mIsCurrent = isCurrent; 64 mIconShapePath = iconShapePath; 65 mTileDrawable = new GridTileDrawable(rows, cols, mIconShapePath); 66 this.name = name; 67 this.rows = rows; 68 this.cols = cols; 69 this.previewImageUri = previewImageUri; 70 this.previewPagesCount = previewPagesCount; 71 } 72 GridOption(Parcel in)73 protected GridOption(Parcel in) { 74 mTitle = in.readString(); 75 mIsCurrent = in.readByte() != 0; 76 mIconShapePath = in.readString(); 77 name = in.readString(); 78 rows = in.readInt(); 79 cols = in.readInt(); 80 previewImageUri = in.readParcelable(Uri.class.getClassLoader()); 81 previewPagesCount = in.readInt(); 82 mTileDrawable = new GridTileDrawable(rows, cols, mIconShapePath); 83 } 84 85 @Override getTitle()86 public String getTitle() { 87 return mTitle; 88 } 89 90 @Override bindThumbnailTile(View view)91 public void bindThumbnailTile(View view) { 92 Context context = view.getContext(); 93 94 mTileDrawable.setColorFilter(context.getResources().getColor( 95 R.color.material_grey500, null), Mode.ADD); 96 ((ImageView) view.findViewById(R.id.grid_option_thumbnail)) 97 .setImageDrawable(mTileDrawable); 98 } 99 100 @Override isActive(CustomizationManager<GridOption> manager)101 public boolean isActive(CustomizationManager<GridOption> manager) { 102 return mIsCurrent; 103 } 104 105 @Override equals(@ullable Object obj)106 public boolean equals(@Nullable Object obj) { 107 if (this == obj) { 108 return true; 109 } 110 111 if (obj instanceof GridOption) { 112 GridOption other = (GridOption) obj; 113 return TextUtils.equals(this.name, other.name) 114 && this.cols == other.cols 115 && this.rows == other.rows; 116 } 117 return false; 118 } 119 120 @Override getLayoutResId()121 public int getLayoutResId() { 122 return R.layout.grid_option; 123 } 124 125 @Override describeContents()126 public int describeContents() { 127 return 0; 128 } 129 130 @Override writeToParcel(Parcel parcel, int i)131 public void writeToParcel(Parcel parcel, int i) { 132 parcel.writeString(mTitle); 133 parcel.writeByte((byte) (mIsCurrent ? 1 : 0)); 134 parcel.writeString(mIconShapePath); 135 parcel.writeString(name); 136 parcel.writeInt(rows); 137 parcel.writeInt(cols); 138 parcel.writeParcelable(previewImageUri, i); 139 parcel.writeInt(previewPagesCount); 140 } 141 } 142