1/* 2 * Copyright (C) 2016 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17package com.googlecode.android_scripting; 18 19import android.app.Service; 20import android.content.Intent; 21 22import com.googlecode.android_scripting.facade.FacadeConfiguration; 23import com.googlecode.android_scripting.facade.FacadeManagerFactory; 24import com.googlecode.android_scripting.jsonrpc.JsonRpcServer; 25import com.googlecode.android_scripting.jsonrpc.RpcReceiverManagerFactory; 26 27import java.net.InetSocketAddress; 28import java.util.UUID; 29 30public class AndroidProxy { 31 32 private InetSocketAddress mAddress; 33 private final JsonRpcServer mJsonRpcServer; 34 private final UUID mSecret; 35 private final RpcReceiverManagerFactory mFacadeManagerFactory; 36 37 /** 38 * 39 * @param service 40 * Android service (required to build facades). 41 * @param intent 42 * the intent that launched the proxy/script. 43 * @param requiresHandshake 44 * indicates whether RPC security protocol should be enabled. 45 */ 46 public AndroidProxy(Service service, Intent intent, boolean requiresHandshake) { 47 if (requiresHandshake) { 48 mSecret = UUID.randomUUID(); 49 } else { 50 mSecret = null; 51 } 52 mFacadeManagerFactory = 53 new FacadeManagerFactory(FacadeConfiguration.getSdkLevel(), service, intent, 54 FacadeConfiguration.getFacadeClasses()); 55 mJsonRpcServer = new JsonRpcServer(mFacadeManagerFactory, getSecret()); 56 } 57 58 public InetSocketAddress getAddress() { 59 return mAddress; 60 } 61 62 public InetSocketAddress startLocal() { 63 return startLocal(0); 64 } 65 66 public InetSocketAddress startLocal(int port) { 67 mAddress = mJsonRpcServer.startLocal(port); 68 return mAddress; 69 } 70 71 public InetSocketAddress startPublic() { 72 return startPublic(0); 73 } 74 75 public InetSocketAddress startPublic(int port) { 76 mAddress = mJsonRpcServer.startPublic(port); 77 return mAddress; 78 } 79 80 public void shutdown() { 81 mJsonRpcServer.shutdown(); 82 } 83 84 public String getSecret() { 85 if (mSecret == null) { 86 return null; 87 } 88 return mSecret.toString(); 89 } 90 91 public RpcReceiverManagerFactory getRpcReceiverManagerFactory() { 92 return mFacadeManagerFactory; 93 } 94} 95