导航菜单

页面标题

页面副标题

微商客 v2.1.2 - DownloadService.java 源代码

正在查看: 微商客 v2.1.2 应用的 DownloadService.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.king.app.updater.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.text.TextUtils;
import androidx.core.content.ContextCompat;
import com.king.app.updater.R;
import com.king.app.updater.UpdateConfig;
import com.king.app.updater.callback.UpdateCallback;
import com.king.app.updater.constant.Constants;
import com.king.app.updater.http.HttpManager;
import com.king.app.updater.http.IHttpManager;
import com.king.app.updater.notify.INotification;
import com.king.app.updater.notify.NotificationImpl;
import com.king.app.updater.util.AppUtils;
import com.king.app.updater.util.LogUtils;
import java.io.File;
import java.util.Locale;

public class DownloadService extends Service {
    private boolean isDownloading;
    private File mApkFile;
    private IHttpManager mHttpManager;
    private INotification mNotification;
    private UpdateCallback mUpdateCallback;
    private DownloadBinder mDownloadBinder = new DownloadBinder();
    private int mCount = 0;

    private Context getContext() {
        return this;
    }

    @Override
    public int onStartCommand(Intent intent, int i, int i2) {
        if (intent != null) {
            if (intent.getBooleanExtra(Constants.KEY_STOP_DOWNLOAD_SERVICE, false)) {
                stopDownload();
            } else if (!this.isDownloading) {
                if (intent.getBooleanExtra(Constants.KEY_RE_DOWNLOAD, false)) {
                    this.mCount++;
                }
                startDownload((UpdateConfig) intent.getParcelableExtra(Constants.KEY_UPDATE_CONFIG));
            } else {
                LogUtils.w("Please do not repeat the download.");
            }
        }
        return super.onStartCommand(intent, i, i2);
    }

    private void startDownload(UpdateConfig updateConfig) {
        startDownload(updateConfig, this.mHttpManager, this.mUpdateCallback, this.mNotification);
    }

    public void startDownload(UpdateConfig updateConfig, IHttpManager iHttpManager, UpdateCallback updateCallback, INotification iNotification) {
        boolean z;
        if (updateCallback != null) {
            updateCallback.onDownloading(this.isDownloading);
        }
        if (this.isDownloading) {
            LogUtils.w("Please do not repeat the download.");
            return;
        }
        String url = updateConfig.getUrl();
        String path = updateConfig.getPath();
        String filename = updateConfig.getFilename();
        if (TextUtils.isEmpty(path)) {
            path = getCacheFilesDir(getContext());
        }
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        if (TextUtils.isEmpty(filename)) {
            filename = AppUtils.getAppFullName(getContext(), url, getResources().getString(R.string.app_name));
        }
        File file2 = new File(path, filename);
        this.mApkFile = file2;
        if (file2.exists()) {
            long versionCode = updateConfig.getVersionCode();
            String apkMD5 = updateConfig.getApkMD5();
            if (!TextUtils.isEmpty(apkMD5)) {
                LogUtils.d(String.format(Locale.getDefault(), "UpdateConfig.apkMD5: %s", apkMD5));
                z = AppUtils.verifyFileMD5(this.mApkFile, apkMD5);
            } else if (versionCode > 0) {
                LogUtils.d(String.format(Locale.getDefault(), "UpdateConfig.versionCode: %d", Long.valueOf(versionCode)));
                z = AppUtils.apkExists(getContext(), versionCode, this.mApkFile);
            } else {
                z = false;
            }
            if (z) {
                LogUtils.d("CacheFile: " + this.mApkFile);
                if (updateConfig.isInstallApk()) {
                    String authority = updateConfig.getAuthority();
                    if (TextUtils.isEmpty(authority)) {
                        authority = AppUtils.getFileProviderAuthority(getContext());
                    }
                    AppUtils.installApk(getContext(), this.mApkFile, authority);
                }
                if (updateCallback != null) {
                    updateCallback.onFinish(this.mApkFile);
                }
                stopService();
                return;
            }
            this.mApkFile.delete();
        }
        LogUtils.d("File: " + this.mApkFile);
        this.mUpdateCallback = updateCallback;
        getHttpManager(iHttpManager).download(url, this.mApkFile.getAbsolutePath(), updateConfig.getRequestProperty(), new AppDownloadCallback(getContext(), this, updateConfig, this.mApkFile, updateCallback, getNotification(iNotification)));
    }

    private IHttpManager getHttpManager(IHttpManager iHttpManager) {
        if (iHttpManager != null) {
            this.mHttpManager = iHttpManager;
        }
        if (this.mHttpManager == null) {
            this.mHttpManager = HttpManager.getInstance();
        }
        return this.mHttpManager;
    }

