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