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.Context;
     20 
     21 import java.net.MalformedURLException;
     22 import java.net.URL;
     23 import java.util.ArrayList;
     24 import java.util.Collections;
     25 import java.util.HashMap;
     26 import java.util.List;
     27 import java.util.Map;
     28 
     29 public class FeaturedInterpreters {
     30   private static final Map<String, FeaturedInterpreter> mNameMap =
     31       new HashMap<String, FeaturedInterpreter>();
     32   private static final Map<String, FeaturedInterpreter> mExtensionMap =
     33       new HashMap<String, FeaturedInterpreter>();
     34 
     35   static {
     36     try {
     37       FeaturedInterpreter interpreters[] =
     38           {
     39             new FeaturedInterpreter("BeanShell 2.0b4", ".bsh",
     40                 "http://android-scripting.googlecode.com/files/beanshell_for_android_r2.apk"),
     41             new FeaturedInterpreter("JRuby", ".rb",
     42                 "https://github.com/downloads/ruboto/sl4a_jruby_interpreter/JRubyForAndroid_r2dev.apk"),
     43             new FeaturedInterpreter("Lua 5.1.4", ".lua",
     44                 "http://android-scripting.googlecode.com/files/lua_for_android_r1.apk"),
     45             new FeaturedInterpreter("Perl 5.10.1", ".pl",
     46                 "http://android-scripting.googlecode.com/files/perl_for_android_r1.apk"),
     47             new FeaturedInterpreter("Python 2.6.2", ".py",
     48                 "http://python-for-android.googlecode.com/files/PythonForAndroid_r5.apk"),
     49             new FeaturedInterpreter("Rhino 1.7R2", ".js",
     50                 "http://android-scripting.googlecode.com/files/rhino_for_android_r2.apk"),
     51             new FeaturedInterpreter("PHP 5.3.3", ".php",
     52                 "http://php-for-android.googlecode.com/files/phpforandroid_r1.apk") };
     53       for (FeaturedInterpreter interpreter : interpreters) {
     54         mNameMap.put(interpreter.mmName, interpreter);
     55         mExtensionMap.put(interpreter.mmExtension, interpreter);
     56       }
     57     } catch (MalformedURLException e) {
     58       Log.e(e);
     59     }
     60   }
     61 
     62   public static List<String> getList() {
     63     ArrayList<String> list = new ArrayList<String>(mNameMap.keySet());
     64     Collections.sort(list);
     65     return list;
     66   }
     67 
     68   public static URL getUrlForName(String name) {
     69     if (!mNameMap.containsKey(name)) {
     70       return null;
     71     }
     72     return mNameMap.get(name).mmUrl;
     73   }
     74 
     75   public static String getInterpreterNameForScript(String fileName) {
     76     String extension = getExtension(fileName);
     77     if (extension == null || !mExtensionMap.containsKey(extension)) {
     78       return null;
     79     }
     80     return mExtensionMap.get(extension).mmName;
     81   }
     82 
     83   public static boolean isSupported(String fileName) {
     84     String extension = getExtension(fileName);
     85     return (extension != null) && (mExtensionMap.containsKey(extension));
     86   }
     87 
     88   public static int getInterpreterIcon(Context context, String key) {
     89     String packageName = context.getPackageName();
     90     String name = "_icon";
     91     if (key.contains(".")) {
     92       name = key.substring(key.lastIndexOf('.') + 1) + name;
     93     } else {
     94       name = key + name;
     95     }
     96     return context.getResources().getIdentifier(name, "drawable", packageName);
     97   }
     98 
     99   private static String getExtension(String fileName) {
    100     int dotIndex = fileName.lastIndexOf('.');
    101     if (dotIndex == -1) {
    102       return null;
    103     }
    104     return fileName.substring(dotIndex);
    105   }
    106 
    107   private static class FeaturedInterpreter {
    108     private final String mmName;
    109     private final String mmExtension;
    110     private final URL mmUrl;
    111 
    112     private FeaturedInterpreter(String name, String extension, String url)
    113         throws MalformedURLException {
    114       mmName = name;
    115       mmExtension = extension;
    116       mmUrl = new URL(url);
    117     }
    118   }
    119 
    120 }
    121