Skip to content

Commit

Permalink
Merge pull request #650 from xscreach/feature/Remove-custom-plugins-f…
Browse files Browse the repository at this point in the history
…rom-IITC-Mobile
  • Loading branch information
modos189 authored Jul 25, 2023
2 parents 185c7b4 + bb2c168 commit 0045460
Show file tree
Hide file tree
Showing 9 changed files with 285 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import org.exarhteam.iitc_mobile.IITC_Mobile.ResponseHandler;
import org.exarhteam.iitc_mobile.async.UpdateScript;
import org.exarhteam.iitc_mobile.prefs.PluginInfo;
import org.exarhteam.iitc_mobile.prefs.PluginPreferenceActivity;
import org.json.JSONObject;

Expand All @@ -44,7 +45,6 @@
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -90,38 +90,29 @@ public static void copyStream(final InputStream inStream, final OutputStream out
}
}

public static HashMap<String, String> getScriptInfo(final String js) {
final HashMap<String, String> map = new HashMap<String, String>();
public static PluginInfo getScriptInfo(final String js) {
final PluginInfo info = new PluginInfo();
String header = "";
// get metadata of javascript file
if (js != null && js.contains("==UserScript==") && js.contains("==/UserScript==")) {
header = js.substring(js.indexOf("==UserScript=="),
js.indexOf("==/UserScript=="));
}
// add default values
map.put("id", "unknown");
map.put("version", "not found");
map.put("name", "unknown");
map.put("description", "");
map.put("category", "Misc");

final BufferedReader reader = new BufferedReader(new StringReader(header));
try {
final Pattern p = Pattern.compile("^\\s*//\\s*@(\\S+)(.*)$");
String headerLine;
while ((headerLine = reader.readLine()) != null) {
final Matcher m = p.matcher(headerLine);
if (m.matches()) {
map.put(m.group(1), m.group(2).trim());
info.put(m.group(1), m.group(2).trim());
}
}
} catch (final IOException e) {
Log.w(e);
}
return map;
}

public static String readFile(final File file) throws IOException {
return readStream(new FileInputStream(file));
return info;
}

public static String readStream(final InputStream stream) {
Expand All @@ -138,26 +129,24 @@ public static String readStream(final InputStream stream) {

private final AssetManager mAssetManager;
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 static final String IITC_PATH = Environment.getExternalStorageDirectory().getPath() + "/IITC_Mobile/";
public static final String IITC_DEV_PATH = IITC_PATH + "dev/";
public static final String USER_PLUGINS_PATH = IITC_PATH + "plugins/";

public IITC_FileManager(final Activity activity) {
mActivity = activity;
mIitcPath = Environment.getExternalStorageDirectory().getPath() + "/IITC_Mobile/";
mPrefs = PreferenceManager.getDefaultSharedPreferences(activity);
mAssetManager = mActivity.getAssets();
}

private InputStream getAssetFile(final String filename) throws IOException {
public InputStream getAssetFile(final String filename) throws IOException {
if (mPrefs.getBoolean("pref_dev_checkbox", false)) {
final File file = new File(mIitcPath + "dev/" + filename);
final File file = new File(IITC_DEV_PATH + filename);
try {
return new FileInputStream(file);
} catch (final FileNotFoundException e) {
mActivity.runOnUiThread(() -> Toast.makeText(mActivity, "File " + mIitcPath +
"dev/" + filename + " not found. " +
mActivity.runOnUiThread(() -> Toast.makeText(mActivity, "File " + IITC_DEV_PATH + filename + " not found. " +
"Disable developer mode or add iitc files to the dev folder.",
Toast.LENGTH_SHORT).show());
Log.w(e);
Expand Down Expand Up @@ -186,7 +175,7 @@ private WebResourceResponse getScript(final Uri uri) {
return new WebResourceResponse("application/javascript", "UTF-8", data);
}

private HashMap<String, String> getScriptInfo(final InputStream stream) {
private PluginInfo getScriptInfo(final InputStream stream) {
return getScriptInfo(readStream(stream));
}

Expand All @@ -211,7 +200,7 @@ private WebResourceResponse getUserPlugin(final Uri uri) {

private InputStream prepareUserScript(final InputStream stream) {
String content = readStream(stream);
final HashMap<String, String> info = getScriptInfo(content);
final PluginInfo info = getScriptInfo(content);

final JSONObject jObject = new JSONObject(info);
final String gmInfo = "var GM_info={\"script\":" + jObject.toString() + "};\n";
Expand All @@ -228,7 +217,7 @@ public String getFileRequestPrefix() {
public String getIITCVersion() throws IOException {
final InputStream stream = getAssetFile("total-conversion-build.user.js");

return getScriptInfo(stream).get("version");
return getScriptInfo(stream).getVersion();
}

public WebResourceResponse getResponse(final Uri uri) {
Expand Down Expand Up @@ -295,10 +284,10 @@ public void run() {
// afterwards reading it again while copying
is = mActivity.getContentResolver().openInputStream(uri);
final InputStream isCopy = mActivity.getContentResolver().openInputStream(uri);
fileName = getScriptInfo(isCopy).get("id") + ".user.js";
fileName = getScriptInfo(isCopy).getId() + ".user.js";
}
// create IITCm external plugins directory if it doesn't already exist
final File pluginsDirectory = new File(PLUGINS_PATH);
final File pluginsDirectory = new File(USER_PLUGINS_PATH);
pluginsDirectory.mkdirs();

// create in and out streams and copy plugin
Expand Down Expand Up @@ -338,7 +327,7 @@ public void updatePlugins(final boolean force) {
for (final Map.Entry<String, ?> entry : all_prefs.entrySet()) {
final String plugin = entry.getKey();
if (plugin.endsWith(".user.js") && entry.getValue().toString().equals("true")) {
if (plugin.startsWith(PLUGINS_PATH)) {
if (plugin.startsWith(USER_PLUGINS_PATH)) {
new UpdateScript(new ScriptUpdatedCallback(), forceSecureUpdates).execute(plugin);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.exarhteam.iitc_mobile.prefs.PluginInfo;

public class UpdateScript extends AsyncTask<String, Void, Boolean> {

Expand All @@ -41,11 +41,11 @@ protected Boolean doInBackground(final String... urls) {
final String filePath = urls[0];
// get local script meta information
final String script = IITC_FileManager.readStream(new FileInputStream(new File(filePath)));
final HashMap<String, String> mScriptInfo = IITC_FileManager.getScriptInfo(script);
final PluginInfo mScriptInfo = IITC_FileManager.getScriptInfo(script);

mScriptName = mScriptInfo.get("name");
String updateURL = mScriptInfo.get("updateURL");
String downloadURL = mScriptInfo.get("downloadURL");
mScriptName = mScriptInfo.getName();
String updateURL = mScriptInfo.getUpdateURL();
String downloadURL = mScriptInfo.getDownloadURL();
if (updateURL == null) updateURL = downloadURL;
if (updateURL == null) return false;
if (!isUpdateAllowed(updateURL)) return false;
Expand All @@ -54,13 +54,12 @@ protected Boolean doInBackground(final String... urls) {
if (updateMetaScript == null) {
return false;
}
final HashMap<String, String> updateInfo =
IITC_FileManager.getScriptInfo(updateMetaScript);
final PluginInfo updateInfo = IITC_FileManager.getScriptInfo(updateMetaScript);

final String remote_version = updateInfo.get("version");
final String remote_version = updateInfo.getVersion();

final File local_file = new File(filePath);
final String local_version = mScriptInfo.get("version");
final String local_version = mScriptInfo.getVersion();

if (local_version.compareTo(remote_version) >= 0) return false;

Expand All @@ -70,8 +69,8 @@ protected Boolean doInBackground(final String... urls) {
if (updateURL.equals(downloadURL)) {
updatedScript = updateMetaScript;
} else {
if (updateInfo.get("downloadURL") != null) {
downloadURL = updateInfo.get("downloadURL");
if (updateInfo.getDownloadURL() != null) {
downloadURL = updateInfo.getDownloadURL();
}

if (!isUpdateAllowed(downloadURL)) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import android.app.ActionBar;
import android.os.Bundle;
import android.preference.PreferenceFragment;

import android.preference.PreferenceScreen;
import android.widget.Toast;
import org.exarhteam.iitc_mobile.R;
import org.exarhteam.iitc_mobile.prefs.PluginInfo;
import org.exarhteam.iitc_mobile.prefs.PluginPreference;
import org.exarhteam.iitc_mobile.prefs.PluginPreferenceActivity;

import java.io.File;
import java.util.ArrayList;

public class PluginsFragment extends PreferenceFragment {
Expand All @@ -26,12 +29,13 @@ public void onCreate(Bundle savedInstanceState) {
// get plugins category for this fragments and plugins list
String category = getArguments().getString("category");
boolean userPlugin = getArguments().getBoolean("userPlugin");
ArrayList<PluginPreference> prefs =
PluginPreferenceActivity.getPluginPreference(category, userPlugin);
ArrayList<PluginInfo> prefs =
PluginPreferenceActivity.getPluginInfo(category, userPlugin);

// add plugin checkbox preferences
for (PluginPreference pref : prefs) {
getPreferenceScreen().addPreference(pref);
PreferenceScreen preferenceScreen = getPreferenceScreen();
for (PluginInfo pluginInfo : prefs) {
preferenceScreen.addPreference(createPluginPreference(prefs, preferenceScreen, pluginInfo));
}

// set action bar stuff
Expand All @@ -41,4 +45,25 @@ public void onCreate(Bundle savedInstanceState) {
}
}

private PluginPreference createPluginPreference(ArrayList<PluginInfo> prefs, PreferenceScreen preferenceScreen, PluginInfo pluginInfo) {
final PluginPreference preference = new PluginPreference(getActivity());
preference.setDefaultValue(false);
preference.setPersistent(true);
preference.setPluginInfo(pluginInfo);

if (pluginInfo.isUserPlugin()) {
preference.setOnConfirmDelete(() -> {
deletePlugin(pluginInfo);
Toast.makeText(getActivity(), getString(R.string.plugin_deleted, pluginInfo.getName()), Toast.LENGTH_SHORT).show();
prefs.remove(pluginInfo);
preferenceScreen.removePreference(preference);
});
}
return preference;
}

private void deletePlugin(PluginInfo pluginInfo) {
new File(pluginInfo.getKey()).delete();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package org.exarhteam.iitc_mobile.prefs;

import java.util.HashMap;

public class PluginInfo extends HashMap<String, String> {

public static final String KEY_CATEGORY = "category";
public static final String KEY_DESCRIPTION = "description";
public static final String KEY_NAME = "name";
public static final String KEY_VERSION = "version";
public static final String KEY_ID = "id";
public static final String KEY_UPDATE_URL = "updateURL";
public static final String KEY_DOWNLOAD_URL = "downloadURL";

private String key;
private boolean userPlugin;

public PluginInfo() {
super();
this.setId("unknown");
this.setVersion("not found");
this.setName("unknown");
this.setDescription("");
this.setCategory("Misc");
}

public void setId(String id) {
this.put(KEY_ID, id);
}

public void setVersion(String version) {
this.put(KEY_VERSION, version);
}

public void setName(String name) {
if (name != null) {
name = name.replace("IITC Plugin: ", "");
name = name.replace("IITC plugin: ", "");
}
this.put(KEY_NAME, name);
}

public void setDescription(String description) {
this.put(KEY_DESCRIPTION, description);
}

public void setCategory(String category) {
this.put(KEY_CATEGORY, category);
}

public String getVersion() {
return this.get(KEY_VERSION);
}

public String getId() {
return this.get(KEY_ID);
}

public String getName() {
String name = this.get(KEY_NAME);
if (name != null) {
name = name.replace("IITC Plugin: ", "");
name = name.replace("IITC plugin: ", "");
}
return name;
}

public String getUpdateURL() {
return this.get(KEY_UPDATE_URL);
}

public String getDownloadURL() {
return this.get(KEY_DOWNLOAD_URL);
}

public String getCategory() {
return this.get(KEY_CATEGORY);
}

public String getDescription() {
return this.get(KEY_DESCRIPTION);
}

public void setKey(String key) {
this.key = key;
}

public String getKey() {
return this.key;
}

public void setUserPlugin(boolean userPlugin) {
this.userPlugin = userPlugin;
}

public boolean isUserPlugin() {
return userPlugin;
}
}
Loading

0 comments on commit 0045460

Please sign in to comment.