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 17package com.android.layoutlib.bridge.remote.client.adapters; 18 19import com.android.ide.common.rendering.api.AdapterBinding; 20import com.android.ide.common.rendering.api.IProjectCallback.ViewAttribute; 21import com.android.ide.common.rendering.api.LayoutlibCallback; 22import com.android.ide.common.rendering.api.ResourceReference; 23import com.android.ide.common.rendering.api.ResourceValue; 24import com.android.ide.common.rendering.api.SessionParams.Key; 25import com.android.layout.remote.api.RemoteActionBarCallback; 26import com.android.layout.remote.api.RemoteILayoutPullParser; 27import com.android.layout.remote.api.RemoteLayoutlibCallback; 28import com.android.layout.remote.api.RemoteParserFactory; 29import com.android.layout.remote.api.RemoteXmlPullParser; 30import com.android.resources.ResourceType; 31import com.android.tools.layoutlib.annotations.NotNull; 32import com.android.tools.layoutlib.annotations.Nullable; 33import com.android.util.Pair; 34 35import java.net.URISyntaxException; 36import java.net.URL; 37import java.nio.file.Path; 38import java.nio.file.Paths; 39import java.rmi.RemoteException; 40import java.rmi.server.UnicastRemoteObject; 41 42public class RemoteLayoutlibCallbackAdapter implements RemoteLayoutlibCallback { 43 private final LayoutlibCallback mDelegate; 44 45 private RemoteLayoutlibCallbackAdapter(@NotNull LayoutlibCallback delegate) { 46 mDelegate = delegate; 47 } 48 49 public static RemoteLayoutlibCallback create(@NotNull LayoutlibCallback delegate) 50 throws RemoteException { 51 return (RemoteLayoutlibCallback) UnicastRemoteObject.exportObject( 52 new RemoteLayoutlibCallbackAdapter(delegate), 0); 53 } 54 55 @Override 56 public boolean supports(int ideFeature) { 57 return mDelegate.supports(ideFeature); 58 } 59 60 @Override 61 public Object loadView(String name, Class[] constructorSignature, Object[] constructorArgs) 62 throws Exception { 63 throw new UnsupportedOperationException("Not implemented yet"); 64 } 65 66 @Override 67 public String getNamespace() { 68 return mDelegate.getNamespace(); 69 } 70 71 @Override 72 public RemoteResolveResult resolveResourceId(int id) { 73 Pair<ResourceType, String> result = mDelegate.resolveResourceId(id); 74 return result != null ? new RemoteResolveResult(result.getFirst(), result.getSecond()) : 75 null; 76 } 77 78 @Override 79 public String resolveResourceId(int[] id) { 80 return mDelegate.resolveResourceId(id); 81 } 82 83 @Override 84 public Integer getResourceId(ResourceType type, String name) { 85 return mDelegate.getResourceId(type, name); 86 } 87 88 @Override 89 public RemoteILayoutPullParser getParser(ResourceValue layoutResource) { 90 try { 91 return RemoteILayoutPullParserAdapter.create(mDelegate.getParser(layoutResource)); 92 } catch (RemoteException e) { 93 throw new RuntimeException(e); 94 } 95 } 96 97 @Override 98 public Object getAdapterItemValue(ResourceReference adapterView, Object adapterCookie, 99 ResourceReference itemRef, int fullPosition, int positionPerType, 100 int fullParentPosition, int parentPositionPerType, ResourceReference viewRef, 101 ViewAttribute viewAttribute, Object defaultValue) { 102 throw new UnsupportedOperationException("Not implemented yet"); 103 } 104 105 @Override 106 public AdapterBinding getAdapterBinding(ResourceReference adapterViewRef, Object adapterCookie, 107 Object viewObject) { 108 throw new UnsupportedOperationException("Not implemented yet"); 109 } 110 111 @Override 112 public RemoteActionBarCallback getActionBarCallback() { 113 try { 114 return RemoteActionBarCallbackAdapter.create(mDelegate.getActionBarCallback()); 115 } catch (RemoteException e) { 116 throw new RuntimeException(e); 117 } 118 } 119 120 @Override 121 public <T> T getFlag(Key<T> key) { 122 return mDelegate.getFlag(key); 123 } 124 125 @Override 126 public RemoteParserFactory getParserFactory() { 127 try { 128 return RemoteParserFactoryAdapter.create(mDelegate.getParserFactory()); 129 } catch (RemoteException e) { 130 throw new RuntimeException(e); 131 } 132 } 133 134 @Nullable 135 @Override 136 public Path findClassPath(String name) { 137 try { 138 Class<?> clazz = mDelegate.findClass(name); 139 URL url = clazz.getProtectionDomain().getCodeSource().getLocation(); 140 if (url != null) { 141 return Paths.get(url.toURI()); 142 } 143 } catch (ClassNotFoundException ignore) { 144 } catch (URISyntaxException e) { 145 throw new RuntimeException(e); 146 } 147 148 return null; 149 } 150 151 @Override 152 public RemoteXmlPullParser getXmlFileParser(String fileName) { 153 try { 154 return RemoteXmlPullParserAdapter.create(mDelegate.getXmlFileParser(fileName)); 155 } catch (RemoteException e) { 156 throw new RuntimeException(e); 157 } 158 } 159} 160