Debian Codegraph @

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

raw
1 /*
2 * Copyright (C) 2010 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.rendering.api;
18
19 import com.android.ide.common.rendering.api.SessionParams.Key;
20 import com.android.resources.Density;
21 import com.android.resources.ScreenSize;
22
23 import java.util.HashMap;
24 import java.util.Map;
25
26 /**
27 * Base class for rendering parameters. This include the generic parameters but not what needs
28 * to be rendered or additional parameters.
29 */
30 public abstract class RenderParams {
31
32 public static final long DEFAULT_TIMEOUT = 250; //ms
33
34 private final Object mProjectKey;
35 private final HardwareConfig mHardwareConfig;
36 private final RenderResources mRenderResources;
37 private final LayoutlibCallback mLayoutlibCallback;
38 private final int mMinSdkVersion;
39 private final int mTargetSdkVersion;
40 private final LayoutLog mLog;
41
42 private boolean mCustomBackgroundEnabled;
43 private int mCustomBackgroundColor;
44 private long mTimeout;
45
46 private AssetRepository mAssetRepository;
47 private IImageFactory mImageFactory;
48
49 private String mAppIcon;
50 private String mAppLabel;
51 private String mLocale;
52 private String mActivityName;
53 private boolean mForceNoDecor;
54 private boolean mSupportsRtl;
55
56 /**
57 * A flexible map to pass additional flags to LayoutLib. LayoutLib will ignore flags that it
58 * doesn't recognize.
59 */
60 private Map<Key, Object> mFlags;
61
62 /**
63 * @param projectKey An Object identifying the project. This is used for the cache mechanism.
64 * @param hardwareConfig the {@link HardwareConfig}.
65 * @param renderResources a {@link RenderResources} object providing access to the resources.
66 * @param layoutlibCallback The {@link LayoutlibCallback} object to get information from
67 * the project.
68 * @param minSdkVersion the minSdkVersion of the project
69 * @param targetSdkVersion the targetSdkVersion of the project
70 * @param log the object responsible for displaying warning/errors to the user.
71 */
72 public RenderParams(
73 Object projectKey,
74 HardwareConfig hardwareConfig,
75 RenderResources renderResources,
76 LayoutlibCallback layoutlibCallback,
77 int minSdkVersion, int targetSdkVersion,
78 LayoutLog log) {
79 mProjectKey = projectKey;
80 mHardwareConfig = hardwareConfig;
81 mRenderResources = renderResources;
82 mLayoutlibCallback = layoutlibCallback;
83 mMinSdkVersion = minSdkVersion;
84 mTargetSdkVersion = targetSdkVersion;
85 mLog = log;
86 mCustomBackgroundEnabled = false;
87 mTimeout = DEFAULT_TIMEOUT;
88 }
89
90 /**
91 * Copy constructor.
92 */
93 public RenderParams(RenderParams params) {
94 mProjectKey = params.mProjectKey;
95 mHardwareConfig = params.mHardwareConfig;
96 mRenderResources = params.mRenderResources;
97 mAssetRepository = params.mAssetRepository;
98 mLayoutlibCallback = params.mLayoutlibCallback;
99 mMinSdkVersion = params.mMinSdkVersion;
100 mTargetSdkVersion = params.mTargetSdkVersion;
101 mLog = params.mLog;
102 mCustomBackgroundEnabled = params.mCustomBackgroundEnabled;
103 mCustomBackgroundColor = params.mCustomBackgroundColor;
104 mTimeout = params.mTimeout;
105 mImageFactory = params.mImageFactory;
106 mAppIcon = params.mAppIcon;
107 mAppLabel = params.mAppLabel;
108 mLocale = params.mLocale;
109 mActivityName = params.mActivityName;
110 mForceNoDecor = params.mForceNoDecor;
111 mSupportsRtl = params.mSupportsRtl;
112 if (params.mFlags != null) {
113 mFlags = new HashMap<Key, Object>(params.mFlags);
114 }
115 }
116
117 public void setOverrideBgColor(int color) {
118 mCustomBackgroundEnabled = true;
119 mCustomBackgroundColor = color;
120 }
121
122 public void setTimeout(long timeout) {
123 mTimeout = timeout;
124 }
125
126 public void setImageFactory(IImageFactory imageFactory) {
127 mImageFactory = imageFactory;
128 }
129
130 public void setAppIcon(String appIcon) {
131 mAppIcon = appIcon;
132 }
133
134 public void setAppLabel(String appLabel) {
135 mAppLabel = appLabel;
136 }
137
138 public void setLocale(String locale) {
139 mLocale = locale;
140 }
141
142 public void setActivityName(String activityName) {
143 mActivityName = activityName;
144 }
145
146 public void setForceNoDecor() {
147 mForceNoDecor = true;
148 }
149
150 public void setRtlSupport(boolean supportsRtl) {
151 mSupportsRtl = supportsRtl;
152 }
153
154 public void setAssetRepository(AssetRepository assetRepository) {
155 mAssetRepository = assetRepository;
156 }
157
158 public Object getProjectKey() {
159 return mProjectKey;
160 }
161
162 public HardwareConfig getHardwareConfig() {
163 return mHardwareConfig;
164 }
165
166 public int getMinSdkVersion() {
167 return mMinSdkVersion;
168 }
169
170 public int getTargetSdkVersion() {
171 return mTargetSdkVersion;
172 }
173
174 /**
175 * @deprecated Use {@link #getHardwareConfig()}
176 */
177 @Deprecated
178 public int getScreenWidth() {
179 return mHardwareConfig.getScreenWidth();
180 }
181
182 /**
183 * @deprecated Use {@link #getHardwareConfig()}
184 */
185 @Deprecated
186 public int getScreenHeight() {
187 return mHardwareConfig.getScreenHeight();
188 }
189
190 /**
191 * @deprecated Use {@link #getHardwareConfig()}
192 */
193 @Deprecated
194 public Density getDensity() {
195 return mHardwareConfig.getDensity();
196 }
197
198 /**
199 * @deprecated Use {@link #getHardwareConfig()}
200 */
201 @Deprecated
202 public float getXdpi() {
203 return mHardwareConfig.getXdpi();
204 }
205
206 /**
207 * @deprecated Use {@link #getHardwareConfig()}
208 */
209 @Deprecated
210 public float getYdpi() {
211 return mHardwareConfig.getYdpi();
212 }
213
214 public RenderResources getResources() {
215 return mRenderResources;
216 }
217
218 public AssetRepository getAssets() {
219 return mAssetRepository;
220 }
221
222 /** @deprecated use {@link #getLayoutlibCallback()} */
223 @Deprecated
224 public IProjectCallback getProjectCallback() {
225 return getLayoutlibCallback();
226 }
227
228 public LayoutlibCallback getLayoutlibCallback() {
229 return mLayoutlibCallback;
230 }
231
232 public LayoutLog getLog() {
233 return mLog;
234 }
235
236 public boolean isBgColorOverridden() {
237 return mCustomBackgroundEnabled;
238 }
239
240 public int getOverrideBgColor() {
241 return mCustomBackgroundColor;
242 }
243
244 public long getTimeout() {
245 return mTimeout;
246 }
247
248 public IImageFactory getImageFactory() {
249 return mImageFactory;
250 }
251
252 /**
253 * @deprecated Use {@link #getHardwareConfig()}
254 */
255 @Deprecated
256 public ScreenSize getConfigScreenSize() {
257 return mHardwareConfig.getScreenSize();
258 }
259
260 public String getAppIcon() {
261 return mAppIcon;
262 }
263
264 public String getAppLabel() {
265 return mAppLabel;
266 }
267
268 public String getLocale() {
269 return mLocale;
270 }
271
272 public String getActivityName() {
273 return mActivityName;
274 }
275
276 public boolean isForceNoDecor() {
277 return mForceNoDecor;
278 }
279
280 public boolean isRtlSupported() {
281 return mSupportsRtl;
282 }
283
284 public <T> void setFlag(Key<T> key, T value) {
285 if (mFlags == null) {
286 mFlags = new HashMap<Key, Object>();
287 }
288 mFlags.put(key, value);
289 }
290
291 public <T> T getFlag(Key<T> key) {
292
293 // noinspection since the values in the map can be added only by setFlag which ensures that
294 // the types match.
295 //noinspection unchecked
296 return mFlags == null ? null : (T) mFlags.get(key);
297 }
298 }