修复无法直接下载最新版本的问题

This commit is contained in:
hyb1996 2018-07-11 17:59:04 +08:00
parent f78f569461
commit 5251028b0b
10 changed files with 91 additions and 191 deletions

View File

@ -8,8 +8,8 @@ android {
applicationId "org.autojs.autojs"
minSdkVersion 17
targetSdkVersion 23
versionCode 404
versionName "4.0.1 Beta"
versionCode 403
versionName "4.0.2 Alpha"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
ndk {

View File

@ -236,14 +236,6 @@
</intent-filter>
</receiver>
<!-- 声明广告SDK所需要的组件 -->
<service
android:name="com.qq.e.comm.DownloadService"
android:exported="false"/>
<activity
android:name="com.qq.e.ads.ADActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"/>
</application>
</manifest>

View File

@ -4,6 +4,7 @@ import org.autojs.autojs.network.entity.VersionInfo;
import io.reactivex.Observable;
import retrofit2.http.GET;
import retrofit2.http.Headers;
/**
* Created by Stardust on 2017/9/20.
@ -12,6 +13,7 @@ import retrofit2.http.GET;
public interface UpdateCheckApi {
@GET("/assets/autojs/version.json")
@Headers("Cache-Control: no-cache")
Observable<VersionInfo> checkForUpdates();
}

View File

@ -1,11 +1,20 @@
package org.autojs.autojs.network.download;
import android.content.Context;
import android.util.Log;
import com.afollestad.materialdialogs.MaterialDialog;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import com.stardust.concurrent.VolatileBox;
import com.stardust.pio.PFiles;
import org.autojs.autojs.R;
import org.autojs.autojs.model.script.ScriptFile;
import org.autojs.autojs.network.NodeBB;
import org.autojs.autojs.network.api.DownloadApi;
import org.autojs.autojs.tool.SimpleObserver;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
@ -14,6 +23,7 @@ import java.net.URLDecoder;
import java.util.concurrent.ConcurrentHashMap;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.PublishSubject;
import okhttp3.OkHttpClient;
@ -28,6 +38,7 @@ import retrofit2.Retrofit;
public class DownloadManager {
private static final String LOG_TAG = "DownloadManager";
private static DownloadManager sInstance;
private static final int RETRY_COUNT = 3;
@ -81,6 +92,44 @@ public class DownloadManager {
return task.progress();
}
public Observable<File> downloadWithProgress(Context context, String url, String path) {
String fileName = DownloadManager.parseFileNameLocally(url);
return download(url, path, createDownloadProgressDialog(context, url, fileName));
}
private MaterialDialog createDownloadProgressDialog(Context context, String url, String fileName) {
return new MaterialDialog.Builder(context)
.progress(false, 100)
.title(fileName)
.cancelable(false)
.positiveText(R.string.text_cancel_download)
.onPositive((dialog, which) -> DownloadManager.getInstance().cancelDownload(url))
.show();
}
private Observable<File> download(String url, String path, MaterialDialog progressDialog) {
PublishSubject<File> subject = PublishSubject.create();
DownloadManager.getInstance().download(url, path)
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(progressDialog::setProgress)
.subscribe(new SimpleObserver<Integer>() {
@Override
public void onComplete() {
progressDialog.dismiss();
subject.onNext(new File(path));
subject.onComplete();
}
@Override
public void onError(Throwable error) {
Log.e(LOG_TAG, "Download failed", error);
progressDialog.dismiss();
subject.onError(error);
}
});
return subject;
}
public void cancelDownload(String url) {
VolatileBox<Boolean> status = mDownloadStatuses.get(url);
if (status != null) {

View File

@ -21,6 +21,7 @@ import com.stardust.app.GlobalAppContext;
import com.stardust.pio.PFile;
import com.stardust.pio.PFiles;
import com.stardust.pio.UncheckedIOException;
import org.autojs.autojs.R;
import org.autojs.autojs.external.ScriptIntents;
import org.autojs.autojs.storage.file.TmpScriptFiles;
@ -260,48 +261,15 @@ public class ScriptOperations {
}
});
})
.flatMap(savePath -> download(url, savePath, createDownloadProgressDialog(url, fileName)));
}
private MaterialDialog createDownloadProgressDialog(String url, String fileName) {
return new MaterialDialog.Builder(mContext)
.progress(false, 100)
.title(fileName)
.cancelable(false)
.positiveText(R.string.text_cancel_download)
.onPositive((dialog, which) -> DownloadManager.getInstance().cancelDownload(url))
.show();
}
public Observable<ScriptFile> download(String url, String path, MaterialDialog progressDialog) {
PublishSubject<ScriptFile> subject = PublishSubject.create();
DownloadManager.getInstance().download(url, path)
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(progressDialog::setProgress)
.subscribe(new SimpleObserver<Integer>() {
@Override
public void onComplete() {
progressDialog.dismiss();
subject.onNext(new ScriptFile(path));
subject.onComplete();
}
@Override
public void onError(Throwable error) {
Log.e(LOG_TAG, "Download failed", error);
progressDialog.dismiss();
showMessage(R.string.text_download_failed);
subject.onError(error);
}
});
return subject;
.flatMap(savePath -> DownloadManager.getInstance().downloadWithProgress(mContext, url, savePath))
.map(ScriptFile::new);
}
public Observable<ScriptFile> temporarilyDownload(String url) {
String fileName = DownloadManager.parseFileNameLocally(url);
return Observable.fromCallable(() -> TmpScriptFiles.create(mContext))
.flatMap(tmpFile ->
download(url, tmpFile.getPath(), createDownloadProgressDialog(url, fileName)));
DownloadManager.getInstance().downloadWithProgress(mContext, url, tmpFile.getPath()))
.map(ScriptFile::new);
}
public void importFile() {

View File

@ -1,5 +1,6 @@
package org.autojs.autojs.ui.main.community;
import android.annotation.SuppressLint;
import android.content.Context;
import android.net.Uri;
import android.support.design.widget.BottomSheetDialog;
@ -7,6 +8,7 @@ import android.support.design.widget.Snackbar;
import android.util.AttributeSet;
import android.webkit.ValueCallback;
import android.webkit.WebView;
import android.widget.Toast;
import org.autojs.autojs.R;
import org.autojs.autojs.network.NodeBB;
@ -62,6 +64,7 @@ public class CommunityWebView extends EWebView {
mBottomSheetDialog.show();
}
@SuppressLint("CheckResult")
@Optional
@OnClick(R.id.save)
void save() {
@ -69,14 +72,18 @@ public class CommunityWebView extends EWebView {
new ScriptOperations(getContext(), CommunityWebView.this)
.download(mUrl)
.observeOn(AndroidSchedulers.mainThread())
.onErrorResumeNext(Observable.empty())
.subscribe(file ->
Snackbar.make(CommunityWebView.this, getResources().getString(R.string.format_file_downloaded, file.getPath())
, Snackbar.LENGTH_LONG)
.setAction(R.string.text_open, v -> Scripts.edit(file))
.show());
Snackbar.make(CommunityWebView.this, getResources().getString(R.string.format_file_downloaded, file.getPath())
, Snackbar.LENGTH_LONG)
.setAction(R.string.text_open, v -> Scripts.edit(file))
.show(),
error -> {
error.printStackTrace();
Snackbar.make(CommunityWebView.this, R.string.text_download_failed, Toast.LENGTH_SHORT).show();
});
}
@SuppressLint("CheckResult")
@Optional
@OnClick(R.id.run)
void run() {
@ -84,10 +91,12 @@ public class CommunityWebView extends EWebView {
new ScriptOperations(getContext(), CommunityWebView.this)
.temporarilyDownload(mUrl)
.observeOn(AndroidSchedulers.mainThread())
.onErrorResumeNext(Observable.empty())
.subscribe(file -> {
Snackbar.make(CommunityWebView.this, R.string.text_start_running, Snackbar.LENGTH_SHORT).show();
Scripts.run(file);
}, error -> {
error.printStackTrace();
Snackbar.make(CommunityWebView.this, R.string.text_download_failed, Toast.LENGTH_SHORT).show();
});
}

View File

@ -1,5 +1,6 @@
package org.autojs.autojs.ui.update;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
@ -14,15 +15,18 @@ import android.widget.TextView;
import android.widget.Toast;
import com.afollestad.materialdialogs.MaterialDialog;
import com.stardust.util.IntentUtil;
import org.autojs.autojs.BuildConfig;
import org.autojs.autojs.R;
import org.autojs.autojs.network.download.DownloadManager;
import org.autojs.autojs.network.entity.VersionInfo;
import org.autojs.autojs.storage.file.StorageFileProvider;
import org.autojs.autojs.tool.IntentTool;
import com.stardust.util.DownloadTask;
import com.stardust.util.IntentUtil;
import org.autojs.autojs.ui.widget.CommonMarkdownView;
import io.reactivex.android.schedulers.AndroidSchedulers;
/**
* Created by Stardust on 2017/4/9.
*/
@ -103,44 +107,26 @@ public class UpdateInfoDialogBuilder extends MaterialDialog.Builder {
}
Button button = (Button) View.inflate(getContext(), R.layout.dialog_update_info_btn, null);
button.setText(R.string.text_directly_download);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
directlyDownload(info.downloadUrl);
}
});
button.setOnClickListener(v -> directlyDownload(info.downloadUrl));
container.addView(button);
}
@SuppressLint("CheckResult")
private void directlyDownload(String downloadUrl) {
final MaterialDialog dialog = new MaterialDialog.Builder(getContext())
.title(R.string.text_downloading)
.progress(false, 100)
.show();
final String path = StorageFileProvider.getDefaultDirectoryPath() + "AutoJs.apk";
final DownloadTask task = new DownloadTask() {
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
dialog.setProgress(values[0]);
}
DownloadManager.getInstance().downloadWithProgress(getContext(), downloadUrl, path)
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(file -> IntentUtil.installApk(getContext(), file.getPath()),
error -> {
error.printStackTrace();
Toast.makeText(getContext(), R.string.text_download_failed, Toast.LENGTH_SHORT).show();
});
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
dialog.dismiss();
if (!result) {
Toast.makeText(getContext(), R.string.text_download_failed, Toast.LENGTH_SHORT).show();
} else {
IntentUtil.installApk(getContext(), path);
}
}
};
task.execute(downloadUrl, path);
}
private void setReleaseNotes(View view, VersionInfo info) {
CommonMarkdownView markdownView = (CommonMarkdownView) view.findViewById(R.id.release_notes);
CommonMarkdownView markdownView = view.findViewById(R.id.release_notes);
markdownView.loadMarkdown(info.releaseNotes);
}
}

