• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.SdkConstants;
20 import com.android.ide.common.resources.configuration.Configurable;
21 import com.android.ide.common.resources.configuration.FolderConfiguration;
22 import com.android.io.IAbstractFile;
23 import com.android.io.IAbstractFolder;
24 import com.android.resources.FolderTypeRelationship;
25 import com.android.resources.ResourceFolderType;
26 import com.android.resources.ResourceType;
27 import com.android.utils.SdkUtils;
28 
29 import java.util.List;
30 
31 /**
32  * @deprecated This class is part of an obsolete resource repository system that is no longer used
33  *     in production code. The class is preserved temporarily for LayoutLib tests.
34  */
35 @Deprecated
36 public final class ResourceFolder implements Configurable {
37     private final ResourceFolderType mType;
38     final FolderConfiguration mConfiguration;
39     IAbstractFolder mFolder;
40     private final ResourceRepository mRepository;
41 
42     /**
43      * Creates a new {@link com.android.ide.common.resources.deprecated.ResourceFolder}
44      * @param type The type of the folder
45      * @param config The configuration of the folder
46      * @param folder The associated {@link IAbstractFolder} object.
47      * @param repository The associated {@link ResourceRepository}
48      */
ResourceFolder(ResourceFolderType type, FolderConfiguration config, IAbstractFolder folder, ResourceRepository repository)49     protected ResourceFolder(ResourceFolderType type, FolderConfiguration config,
50             IAbstractFolder folder, ResourceRepository repository) {
51         mType = type;
52         mConfiguration = config;
53         mFolder = folder;
54         mRepository = repository;
55     }
56 
57     /**
58      * Processes a file and adds it to its parent folder resource.
59      *
60      * @param file the underlying resource file.
61      * @param kind the file change kind.
62      * @param context a context object with state for the current update, such
63      *            as a place to stash errors encountered
64      * @return the {@link ResourceFile} that was created.
65      */
processFile(TestFileWrapper file, ResourceDeltaKind kind, ScanningContext context)66     public ResourceFile processFile(TestFileWrapper file, ResourceDeltaKind kind,
67             ScanningContext context) {
68         // look for this file if it's already been created
69         ResourceFile resFile = getFile(file, context);
70 
71         if (resFile == null) {
72             if (kind != ResourceDeltaKind.REMOVED) {
73                 // create a ResourceFile for it.
74 
75                 resFile = createResourceFile(file);
76                 resFile.load(context);
77             }
78         } else {
79             if (kind != ResourceDeltaKind.REMOVED) {
80                 resFile.update(context);
81             }
82         }
83 
84         return resFile;
85     }
86 
createResourceFile(TestFileWrapper file)87     private ResourceFile createResourceFile(TestFileWrapper file) {
88         // check if that's a single or multi resource type folder. We have a special case
89         // for ID generating resource types (layout/menu, and XML drawables, etc.).
90         // MultiResourceFile handles the case when several resource types come from a single file
91         // (values files).
92 
93         ResourceFile resFile;
94         if (mType != ResourceFolderType.VALUES) {
95             if (FolderTypeRelationship.isIdGeneratingFolderType(mType) &&
96                 SdkUtils.endsWithIgnoreCase(file.getName(), SdkConstants.DOT_XML)) {
97                 List<ResourceType> types = FolderTypeRelationship.getRelatedResourceTypes(mType);
98                 ResourceType primaryType = types.get(0);
99                 resFile = new IdGeneratingResourceFile(file, this, primaryType);
100             } else {
101                 resFile = new SingleResourceFile(file, this);
102             }
103         } else {
104             resFile = new MultiResourceFile(file, this);
105         }
106         return resFile;
107     }
108 
109     /**
110      * Returns the {@link ResourceFolderType} of this object.
111      */
getType()112     public ResourceFolderType getType() {
113         return mType;
114     }
115 
getRepository()116     public ResourceRepository getRepository() {
117         return mRepository;
118     }
119 
120     @Override
getConfiguration()121     public FolderConfiguration getConfiguration() {
122         return mConfiguration;
123     }
124 
125     /**
126      * Returns the {@link ResourceFile} matching a {@link IAbstractFile} object.
127      *
128      * @param file The {@link IAbstractFile} object.
129      * @param context a context object with state for the current update, such
130      *            as a place to stash errors encountered
131      * @return the {@link ResourceFile} or null if no match was found.
132      */
getFile(TestFileWrapper file, ScanningContext context)133     private ResourceFile getFile(TestFileWrapper file, ScanningContext context) {
134         assert mFolder.equals(file.getParentFolder());
135 
136         // If the file actually exists, the resource folder  may not have been
137         // scanned yet; add it lazily
138         if (file.exists()) {
139             ResourceFile resFile = createResourceFile(file);
140             resFile.load(context);
141             return resFile;
142         }
143 
144         return null;
145     }
146 
147     @Override
toString()148     public String toString() {
149         return mFolder.toString();
150     }
151 }
152