Virtual Trading v2.0.95版本的 MD5 值为:ef0f95d706f7364286da0edbbd29a27d

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


package com.sunny.CustomWebView;

import android.app.Activity;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.webkit.CookieManager;
import android.webkit.URLUtil;
import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import com.google.appinventor.components.runtime.OnDestroyListener;
import java.util.Timer;
import java.util.TimerTask;

@DesignerComponent(androidMinSdk = 21, category = ComponentCategory.EXTENSION, description = "Helper class of CustomWebView extension for downloading files <br> Developed by Sunny Gupta", helpUrl = "https://github.com/vknow360/CustomWebView", iconName = "https://res.cloudinary.com/andromedaviewflyvipul/image/upload/c_scale,h_20,w_20/v1571472765/ktvu4bapylsvnykoyhdm.png", nonVisible = true, version = 1, versionName = "1.1")
@SimpleObject(external = true)
public class DownloadHelper extends AndroidNonvisibleComponent implements OnDestroyListener {
    public BroadcastReceiver completed;
    public Context context;
    public DownloadManager downloadManager;
    public long lastRequestId;

    public DownloadHelper(ComponentContainer componentContainer) {
        super(componentContainer.$form());
        this.completed = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getLongExtra("extra_download_id", -1L) == DownloadHelper.this.lastRequestId) {
                    DownloadHelper.this.DownloadCompleted();
                }
            }
        };
        Activity $context = componentContainer.$context();
        this.context = $context;
        this.downloadManager = (DownloadManager) $context.getSystemService("download");
        this.context.registerReceiver(this.completed, new IntentFilter("android.intent.action.DOWNLOAD_COMPLETE"));
    }

    @SimpleFunction(description = "Cancels the current download request")
    public void Cancel() {
        this.downloadManager.remove(this.lastRequestId);
    }

    @SimpleFunction(description = "Downloads the given file")
    public void Download(String str, String str2, String str3, String str4, String str5) {
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(str));
        request.setMimeType(str2);
        request.addRequestHeader("cookie", CookieManager.getInstance().getCookie(str));
        request.setDescription("Downloading file...");
        request.setTitle(str4);
        request.setNotificationVisibility(1);
        if (str5.isEmpty()) {
            str5 = Environment.DIRECTORY_DOWNLOADS;
        }
        if (str4.isEmpty()) {
            str4 = URLUtil.guessFileName(str, str3, str2);
            request.setTitle(str4);
        }
        if (Build.VERSION.SDK_INT >= 29) {
            request.setDestinationInExternalFilesDir(this.context, str5, str4);
        } else {
            request.setDestinationInExternalPublicDir(str5, str4);
        }
        this.lastRequestId = this.downloadManager.enqueue(request);
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                DownloadManager.Query query = new DownloadManager.Query();
                query.setFilterById(DownloadHelper.this.lastRequestId);
                Cursor query2 = DownloadHelper.this.downloadManager.query(query);
                if (query2.moveToFirst()) {
                    int i = query2.getInt(query2.getColumnIndex("bytes_so_far"));
                    int i2 = query2.getInt(query2.getColumnIndex("total_size"));
                    query2.close();
                    final int i3 = (i * 100) / i2;
                    DownloadHelper.this.form.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (i3 >= 99) {
                                timer.cancel();
                                timer.purge();
                            }
                            DownloadHelper.this.DownloadProgressChanged(i3);
                        }
                    });
                }
            }
        }, 0L, 1000L);
    }

    @SimpleEvent(description = "Event invoked when downloading gets completed")
    public void DownloadCompleted() {
        EventDispatcher.dispatchEvent(this, "DownloadCompleted", new Object[0]);
    }

    @SimpleEvent(description = "Event invoked when downloading progress changes")
    public void DownloadProgressChanged(int i) {
        EventDispatcher.dispatchEvent(this, "DownloadProgressChanged", Integer.valueOf(i));
    }

    @SimpleFunction(description = "Tries to open the last downloaded file")
    public void OpenFile() {
        try {
            this.downloadManager.openDownloadedFile(this.lastRequestId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
        this.context.unregisterReceiver(this.completed);
    }
}