Skip to content

Commit

Permalink
Distinguish channels when checking updates (LSPosed#2181)
Browse files Browse the repository at this point in the history
  • Loading branch information
Howard20181 authored Oct 18, 2022
1 parent 9b0b74d commit f4ce1b6
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 6 deletions.
54 changes: 52 additions & 2 deletions app/src/main/java/org/lsposed/manager/repo/RepoLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

package org.lsposed.manager.repo;

import android.content.res.Resources;
import android.util.Log;

import androidx.annotation.NonNull;
Expand All @@ -28,16 +29,20 @@
import com.google.gson.Gson;

import org.lsposed.manager.App;
import org.lsposed.manager.R;
import org.lsposed.manager.repo.model.OnlineModule;
import org.lsposed.manager.repo.model.Release;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -74,6 +79,8 @@ public boolean upgradable(long versionCode, String versionName) {
private static final String originRepoUrl = "https://modules.lsposed.org/";
private static final String backupRepoUrl = "https://cdn.jsdelivr.net/gh/Xposed-Modules-Repo/modules@gh-pages/";
private static String repoUrl = originRepoUrl;
private final Resources resources = App.getInstance().getResources();
private final String[] channels = resources.getStringArray(R.array.update_channel_values);

public boolean isRepoLoaded() {
return repoLoaded;
Expand Down Expand Up @@ -103,10 +110,18 @@ synchronized public void loadRemoteData() {
Map<String, OnlineModule> modules = new HashMap<>();
OnlineModule[] repoModules = gson.fromJson(bodyString, OnlineModule[].class);
Arrays.stream(repoModules).forEach(onlineModule -> modules.put(onlineModule.getName(), onlineModule));

var channel = App.getPreferences().getString("update_channel", channels[0]);
Map<String, ModuleVersion> versions = new ConcurrentHashMap<>();
for (var module : repoModules) {
var release = module.getLatestRelease();
String release = module.getLatestRelease();
if (channel.equals(channels[1]) && !(module.getLatestBetaRelease() != null && module.getLatestBetaRelease().isEmpty())) {
release = module.getLatestBetaRelease();
} else if (channel.equals(channels[2])) {
if (!(module.getLatestSnapshotRelease() != null && module.getLatestSnapshotRelease().isEmpty()))
release = module.getLatestSnapshotRelease();
else if (!(module.getLatestBetaRelease() != null && module.getLatestBetaRelease().isEmpty()))
release = module.getLatestBetaRelease();
}
if (release == null || release.isEmpty()) continue;
var splits = release.split("-", 2);
if (splits.length < 2) continue;
Expand Down Expand Up @@ -156,6 +171,41 @@ public ModuleVersion getModuleLatestVersion(String packageName) {
return repoLoaded ? latestVersion.getOrDefault(packageName, null) : null;
}

@Nullable
public List<Release> getReleases(String packageName) {
var channel = App.getPreferences().getString("update_channel", channels[0]);
List<Release> releases = new ArrayList<>();
if (repoLoaded) {
var module = onlineModules.get(packageName);
if (module != null) {
releases = module.getReleases();
if (!module.releasesLoaded) {
if (channel.equals(channels[1]) && !module.getBetaReleases().isEmpty()) {
releases = module.getBetaReleases();
} else if (channel.equals(channels[2]) && !module.getSnapshotReleases().isEmpty())
releases = module.getSnapshotReleases();
}
}
}
return releases;
}

@Nullable
public String getLatestReleaseTime(String packageName, String channel) {
String releaseTime = null;
if (repoLoaded) {
var module = onlineModules.get(packageName);
if (module != null) {
releaseTime = module.getLatestReleaseTime();
if (channel.equals(channels[1]) && module.getLatestBetaReleaseTime() != null) {
releaseTime = module.getLatestBetaReleaseTime();
} else if (channel.equals(channels[2]) && module.getLatestSnapshotReleaseTime() != null)
releaseTime = module.getLatestSnapshotReleaseTime();
}
}
return releaseTime;
}

public void loadRemoteReleases(String packageName) {
App.getOkHttpClient().newCall(new Request.Builder()
.url(String.format(repoUrl + "module/%s.json", packageName))
Expand Down
48 changes: 48 additions & 0 deletions app/src/main/java/org/lsposed/manager/repo/model/OnlineModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

package org.lsposed.manager.repo.model;

import androidx.annotation.Nullable;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

Expand Down Expand Up @@ -49,9 +51,27 @@ public class OnlineModule {
@SerializedName("latestReleaseTime")
@Expose
private String latestReleaseTime;
@SerializedName("latestBetaRelease")
@Expose
private String latestBetaRelease;
@SerializedName("latestBetaReleaseTime")
@Expose
private String latestBetaReleaseTime;
@SerializedName("latestSnapshotRelease")
@Expose
private String latestSnapshotRelease;
@SerializedName("latestSnapshotReleaseTime")
@Expose
private String latestSnapshotReleaseTime;
@SerializedName("releases")
@Expose
private List<Release> releases = new ArrayList<>();
@SerializedName("betaReleases")
@Expose
private List<Release> betaReleases = new ArrayList<>();
@SerializedName("snapshotReleases")
@Expose
private List<Release> snapshotReleases = new ArrayList<>();
@SerializedName("readme")
@Expose
private String readme;
Expand Down Expand Up @@ -224,4 +244,32 @@ public String getLatestRelease() {
public void setLatestRelease(String latestRelease) {
this.latestRelease = latestRelease;
}

@Nullable
public String getLatestBetaRelease() {
return latestBetaRelease;
}

@Nullable
public String getLatestBetaReleaseTime() {
return latestBetaReleaseTime;
}

@Nullable
public String getLatestSnapshotRelease() {
return latestSnapshotRelease;
}

@Nullable
public String getLatestSnapshotReleaseTime() {
return latestSnapshotReleaseTime;
}

public List<Release> getBetaReleases() {
return betaReleases;
}

public List<Release> getSnapshotReleases() {
return snapshotReleases;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.lsposed.manager.ui.fragment;

import android.annotation.SuppressLint;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
Expand Down Expand Up @@ -260,6 +261,10 @@ private class RepoAdapter extends EmptyStateRecyclerView.EmptyStateAdapter<RepoA
private List<OnlineModule> fullList, showList;
private final LabelComparator labelComparator = new LabelComparator();
private boolean isLoaded = false;
private final Resources resources = App.getInstance().getResources();
private final String[] channels = resources.getStringArray(R.array.update_channel_values);
private String channel;
private final RepoLoader repoLoader = RepoLoader.getInstance();

RepoAdapter() {
fullList = showList = Collections.emptyList();
Expand All @@ -286,7 +291,10 @@ public void onBindViewHolder(@NonNull RepoAdapter.ViewHolder holder, int positio
OnlineModule module = showList.get(position);
holder.appName.setText(module.getDescription());
holder.appPackageName.setText(module.getName());
var instant = Instant.parse(module.getLatestReleaseTime());
Instant instant;
channel = App.getPreferences().getString("update_channel", channels[0]);

instant = Instant.parse(repoLoader.getLatestReleaseTime(module.getName(), channel));
var formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
.withLocale(App.getLocale()).withZone(ZoneId.systemDefault());
holder.publishedTime.setText(String.format(getString(R.string.module_repo_updated_time), formatter.format(instant)));
Expand Down Expand Up @@ -344,9 +352,11 @@ private void setLoaded(List<OnlineModule> list, boolean isLoaded) {
public void setData(Collection<OnlineModule> modules) {
if (modules == null) return;
setLoaded(null, false);
channel = App.getPreferences().getString("update_channel", channels[0]);
int sort = App.getPreferences().getInt("repo_sort", 0);
boolean upgradableFirst = App.getPreferences().getBoolean("upgradable_first", true);
ConcurrentHashMap<String, Boolean> upgradable = new ConcurrentHashMap<>();

fullList = modules.parallelStream().filter((onlineModule -> !onlineModule.isHide() && !onlineModule.getReleases().isEmpty()))
.sorted((a, b) -> {
if (upgradableFirst) {
Expand All @@ -358,7 +368,7 @@ public void setData(Collection<OnlineModule> modules) {
if (sort == 0) {
return labelComparator.compare(a.getDescription(), b.getDescription());
} else {
return Instant.parse(b.getLatestReleaseTime()).compareTo(Instant.parse(a.getLatestReleaseTime()));
return Instant.parse(repoLoader.getLatestReleaseTime(b.getName(), channel)).compareTo(Instant.parse(repoLoader.getLatestReleaseTime(a.getName(), channel)));
}
}).collect(Collectors.toList());
String queryStr = searchView != null ? searchView.getQuery().toString() : "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,8 @@ public ReleaseAdapter() {
public void loadItems() {
var channels = resources.getStringArray(R.array.update_channel_values);
var channel = App.getPreferences().getString("update_channel", channels[0]);
var releases = module.getReleases();
var releases = RepoLoader.getInstance().getReleases(module.getName());
if (releases == null) releases = module.getReleases();
List<Release> tmpList;
if (channel.equals(channels[0])) {
tmpList = releases.parallelStream().filter(t -> {
Expand All @@ -383,7 +384,9 @@ public void loadItems() {
var name = t.getName().toLowerCase(LocaleDelegate.getDefaultLocale());
return !name.startsWith("snapshot") && !name.startsWith("nightly");
}).collect(Collectors.toList());
} else tmpList = releases;
} else {
tmpList = releases;
}
runOnUiThread(() -> {
items = tmpList;
notifyDataSetChanged();
Expand Down

0 comments on commit f4ce1b6

Please sign in to comment.