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

233 lines
6.8 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-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-18 16:56:12 +02:00
private String mSupportUrl, mDonateUrl, mZipUrl, mAuthor, mLogUrl;
private boolean mEnable = false, mRemove = false, mUpdateAvailable = false, mIsInstalled,
mIsCacheModule = false;
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
switch (props[0]) {
2016-09-18 16:56:12 +02:00
case "id":
this.mId = props[1];
2016-09-06 23:54:08 +02:00
break;
case "name":
this.mName = props[1];
2016-09-06 23:54:08 +02:00
break;
case "version":
this.mVersion = props[1];
break;
2016-09-18 16:56:12 +02:00
case "versionCode":
this.mVersionCode = Integer.parseInt(props[1]);
break;
2016-09-18 16:56:12 +02:00
case "author":
this.mAuthor = props[1];
break;
2016-09-18 16:56:12 +02:00
case "description":
this.mDescription = 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];
2016-09-06 23:54:08 +02:00
break;
2016-09-18 16:56:12 +02:00
case "donate":
this.mDonateUrl = props[1];
2016-09-06 23:54:08 +02:00
break;
2016-09-18 16:56:12 +02:00
case "cacheModule":
this.mIsCacheModule = Boolean.parseBoolean(props[1]);
2016-09-06 23:54:08 +02:00
break;
default:
2016-09-13 22:44:07 +02:00
Log.d("Magisk", "Module: Manifest string not recognized: " + props[0]);
2016-09-06 23:54:08 +02:00
break;
}
2016-09-08 21:47:04 +02:00
}
2016-09-13 22:44:07 +02:00
Log.d("Magisk","Module: Loaded module with ID of " + this.mId + " or " + mId);
2016-09-06 23:54:08 +02:00
2016-09-08 21:47:04 +02:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
if (this.mId != null && !this.mId.isEmpty()) {
String preferenceString = "repo_" + this.mId;
String preferenceKey = prefs.getString(preferenceString,"nope");
Log.d("Magisk", "Module: Checking for preference named " + preferenceString);
if (!preferenceKey.equals("nope")) {
Log.d("Magisk", "Module: repo_" + mId + " found.");
String entryString = prefs.getString("repo_" + mId, "");
String[] subStrings = entryString.split("\n");
for (String subKeys : subStrings) {
String[] idEntry = subKeys.split("=", 2);
if (idEntry[0].equals("id")) {
if (idEntry.length != 2) {
continue;
2016-09-06 23:54:08 +02:00
}
2016-09-08 21:47:04 +02:00
if (idEntry[1].equals(mId)) {
2016-09-13 22:44:07 +02:00
Log.d("Magisk", "Module: Hey, I know " + mId + " is online...");
mIsInstalled = true;
} else mIsInstalled = false;
2016-09-08 21:47:04 +02:00
}
if (idEntry[0].equals("logUrl")) {
mLogUrl = idEntry[1];
}
if (idEntry[0].equals("support")) {
mSupportUrl = idEntry[1];
}
if (idEntry[0].equals("zipUrl")) {
mZipUrl = idEntry[1];
}
if (idEntry[0].equals("donate")) {
mDonateUrl = idEntry[1];
}
2016-09-08 21:47:04 +02:00
if (idEntry[0].equals("versionCode")) {
if (idEntry.length != 2) {
continue;
2016-09-06 23:54:08 +02:00
}
2016-09-08 21:47:04 +02:00
if (Integer.valueOf(idEntry[1]) > mVersionCode) {
mUpdateAvailable = true;
Log.d("Magisk", "Module: Hey, I have an update...");
} else mUpdateAvailable = false;
2016-09-06 23:54:08 +02:00
}
2016-09-08 21:47:04 +02:00
}
2016-09-06 23:54:08 +02:00
2016-09-08 21:47:04 +02:00
}
2016-09-06 23:54:08 +02:00
2016-09-08 21:47:04 +02:00
SharedPreferences.Editor editor = prefs.edit();
2016-09-13 22:44:07 +02:00
if (mIsInstalled) {
2016-09-08 21:47:04 +02:00
editor.putBoolean("repo-isInstalled_" + mId, true);
} else {
editor.putBoolean("repo-isInstalled_" + mId, false);
}
if (mUpdateAvailable) {
editor.putBoolean("repo-canUpdate_" + mId, true);
} else {
editor.putBoolean("repo-canUpdate_" + mId, false);
}
2016-09-08 21:47:04 +02:00
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
}
2016-09-18 16:56:12 +02:00
// public Module(Repo repo) {
//
// mName = repo.getName();
// mVersion = repo.getmVersion();
// mDescription = repo.getDescription();
// mId = repo.getId();
// mVersionCode = repo.getmVersionCode();
// mUrl = repo.getmZipUrl();
// mEnable = true;
// mRemove = false;
//
// }
2016-09-01 23:58:26 +02:00
2016-08-17 13:00:55 +02:00
public String getName() {
return mName;
}
public String getVersion() {
return mVersion;
}
public String getAuthor() {
return mAuthor;
}
public String getId() {return mId; }
2016-09-13 22:44:07 +02:00
public String getmLogUrl() {return mLogUrl; }
2016-08-17 13:00:55 +02:00
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-18 16:56:12 +02:00
public boolean isCache() {
return mIsCacheModule;
}
public void setCache() {
mIsCacheModule = true;
}
public String getmDonateUrl() {
return mDonateUrl;
}
2016-09-13 22:44:07 +02:00
public String getmZipUrl() { return mZipUrl; }
public String getmSupportUrl() {
return mSupportUrl;
}
public boolean isUpdateAvailable() { return mUpdateAvailable; }
2016-09-06 23:54:08 +02:00
2016-08-17 13:00:55 +02:00
}