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.remote.server.adapters;
18 
19 import com.android.ide.common.rendering.api.LayoutLog;
20 import com.android.ide.common.rendering.api.RenderResources;
21 import com.android.ide.common.rendering.api.ResourceReference;
22 import com.android.ide.common.rendering.api.ResourceValue;
23 import com.android.ide.common.rendering.api.StyleResourceValue;
24 import com.android.layout.remote.api.RemoteRenderResources;
25 import com.android.layout.remote.api.RemoteResourceValue;
26 import com.android.tools.layoutlib.annotations.NotNull;
27 
28 import java.rmi.RemoteException;
29 import java.util.List;
30 import java.util.stream.Collectors;
31 
32 public class RemoteRenderResourcesAdapter extends RenderResources {
33     private final RemoteRenderResources mDelegate;
34 
RemoteRenderResourcesAdapter(@otNull RemoteRenderResources remoteRenderResources)35     public RemoteRenderResourcesAdapter(@NotNull RemoteRenderResources remoteRenderResources) {
36         mDelegate = remoteRenderResources;
37     }
38 
39     @Override
setLogger(LayoutLog logger)40     public void setLogger(LayoutLog logger) {
41         // Ignored for remote operations.
42     }
43 
44     @Override
getDefaultTheme()45     public StyleResourceValue getDefaultTheme() {
46         try {
47             return mDelegate.getDefaultTheme().toResourceValue();
48         } catch (RemoteException e) {
49             throw new RuntimeException(e);
50         }
51     }
52 
53     @Override
applyStyle(StyleResourceValue theme, boolean useAsPrimary)54     public void applyStyle(StyleResourceValue theme, boolean useAsPrimary) {
55         try {
56             mDelegate.applyStyle(RemoteResourceValue.fromResourceValue(theme), useAsPrimary);
57         } catch (RemoteException e) {
58             throw new RuntimeException(e);
59         }
60     }
61 
62     @Override
clearStyles()63     public void clearStyles() {
64         try {
65             mDelegate.clearStyles();
66         } catch (RemoteException e) {
67             throw new RuntimeException(e);
68         }
69     }
70 
71     @Override
getAllThemes()72     public List<StyleResourceValue> getAllThemes() {
73         try {
74             return mDelegate.getAllThemes().stream()
75                     .map(RemoteResourceValue::toResourceValue)
76                     .collect(Collectors.toList());
77         } catch (RemoteException e) {
78             throw new RuntimeException(e);
79         }
80     }
81 
82     @Override
findItemInTheme(ResourceReference attr)83     public ResourceValue findItemInTheme(ResourceReference attr) {
84         try {
85             return mDelegate.findItemInTheme(attr).toResourceValue();
86         } catch (RemoteException e) {
87             throw new RuntimeException(e);
88         }
89     }
90 
91     @Override
findItemInStyle(StyleResourceValue style, ResourceReference attr)92     public ResourceValue findItemInStyle(StyleResourceValue style, ResourceReference attr) {
93         try {
94             return mDelegate.findItemInStyle(RemoteResourceValue.fromResourceValue(style), attr)
95                     .toResourceValue();
96         } catch (RemoteException e) {
97             throw new RuntimeException(e);
98         }
99     }
100 
101     @Override
dereference(ResourceValue resourceValue)102     public ResourceValue dereference(ResourceValue resourceValue) {
103         try {
104             return mDelegate.dereference(RemoteResourceValue.fromResourceValue(resourceValue))
105                     .toResourceValue();
106         } catch (RemoteException e) {
107             throw new RuntimeException(e);
108         }
109     }
110 
111     /** Returns a resource by namespace, type and name. The returned resource is unresolved. */
112     @Override
getUnresolvedResource(ResourceReference reference)113     public ResourceValue getUnresolvedResource(ResourceReference reference) {
114         try {
115             return mDelegate.getUnresolvedResource(reference).toResourceValue();
116         } catch (RemoteException e) {
117             throw new RuntimeException(e);
118         }
119     }
120 
121     @Override
resolveResValue(ResourceValue value)122     public ResourceValue resolveResValue(ResourceValue value) {
123         try {
124             return mDelegate.resolveValue(RemoteResourceValue.fromResourceValue(value))
125                     .toResourceValue();
126         } catch (RemoteException e) {
127             throw new RuntimeException(e);
128         }
129     }
130 
131     @Override
getParent(StyleResourceValue style)132     public StyleResourceValue getParent(StyleResourceValue style) {
133         try {
134             return mDelegate.getParent(RemoteResourceValue.fromResourceValue(style))
135                     .toResourceValue();
136         } catch (RemoteException e) {
137             throw new RuntimeException(e);
138         }
139     }
140 
141     @Override
getStyle(ResourceReference reference)142     public StyleResourceValue getStyle(ResourceReference reference) {
143         try {
144             return mDelegate.getStyle(reference).toResourceValue();
145         } catch (RemoteException e) {
146             throw new RuntimeException(e);
147         }
148     }
149 }
150