Magisk/app/src/main/java/com/topjohnwu/magisk/module/Module.java

200 lines
6.0 KiB
Java
Raw Normal View History

2016-08-22 23:18:28 +02:00
package com.topjohnwu.magisk.module;
2016-08-17 13:00:55 +02:00
2016-09-02 20:18:37 +02:00
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
2016-08-22 23:18:28 +02:00
import com.topjohnwu.magisk.utils.Utils;
2016-09-02 20:18:37 +02:00
import java.util.Map;
2016-08-17 13:00:55 +02:00
public class Module {
private String mRemoveFile;
private String mDisableFile;
2016-08-17 13:00:55 +02:00
2016-08-29 00:35:07 +02:00
private String mName = null;
private String mVersion = "(No version provided)";
private String mDescription = "(No description provided)";
2016-09-06 23:54:08 +02:00
private String mUrl,mSupportUrl,mDonateUrl,mZipUrl,mBaseUrl,mManifestUrl,mAuthor;
private boolean mEnable, mRemove,mUpdateAvailable,mIsOnline;
2016-08-17 13:00:55 +02:00
2016-08-29 00:35:07 +02:00
private String mId;
private int mVersionCode;
2016-09-02 20:18:37 +02:00
public Module(String path, Context context) {
mRemoveFile = path + "/remove";
mDisableFile = path + "/disable";
for (String line : Utils.readFile(path + "/module.prop")) {
2016-09-06 23:54:08 +02:00
String[] props = line.split("=", 2);
if (props.length != 2) {
continue;
}
2016-09-06 23:54:08 +02:00
String key = props[0].trim();
if (key.charAt(0) == '#') {
continue;
}
2016-09-06 23:54:08 +02:00
String value = props[1].trim();
switch (props[0]) {
case "versionCode":
this.mVersionCode = Integer.valueOf(props[1]);
break;
case "name":
2016-09-06 23:54:08 +02:00
this.mName = props[1];
break;
case "author":
this.mAuthor = props[1];
break;
case "id":
this.mId = props[1];
break;
case "version":
2016-09-06 23:54:08 +02:00
this.mVersion = props[1];
break;
case "description":
2016-09-06 23:54:08 +02:00
this.mDescription = props[1];
break;
2016-09-06 23:54:08 +02:00
case "donate":
this.mDonateUrl = props[1];
2016-08-29 00:35:07 +02:00
break;
2016-09-06 23:54:08 +02:00
case "support":
this.mSupportUrl = props[1];
break;
case "donateUrl":
this.mDonateUrl = props[1];
break;
case "zipUrl":
this.mZipUrl = props[1];
2016-08-29 00:35:07 +02:00
break;
2016-09-06 23:54:08 +02:00
case "baseUrl":
this.mBaseUrl = props[1];
break;
case "manifestUrl":
this.mManifestUrl = props[1];
break;
default:
Log.d("Magisk", "Manifest string not recognized: " + props[0]);
break;
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
if (mId != null) {
Log.d("Magisk", "Module: Checking for preference named repo_" + mId);
if (prefs.contains("repo_" + mId)) {
String entryString = prefs.getString("repo_" + mId, "");
String entryName = "repo" + mId;
String[] subStrings = entryString.split("\n");
for (String subKeys : subStrings) {
String[] idEntry = subKeys.split("=", 2);
Log.d("Magisk", "Module: Checking entry strings. Key is " + idEntry[0] + " and value is " + idEntry[1]);
if (idEntry[0].equals("id")) {
if (idEntry.length != 2) {
continue;
}
if (idEntry[1].equals(mId)) {
Log.d("Magisk", "Module: Hey, I know I'm online...");
mIsOnline = true;
} else mIsOnline = false;
}
if (idEntry[0].equals("versionCode")) {
if (idEntry.length != 2) {
continue;
}
if (Integer.valueOf(idEntry[1]) > mVersionCode) {
mUpdateAvailable = true;
Log.d("Magisk", "Module: Hey, I have an update...");
} else mUpdateAvailable = false;
}
}
}
SharedPreferences.Editor editor = prefs.edit();
if (mIsOnline) {
editor.putBoolean("repo_isInstalled_" + mId, true);
} else {
editor.putBoolean("repo_isInstalled_" + mId, false);
}
editor.apply();
}
}
2016-08-29 00:35:07 +02:00
if (mName == null) {
int sep = path.lastIndexOf('/');
mName = path.substring(sep + 1);
mId = mName;
}
mEnable = !Utils.fileExist(mDisableFile);
mRemove = Utils.fileExist(mRemoveFile);
2016-08-17 13:00:55 +02:00
}
public Module(Repo repo) {
2016-09-01 23:58:26 +02:00
mName = repo.getName();
2016-09-06 23:54:08 +02:00
mVersion = repo.getmVersion();
mDescription = repo.getDescription();
mId = "foo";
mVersionCode = 111;
2016-09-06 23:54:08 +02:00
mUrl = repo.getmZipUrl();
2016-09-01 23:58:26 +02:00
mEnable = true;
mRemove = false;
}
2016-08-17 13:00:55 +02:00
public String getName() {
return mName;
}
public String getVersion() {
return mVersion;
}
public String getDescription() {
return mDescription;
}
public void createDisableFile() {
mEnable = !Utils.createFile(mDisableFile);
}
2016-08-22 19:44:34 +02:00
public void removeDisableFile() {
mEnable = Utils.removeFile(mDisableFile);
}
public boolean isEnabled() {
return mEnable;
}
public void createRemoveFile() {
mRemove = Utils.createFile(mRemoveFile);
}
2016-08-22 19:44:34 +02:00
public void deleteRemoveFile() {
mRemove = !Utils.removeFile(mRemoveFile);
}
public boolean willBeRemoved() {
return mRemove;
}
2016-09-06 23:54:08 +02:00
public boolean isOnline() {return mIsOnline; }
public boolean isUpdateAvailable() { return mUpdateAvailable; };
2016-08-17 13:00:55 +02:00
}