• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.layoutlib.bridge.intensive.util;
18 
19 import com.android.SdkConstants;
20 import com.android.ide.common.rendering.api.AssetRepository;
21 import com.android.ide.common.rendering.api.IImageFactory;
22 import com.android.ide.common.rendering.api.LayoutLog;
23 import com.android.ide.common.rendering.api.LayoutlibCallback;
24 import com.android.ide.common.rendering.api.ResourceNamespace;
25 import com.android.ide.common.rendering.api.ResourceReference;
26 import com.android.ide.common.rendering.api.SessionParams;
27 import com.android.ide.common.rendering.api.SessionParams.RenderingMode;
28 import com.android.ide.common.resources.ResourceResolver;
29 import com.android.ide.common.resources.configuration.FolderConfiguration;
30 import com.android.ide.common.resources.deprecated.ResourceRepository;
31 import com.android.layoutlib.bridge.intensive.setup.ConfigGenerator;
32 import com.android.layoutlib.bridge.intensive.setup.LayoutPullParser;
33 import com.android.resources.ResourceType;
34 
35 import android.annotation.NonNull;
36 
37 import java.util.HashMap;
38 import java.util.Map;
39 
40 import com.google.common.collect.ImmutableMap;
41 
42 /**
43  * Builder to help setting up {@link SessionParams} objects.
44  */
45 public class SessionParamsBuilder {
46 
47     private LayoutPullParser mLayoutParser;
48     private RenderingMode mRenderingMode = RenderingMode.NORMAL;
49     private Object mProjectKey = null;
50     private ConfigGenerator mConfigGenerator = ConfigGenerator.NEXUS_5;
51     private ResourceRepository mFrameworkResources;
52     private ResourceRepository mProjectResources;
53     private String mThemeName;
54     private boolean isProjectTheme;
55     private LayoutlibCallback mLayoutlibCallback;
56     private int mTargetSdk;
57     private int mMinSdk = 0;
58     private LayoutLog mLayoutLog;
59     private Map<SessionParams.Key, Object> mFlags = new HashMap<>();
60     private AssetRepository mAssetRepository = null;
61     private boolean mDecor = true;
62     private IImageFactory mImageFactory = null;
63 
64     @NonNull
setParser(@onNull LayoutPullParser layoutParser)65     public SessionParamsBuilder setParser(@NonNull LayoutPullParser layoutParser) {
66         mLayoutParser = layoutParser;
67 
68         return this;
69     }
70 
71     @NonNull
setRenderingMode(@onNull RenderingMode renderingMode)72     public SessionParamsBuilder setRenderingMode(@NonNull RenderingMode renderingMode) {
73         mRenderingMode = renderingMode;
74         return this;
75     }
76 
77     @NonNull
setConfigGenerator(@onNull ConfigGenerator configGenerator)78     public SessionParamsBuilder setConfigGenerator(@NonNull ConfigGenerator configGenerator) {
79         mConfigGenerator = configGenerator;
80         return this;
81     }
82 
83     @NonNull
setProjectResources(@onNull ResourceRepository resources)84     public SessionParamsBuilder setProjectResources(@NonNull ResourceRepository resources) {
85         mProjectResources = resources;
86         return this;
87     }
88 
89     @NonNull
setFrameworkResources(@onNull ResourceRepository resources)90     public SessionParamsBuilder setFrameworkResources(@NonNull ResourceRepository resources) {
91         mFrameworkResources = resources;
92         return this;
93     }
94 
95     @NonNull
setTheme(@onNull String themeName, boolean isProjectTheme)96     public SessionParamsBuilder setTheme(@NonNull String themeName, boolean isProjectTheme) {
97         mThemeName = themeName;
98         this.isProjectTheme = isProjectTheme;
99         return this;
100     }
101 
102     @NonNull
setTheme(@onNull String themeName)103     public SessionParamsBuilder setTheme(@NonNull String themeName) {
104         boolean isProjectTheme;
105         if (themeName.startsWith(SdkConstants.PREFIX_ANDROID)) {
106             themeName = themeName.substring(SdkConstants.PREFIX_ANDROID.length());
107             isProjectTheme = false;
108         } else {
109             isProjectTheme = true;
110         }
111         return setTheme(themeName, isProjectTheme);
112     }
113 
114     @NonNull
setCallback(@onNull LayoutlibCallback callback)115     public SessionParamsBuilder setCallback(@NonNull LayoutlibCallback callback) {
116         mLayoutlibCallback = callback;
117         return this;
118     }
119 
120     @NonNull
setTargetSdk(int targetSdk)121     public SessionParamsBuilder setTargetSdk(int targetSdk) {
122         mTargetSdk = targetSdk;
123         return this;
124     }
125 
126     @SuppressWarnings("unused")
127     @NonNull
setMinSdk(int minSdk)128     public SessionParamsBuilder setMinSdk(int minSdk) {
129         mMinSdk = minSdk;
130         return this;
131     }
132 
133     @NonNull
setLayoutLog(@onNull LayoutLog layoutLog)134     public SessionParamsBuilder setLayoutLog(@NonNull LayoutLog layoutLog) {
135         mLayoutLog = layoutLog;
136         return this;
137     }
138 
139     @NonNull
setFlag(@onNull SessionParams.Key flag, Object value)140     public SessionParamsBuilder setFlag(@NonNull SessionParams.Key flag, Object value) {
141         mFlags.put(flag, value);
142         return this;
143     }
144 
145     @NonNull
setAssetRepository(@onNull AssetRepository repository)146     public SessionParamsBuilder setAssetRepository(@NonNull AssetRepository repository) {
147         mAssetRepository = repository;
148         return this;
149     }
150 
151     @NonNull
disableDecoration()152     public SessionParamsBuilder disableDecoration() {
153         mDecor = false;
154         return this;
155     }
156 
157     @NonNull
setImageFactory(@onNull IImageFactory imageFactory)158     public SessionParamsBuilder setImageFactory(@NonNull IImageFactory imageFactory) {
159         mImageFactory = imageFactory;
160         return this;
161     }
162 
163     @NonNull
build()164     public SessionParams build() {
165         assert mFrameworkResources != null;
166         assert mProjectResources != null;
167         assert mThemeName != null;
168         assert mLayoutLog != null;
169         assert mLayoutlibCallback != null;
170 
171         FolderConfiguration config = mConfigGenerator.getFolderConfig();
172         ResourceResolver resourceResolver = ResourceResolver.create(
173                 ImmutableMap.of(
174                         ResourceNamespace.ANDROID, mFrameworkResources.getConfiguredResources(config),
175                         ResourceNamespace.TODO(), mProjectResources.getConfiguredResources(config)),
176                 new ResourceReference(
177                         ResourceNamespace.fromBoolean(!isProjectTheme),
178                         ResourceType.STYLE,
179                         mThemeName));
180 
181         SessionParams params = new SessionParams(mLayoutParser, mRenderingMode, mProjectKey /* for
182         caching */, mConfigGenerator.getHardwareConfig(), resourceResolver, mLayoutlibCallback,
183                 mMinSdk, mTargetSdk, mLayoutLog);
184         if (mImageFactory != null) {
185             params.setImageFactory(mImageFactory);
186         }
187 
188         mFlags.forEach(params::setFlag);
189         params.setAssetRepository(mAssetRepository);
190 
191         if (!mDecor) {
192             params.setForceNoDecor();
193         }
194 
195         return params;
196     }
197 }
198