    private INotification getNotification(INotification iNotification) {
        if (iNotification != null) {
            this.mNotification = iNotification;
        }
        if (this.mNotification == null) {
            this.mNotification = new NotificationImpl();
        }
        return this.mNotification;
    }

    private void stopDownload() {
        IHttpManager iHttpManager = this.mHttpManager;
        if (iHttpManager != null) {
            iHttpManager.cancel();
        }
    }

    private String getCacheFilesDir(Context context) {
        File[] externalFilesDirs = ContextCompat.getExternalFilesDirs(context, Constants.DEFAULT_DIR);
        if (externalFilesDirs != null && externalFilesDirs.length > 0) {
            return externalFilesDirs[0].getAbsolutePath();
        }
        File externalFilesDir = context.getExternalFilesDir(Constants.DEFAULT_DIR);
        if (externalFilesDir != null) {
            return externalFilesDir.getAbsolutePath();
        }
        return new File(context.getFilesDir(), Constants.DEFAULT_DIR).getAbsolutePath();
    }

    public void stopService() {
        this.mCount = 0;
        stopSelf();
    }

    public static class AppDownloadCallback implements IHttpManager.DownloadCallback {
        private File apkFile;
        private String authority;
        private UpdateCallback callback;
        private String channelId;
        private String channelName;
        public UpdateConfig config;
        private Context context;
        private DownloadService downloadService;
        private boolean isDeleteCancelFile;
        private boolean isInstallApk;
        private boolean isReDownload;
        private boolean isShowNotification;
        private boolean isShowPercentage;
        private boolean isSupportCancelDownload;
        private int lastProgress;
        private long lastTime;
        private INotification notification;
        private int notificationIcon;
        private int notifyId;

        private AppDownloadCallback(Context context, DownloadService downloadService, UpdateConfig updateConfig, File file, UpdateCallback updateCallback, INotification iNotification) {
            this.context = context;
            this.downloadService = downloadService;
            this.config = updateConfig;
            this.apkFile = file;
            this.callback = updateCallback;
            this.notification = iNotification;
            this.isShowNotification = updateConfig.isShowNotification();
            this.notifyId = updateConfig.getNotificationId();
            if (Build.VERSION.SDK_INT >= 26) {
                this.channelId = TextUtils.isEmpty(updateConfig.getChannelId()) ? Constants.DEFAULT_NOTIFICATION_CHANNEL_ID : updateConfig.getChannelId();
                this.channelName = TextUtils.isEmpty(updateConfig.getChannelName()) ? "AppUpdater" : updateConfig.getChannelName();
            }
            if (updateConfig.getNotificationIcon() <= 0) {
                this.notificationIcon = AppUtils.getAppIcon(context);
            } else {
                this.notificationIcon = updateConfig.getNotificationIcon();
            }
            this.isInstallApk = updateConfig.isInstallApk();
            this.authority = updateConfig.getAuthority();
            if (TextUtils.isEmpty(updateConfig.getAuthority())) {
                this.authority = AppUtils.getFileProviderAuthority(context);
            }
            this.isShowPercentage = updateConfig.isShowPercentage();
            this.isDeleteCancelFile = updateConfig.isDeleteCancelFile();
            this.isSupportCancelDownload = updateConfig.isSupportCancelDownload();
            this.isReDownload = updateConfig.isReDownload() && downloadService.mCount < updateConfig.getReDownloads();
        }

        @Override
        public void onStart(String str) {
            INotification iNotification;
            LogUtils.i("url: " + str);
            this.downloadService.isDownloading = true;
            this.lastProgress = 0;
            if (this.isShowNotification && (iNotification = this.notification) != null) {
                iNotification.onStart(this.context, this.notifyId, this.channelId, this.channelName, this.notificationIcon, getString(R.string.app_updater_start_notification_title), getString(R.string.app_updater_start_notification_content), this.config.isVibrate(), this.config.isSound(), this.isSupportCancelDownload);
            }
            UpdateCallback updateCallback = this.callback;
            if (updateCallback != null) {
                updateCallback.onStart(str);
            }
        }

