• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.googlecode.android_scripting;
18 
19 import android.app.Service;
20 import android.content.Intent;
21 
22 import com.googlecode.android_scripting.facade.FacadeConfiguration;
23 import com.googlecode.android_scripting.facade.FacadeManagerFactory;
24 import com.googlecode.android_scripting.jsonrpc.JsonRpcServer;
25 import com.googlecode.android_scripting.jsonrpc.RpcReceiverManagerFactory;
26 
27 import java.net.InetSocketAddress;
28 import java.util.UUID;
29 
30 public 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    */
AndroidProxy(Service service, Intent intent, boolean requiresHandshake)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 
getAddress()58   public InetSocketAddress getAddress() {
59     return mAddress;
60   }
61 
startLocal()62   public InetSocketAddress startLocal() {
63     return startLocal(0);
64   }
65 
startLocal(int port)66   public InetSocketAddress startLocal(int port) {
67     mAddress = mJsonRpcServer.startLocal(port);
68     return mAddress;
69   }
70 
startPublic()71   public InetSocketAddress startPublic() {
72     return startPublic(0);
73   }
74 
startPublic(int port)75   public InetSocketAddress startPublic(int port) {
76     mAddress = mJsonRpcServer.startPublic(port);
77     return mAddress;
78   }
79 
shutdown()80   public void shutdown() {
81     mJsonRpcServer.shutdown();
82   }
83 
getSecret()84   public String getSecret() {
85     if (mSecret == null) {
86       return null;
87     }
88     return mSecret.toString();
89   }
90 
getRpcReceiverManagerFactory()91   public RpcReceiverManagerFactory getRpcReceiverManagerFactory() {
92     return mFacadeManagerFactory;
93   }
94 }
95