Update dependencies

Settings activity refactor
This commit is contained in:
NekoInverter committed 2020-03-23 14:26:54 +08:00
1 parent ba6d41b91f
commit a9b7df3284
248 files changed
+8700 -8980

No files matched your search

@@ -23,7 +23,9 @@ import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.util.Log;
import androidx.appcompat.app.AlertDialog;
import com.hippo.app.ListCheckBoxDialogBuilder;
import com.hippo.ehviewer.EhApplication;
import com.hippo.ehviewer.EhDB;
@@ -44,16 +46,19 @@ import com.hippo.util.IoThreadPoolExecutor;
import com.hippo.yorozuya.FileUtils;
import com.hippo.yorozuya.IOUtils;
import com.hippo.yorozuya.collect.LongList;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONException;
import org.json.JSONObject;
public final class CommonOperations {
@@ -68,129 +73,8 @@ public final class CommonOperations {
}
}
private static final class UpdateTask extends AsyncTask<Void, Void, JSONObject> {
private final Activity mActivity;
private final OkHttpClient mHttpClient;
private final boolean mFeedback;
public UpdateTask(Activity activity, boolean feedback) {
mActivity = activity;
mHttpClient = EhApplication.getOkHttpClient(activity);
mFeedback = feedback;
}
private JSONObject fetchUpdateInfo(String url) throws IOException, JSONException {
Log.d(TAG, url);
Request request = new Request.Builder().url(url).build();
Response response = mHttpClient.newCall(request).execute();
return new JSONObject(response.body().string());
}
@Override
protected JSONObject doInBackground(Void... params) {
String url;
if (Settings.getBetaUpdateChannel()) {
url = "http://www.ehviewer.com/update_beta.json";
} else {
url = "http://www.ehviewer.com/update.json";
}
try {
return fetchUpdateInfo(url);
} catch (Throwable e1) {
ExceptionUtils.throwIfFatal(e1);
if (Settings.getBetaUpdateChannel()) {
url = "https://raw.githubusercontent.com/seven332/EhViewer/api/update_beta.json";
} else {
url = "https://raw.githubusercontent.com/seven332/EhViewer/api/update.json";
}
try {
return fetchUpdateInfo(url);
} catch (Throwable e2) {
ExceptionUtils.throwIfFatal(e2);
return null;
}
}
}
private void showUpToDateDialog() {
new AlertDialog.Builder(mActivity)
.setMessage(R.string.update_to_date)
.setPositiveButton(android.R.string.ok, null)
.show();
}
private void showUpdateDialog(String versionName, int versionCode, String size, CharSequence info, final String url) {
new AlertDialog.Builder(mActivity)
.setTitle(R.string.update)
.setMessage(mActivity.getString(R.string.update_plain, versionName, size, info))
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
UrlOpener.openUrl(mActivity, url, false);
}
})
.setNegativeButton(R.string.update_ignore, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Settings.putSkipUpdateVersion(versionCode);
}
}).show();
}
private void handleResult(JSONObject jo) {
if (null == jo || mActivity.isFinishing()) {
return;
}
String versionName;
int versionCode;
String size;
CharSequence info;
String url;
try {
PackageManager pm = mActivity.getPackageManager();
PackageInfo pi = pm.getPackageInfo(mActivity.getPackageName(), PackageManager.GET_ACTIVITIES);
int currentVersionCode = pi.versionCode;
versionCode = jo.getInt("version_code");
if (currentVersionCode >= versionCode) {
// Update to date
if (mFeedback) {
showUpToDateDialog();
}
return;
}
versionName = jo.getString("version_name");
size = FileUtils.humanReadableByteCount(jo.getLong("size"), false);
info = Html.fromHtml(jo.getString("info"));
url = jo.getString("url");
} catch (Throwable e) {
ExceptionUtils.throwIfFatal(e);
return;
}
if (mFeedback || versionCode != Settings.getSkipUpdateVersion()) {
showUpdateDialog(versionName, versionCode, size, info, url);
}
}
@Override
protected void onPostExecute(JSONObject jsonObject) {
try {
handleResult(jsonObject);
} finally {
UPDATING = false;
}
}
}
private static void doAddToFavorites(Activity activity, GalleryInfo galleryInfo,
int slot, EhClient.Callback<Void> listener) {
int slot, EhClient.Callback<Void> listener) {
if (slot == -1) {
EhDB.putLocalFavorites(galleryInfo);
listener.onSuccess(null);
@@ -207,7 +91,7 @@ public final class CommonOperations {
}
public static void addToFavorites(final Activity activity, final GalleryInfo galleryInfo,
final EhClient.Callback<Void> listener) {
final EhClient.Callback<Void> listener) {
int slot = Settings.getDefaultFavSlot();
String[] items = new String[11];
items[0] = activity.getString(R.string.local_favorites);
@@ -220,7 +104,7 @@ public final class CommonOperations {
new ListCheckBoxDialogBuilder(activity, items,
(builder, dialog, position) -> {
int slot1 = position - 1;
String newFavoriteName = (slot1 >= 0 && slot1 <= 9) ? items[slot1+1] : null;
String newFavoriteName = (slot1 >= 0 && slot1 <= 9) ? items[slot1 + 1] : null;
doAddToFavorites(activity, galleryInfo, slot1, new DelegateFavoriteCallback(listener, galleryInfo, newFavoriteName, slot1));
if (builder.isChecked()) {
Settings.putDefaultFavSlot(slot1);
@@ -235,7 +119,7 @@ public final class CommonOperations {
}
public static void removeFromFavorites(Activity activity, GalleryInfo galleryInfo,
final EhClient.Callback<Void> listener) {
final EhClient.Callback<Void> listener) {
EhDB.removeLocalFavorites(galleryInfo.gid);
EhClient client = EhApplication.getEhClient(activity);
EhRequest request = new EhRequest();
@@ -245,40 +129,6 @@ public final class CommonOperations {
client.execute(request);
}
private static class DelegateFavoriteCallback implements EhClient.Callback<Void> {
private final EhClient.Callback<Void> delegate;
private final GalleryInfo info;
private final String newFavoriteName;
private final int slot;
DelegateFavoriteCallback(EhClient.Callback<Void> delegate, GalleryInfo info,
String newFavoriteName, int slot) {
this.delegate = delegate;
this.info = info;
this.newFavoriteName = newFavoriteName;
this.slot = slot;
}
@Override
public void onSuccess(Void result) {
info.favoriteName = newFavoriteName;
info.favoriteSlot = slot;
delegate.onSuccess(result);
EhApplication.getFavouriteStatusRouter().modifyFavourites(info.gid, slot);
}
@Override
public void onFailure(Exception e) {
delegate.onFailure(e);
}
@Override
public void onCancel() {
delegate.onCancel();
}
}
public static void startDownload(final MainActivity activity, final GalleryInfo galleryInfo, boolean forceDefault) {
startDownload(activity, Collections.singletonList(galleryInfo), forceDefault);
}
@@ -409,4 +259,159 @@ public final class CommonOperations {
noMedia.delete();
}
}
private static final class UpdateTask extends AsyncTask<Void, Void, JSONObject> {
private final Activity mActivity;
private final OkHttpClient mHttpClient;
private final boolean mFeedback;
public UpdateTask(Activity activity, boolean feedback) {
mActivity = activity;
mHttpClient = EhApplication.getOkHttpClient(activity);
mFeedback = feedback;
}
private JSONObject fetchUpdateInfo(String url) throws IOException, JSONException {
Log.d(TAG, url);
Request request = new Request.Builder().url(url).build();
Response response = mHttpClient.newCall(request).execute();
return new JSONObject(response.body().string());
}
@Override
protected JSONObject doInBackground(Void... params) {
String url;
if (Settings.getBetaUpdateChannel()) {
url = "http://www.ehviewer.com/update_beta.json";
} else {
url = "http://www.ehviewer.com/update.json";
}
try {
return fetchUpdateInfo(url);
} catch (Throwable e1) {
ExceptionUtils.throwIfFatal(e1);
if (Settings.getBetaUpdateChannel()) {
url = "https://raw.githubusercontent.com/seven332/EhViewer/api/update_beta.json";
} else {
url = "https://raw.githubusercontent.com/seven332/EhViewer/api/update.json";
}
try {
return fetchUpdateInfo(url);
} catch (Throwable e2) {
ExceptionUtils.throwIfFatal(e2);
return null;
}
}
}
private void showUpToDateDialog() {
new AlertDialog.Builder(mActivity)
.setMessage(R.string.update_to_date)
.setPositiveButton(android.R.string.ok, null)
.show();
}
private void showUpdateDialog(String versionName, int versionCode, String size, CharSequence info, final String url) {
new AlertDialog.Builder(mActivity)
.setTitle(R.string.update)
.setMessage(mActivity.getString(R.string.update_plain, versionName, size, info))
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
UrlOpener.openUrl(mActivity, url, false);
}
})
.setNegativeButton(R.string.update_ignore, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Settings.putSkipUpdateVersion(versionCode);
}
}).show();
}
private void handleResult(JSONObject jo) {
if (null == jo || mActivity.isFinishing()) {
return;
}
String versionName;
int versionCode;
String size;
CharSequence info;
String url;
try {
PackageManager pm = mActivity.getPackageManager();
PackageInfo pi = pm.getPackageInfo(mActivity.getPackageName(), PackageManager.GET_ACTIVITIES);
int currentVersionCode = pi.versionCode;
versionCode = jo.getInt("version_code");
if (currentVersionCode >= versionCode) {
// Update to date
if (mFeedback) {
showUpToDateDialog();
}
return;
}
versionName = jo.getString("version_name");
size = FileUtils.humanReadableByteCount(jo.getLong("size"), false);
info = Html.fromHtml(jo.getString("info"));
url = jo.getString("url");
} catch (Throwable e) {
ExceptionUtils.throwIfFatal(e);
return;
}
if (mFeedback || versionCode != Settings.getSkipUpdateVersion()) {
showUpdateDialog(versionName, versionCode, size, info, url);
}
}
@Override
protected void onPostExecute(JSONObject jsonObject) {
try {
handleResult(jsonObject);
} finally {
UPDATING = false;
}
}
}
private static class DelegateFavoriteCallback implements EhClient.Callback<Void> {
private final EhClient.Callback<Void> delegate;
private final GalleryInfo info;
private final String newFavoriteName;
private final int slot;
DelegateFavoriteCallback(EhClient.Callback<Void> delegate, GalleryInfo info,
String newFavoriteName, int slot) {
this.delegate = delegate;
this.info = info;
this.newFavoriteName = newFavoriteName;
this.slot = slot;
}
@Override
public void onSuccess(Void result) {
info.favoriteName = newFavoriteName;
info.favoriteSlot = slot;
delegate.onSuccess(result);
EhApplication.getFavouriteStatusRouter().modifyFavourites(info.gid, slot);
}
@Override
public void onFailure(Exception e) {
delegate.onFailure(e);
}
@Override
public void onCancel() {
delegate.onCancel();
}
}
}