You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
FoxMagiskModuleManager/app/src/main/java/com/fox2code/mmm/repo/RepoUpdater.java

83 lines
2.5 KiB
Java

3 years ago
package com.fox2code.mmm.repo;
import android.util.Log;
import com.fox2code.mmm.utils.Files;
import com.fox2code.mmm.utils.Http;
import org.json.JSONObject;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
3 years ago
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class RepoUpdater {
private static final String TAG = "RepoUpdater";
public final RepoData repoData;
public byte[] indexRaw;
private List<RepoModule> toUpdate;
private Collection<RepoModule> toApply;
3 years ago
public RepoUpdater(RepoData repoData) {
this.repoData = repoData;
}
public int fetchIndex() {
if (!this.repoData.isEnabled()) {
this.indexRaw = null;
this.toUpdate = Collections.emptyList();
this.toApply = Collections.emptySet();
return 0;
}
3 years ago
try {
if (!this.repoData.prepare()) {
this.indexRaw = null;
this.toUpdate = Collections.emptyList();
this.toApply = this.repoData.moduleHashMap.values();
return 0;
}
3 years ago
this.indexRaw = Http.doHttpGet(this.repoData.url, false);
this.toUpdate = this.repoData.populate(new JSONObject(
new String(this.indexRaw, StandardCharsets.UTF_8)));
// Since we reuse instances this should work
this.toApply = new HashSet<>(this.repoData.moduleHashMap.values());
this.toApply.removeAll(this.toUpdate);
// Return repo to update
return this.toUpdate.size();
} catch (Exception e) {
Log.e(TAG, "Failed to get manifest", e);
this.indexRaw = null;
this.toUpdate = Collections.emptyList();
this.toApply = Collections.emptySet();
return 0;
}
}
public List<RepoModule> toUpdate() {
return this.toUpdate;
}
public Collection<RepoModule> toApply() {
3 years ago
return this.toApply;
}
public boolean finish() {
final boolean success = this.indexRaw != null;
if (this.indexRaw != null) {
try {
Files.write(this.repoData.metaDataCache, this.indexRaw);
} catch (IOException e) {
e.printStackTrace();
}
this.indexRaw = null;
}
this.toUpdate = null;
this.toApply = null;
return success;
}
}