fix apk install
This commit is contained in:
@@ -9,18 +9,20 @@ import android.content.IntentFilter;
|
||||
import android.net.Uri;
|
||||
import android.os.Environment;
|
||||
|
||||
import androidx.core.content.FileProvider;
|
||||
|
||||
import com.vk.core.dialogs.alert.VkAlertDialog;
|
||||
import com.vtosters.lite.R;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import b.h.g.m.FileUtils;
|
||||
import ru.vtosters.lite.ui.activities.APKInstallActivity;
|
||||
import ru.vtosters.lite.utils.AndroidUtils;
|
||||
|
||||
public class OTADownloader {
|
||||
|
||||
private static final BroadcastReceiver callback = new BroadcastReceiver() {
|
||||
@SuppressLint("Range")
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
var extras = intent.getExtras();
|
||||
@@ -30,24 +32,15 @@ public class OTADownloader {
|
||||
var query = new DownloadManager.Query();
|
||||
var id = extras.getLong(DownloadManager.EXTRA_DOWNLOAD_ID);
|
||||
query.setFilterById(id);
|
||||
var manager = (DownloadManager) AndroidUtils.getGlobalContext().getSystemService(Context.DOWNLOAD_SERVICE);
|
||||
var manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
|
||||
var cursor = manager.query(query);
|
||||
if (cursor.moveToFirst()) {
|
||||
@SuppressLint("Range") var status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
|
||||
if (status == DownloadManager.STATUS_SUCCESSFUL) {
|
||||
var downloadDir = new File(Environment.getExternalStorageDirectory(), "Download");
|
||||
if (!downloadDir.exists()) return;
|
||||
var list = downloadDir.listFiles();
|
||||
if (list == null || list.length == 0) return;
|
||||
var maxIndex = -1;
|
||||
for (File file : list) {
|
||||
var name = file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf("/") + 1);
|
||||
if (file.isFile()
|
||||
&& name.matches("VTLite-\\d+\\.apk"))
|
||||
maxIndex = Math.max(maxIndex, Integer.parseInt(name.replaceFirst(".+-(\\d+).+", "$1")));
|
||||
}
|
||||
File apk = maxIndex == -1 ? new File(downloadDir, "VTLite.apk") : new File(downloadDir, "VTLite-" + maxIndex + ".apk");
|
||||
APKInstallActivity.installOta(context, FileUtils.uriFromFile(apk));
|
||||
var uriStr = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
|
||||
var apk = new File(uriStr.replace("file://", ""));
|
||||
var uri = FileProvider.getUriForFile(context, "com.vtosters.lite.common.VKFileProvider", apk);
|
||||
APKInstallActivity.installOta(context, uri);
|
||||
} else if (status == DownloadManager.STATUS_FAILED) {
|
||||
AndroidUtils.sendToast(context.getString(R.string.downloaderr));
|
||||
}
|
||||
@@ -55,7 +48,7 @@ public class OTADownloader {
|
||||
}
|
||||
};
|
||||
|
||||
public static void downloadBuild(String url) {
|
||||
public static void downloadBuild(String url, String commitSHA) {
|
||||
var context = AndroidUtils.getGlobalContext();
|
||||
try {
|
||||
var uri = Uri.parse(url);
|
||||
@@ -64,8 +57,8 @@ public class OTADownloader {
|
||||
|
||||
var request = new DownloadManager.Request(uri);
|
||||
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
|
||||
request.setTitle("VTLite.apk");
|
||||
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "VTLite_" + System.currentTimeMillis() / 1000 + ".apk");
|
||||
request.setTitle("Скачивание обновления");
|
||||
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "VTLite-" + commitSHA + ".apk");
|
||||
|
||||
var manager = (DownloadManager) AndroidUtils.getGlobalContext().getSystemService(Context.DOWNLOAD_SERVICE);
|
||||
manager.enqueue(request);
|
||||
|
||||
@@ -67,6 +67,7 @@ public class APKInstallActivity extends AppCompatActivity {
|
||||
|
||||
startActivity(intent);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
Toast.makeText(this, getString(R.string.apk_install_error) + " " + e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ public class OTADialog implements OTAUtils.OTAListener {
|
||||
.setTitle(mActivity.getString(R.string.newversion) + " " + mHelper.getNewVersionName())
|
||||
.setUpdateInfoView(mHelper.getUpdateDescription())
|
||||
.setPositiveButton(mActivity.getString(R.string.updateanddownload), () -> {
|
||||
OTADownloader.downloadBuild(mHelper.getDownloadUrl());
|
||||
OTADownloader.downloadBuild(mHelper.getDownloadUrl(), mHelper.getCommitSHA());
|
||||
})
|
||||
.show();
|
||||
});
|
||||
|
||||
@@ -21,6 +21,7 @@ public class OTAUtils {
|
||||
private final OTAListener mListener;
|
||||
private JSONObject mReleaseJson;
|
||||
private JSONObject mCommitJson;
|
||||
private String mCommitSHA = "";
|
||||
|
||||
public OTAUtils(OTAListener listener) {
|
||||
this.mListener = listener;
|
||||
@@ -69,9 +70,11 @@ public class OTAUtils {
|
||||
public void a(Call call, Response response) throws IOException {
|
||||
try {
|
||||
mCommitJson = new JSONObject(response.a().g());
|
||||
mCommitSHA = mCommitJson.getJSONObject("object").getString("sha");
|
||||
if (isNewVersion())
|
||||
mListener.onUpdateApplied();
|
||||
else mListener.onUpdateCanceled();
|
||||
else
|
||||
mListener.onUpdateCanceled();
|
||||
} catch (NullPointerException | JSONException e) {
|
||||
e.printStackTrace();
|
||||
mListener.onUpdateCanceled();
|
||||
@@ -80,10 +83,13 @@ public class OTAUtils {
|
||||
});
|
||||
}
|
||||
|
||||
public String getCommitSHA() {
|
||||
return mCommitSHA;
|
||||
}
|
||||
|
||||
public boolean isNewVersion() {
|
||||
try {
|
||||
String commitSHA = mCommitJson.getJSONObject("object").getString("sha");
|
||||
return !commitSHA.startsWith(About.getBuildNumber());
|
||||
return !mCommitSHA.startsWith(About.getBuildNumber());
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user