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.client;
18 
19 import com.android.ide.common.rendering.api.Bridge;
20 import com.android.ide.common.rendering.api.DrawableParams;
21 import com.android.ide.common.rendering.api.ILayoutLog;
22 import com.android.ide.common.rendering.api.RenderSession;
23 import com.android.ide.common.rendering.api.Result;
24 import com.android.ide.common.rendering.api.SessionParams;
25 import com.android.layout.remote.api.RemoteBridge;
26 import com.android.layout.remote.api.RemoteSessionParams;
27 import com.android.layoutlib.bridge.remote.client.adapters.RemoteDrawableParamsAdapter;
28 import com.android.layoutlib.bridge.remote.client.adapters.RemoteLayoutLogAdapter;
29 import com.android.layoutlib.bridge.remote.client.adapters.RemoteRenderSessionAdapter;
30 import com.android.layoutlib.bridge.remote.client.adapters.RemoteSessionParamsAdapter;
31 import com.android.tools.layoutlib.annotations.NotNull;
32 
33 import java.io.File;
34 import java.rmi.NotBoundException;
35 import java.rmi.RemoteException;
36 import java.rmi.registry.LocateRegistry;
37 import java.rmi.registry.Registry;
38 import java.util.Map;
39 
40 public class RemoteBridgeClient extends Bridge {
41     private final RemoteBridge mDelegate;
42 
RemoteBridgeClient(@otNull RemoteBridge delegate)43     private RemoteBridgeClient(@NotNull RemoteBridge delegate) {
44         mDelegate = delegate;
45     }
46 
47     @NotNull
getRemoteBridge(int registryPort)48     public static RemoteBridgeClient getRemoteBridge(int registryPort) throws RemoteException,
49             NotBoundException {
50         Registry registry = LocateRegistry.getRegistry(registryPort);
51         RemoteBridge remoteBridge = (RemoteBridge) registry.lookup(RemoteBridge.class.getName());
52 
53         return new RemoteBridgeClient(remoteBridge);
54     }
55 
56     @Override
init(Map<String, String> platformProperties, File fontLocation, String nativeLibPath, String icuDataPath, Map<String, Map<String, Integer>> enumValueMap, ILayoutLog log)57     public boolean init(Map<String, String> platformProperties,
58             File fontLocation,
59             String nativeLibPath,
60             String icuDataPath,
61             Map<String, Map<String, Integer>> enumValueMap,
62             ILayoutLog log) {
63         try {
64             return mDelegate.init(platformProperties, fontLocation, nativeLibPath, icuDataPath,
65                     enumValueMap, RemoteLayoutLogAdapter.create(log));
66         } catch (RemoteException e) {
67             throw new RuntimeException(e);
68         }
69     }
70 
71     @Override
dispose()72     public boolean dispose() {
73         try {
74             return mDelegate.dispose();
75         } catch (RemoteException e) {
76             throw new RuntimeException(e);
77         }
78     }
79 
80     @Override
createSession(SessionParams params)81     public RenderSession createSession(SessionParams params) {
82         try {
83             RemoteSessionParams remoteParams = RemoteSessionParamsAdapter.create(params);
84 
85             return new RemoteRenderSessionAdapter(mDelegate.createSession(remoteParams));
86         } catch (RemoteException e) {
87             throw new RuntimeException(e);
88         }
89     }
90 
91     @Override
renderDrawable(DrawableParams params)92     public Result renderDrawable(DrawableParams params) {
93         try {
94             return mDelegate.renderDrawable(RemoteDrawableParamsAdapter.create(params));
95         } catch (RemoteException e) {
96             throw new RuntimeException(e);
97         }
98     }
99 
100     @Override
clearResourceCaches(Object projectKey)101     public void clearResourceCaches(Object projectKey) {
102         throw new UnsupportedOperationException();
103     }
104 
105     @Override
getViewParent(Object viewObject)106     public Result getViewParent(Object viewObject) {
107         throw new UnsupportedOperationException();
108     }
109 
110     @Override
getViewIndex(Object viewObject)111     public Result getViewIndex(Object viewObject) {
112         throw new UnsupportedOperationException();
113     }
114 
115     @Override
isRtl(String locale)116     public boolean isRtl(String locale) {
117         try {
118             return mDelegate.isRtl(locale);
119         } catch (RemoteException e) {
120             throw new RuntimeException(e);
121         }
122     }
123 }
124