| 1 |
/* |
| 2 |
* Copyright (C) 2011 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 java.util.Collections; |
| 20 |
import java.util.HashMap; |
| 21 |
import java.util.Map; |
| 22 |
|
| 23 |
/** |
| 24 |
* Rendering parameters for a {@link RenderSession}. |
| 25 |
*/ |
| 26 |
public class SessionParams extends RenderParams { |
| 27 |
|
| 28 |
public enum RenderingMode { |
| 29 |
NORMAL(false, false), |
| 30 |
V_SCROLL(false, true), |
| 31 |
H_SCROLL(true, false), |
| 32 |
FULL_EXPAND(true, true); |
| 33 |
|
| 34 |
private final boolean mHorizExpand; |
| 35 |
private final boolean mVertExpand; |
| 36 |
|
| 37 |
RenderingMode(boolean horizExpand, boolean vertExpand) { |
| 38 |
mHorizExpand = horizExpand; |
| 39 |
mVertExpand = vertExpand; |
| 40 |
} |
| 41 |
|
| 42 |
public boolean isHorizExpand() { |
| 43 |
return mHorizExpand; |
| 44 |
} |
| 45 |
|
| 46 |
public boolean isVertExpand() { |
| 47 |
return mVertExpand; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
private final ILayoutPullParser mLayoutDescription; |
| 52 |
private final RenderingMode mRenderingMode; |
| 53 |
private boolean mLayoutOnly = false; |
| 54 |
private Map<ResourceReference, AdapterBinding> mAdapterBindingMap; |
| 55 |
private boolean mExtendedViewInfoMode = false; |
| 56 |
private final int mSimulatedPlatformVersion; |
| 57 |
|
| 58 |
/** |
| 59 |
* |
| 60 |
* @param layoutDescription the {@link ILayoutPullParser} letting the LayoutLib Bridge visit the |
| 61 |
* layout file. |
| 62 |
* @param renderingMode The rendering mode. |
| 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 SessionParams( |
| 73 |
ILayoutPullParser layoutDescription, |
| 74 |
RenderingMode renderingMode, |
| 75 |
Object projectKey, |
| 76 |
HardwareConfig hardwareConfig, |
| 77 |
RenderResources renderResources, |
| 78 |
LayoutlibCallback layoutlibCallback, |
| 79 |
int minSdkVersion, int targetSdkVersion, |
| 80 |
LayoutLog log) { |
| 81 |
this(layoutDescription, renderingMode, projectKey, hardwareConfig, |
| 82 |
renderResources, layoutlibCallback, minSdkVersion, targetSdkVersion, log, 0); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* |
| 87 |
* @param layoutDescription the {@link ILayoutPullParser} letting the LayoutLib Bridge visit the |
| 88 |
* layout file. |
| 89 |
* @param renderingMode The rendering mode. |
| 90 |
* @param projectKey An Object identifying the project. This is used for the cache mechanism. |
| 91 |
* @param hardwareConfig the {@link HardwareConfig}. |
| 92 |
* @param renderResources a {@link RenderResources} object providing access to the resources. |
| 93 |
* @param projectCallback The {@link LayoutlibCallback} object to get information from |
| 94 |
* the project. |
| 95 |
* @param minSdkVersion the minSdkVersion of the project |
| 96 |
* @param targetSdkVersion the targetSdkVersion of the project |
| 97 |
* @param log the object responsible for displaying warning/errors to the user. |
| 98 |
* @param simulatedPlatformVersion try to simulate an old android platform. 0 means disabled. |
| 99 |
*/ |
| 100 |
public SessionParams( |
| 101 |
ILayoutPullParser layoutDescription, |
| 102 |
RenderingMode renderingMode, |
| 103 |
Object projectKey, |
| 104 |
HardwareConfig hardwareConfig, |
| 105 |
RenderResources renderResources, |
| 106 |
LayoutlibCallback projectCallback, |
| 107 |
int minSdkVersion, int targetSdkVersion, |
| 108 |
LayoutLog log, int simulatedPlatformVersion) { |
| 109 |
super(projectKey, hardwareConfig, renderResources, projectCallback, |
| 110 |
minSdkVersion, targetSdkVersion, log); |
| 111 |
|
| 112 |
mLayoutDescription = layoutDescription; |
| 113 |
mRenderingMode = renderingMode; |
| 114 |
mSimulatedPlatformVersion = simulatedPlatformVersion; |
| 115 |
} |
| 116 |
|
| 117 |
public SessionParams(SessionParams params) { |
| 118 |
super(params); |
| 119 |
mLayoutDescription = params.mLayoutDescription; |
| 120 |
mRenderingMode = params.mRenderingMode; |
| 121 |
mSimulatedPlatformVersion = params.mSimulatedPlatformVersion; |
| 122 |
if (params.mAdapterBindingMap != null) { |
| 123 |
mAdapterBindingMap = new HashMap<ResourceReference, AdapterBinding>( |
| 124 |
params.mAdapterBindingMap); |
| 125 |
} |
| 126 |
mExtendedViewInfoMode = params.mExtendedViewInfoMode; |
| 127 |
} |
| 128 |
|
| 129 |
public ILayoutPullParser getLayoutDescription() { |
| 130 |
return mLayoutDescription; |
| 131 |
} |
| 132 |
|
| 133 |
public RenderingMode getRenderingMode() { |
| 134 |
return mRenderingMode; |
| 135 |
} |
| 136 |
|
| 137 |
public void setLayoutOnly() { |
| 138 |
mLayoutOnly = true; |
| 139 |
} |
| 140 |
|
| 141 |
public boolean isLayoutOnly() { |
| 142 |
return mLayoutOnly; |
| 143 |
} |
| 144 |
|
| 145 |
public void addAdapterBinding(ResourceReference reference, AdapterBinding data) { |
| 146 |
if (mAdapterBindingMap == null) { |
| 147 |
mAdapterBindingMap = new HashMap<ResourceReference, AdapterBinding>(); |
| 148 |
} |
| 149 |
|
| 150 |
mAdapterBindingMap.put(reference, data); |
| 151 |
} |
| 152 |
|
| 153 |
public Map<ResourceReference, AdapterBinding> getAdapterBindings() { |
| 154 |
if (mAdapterBindingMap == null) { |
| 155 |
return Collections.emptyMap(); |
| 156 |
} |
| 157 |
|
| 158 |
return Collections.unmodifiableMap(mAdapterBindingMap); |
| 159 |
} |
| 160 |
|
| 161 |
public void setExtendedViewInfoMode(boolean mode) { |
| 162 |
mExtendedViewInfoMode = mode; |
| 163 |
} |
| 164 |
|
| 165 |
public boolean getExtendedViewInfoMode() { |
| 166 |
return mExtendedViewInfoMode; |
| 167 |
} |
| 168 |
|
| 169 |
public int getSimulatedPlatformVersion() { |
| 170 |
return mSimulatedPlatformVersion; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* The class should be in RenderParams, but is here because it was |
| 175 |
* originally here and we cannot change the API without breaking backwards |
| 176 |
* compatibility. |
| 177 |
*/ |
| 178 |
public static class Key<T> { |
| 179 |
public final Class<T> mExpectedClass; |
| 180 |
public final String mName; |
| 181 |
|
| 182 |
public Key(String name, Class<T> expectedClass) { |
| 183 |
assert name != null; |
| 184 |
assert expectedClass != null; |
| 185 |
|
| 186 |
mExpectedClass = expectedClass; |
| 187 |
mName = name; |
| 188 |
} |
| 189 |
|
| 190 |
@Override |
| 191 |
public int hashCode() { |
| 192 |
int result = mExpectedClass.hashCode(); |
| 193 |
return 31 * result + mName.hashCode(); |
| 194 |
} |
| 195 |
|
| 196 |
@Override |
| 197 |
public boolean equals(Object obj) { |
| 198 |
if (this == obj) { |
| 199 |
return true; |
| 200 |
} |
| 201 |
if (obj != null && getClass() == obj.getClass()) { |
| 202 |
Key<?> k = (Key<?>) obj; |
| 203 |
return mExpectedClass.equals(k.mExpectedClass) && mName.equals(k.mName); |
| 204 |
} |
| 205 |
return false; |
| 206 |
} |
| 207 |
|
| 208 |
@Override |
| 209 |
public String toString() { |
| 210 |
return mName; |
| 211 |
} |
| 212 |
} |
| 213 |
} |