diff --git a/mobile/AndroidManifest.xml b/mobile/AndroidManifest.xml index 8472c725..f5a18f1b 100644 --- a/mobile/AndroidManifest.xml +++ b/mobile/AndroidManifest.xml @@ -72,6 +72,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + Base Layer Overlay Layers + Install external plugin? + + IITCm was requested to install the following plugin:\n\n%1$s\n\nDo you want to proceed? + + diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_FileManager.java b/mobile/src/com/cradle/iitc_mobile/IITC_FileManager.java index 6ec999eb..c66a0ccb 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_FileManager.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_FileManager.java @@ -1,7 +1,9 @@ package com.cradle.iitc_mobile; import android.app.Activity; +import android.app.AlertDialog; import android.content.ActivityNotFoundException; +import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.AssetManager; @@ -22,11 +24,14 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PipedInputStream; import java.io.PipedOutputStream; +import java.net.URL; +import java.net.URLConnection; import java.net.URLEncoder; import java.util.HashMap; @@ -118,15 +123,17 @@ public class IITC_FileManager { } private final AssetManager mAssetManager; - private final IITC_Mobile mIitc; + private final Activity mActivity; private final String mIitcPath; private final SharedPreferences mPrefs; + public static final String PLUGINS_PATH = Environment.getExternalStorageDirectory().getPath() + + "/IITC_Mobile/plugins/"; - public IITC_FileManager(final IITC_Mobile iitc) { - mIitc = iitc; - mIitcPath = Environment.getExternalStorageDirectory().getPath() + "/IITC_Mobile/"; - mPrefs = PreferenceManager.getDefaultSharedPreferences(iitc); - mAssetManager = mIitc.getAssets(); + public IITC_FileManager(final Activity activity) { + mActivity = activity; + mIitcPath = Environment.getExternalStorageDirectory().getPath() + "/Activity/"; + mPrefs = PreferenceManager.getDefaultSharedPreferences(activity); + mAssetManager = mActivity.getAssets(); } private InputStream getAssetFile(final String filename) throws IOException { @@ -135,10 +142,10 @@ public class IITC_FileManager { try { return new FileInputStream(file); } catch (final FileNotFoundException e) { - mIitc.runOnUiThread(new Runnable() { + mActivity.runOnUiThread(new Runnable() { @Override public void run() { - Toast.makeText(mIitc, "File " + mIitcPath + + Toast.makeText(mActivity, "File " + mIitcPath + "dev/" + filename + " not found. " + "Disable developer mode or add iitc files to the dev folder.", Toast.LENGTH_SHORT).show(); @@ -233,6 +240,73 @@ public class IITC_FileManager { return EMPTY; } + public void installPlugin(final String uri, final boolean invalidateHeaders) { + if (uri != null) { + AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mActivity); + + // set title + alertDialogBuilder.setTitle(mActivity.getString(R.string.install_dialog_top)); + + // set dialog message + String text = mActivity.getString(R.string.install_dialog_msg); + text = String.format(text, uri); + alertDialogBuilder + .setMessage(text) + .setCancelable(true) + .setPositiveButton("Yes", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + copyPlugin(uri, invalidateHeaders); + } + }) + .setNegativeButton("No", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.cancel(); + } + }); + + // create alert dialog + AlertDialog alertDialog = alertDialogBuilder.create(); + + // show it + alertDialog.show(); + } + } + + private void copyPlugin(final String uri, final boolean invalidateHeaders) { + Thread thread = new Thread(new Runnable() { + @Override + public void run() { + try { + final URL url = new URL(uri); + final URLConnection conn = url.openConnection(); + final String fileName = uri.substring( uri.lastIndexOf('/')+1, uri.length() ); + final InputStream is = conn.getInputStream(); + // create IITCm external plugins directory if it doesn't already exist + final File pluginsDirectory = new File(PLUGINS_PATH); + pluginsDirectory.mkdirs(); + + // create in and out streams and copy plugin + File outFile = new File(pluginsDirectory + "/" + fileName); + OutputStream os = new FileOutputStream(outFile); + IITC_FileManager.copyStream(is, os, true); + } catch (IOException e) { + Log.w(e); + } + } + }); + thread.start(); + if (invalidateHeaders) { + try { + thread.join(); + ((IITC_PluginPreferenceActivity) mActivity).invalidateHeaders(); + } catch (InterruptedException e) { + Log.w(e); + } + } + } + private class FileRequest extends WebResourceResponse implements ResponseHandler, Runnable { private Intent mData; private final String mFunctionName; @@ -258,16 +332,18 @@ public class IITC_FileManager { target.addCategory(Intent.CATEGORY_OPENABLE); try { - mIitc.startActivityForResult(Intent.createChooser(target, "Choose file"), this); + final IITC_Mobile iitc = (IITC_Mobile) mActivity; + iitc.startActivityForResult(Intent.createChooser(target, "Choose file"), this); } catch (final ActivityNotFoundException e) { - Toast.makeText(mIitc, "No activity to select a file found." + + Toast.makeText(mActivity, "No activity to select a file found." + "Please install a file browser of your choice!", Toast.LENGTH_LONG).show(); } } @Override public void onActivityResult(final int resultCode, final Intent data) { - mIitc.deleteResponseHandler(this); // to enable garbage collection + final IITC_Mobile iitc = (IITC_Mobile) mActivity; + iitc.deleteResponseHandler(this); // to enable garbage collection mResultCode = resultCode; mData = data; diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java b/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java index 8c619247..2cea8b6a 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java @@ -228,6 +228,13 @@ public class IITC_Mobile extends Activity .show(); } } + + if (uri.getPath().endsWith(".user.js")) { + final Intent prefIntent = new Intent(this, IITC_PluginPreferenceActivity.class); + prefIntent.putExtra("url", uri.toString()); + startActivity(prefIntent); + // TODO receive intent, start dialog if user want to install $plugin, reload IITC + } } if (Intent.ACTION_SEARCH.equals(action)) { diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_PluginPreferenceActivity.java b/mobile/src/com/cradle/iitc_mobile/IITC_PluginPreferenceActivity.java index b3157a01..f588c272 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_PluginPreferenceActivity.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_PluginPreferenceActivity.java @@ -5,7 +5,6 @@ import android.content.Context; import android.content.Intent; import android.content.res.AssetManager; import android.os.Bundle; -import android.os.Environment; import android.preference.PreferenceActivity; import android.text.TextUtils; import android.view.LayoutInflater; @@ -23,10 +22,8 @@ import com.cradle.iitc_mobile.fragments.PluginsFragment; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -47,6 +44,8 @@ public class IITC_PluginPreferenceActivity extends PreferenceActivity { new TreeMap>(); private static int mDeletedPlugins = 0; + private IITC_FileManager mFileManager; + @Override public void setListAdapter(final ListAdapter adapter) { if (adapter == null) { @@ -86,6 +85,13 @@ public class IITC_PluginPreferenceActivity extends PreferenceActivity { getIntent() .putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT, PluginsFragment.class.getName()); } + + mFileManager = new IITC_FileManager(this); + + final String uri = getIntent().getStringExtra("url"); + if (uri != null) { + mFileManager.installPlugin(uri, true); + } super.onCreate(savedInstanceState); } @@ -147,7 +153,7 @@ public class IITC_PluginPreferenceActivity extends PreferenceActivity { case COPY_PLUGIN_REQUEST: if (data != null && data.getData() != null) { String filePath = data.getData().getPath(); - copyPlugin(filePath); + mFileManager.installPlugin("file://" + filePath, true); return; } break; @@ -158,28 +164,6 @@ public class IITC_PluginPreferenceActivity extends PreferenceActivity { } } - private void copyPlugin(String pluginPath) { - try { - File inFile = new File(pluginPath); - // create IITCm external plugins directory if it doesn't already exist - File pluginsDirectory = getUserPluginsDirectory(); - pluginsDirectory.mkdirs(); - - // create in and out streams and copy plugin - File outFile = new File(pluginsDirectory.getPath() + "/" + inFile.getName()); - InputStream is = new FileInputStream(inFile); - OutputStream os = new FileOutputStream(outFile); - IITC_FileManager.copyStream(is, os, true); - - // invalidate headers to build a fresh preference screen with the new plugin listed - invalidateHeaders(); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - } - @Override protected boolean isValidFragment(final String s) { return true; @@ -206,15 +190,8 @@ public class IITC_PluginPreferenceActivity extends PreferenceActivity { return asset_array; } - private File getUserPluginsDirectory() { - final String iitc_path = Environment.getExternalStorageDirectory().getPath() - + "/IITC_Mobile/"; - final File directory = new File(iitc_path + "plugins/"); - return directory; - } - private File[] getUserPlugins() { - final File directory = getUserPluginsDirectory(); + final File directory = new File(IITC_FileManager.PLUGINS_PATH); File[] files = directory.listFiles(); if (files == null) { files = new File[0];