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.theme.custom; 17 18 import android.content.Context; 19 import android.view.View; 20 21 import androidx.annotation.NonNull; 22 import androidx.annotation.Nullable; 23 24 import com.android.customization.model.CustomizationManager; 25 import com.android.customization.model.theme.ThemeBundle; 26 import com.android.wallpaper.R; 27 28 import java.util.Map; 29 import java.util.UUID; 30 31 public class CustomTheme extends ThemeBundle { 32 newId()33 public static String newId() { 34 return UUID.randomUUID().toString(); 35 } 36 37 /** 38 * Used to uniquely identify a custom theme since names can change. 39 */ 40 private final String mId; 41 CustomTheme(@onNull String id, String title, Map<String, String> overlayPackages, @Nullable PreviewInfo previewInfo)42 private CustomTheme(@NonNull String id, String title, Map<String, String> overlayPackages, 43 @Nullable PreviewInfo previewInfo) { 44 super(title, overlayPackages, false, previewInfo); 45 mId = id; 46 } 47 getId()48 public String getId() { 49 return mId; 50 } 51 52 @Override equals(Object obj)53 public boolean equals(Object obj) { 54 if (!(obj instanceof CustomTheme)) { 55 return false; 56 } 57 CustomTheme other = (CustomTheme) obj; 58 return mId.equals(other.mId); 59 } 60 61 @Override hashCode()62 public int hashCode() { 63 return mId.hashCode(); 64 } 65 66 @Override bindThumbnailTile(View view)67 public void bindThumbnailTile(View view) { 68 if (isDefined()) { 69 super.bindThumbnailTile(view); 70 } 71 } 72 73 @Override getLayoutResId()74 public int getLayoutResId() { 75 return isDefined() ? R.layout.theme_option : R.layout.custom_theme_option; 76 } 77 78 @Override isActive(CustomizationManager<ThemeBundle> manager)79 public boolean isActive(CustomizationManager<ThemeBundle> manager) { 80 return isDefined() && super.isActive(manager); 81 } 82 83 @Override isEquivalent(ThemeBundle other)84 public boolean isEquivalent(ThemeBundle other) { 85 return isDefined() && super.isEquivalent(other); 86 } 87 isDefined()88 public boolean isDefined() { 89 return getPreviewInfo() != null; 90 } 91 92 public static class Builder extends ThemeBundle.Builder { 93 private String mId; 94 95 @Override build(Context context)96 public CustomTheme build(Context context) { 97 return new CustomTheme(mId, mTitle, mPackages, 98 mPackages.isEmpty() ? null : createPreviewInfo(context)); 99 } 100 setId(String id)101 public Builder setId(String id) { 102 mId = id; 103 return this; 104 } 105 } 106 } 107