View File

@ -1,107 +0,0 @@
package com.stardust.util;
import android.os.AsyncTask;
import com.stardust.net.AutoHttpURLConnection;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.Callable;
/**
* Created by Stardust on 2017/4/10.
*/
public class DownloadTask extends AsyncTask<String, Integer, Boolean> {
public interface ProgressListener {
void publishProgress(int i);
}
public static class Download implements Callable<Boolean> {
private String mUrl;
private String mPath;
private ProgressListener mListener;
private volatile boolean mCanceled = false;
public Download(String url, String path, ProgressListener listener) {
mUrl = url;
mPath = path;
mListener = listener;
}
@Override
public Boolean call() throws IOException {
try (AutoHttpURLConnection connection = new AutoHttpURLConnection(mUrl)) {
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
return false;
}
int total = connection.getContentLength();
InputStream input = connection.getInputStream();
FileOutputStream output = new FileOutputStream(mPath);
return download(input, output, total);
} catch (Exception e) {
throw e;
}
}
public void cancel() {
mCanceled = true;
}
private boolean download(InputStream input, FileOutputStream output, int total) throws IOException {
byte buffer[] = new byte[8192];
long downloaded = 0;
int read;
while ((read = input.read(buffer)) != -1) {
if (mCanceled) {
input.close();
return false;
}
downloaded += read;
if (total > 0)
publishProgress((int) (downloaded * 100 / total));
output.write(buffer, 0, read);
}
return true;
}
private void publishProgress(int i) {
if (mListener != null) {
mListener.publishProgress(i);
}
}
}
private Download mDownload;
@Override
protected Boolean doInBackground(String... params) {
mDownload = new Download(params[0], params[1], new ProgressListener() {
@Override
public void publishProgress(int i) {
DownloadTask.this.publishProgress(i);
}
});
try {
return mDownload.call();
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
mDownload = null;
}
}
@Override
protected void onCancelled() {
super.onCancelled();
if (mDownload != null)
mDownload.cancel();
}
}

View File

@ -86,6 +86,7 @@ public class IntentUtil {
public static void installApk(Context context, String path) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);