        @Override
        public void onProgress(long j, long j2) {
            int i;
            long currentTimeMillis = System.currentTimeMillis();
            boolean z = false;
            if (this.lastTime + 200 < currentTimeMillis || j == j2) {
                this.lastTime = currentTimeMillis;
                if (j2 > 0) {
                    int round = Math.round(((j * 1.0f) / j2) * 100.0f);
                    if (round != this.lastProgress) {
                        this.lastProgress = round;
                        z = true;
                    }
                    LogUtils.i(String.format(Locale.getDefault(), "%d%%\t| %d/%d", Integer.valueOf(round), Long.valueOf(j), Long.valueOf(j2)));
                    i = round;
                } else {
                    LogUtils.i(String.format(Locale.getDefault(), "%d/%d", Long.valueOf(j), Long.valueOf(j2)));
                    i = 0;
                }
                if (this.isShowNotification && this.notification != null) {
                    String string = this.context.getString(R.string.app_updater_progress_notification_content);
                    if (j2 > 0) {
                        String format = this.isShowPercentage ? String.format(Locale.getDefault(), "%s%d%%", string, Integer.valueOf(i)) : string;
                        INotification iNotification = this.notification;
                        Context context = this.context;
                        iNotification.onProgress(context, this.notifyId, this.channelId, this.notificationIcon, context.getString(R.string.app_updater_progress_notification_title), format, i, 100, this.isSupportCancelDownload);
                    } else {
                        INotification iNotification2 = this.notification;
                        Context context2 = this.context;
                        iNotification2.onProgress(context2, this.notifyId, this.channelId, this.notificationIcon, context2.getString(R.string.app_updater_progress_notification_title), string, (int) j, -1, this.isSupportCancelDownload);
                    }
                }
            }
            boolean z2 = z;
            UpdateCallback updateCallback = this.callback;
            if (updateCallback == null || j2 <= 0) {
                return;
            }
            updateCallback.onProgress(j, j2, z2);
        }

        @Override
        public void onFinish(File file) {
            INotification iNotification;
            LogUtils.d("File: " + file);
            this.downloadService.isDownloading = false;
            if (this.isShowNotification && (iNotification = this.notification) != null) {
                iNotification.onFinish(this.context, this.notifyId, this.channelId, this.notificationIcon, getString(R.string.app_updater_finish_notification_title), getString(R.string.app_updater_finish_notification_content), file, this.authority);
            }
            if (this.isInstallApk) {
                AppUtils.installApk(this.context, file, this.authority);
            }
            UpdateCallback updateCallback = this.callback;
            if (updateCallback != null) {
                updateCallback.onFinish(file);
            }
            this.downloadService.stopService();
        }

        @Override
        public void onError(Exception exc) {
            LogUtils.w(exc.getMessage());
            this.downloadService.isDownloading = false;
            if (this.isShowNotification && this.notification != null) {
                this.notification.onError(this.context, this.notifyId, this.channelId, this.notificationIcon, getString(R.string.app_updater_error_notification_title), getString(this.isReDownload ? R.string.app_updater_error_notification_content_re_download : R.string.app_updater_error_notification_content), this.isReDownload, this.config);
            }
            UpdateCallback updateCallback = this.callback;
            if (updateCallback != null) {
                updateCallback.onError(exc);
            }
            if (this.isReDownload) {
                return;
            }
            this.downloadService.stopService();
        }

        @Override
        public void onCancel() {
            File file;
            INotification iNotification;
            LogUtils.d("Cancel download.");
            this.downloadService.isDownloading = false;
            if (this.isShowNotification && (iNotification = this.notification) != null) {
                iNotification.onCancel(this.context, this.notifyId);
            }
            UpdateCallback updateCallback = this.callback;
            if (updateCallback != null) {
                updateCallback.onCancel();
            }
            if (this.isDeleteCancelFile && (file = this.apkFile) != null) {
                file.delete();
            }
            this.downloadService.stopService();
        }

        private String getString(int i) {
            return this.context.getString(i);
        }
    }

    @Override
    public void onDestroy() {
        this.isDownloading = false;
        this.mHttpManager = null;
        this.mUpdateCallback = null;
        this.mNotification = null;
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return this.mDownloadBinder;
    }

    public class DownloadBinder extends Binder {
        public DownloadBinder() {
        }

        public void start(UpdateConfig updateConfig) {
            start(updateConfig, null);
        }

        public void start(UpdateConfig updateConfig, UpdateCallback updateCallback) {
            start(updateConfig, null, updateCallback);
        }

        public void start(UpdateConfig updateConfig, IHttpManager iHttpManager, UpdateCallback updateCallback) {
            start(updateConfig, iHttpManager, updateCallback, null);
        }

        public void start(UpdateConfig updateConfig, IHttpManager iHttpManager, UpdateCallback updateCallback, INotification iNotification) {
            DownloadService.this.startDownload(updateConfig, iHttpManager, updateCallback, iNotification);
        }
    }
}