Home | History | Annotate | Download | only in android_scripting
      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.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    */
     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