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.content.Intent;
     20 
     21 import com.googlecode.android_scripting.interpreter.Interpreter;
     22 import com.googlecode.android_scripting.interpreter.InterpreterConfiguration;
     23 import com.googlecode.android_scripting.interpreter.InterpreterProcess;
     24 
     25 import java.io.File;
     26 
     27 public class ScriptLauncher {
     28 
     29   private ScriptLauncher() {
     30     // Utility class.
     31   }
     32 
     33   public static InterpreterProcess launchInterpreter(final AndroidProxy proxy, Intent intent,
     34       InterpreterConfiguration config, Runnable shutdownHook) {
     35     Interpreter interpreter;
     36     String interpreterName;
     37     interpreterName = intent.getStringExtra(Constants.EXTRA_INTERPRETER_NAME);
     38     interpreter = config.getInterpreterByName(interpreterName);
     39     InterpreterProcess process = new InterpreterProcess(interpreter, proxy);
     40     if (shutdownHook == null) {
     41       process.start(new Runnable() {
     42         @Override
     43         public void run() {
     44           proxy.shutdown();
     45         }
     46       });
     47     } else {
     48       process.start(shutdownHook);
     49     }
     50     return process;
     51   }
     52 
     53   public static ScriptProcess launchScript(File script, InterpreterConfiguration configuration,
     54       final AndroidProxy proxy, Runnable shutdownHook) {
     55     if (!script.exists()) {
     56       throw new RuntimeException("No such script to launch.");
     57     }
     58     ScriptProcess process = new ScriptProcess(script, configuration, proxy);
     59     if (shutdownHook == null) {
     60       process.start(new Runnable() {
     61         @Override
     62         public void run() {
     63           proxy.shutdown();
     64         }
     65       });
     66     } else {
     67       process.start(shutdownHook);
     68     }
     69     return process;
     70   }
     71 }
     72