added 'add external plugin' button to IITC plugins preference. ensures that the scripts are always copied to the correct folder.

This commit is contained in:
Philipp Schaefer 2014-02-12 14:53:31 +01:00
parent 5e723ca328
commit 2054b5f790
8 changed files with 93 additions and 10 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 262 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 185 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 250 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 288 B

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_plugins_add"
android:icon="@drawable/ic_action_new"
android:orderInCategory="10"
android:showAsAction="ifRoom|collapseActionView"
android:title="@string/menu_plugins_add"/>
</menu>

View File

@ -86,9 +86,8 @@
<string name="notice_extplugins"> <string name="notice_extplugins">
<![CDATA[Hint:<br><br> <![CDATA[Hint:<br><br>
IITC Mobile is able to load external plugins too!<br><br> IITC Mobile is able to load external plugins too!<br><br>
• create <b>%1$s</b><br> Add them by clicking the (+) icon at the top right.
• move *.user.js files there<br> The plugin files have to end with \'.user.js\' and are copied to <b>%1$s</b><br>]]>
• plugins should be listed above the official plugins]]>
</string> </string>
<string name="notice_sharing"> <string name="notice_sharing">
<![CDATA[With <em>Share portal</em> you can:<br> <![CDATA[With <em>Share portal</em> you can:<br>
@ -162,6 +161,7 @@
<string name="menu_clear_cookies">Clear Cookies</string> <string name="menu_clear_cookies">Clear Cookies</string>
<string name="menu_search">Search</string> <string name="menu_search">Search</string>
<string name="menu_debug">Debug</string> <string name="menu_debug">Debug</string>
<string name="menu_plugins_add">Add external plugins</string>
<string name="choose_account_to_login">Choose account to login</string> <string name="choose_account_to_login">Choose account to login</string>
<string name="login_failed">Login failed.</string> <string name="login_failed">Login failed.</string>
<string name="search_hint">Search Locations</string> <string name="search_hint">Search Locations</string>

View File

@ -1,26 +1,32 @@
package com.cradle.iitc_mobile; package com.cradle.iitc_mobile;
import android.content.ActivityNotFoundException;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager; import android.content.res.AssetManager;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.ListAdapter; import android.widget.ListAdapter;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import com.cradle.iitc_mobile.fragments.PluginsFragment; import com.cradle.iitc_mobile.fragments.PluginsFragment;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -29,6 +35,8 @@ import java.util.TreeMap;
public class IITC_PluginPreferenceActivity extends PreferenceActivity { public class IITC_PluginPreferenceActivity extends PreferenceActivity {
private final static int COPY_PLUGIN_REQUEST = 1;
private List<Header> mHeaders; private List<Header> mHeaders;
// we use a tree map to have a map with alphabetical order // we use a tree map to have a map with alphabetical order
// don't initialize the asset plugin map, because it tells us if the settings are started the first time // don't initialize the asset plugin map, because it tells us if the settings are started the first time
@ -103,17 +111,75 @@ public class IITC_PluginPreferenceActivity extends PreferenceActivity {
} }
} }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.plugins, menu);
return super.onCreateOptionsMenu(menu);
}
@Override @Override
public boolean onOptionsItemSelected(final MenuItem item) { public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case android.R.id.home: // exit settings when home button (iitc icon) is pressed case android.R.id.home: // exit settings when home button (iitc icon) is pressed
onBackPressed(); onBackPressed();
return true; return true;
case R.id.menu_plugins_add:
// create the chooser Intent
final Intent target = new Intent(Intent.ACTION_GET_CONTENT);
// iitcm only parses *.user.js scripts
target.setType("file/*");
target.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(Intent.createChooser(target, "Choose file"), COPY_PLUGIN_REQUEST);
} catch (final ActivityNotFoundException e) {
Toast.makeText(this, "No activity to select a file found." +
"Please install a file browser of your choice!", Toast.LENGTH_LONG).show();
}
default: default:
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
} }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case COPY_PLUGIN_REQUEST:
if (data != null && data.getData() != null) {
String filePath = data.getData().getPath();
copyPlugin(filePath);
return;
}
break;
default:
super.onActivityResult(requestCode, resultCode, data);
break;
}
}
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 @Override
protected boolean isValidFragment(final String s) { protected boolean isValidFragment(final String s) {
return true; return true;
@ -140,10 +206,15 @@ public class IITC_PluginPreferenceActivity extends PreferenceActivity {
return asset_array; return asset_array;
} }
private File[] getUserPlugins() { private File getUserPluginsDirectory() {
final String iitc_path = Environment.getExternalStorageDirectory().getPath() final String iitc_path = Environment.getExternalStorageDirectory().getPath()
+ "/IITC_Mobile/"; + "/IITC_Mobile/";
final File directory = new File(iitc_path + "plugins/"); final File directory = new File(iitc_path + "plugins/");
return directory;
}
private File[] getUserPlugins() {
final File directory = getUserPluginsDirectory();
File[] files = directory.listFiles(); File[] files = directory.listFiles();
if (files == null) { if (files == null) {
files = new File[0]; files = new File[0];
@ -164,6 +235,8 @@ public class IITC_PluginPreferenceActivity extends PreferenceActivity {
if ((userPlugins.length + officialPlugins.length) != (numPlugins + mDeletedPlugins)) { if ((userPlugins.length + officialPlugins.length) != (numPlugins + mDeletedPlugins)) {
Log.d("new or less plugins found since last start, rebuild preferences"); Log.d("new or less plugins found since last start, rebuild preferences");
sAssetPlugins.clear(); sAssetPlugins.clear();
sUserPlugins.clear();
mDeletedPlugins = 0;
setUpPluginPreferenceScreen(); setUpPluginPreferenceScreen();
} }
} }

View File

@ -158,19 +158,17 @@ From here you can remove/disable individual plugins or IITC itself.
END END
), ),
'mobile-plugins' => Array ( "IITC Mobile: Is it possible to add other plugins to IITC Mobile?", 'mobile-plugins' => Array ( "IITC Mobile: Is it possible to add external plugins to IITC Mobile?",
<<<'END' <<<'END'
Yes it is! Yes it is!
<ul> <ul>
<li>Create a folder named "IITC_Mobile" in your home directory.</li> <li>Navigate to the IITC Plugins preference screen and click the (+) icon at the top right. You can select the script using a file explorer of your choice.</li>
<li>Inside this folder, create a new folder named "plugins".</li> <li>IITCm creates a new folder in your home directory, named "IITC_Mobile". Inside this folder you'll find a "plugins" folder where all external plugins are copied to.</li>
<li>Copy all your additional plugins to this folder.</li>
<li>You should see your plugins listed above the official plugins.</li>
</ul> </ul>
Note: Note:
<ul> <ul>
<li>The filename has to end with *.user.js.</li> <li>The filename has to end with *.user.js.</li>
<li>If you don't know where to find your home directory: Enable dev-mode in the settings and follow the hint.</li> <li>You need a file explorer app installed to add external plugins</li>
</ul> </ul>
END END
), ),