Debian Codegraph @

layoutlib-api/src/main/java/com/android/ide/common/rendering/api/LayoutlibCallback.java

raw
1 /*
2 * Copyright (C) 2015 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.ide.common.rendering.api;
17
18 import com.android.annotations.NonNull;
19 import com.android.annotations.Nullable;
20 import com.android.resources.ResourceType;
21 import com.android.util.Pair;
22
23 import org.intellij.lang.annotations.MagicConstant;
24 import org.xmlpull.v1.XmlPullParser;
25
26 /**
27 * Intermediary class implementing parts of both the old and new ProjectCallback from the
28 * LayoutLib API.
29 * <p>
30 * Even newer LayoutLibs use this directly instead of the the interface. This allows the flexibility
31 * to add newer methods without having to update {@link Bridge#API_CURRENT LayoutLib API version}.
32 * <p>
33 * Clients should use this instead of {@link IProjectCallback} to target both old and new
34 * Layout Libraries.
35 */
36 @SuppressWarnings({"deprecation", "MethodMayBeStatic", "unused"})
37 public abstract class LayoutlibCallback implements IProjectCallback,
38 com.android.layoutlib.api.IProjectCallback {
39
40 /**
41 * Like {@link #loadView(String, Class[], Object[])}, but intended for loading classes that may
42 * not be custom views.
43 *
44 * @param name className in binary format (see {@link ClassLoader})
45 * @return an new instance created by calling the given constructor.
46 * @throws ClassNotFoundException any exceptions thrown when creating the instance is wrapped in
47 * ClassNotFoundException.
48 * @since API 15
49 */
50 public Object loadClass(@NonNull String name, @Nullable Class[] constructorSignature,
51 @Nullable Object[] constructorArgs) throws ClassNotFoundException {
52 try {
53 return loadView(name, constructorSignature, constructorArgs);
54 }
55 catch (ClassNotFoundException e) {
56 throw e;
57 }
58 catch (Exception e) {
59 throw new ClassNotFoundException(name + " not found.", e);
60 }
61 }
62
63 /**
64 * Returns if the IDE supports the requested feature.
65 * @see Features
66 * @since API 15
67 */
68 public abstract boolean supports(
69 @MagicConstant(valuesFromClass = Features.class) int ideFeature);
70
71 /**
72 * A callback to query arbitrary data. This is similar to {@link RenderParams#setFlag(SessionParams.Key,
73 * Object)}. The main difference is that when using this, the IDE doesn't have to compute the
74 * value in advance and thus may save on some computation.
75 * @since API 15
76 */
77 @Nullable
78 public <T> T getFlag(@NonNull SessionParams.Key<T> key) {
79 return null;
80 }
81
82 /**
83 * Get a ParserFactory which can be used to create XmlPullParsers.
84 * @since API 15
85 */
86 @NonNull
87 public ParserFactory getParserFactory() {
88 throw new UnsupportedOperationException("getParserFactory not supported.");
89 }
90
91 /**
92 * Find a custom class in the project.
93 * <p>
94 * Like {@link #loadClass(String, Class[], Object[])}, but doesn't instantiate
95 * an object and just returns the class found.
96 * @param name className in binary format. (see {@link ClassLoader}.
97 * @since API 15
98 */
99 @NonNull
100 public Class<?> findClass(@NonNull String name) throws ClassNotFoundException {
101 throw new ClassNotFoundException(name + " not found.");
102 }
103
104 /**
105 * Returns an {@link XmlPullParser} for the psi version of an xml file.
106 *
107 * The call to the method should be guarded by a check for
108 * {@code RenderParamsFlag.FLAG_KEY_XML_FILE_PARSER_SUPPORT}.
109 *
110 * @param fileName name of the file to parse
111 * @since API 16
112 */
113 @Nullable
114 public XmlPullParser getXmlFileParser(String fileName) {
115 return null;
116 }
117
118 // ------ implementation of the old interface using the new interface.
119
120 @Override
121 public final Integer getResourceValue(String type, String name) {
122 return getResourceId(ResourceType.getEnum(type), name);
123 }
124
125 @Override
126 public final String[] resolveResourceValue(int id) {
127 Pair<ResourceType, String> info = resolveResourceId(id);
128 if (info != null) {
129 return new String[] { info.getSecond(), info.getFirst().getName() };
130 }
131
132 return null;
133 }
134
135 @Override
136 public final String resolveResourceValue(int[] id) {
137 return resolveResourceId(id);
138 }
139 }