1 /*
2  * Copyright (C) 2022 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 
17 package com.android.ide.common.resources.deprecated;
18 
19 import com.android.ide.common.rendering.api.ResourceValue;
20 import com.android.ide.common.resources.configuration.FolderConfiguration;
21 import com.android.resources.ResourceType;
22 
23 import java.util.ArrayList;
24 import java.util.List;
25 
26 /**
27  * @deprecated This class is part of an obsolete resource repository system that is no longer used
28  *     in production code. The class is preserved temporarily for LayoutLib tests.
29  */
30 @Deprecated
31 public class ResourceItem implements Comparable<ResourceItem> {
32     private final String mName;
33 
34     /**
35      * List of files generating this ResourceItem.
36      */
37     private final List<ResourceFile> mFiles = new ArrayList<>();
38 
39     /**
40      * Constructs a new ResourceItem.
41      * @param name the name of the resource as it appears in the XML and R.java files.
42      */
ResourceItem(String name)43     public ResourceItem(String name) {
44         mName = name;
45     }
46 
47     /**
48      * Returns the name of the resource.
49      */
getName()50     public final String getName() {
51         return mName;
52     }
53 
54     /**
55      * Compares the {@link com.android.ide.common.resources.deprecated.ResourceItem} to another.
56      * @param other the ResourceItem to be compared to.
57      */
58     @Override
compareTo(com.android.ide.common.resources.deprecated.ResourceItem other)59     public int compareTo(com.android.ide.common.resources.deprecated.ResourceItem other) {
60         return mName.compareTo(other.mName);
61     }
62 
63     /**
64      * Returns a {@link ResourceValue} for this item based on the given configuration.
65      * If the ResourceItem has several source files, one will be selected based on the config.
66      * @param type the type of the resource. This is necessary because ResourceItem doesn't embed
67      *     its type, but ResourceValue does.
68      * @param referenceConfig the config of the resource item.
69      * @return a ResourceValue or null if none match the config.
70      */
getResourceValue(ResourceType type, FolderConfiguration referenceConfig)71     public ResourceValue getResourceValue(ResourceType type, FolderConfiguration referenceConfig) {
72         // look for the best match for the given configuration
73         // the match has to be of type ResourceFile since that's what the input list contains
74         ResourceFile match = referenceConfig.findMatchingConfigurable(mFiles);
75 
76         if (match != null) {
77             // get the value of this configured resource.
78             return match.getValue(type, mName);
79         }
80 
81         return null;
82     }
83 
84     /**
85      * Adds a new source file.
86      * @param file the source file.
87      */
add(ResourceFile file)88     protected void add(ResourceFile file) {
89         mFiles.add(file);
90     }
91 
92     /**
93      * Removes a file from the list of source files.
94      * @param file the file to remove
95      */
removeFile(ResourceFile file)96     protected void removeFile(ResourceFile file) {
97         mFiles.remove(file);
98     }
99 
100     /**
101      * Returns {@code true} if the item has no source file.
102      * @return true if the item has no source file.
103      */
hasNoSourceFile()104     protected boolean hasNoSourceFile() {
105         return mFiles.isEmpty();
106     }
107 }
108