Skip to content

Commit

Permalink
app: fix downloader apk
Browse files Browse the repository at this point in the history
  • Loading branch information
hariimurti committed May 5, 2020
1 parent 8211373 commit a53ee75
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
android:name=".App"
Expand Down
35 changes: 28 additions & 7 deletions app/src/main/java/net/harimurti/tv/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.viewpager2.widget.ViewPager2;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.DownloadManager;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Switch;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
Expand Down Expand Up @@ -102,7 +108,6 @@ else if (!Network.IsConnected()) {
response -> {
try {
Release release = new Gson().fromJson(response, Release.class);
preferences.setLastCheckUpdate();

if (release.versionCode <= BuildConfig.VERSION_CODE) return;

Expand Down Expand Up @@ -173,20 +178,36 @@ private void showAlertError(String error) {
}

private void showAlertUpdate(String message, String fileUrl) {
if (Build.VERSION.SDK_INT >= VERSION_CODES.M
&& ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },1000);
}

AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle(R.string.alert_new_update)
.setMessage(message)
.setPositiveButton(R.string.dialog_download, (dialog, id) -> downloadFile(fileUrl))
.setNegativeButton(R.string.dialog_close, (dialog, id) -> dialog.cancel());
.setNegativeButton(R.string.dialog_skip, (dialog, id) -> preferences.setLastCheckUpdate());
alert.create().show();
}

private void downloadFile(String url) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
DownloadManager manager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
if (manager != null) {
manager.enqueue(request);
DownloadManager dm = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
if (dm == null) return;

Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri)
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, uri.getLastPathSegment())
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setVisibleInDownloadsUi(true);

try {
dm.enqueue(request);
}
catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

<!-- alert update -->
<string name="alert_new_update">Pembaharuan</string>
<string name="dialog_download">Unduh</string>
<string name="dialog_close">Tutup</string>
<string name="dialog_download">Unduh APK</string>
<string name="dialog_skip">Abaikan Sehari</string>
<string name="message_update">Versi\n• Terpasang : %s (%d)\n• Terkini : %s (%d)\n\nPerubahan :</string>
<string name="message_update_changelog">\n• %s</string>
<string name="message_update_no_changelog">\n• lain-lain</string>
Expand Down

0 comments on commit a53ee75

Please sign in to comment.