Create BaseModule (for future merging with repo)

This commit is contained in:
topjohnwu 2016-09-19 11:47:52 +08:00
parent f0d3a4e4b7
commit c9f6e2e257
3 changed files with 124 additions and 111 deletions

View File

@ -0,0 +1,103 @@
package com.topjohnwu.magisk.module;
import android.util.Log;
import java.util.List;
public abstract class BaseModule {
protected String mId, mName, mVersion, mDescription, mSupportUrl, mDonateUrl, mAuthor;
protected boolean mIsCacheModule = false;
protected int mVersionCode = 0;
public BaseModule(List<String> props) {
this(props.toArray(new String[props.size()]));
}
public BaseModule(String[] props) {
for (String line : props) {
String[] prop = line.split("=", 2);
if (prop.length != 2) {
continue;
}
String key = prop[0].trim();
if (key.charAt(0) == '#') {
continue;
}
switch (key) {
case "id":
this.mId = prop[1];
break;
case "name":
this.mName = prop[1];
break;
case "version":
this.mVersion = prop[1];
break;
case "versionCode":
this.mVersionCode = Integer.parseInt(prop[1]);
break;
case "author":
this.mAuthor = prop[1];
break;
case "description":
this.mDescription = prop[1];
break;
case "support":
this.mSupportUrl = prop[1];
break;
case "donate":
this.mDonateUrl = prop[1];
break;
case "cacheModule":
this.mIsCacheModule = Boolean.parseBoolean(prop[1]);
break;
default:
Log.d("Magisk", "Module: Manifest string not recognized: " + prop[0]);
break;
}
}
}
public String getName() {
return mName;
}
public String getVersion() {
return mVersion;
}
public String getAuthor() {
return mAuthor;
}
public String getId() {return mId; }
public String getDescription() {
return mDescription;
}
public boolean isCache() {
return mIsCacheModule;
}
public void setCache() {
mIsCacheModule = true;
}
public int getVersionCode() {
return mVersionCode;
}
public String getmDonateUrl() {
return mDonateUrl;
}
public String getmSupportUrl() {
return mSupportUrl;
}
}

View File

@ -5,75 +5,39 @@ import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.utils.Utils;
public class Module {
public class Module extends BaseModule {
private String mRemoveFile;
private String mDisableFile;
private String mName = null;
private String mVersion = "(No version provided)";
private String mDescription = "(No description provided)";
private String mSupportUrl, mDonateUrl, mZipUrl, mAuthor, mLogUrl;
private boolean mEnable = false, mRemove = false, mUpdateAvailable = false, mIsInstalled,
mIsCacheModule = false;
private String mId;
private int mVersionCode;
private String mZipUrl, mLogUrl;
private boolean mEnable, mRemove, mUpdateAvailable = false, mIsInstalled;
public Module(String path, Context context) {
super(Utils.readFile(path + "/module.prop"));
mRemoveFile = path + "/remove";
mDisableFile = path + "/disable";
for (String line : Utils.readFile(path + "/module.prop")) {
String[] props = line.split("=", 2);
if (props.length != 2) {
continue;
}
String key = props[0].trim();
if (key.charAt(0) == '#') {
continue;
}
switch (props[0]) {
case "id":
this.mId = props[1];
break;
case "name":
this.mName = props[1];
break;
case "version":
this.mVersion = props[1];
break;
case "versionCode":
this.mVersionCode = Integer.parseInt(props[1]);
break;
case "author":
this.mAuthor = props[1];
break;
case "description":
this.mDescription = props[1];
break;
case "support":
this.mSupportUrl = props[1];
break;
case "donate":
this.mDonateUrl = props[1];
break;
case "cacheModule":
this.mIsCacheModule = Boolean.parseBoolean(props[1]);
break;
default:
Log.d("Magisk", "Module: Manifest string not recognized: " + props[0]);
break;
}
if (mId == null) {
int sep = path.lastIndexOf('/');
mId = path.substring(sep + 1);
}
Log.d("Magisk","Module: Loaded module with ID of " + this.mId + " or " + mId);
if (mName == null)
mName = mId;
if (mDescription == null)
mDescription = context.getString(R.string.no_info_provided);
if (mVersion == null)
mVersion = context.getString(R.string.no_info_provided);
Log.d("Magisk","Module: Loaded module with ID of " + mId );
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
@ -140,52 +104,13 @@ public class Module {
editor.apply();
}
if (mName == null) {
int sep = path.lastIndexOf('/');
mName = path.substring(sep + 1);
mId = mName;
}
mEnable = !Utils.fileExist(mDisableFile);
mRemove = Utils.fileExist(mRemoveFile);
}
// 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;
//
// }
public String getName() {
return mName;
}
public String getVersion() {
return mVersion;
}
public String getAuthor() {
return mAuthor;
}
public String getId() {return mId; }
public String getmLogUrl() {return mLogUrl; }
public String getDescription() {
return mDescription;
}
public void createDisableFile() {
mEnable = !Utils.createFile(mDisableFile);
}
@ -210,24 +135,8 @@ public class Module {
return mRemove;
}
public boolean isCache() {
return mIsCacheModule;
}
public void setCache() {
mIsCacheModule = true;
}
public String getmDonateUrl() {
return mDonateUrl;
}
public String getmZipUrl() { return mZipUrl; }
public String getmSupportUrl() {
return mSupportUrl;
}
public boolean isUpdateAvailable() { return mUpdateAvailable; }
}

View File

@ -40,6 +40,7 @@
<string name="selinux_samsung_info">Samsung need custom kernel for switching SELinux status!</string>
<!--Module Fragment-->
<string name="no_info_provided">(No info provided)</string>
<string name="cache_modules">Cache modules</string>
<string name="no_modules_found">No modules found</string>
<string name="module_update_available">An update is available!</string>