EhViewer v1.9.7.7版本的 MD5 值为:828893cd54837a794dab17a02809e194

以下内容为反编译后的 CommonOperations.java 源代码,内容仅作参考


package com.hippo.ehviewer.ui;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Parcelable;
import android.text.Spanned;
import android.util.Log;
import androidx.appcompat.app.AlertDialog;
import com.google.android.gms.common.internal.ImagesContract;
import com.hippo.app.ListCheckBoxDialogBuilder;
import com.hippo.ehviewer.EhApplication;
import com.hippo.ehviewer.EhDB;
import com.hippo.ehviewer.Settings;
import com.hippo.ehviewer.UrlOpener;
import com.hippo.ehviewer.client.EhClient;
import com.hippo.ehviewer.client.EhRequest;
import com.hippo.ehviewer.client.data.GalleryInfo;
import com.hippo.ehviewer.dao.DownloadLabel;
import com.hippo.ehviewer.download.DownloadManager;
import com.hippo.ehviewer.download.DownloadService;
import com.hippo.text.Html;
import com.hippo.unifile.UniFile;
import com.hippo.util.ExceptionUtils;
import com.hippo.util.IoThreadPoolExecutor;
import com.hippo.yorozuya.FileUtils;
import com.hippo.yorozuya.IOUtils;
import com.hippo.yorozuya.collect.LongList;
import com.xjs.ehviewer.R;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.json.JSONException;
import org.json.JSONObject;
public final class CommonOperations {
    private static final String TAG = "CommonOperations";
    private static boolean UPDATING;

    public static void checkUpdate(Activity activity, boolean z) {
        if (UPDATING) {
            return;
        }
        UPDATING = true;
        new UpdateTask(activity, z).executeOnExecutor(IoThreadPoolExecutor.getInstance(), new Void[0]);
    }

    public static final class UpdateTask extends AsyncTask<Void, Void, JSONObject> {
        private final Activity mActivity;
        private final boolean mFeedback;
        private final OkHttpClient mHttpClient;

        public UpdateTask(Activity activity, boolean z) {
            this.mActivity = activity;
            this.mHttpClient = EhApplication.getOkHttpClient(activity);
            this.mFeedback = z;
        }

        private JSONObject fetchUpdateInfo(String str) throws IOException, JSONException {
            Log.d(CommonOperations.TAG, str);
            return new JSONObject(this.mHttpClient.newCall(new Request.Builder().url(str).build()).execute().body().string());
        }

        @Override
        public JSONObject doInBackground(Void... voidArr) {
            try {
                return fetchUpdateInfo(Settings.getBetaUpdateChannel() ? "http://www.ehviewer.com/update_beta.json" : "http://www.ehviewer.com/update.json");
            } catch (Throwable th) {
                ExceptionUtils.throwIfFatal(th);
                try {
                    return fetchUpdateInfo(Settings.getBetaUpdateChannel() ? "https://raw.githubusercontent.com/seven332/EhViewer/api/update_beta.json" : "https://raw.githubusercontent.com/seven332/EhViewer/api/update.json");
                } catch (Throwable th2) {
                    ExceptionUtils.throwIfFatal(th2);
                    return null;
                }
            }
        }

        private void showUpToDateDialog() {
            new AlertDialog.Builder(this.mActivity).setMessage(R.string.update_to_date).setPositiveButton(17039370, (DialogInterface.OnClickListener) null).show();
        }

