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