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.interpreter;
18 
19 import android.content.Context;
20 
21 import java.io.File;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 
27 /**
28  * A description of the interpreters hosted by the SL4A project.
29  *
30  * @author Damon Kohler (damonkohler@gmail.com)
31  * @author Alexey Reznichenko (alexey.reznichenko@gmail.com)
32  */
33 public abstract class Sl4aHostedInterpreter implements InterpreterDescriptor {
34 
35   public static final String BASE_INSTALL_URL = "http://android-scripting.googlecode.com/files/";
36   public static final String DALVIKVM = "/system/bin/dalvikvm";
37 
38   // TODO(damonkohler): Remove getVersion() and pull these three version methods up in to the base
39   // class.
40 
getBaseInstallUrl()41   public String getBaseInstallUrl() {
42     return BASE_INSTALL_URL;
43   }
44 
getInterpreterVersion()45   public int getInterpreterVersion() {
46     return getVersion();
47   }
48 
getExtrasVersion()49   public int getExtrasVersion() {
50     return getVersion();
51   }
52 
getScriptsVersion()53   public int getScriptsVersion() {
54     return getVersion();
55   }
56 
57   @Override
getInterpreterArchiveName()58   public String getInterpreterArchiveName() {
59     return String.format("%s_r%s.zip", getName(), getInterpreterVersion());
60   }
61 
62   @Override
getExtrasArchiveName()63   public String getExtrasArchiveName() {
64     return String.format("%s_extras_r%s.zip", getName(), getExtrasVersion());
65   }
66 
67   @Override
getScriptsArchiveName()68   public String getScriptsArchiveName() {
69     return String.format("%s_scripts_r%s.zip", getName(), getScriptsVersion());
70   }
71 
72   @Override
getInterpreterArchiveUrl()73   public String getInterpreterArchiveUrl() {
74     return getBaseInstallUrl() + getInterpreterArchiveName();
75   }
76 
77   @Override
getExtrasArchiveUrl()78   public String getExtrasArchiveUrl() {
79     return getBaseInstallUrl() + getExtrasArchiveName();
80   }
81 
82   @Override
getScriptsArchiveUrl()83   public String getScriptsArchiveUrl() {
84     return getBaseInstallUrl() + getScriptsArchiveName();
85   }
86 
87   @Override
getInteractiveCommand(Context context)88   public String getInteractiveCommand(Context context) {
89     return "";
90   }
91 
92   @Override
hasInteractiveMode()93   public boolean hasInteractiveMode() {
94     return true;
95   }
96 
97   @Override
getScriptCommand(Context context)98   public String getScriptCommand(Context context) {
99     return "%s";
100   }
101 
102   @Override
getArguments(Context context)103   public List<String> getArguments(Context context) {
104     return new ArrayList<String>();
105   }
106 
107   @Override
getEnvironmentVariables(Context context)108   public Map<String, String> getEnvironmentVariables(Context context) {
109     return new HashMap<String, String>();
110   }
111 
112   // TODO(damonkohler): This shouldn't be public.
getExtrasPath(Context context)113   public File getExtrasPath(Context context) {
114     if (!hasInterpreterArchive() && hasExtrasArchive()) {
115       return new File(InterpreterConstants.SDCARD_ROOT + this.getClass().getPackage().getName()
116           + InterpreterConstants.INTERPRETER_EXTRAS_ROOT, getName());
117     }
118     return InterpreterUtils.getInterpreterRoot(context, getName());
119   }
120 }
121