275 lines
8.3 KiB
Java
Raw Normal View History

package com.topjohnwu.magisk.module;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
2016-09-08 15:47:10 -05:00
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.magisk.utils.WebRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
2016-09-02 13:18:37 -05:00
import java.util.Date;
public class Repo {
2016-09-06 16:54:08 -05:00
private String mBaseUrl;
private String mZipUrl;
2016-09-13 15:44:07 -05:00
private String mLogUrl;
2016-09-06 16:54:08 -05:00
private String mManifestUrl;
private String mVersion;
private String mName;
private String mDescription;
private String mAuthor;
public String mAuthorUrl;
private String mId;
private String mVersionCode;
private String mSupportUrl;
private String mDonateUrl;
2016-09-08 14:47:04 -05:00
private String lastUpdate;
private Context appContext;
private boolean mIsInstalled,mCanUpdate,mIsCacheModule;
2016-09-08 15:47:10 -05:00
2016-09-06 16:54:08 -05:00
public Repo(String manifestString, Context context) {
appContext = context;
2016-09-08 14:47:04 -05:00
ParseProps(manifestString);
2016-09-06 16:54:08 -05:00
}
2016-09-08 14:47:04 -05:00
2016-09-02 13:18:37 -05:00
public Repo(String name, String url, Date updated, Context context) {
appContext = context;
2016-09-06 16:54:08 -05:00
this.mName = name;
this.mBaseUrl = url;
2016-09-08 14:47:04 -05:00
this.lastUpdate = updated.toString();
2016-09-08 15:47:10 -05:00
2016-09-02 13:18:37 -05:00
this.fetch();
}
2016-09-02 13:18:37 -05:00
public Repo(String moduleName, String moduleDescription, String zipUrl, Date lastUpdated, Context context) {
appContext = context;
2016-09-06 16:54:08 -05:00
this.mZipUrl = zipUrl;
this.mDescription = moduleDescription;
this.mName = moduleName;
2016-09-08 14:47:04 -05:00
this.lastUpdate = lastUpdated.toString();
2016-09-06 16:54:08 -05:00
this.fetch();
2016-09-02 13:18:37 -05:00
}
2016-09-08 14:47:04 -05:00
public void fetch() {
2016-09-08 15:47:10 -05:00
WebRequest webreq = new WebRequest();
// Construct initial url for contents
2016-09-08 15:47:10 -05:00
String repoString = webreq.makeWebServiceCall(mBaseUrl + "/contents?access_token=" + Utils.procFile(appContext.getString(R.string.some_string),appContext), WebRequest.GET);
try {
JSONArray repoArray = new JSONArray(repoString);
for (int f = 0; f < repoArray.length(); f++) {
JSONObject jsonobject = repoArray.getJSONObject(f);
String name = jsonobject.getString("name");
2016-09-13 15:44:07 -05:00
String url = jsonobject.getString("download_url").trim();
Log.d("Magisk","Repo - checking object named " + name + " with value of " + url);
if (name.contains(".zip")) {
2016-09-13 15:44:07 -05:00
this.mZipUrl = url;
}
if (name.equals("module.prop")) {
this.mManifestUrl = url;
}
if (name.contains("log.txt")) {
Log.d("Magisk","Repo: Setting log URL for " + name + " of " + url);
this.mLogUrl = url;
}
}
} catch (JSONException e) {
e.printStackTrace();
}
2016-09-06 16:54:08 -05:00
WebRequest propReq = new WebRequest();
2016-09-08 14:47:04 -05:00
String manifestString = propReq.makeWebServiceCall(mManifestUrl,WebRequest.GET,true);
2016-09-06 16:54:08 -05:00
if (ParseProps(manifestString)) {
PutProps(manifestString);
}
}
private void PutProps(String manifestString) {
2016-09-13 15:44:07 -05:00
manifestString = manifestString + "zipUrl=" + mZipUrl + "\nbaseUrl=" + mBaseUrl + "\nlogUrl=" + mLogUrl + "\nmanifestUrl=" + mManifestUrl;
2016-09-06 16:54:08 -05:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("repo_" + mId, manifestString);
editor.putBoolean("hasCachedRepos", true);
editor.putString("updated_" + mId, this.lastUpdate);
2016-09-06 16:54:08 -05:00
editor.apply();
}
private boolean ParseProps(String string) {
2016-09-08 14:47:04 -05:00
2016-09-06 16:54:08 -05:00
if ((string.length() <= 1) | (!string.contains("id"))) {
return false;
} else {
String lines[] = string.split("\\n");
for (String line : lines) {
if (line != "") {
String props[] = line.split("=");
switch (props[0]) {
case "versionCode":
this.mVersionCode = props[1];
break;
case "name":
this.mName = props[1];
break;
case "author":
this.mAuthor = props[1];
break;
case "id":
this.mId = props[1];
break;
case "version":
this.mVersion = props[1];
break;
case "description":
this.mDescription = props[1];
break;
case "donate":
this.mDonateUrl = props[1];
break;
case "cacheModule":
this.mIsCacheModule = Boolean.valueOf(props[1]);
break;
2016-09-06 16:54:08 -05:00
case "support":
this.mSupportUrl = props[1];
break;
2016-09-13 15:44:07 -05:00
case "logUrl":
this.mLogUrl = props[1];
break;
2016-09-06 16:54:08 -05:00
case "donateUrl":
this.mDonateUrl = props[1];
break;
case "zipUrl":
this.mZipUrl = props[1];
break;
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;
}
}
}
2016-09-08 14:47:04 -05:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext);
if (prefs.contains("repo-isInstalled_" + this.mId)) {
mIsInstalled = prefs.getBoolean("repo-isInstalled_" + this.mId,false);
}
if (prefs.contains("repo-canUpdate_" + this.mId)) {
mCanUpdate = prefs.getBoolean("repo-canUpdate_" + this.mId,false);
}
if (prefs.contains("updated_" + this.mId)) {
lastUpdate = prefs.getString("updated_" + this.mId,"");
}
2016-09-13 15:44:07 -05:00
2016-09-08 14:47:04 -05:00
return this.mId != null;
}
2016-09-06 16:54:08 -05:00
}
2016-09-08 15:47:10 -05:00
2016-09-06 16:54:08 -05:00
public String getStringProperty(String mValue) {
switch (mValue) {
case "author":
return mAuthor;
case "id":
return mId;
case "version":
return mVersion;
case "description":
return mDescription;
case "supportUrl":
return mSupportUrl;
case "donateUrl":
return mDonateUrl;
case "baseeUrl":
return mBaseUrl;
case "zipUrl":
return mZipUrl;
default:
return null;
}
}
public String getName() {
2016-09-06 16:54:08 -05:00
return mName;
}
public String getmVersion() {
return mVersion;
}
2016-09-06 16:54:08 -05:00
public int getmVersionCode() {
return Integer.valueOf(mVersionCode);
}
public String getDescription() {
2016-09-06 16:54:08 -05:00
return mDescription;
}
public String getId() {
return mId;
}
public String getmZipUrl() {
return mZipUrl;
}
public String getmBaseUrl() {
return mBaseUrl;
}
2016-09-13 15:44:07 -05:00
public String getmLogUrl() {
return mLogUrl;
2016-09-06 16:54:08 -05:00
}
public String getmAuthor() {
return mAuthor;
}
public String getmDonateUrl() {
return mDonateUrl;
}
public String getmManifestUrl() {
return mManifestUrl;
}
2016-09-06 16:54:08 -05:00
public String getmSupportUrl() {
return mSupportUrl;
}
2016-09-08 14:47:04 -05:00
public String getLastUpdate() {
2016-09-06 16:54:08 -05:00
return lastUpdate;
}
2016-09-06 16:54:08 -05:00
public boolean isInstalled() { return mIsInstalled; }
2016-09-08 14:47:04 -05:00
public boolean canUpdate() { return mCanUpdate; }
public boolean isCacheModule() { return mIsCacheModule; }
2016-09-06 16:54:08 -05:00
}