        private void showUpdateDialog(String str, final int i, String str2, CharSequence charSequence, final String str3) {
            new AlertDialog.Builder(this.mActivity).setTitle(R.string.update).setMessage(this.mActivity.getString(R.string.update_plain, new Object[]{str, str2, charSequence})).setPositiveButton(17039370, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i2) {
                    UrlOpener.openUrl(UpdateTask.this.mActivity, str3, false);
                }
            }).setNegativeButton(R.string.update_ignore, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i2) {
                    Settings.putSkipUpdateVersion(i);
                }
            }).show();
        }

        private void handleResult(JSONObject jSONObject) {
            if (jSONObject == null || this.mActivity.isFinishing()) {
                return;
            }
            try {
                int i = this.mActivity.getPackageManager().getPackageInfo(this.mActivity.getPackageName(), 1).versionCode;
                int i2 = jSONObject.getInt("version_code");
                if (i >= i2) {
                    if (this.mFeedback) {
                        showUpToDateDialog();
                        return;
                    }
                    return;
                }
                String string = jSONObject.getString("version_name");
                String humanReadableByteCount = FileUtils.humanReadableByteCount(jSONObject.getLong("size"), false);
                Spanned fromHtml = Html.fromHtml(jSONObject.getString("info"));
                String string2 = jSONObject.getString(ImagesContract.URL);
                if (this.mFeedback || i2 != Settings.getSkipUpdateVersion()) {
                    showUpdateDialog(string, i2, humanReadableByteCount, fromHtml, string2);
                }
            } catch (Throwable th) {
                ExceptionUtils.throwIfFatal(th);
            }
        }

        @Override
        public void onPostExecute(JSONObject jSONObject) {
            try {
                handleResult(jSONObject);
            } finally {
                CommonOperations.UPDATING = false;
            }
        }
    }

    private static void doAddToFavorites(Activity activity, GalleryInfo galleryInfo, int i, EhClient.Callback<Void> callback) {
        if (i == -1) {
            EhDB.putLocalFavorite(galleryInfo);
            callback.onSuccess(null);
        } else if (i >= 0 && i <= 9) {
            EhClient ehClient = EhApplication.getEhClient(activity);
            EhRequest ehRequest = new EhRequest();
            ehRequest.setMethod(9);
            ehRequest.setArgs(Long.valueOf(galleryInfo.gid), galleryInfo.token, Integer.valueOf(i), "");
            ehRequest.setCallback(callback);
            ehClient.execute(ehRequest);
        } else {
            callback.onFailure(new Exception());
        }
    }

    public static void addToFavorites(final Activity activity, final GalleryInfo galleryInfo, final EhClient.Callback<Void> callback) {
        int defaultFavSlot = Settings.getDefaultFavSlot();
        final String[] strArr = new String[11];
        strArr[0] = activity.getString(R.string.local_favorites);
        System.arraycopy(Settings.getFavCat(), 0, strArr, 1, 10);
        if (defaultFavSlot >= -1 && defaultFavSlot <= 9) {
            doAddToFavorites(activity, galleryInfo, defaultFavSlot, new DelegateFavoriteCallback(callback, galleryInfo, defaultFavSlot >= 0 ? strArr[defaultFavSlot + 1] : null, defaultFavSlot));
        } else {
            new ListCheckBoxDialogBuilder(activity, strArr, new ListCheckBoxDialogBuilder.OnItemClickListener() {
                @Override
                public final void onItemClick(ListCheckBoxDialogBuilder listCheckBoxDialogBuilder, AlertDialog alertDialog, int i) {
                    CommonOperations.lambda$addToFavorites$0(strArr, activity, galleryInfo, callback, listCheckBoxDialogBuilder, alertDialog, i);
                }
            }, activity.getString(R.string.remember_favorite_collection), false).setTitle(R.string.add_favorites_dialog_title).setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public final void onCancel(DialogInterface dialogInterface) {
                    EhClient.Callback.this.onCancel();
                }
            }).show();
        }
    }

    public static void lambda$addToFavorites$0(String[] strArr, Activity activity, GalleryInfo galleryInfo, EhClient.Callback callback, ListCheckBoxDialogBuilder listCheckBoxDialogBuilder, AlertDialog alertDialog, int i) {
        int i2 = i - 1;
        doAddToFavorites(activity, galleryInfo, i2, new DelegateFavoriteCallback(callback, galleryInfo, (i2 < 0 || i2 > 9) ? null : strArr[i2 + 1], i2));
        if (listCheckBoxDialogBuilder.isChecked()) {
            Settings.putDefaultFavSlot(i2);
        } else {
            Settings.putDefaultFavSlot(-2);
        }
    }

    public static void removeFromFavorites(Activity activity, GalleryInfo galleryInfo, EhClient.Callback<Void> callback) {
        EhDB.removeLocalFavorites(galleryInfo.gid);
        EhClient ehClient = EhApplication.getEhClient(activity);
        EhRequest ehRequest = new EhRequest();
        ehRequest.setMethod(9);
        ehRequest.setArgs(Long.valueOf(galleryInfo.gid), galleryInfo.token, -1, "");
        ehRequest.setCallback(new DelegateFavoriteCallback(callback, galleryInfo, null, -2));
        ehClient.execute(ehRequest);
    }

    public 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> callback, GalleryInfo galleryInfo, String str, int i) {
            this.delegate = callback;
            this.info = galleryInfo;
            this.newFavoriteName = str;
            this.slot = i;
        }

        @Override
        public void onSuccess(Void r4) {
            this.info.favoriteName = this.newFavoriteName;
            this.info.favoriteSlot = this.slot;
            this.delegate.onSuccess(r4);
            EhApplication.getFavouriteStatusRouter().modifyFavourites(this.info.gid, this.slot);
        }

        @Override
        public void onFailure(Exception exc) {
            this.delegate.onFailure(exc);
        }

        @Override
        public void onCancel() {
            this.delegate.onCancel();
        }
    }

    public static void startDownload(MainActivity mainActivity, GalleryInfo galleryInfo, boolean z) {
        startDownload(mainActivity, Collections.singletonList(galleryInfo), z);
    }

    public static void startDownload(final MainActivity mainActivity, List<GalleryInfo> list, boolean z) {
        String str;
        final DownloadManager downloadManager = EhApplication.getDownloadManager(mainActivity);
        LongList longList = new LongList();
        final ArrayList<Parcelable> arrayList = new ArrayList();
        for (GalleryInfo galleryInfo : list) {
            if (downloadManager.containDownloadInfo(galleryInfo.gid)) {
                longList.add(galleryInfo.gid);
            } else {
                arrayList.add(galleryInfo);
            }
        }
        if (!longList.isEmpty()) {
            Intent intent = new Intent(mainActivity, DownloadService.class);
            intent.setAction(DownloadService.ACTION_START_RANGE);
            intent.putExtra(DownloadService.KEY_GID_LIST, longList);
            mainActivity.startService(intent);
        }
        int i = 0;
        if (arrayList.isEmpty()) {
            mainActivity.showTip(R.string.added_to_download_list, 0);
            return;
        }
        String str2 = null;
        if (z || !Settings.getHasDefaultDownloadLabel()) {
            str = null;
        } else {
            String defaultDownloadLabel = Settings.getDefaultDownloadLabel();
            str = defaultDownloadLabel;
            z = defaultDownloadLabel == null || downloadManager.containLabel(defaultDownloadLabel);
        }
        if (z || downloadManager.getLabelList().size() != 0) {
            str2 = str;
        } else {
            z = true;
        }
        if (z) {
            for (Parcelable parcelable : arrayList) {
                Intent intent2 = new Intent(mainActivity, DownloadService.class);
                intent2.setAction(DownloadService.ACTION_START);
                intent2.putExtra(DownloadService.KEY_LABEL, str2);
                intent2.putExtra("gallery_info", parcelable);
                mainActivity.startService(intent2);
            }
            mainActivity.showTip(R.string.added_to_download_list, 0);
            return;
        }
        List<DownloadLabel> labelList = downloadManager.getLabelList();
        final String[] strArr = new String[labelList.size() + 1];
        strArr[0] = mainActivity.getString(R.string.default_download_label_name);
        int size = labelList.size();
        while (i < size) {
            int i2 = i + 1;
            strArr[i2] = labelList.get(i).getLabel();
            i = i2;
        }
        new ListCheckBoxDialogBuilder(mainActivity, strArr, new ListCheckBoxDialogBuilder.OnItemClickListener() {
            @Override
            public final void onItemClick(ListCheckBoxDialogBuilder listCheckBoxDialogBuilder, AlertDialog alertDialog, int i3) {
                CommonOperations.lambda$startDownload$2(strArr, downloadManager, arrayList, mainActivity, listCheckBoxDialogBuilder, alertDialog, i3);
            }
        }, mainActivity.getString(R.string.remember_download_label), false).setTitle(R.string.download).show();
    }

    public static void lambda$startDownload$2(String[] strArr, DownloadManager downloadManager, List list, MainActivity mainActivity, ListCheckBoxDialogBuilder listCheckBoxDialogBuilder, AlertDialog alertDialog, int i) {
        String str = null;
        if (i != 0) {
            String str2 = strArr[i];
            if (downloadManager.containLabel(str2)) {
                str = str2;
            }
        }
        Iterator it = list.iterator();
        while (it.hasNext()) {
            Intent intent = new Intent(mainActivity, DownloadService.class);
            intent.setAction(DownloadService.ACTION_START);
            intent.putExtra(DownloadService.KEY_LABEL, str);
            intent.putExtra("gallery_info", (GalleryInfo) it.next());
            mainActivity.startService(intent);
        }
        if (listCheckBoxDialogBuilder.isChecked()) {
            Settings.putHasDefaultDownloadLabel(true);
            Settings.putDefaultDownloadLabel(str);
        } else {
            Settings.putHasDefaultDownloadLabel(false);
        }
        mainActivity.showTip(R.string.added_to_download_list, 0);
    }

    public static void ensureNoMediaFile(UniFile uniFile) {
        UniFile createFile;
        if (uniFile == null || (createFile = uniFile.createFile(".nomedia")) == null) {
            return;
        }
        try {
            IOUtils.closeQuietly(createFile.openInputStream());
        } catch (IOException unused) {
            IOUtils.closeQuietly(null);
        } catch (Throwable th) {
            IOUtils.closeQuietly(null);
            throw th;
        }
    }

    public static void removeNoMediaFile(UniFile uniFile) {
        UniFile subFile;
        if (uniFile == null || (subFile = uniFile.subFile(".nomedia")) == null || !subFile.isFile()) {
            return;
        }
        subFile.delete();
    }
}