From 67ded620b9a0916aa52c7589da0e3dfbb535892b Mon Sep 17 00:00:00 2001 From: Harimurti Date: Fri, 10 Apr 2020 09:43:47 +0700 Subject: [PATCH] app: add updater --- app/src/main/AndroidManifest.xml | 1 + app/src/main/java/net/harimurti/tv/App.java | 20 ++++++ .../java/net/harimurti/tv/MainActivity.java | 61 +++++++++++++++++-- .../java/net/harimurti/tv/data/Release.java | 11 ++++ .../net/harimurti/tv/extra/Preferences.java | 47 ++++++++++++++ app/src/main/res/values/strings.xml | 11 ++++ app/version.prop | 6 +- json/release.json | 8 +-- 8 files changed, 153 insertions(+), 12 deletions(-) create mode 100644 app/src/main/java/net/harimurti/tv/App.java create mode 100644 app/src/main/java/net/harimurti/tv/data/Release.java create mode 100644 app/src/main/java/net/harimurti/tv/extra/Preferences.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 54ff441..793f661 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -6,6 +6,7 @@ { try { @@ -67,16 +76,49 @@ protected void onCreate(Bundle savedInstanceState) { }, error -> ShowErrorMessage(error.getMessage(), true)); + StringRequest update = new StringRequest(Request.Method.GET, + getString(R.string.json_release), + response -> { + try { + Release release = new Gson().fromJson(response, Release.class); + preferences.setLastCheckUpdate(); + + if (release.versionCode <= BuildConfig.VERSION_CODE) return; + + StringBuilder message = new StringBuilder( + String.format(getString(R.string.message_update), + BuildConfig.VERSION_NAME, BuildConfig.VERSION_CODE, + release.versionName, release.versionCode)); + for (String log : release.changelog) { + message.append(String.format(getString(R.string.message_update_changelog), log)); + } + if (release.changelog.size() == 0) { + message.append(getString(R.string.message_update_no_changelog)); + } + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(R.string.alert_new_update); + builder.setMessage(message.toString()) + .setPositiveButton(R.string.alert_download, (dialog, id) -> + DownloadFile(release.downloadUrl)) + .setNegativeButton(R.string.alert_close, (dialog, id) -> + dialog.cancel()); + builder.create().show(); + } catch (Exception ignore) {} + }, null); + BaseHttpStack stack = new HurlStack(); if (Build.VERSION.SDK_INT == VERSION_CODES.KITKAT) { try { stack = new HurlStack(null, new TLSSocketFactory()); } catch (KeyManagementException | NoSuchAlgorithmException e) { - Log.e("Volley", "Could not create new stack for TLS v1.2"); + Log.e("HttpStack", "Could not create new stack for TLS v1.2"); } } volley = Volley.newRequestQueue(this, stack); - volley.add(request); + volley.add(playlist); + if (!preferences.isCheckedUpdate()) { + volley.add(update); + } } private void ShowLayoutMessage(int visibility, boolean isMessage) { @@ -114,7 +156,7 @@ public void onCountDown(int left) { @Override public void onFinish() { if (network.IsConnected()) { - volley.add(request); + volley.add(playlist); } else { ShowErrorMessage(getString(R.string.no_network), true); @@ -123,6 +165,15 @@ public void onFinish() { }).start(5); } + 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); + } + } + @Override public void onBackPressed() { //super.onBackPressed(); diff --git a/app/src/main/java/net/harimurti/tv/data/Release.java b/app/src/main/java/net/harimurti/tv/data/Release.java new file mode 100644 index 0000000..d8fe6f7 --- /dev/null +++ b/app/src/main/java/net/harimurti/tv/data/Release.java @@ -0,0 +1,11 @@ +package net.harimurti.tv.data; + +import java.util.List; + +public class Release +{ + public int versionCode; + public String versionName; + public List changelog; + public String downloadUrl; +} diff --git a/app/src/main/java/net/harimurti/tv/extra/Preferences.java b/app/src/main/java/net/harimurti/tv/extra/Preferences.java new file mode 100644 index 0000000..0d71f12 --- /dev/null +++ b/app/src/main/java/net/harimurti/tv/extra/Preferences.java @@ -0,0 +1,47 @@ +package net.harimurti.tv.extra; + +import android.content.SharedPreferences; +import android.preference.PreferenceManager; + +import net.harimurti.tv.App; + +import java.util.Calendar; +import java.util.Date; + +public class Preferences { + private static final String LAST_CHECK_UPDATE = "LAST_CHECK_UPDATE"; + + private SharedPreferences preferences; + private SharedPreferences.Editor editor; + + public Preferences() { + preferences = PreferenceManager.getDefaultSharedPreferences(App.getContext()); + } + + public void setLastCheckUpdate() { + Calendar nextday = Calendar.getInstance(); + nextday.add(Calendar.DATE, 1); + nextday.set(Calendar.HOUR_OF_DAY, 0); + + editor = preferences.edit(); + editor.putLong(LAST_CHECK_UPDATE, nextday.getTimeInMillis()); + editor.apply(); + } + + public boolean isCheckedUpdate() { + try { + Calendar last = Calendar.getInstance(); + last.setTimeInMillis(preferences.getLong(LAST_CHECK_UPDATE, 0)); + Date dateLast = last.getTime(); + + Calendar now = Calendar.getInstance(); + now.set(Calendar.HOUR_OF_DAY, 0); + Date dateNow = now.getTime(); + + return dateLast.after(dateNow); + } + catch (Exception ignore) { + return false; + } + } +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7161db6..0115e00 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,6 +1,7 @@ Nonton TV + Ada sesuatu yang salah!!! Siaran tidak dapat diakses!!! akan dicoba ulang otomatis dalam 5 @@ -8,5 +9,15 @@ akan dicoba ulang otomatis sekarang Tidak ada koneksi internet!!! + https://github.com/hariimurti/NontonTV/raw/master/json/playlist.json + https://github.com/hariimurti/NontonTV/raw/master/json/release.json + + + Pembaharuan!!! + Unduh + Tutup + Versi\n• Terpasang : %s (%d)\n• Terkini : %s (%d)\n\nPerubahan : + \n• %s + \n• lain-lain diff --git a/app/version.prop b/app/version.prop index 86696eb..87175ef 100644 --- a/app/version.prop +++ b/app/version.prop @@ -1,4 +1,4 @@ -#Thu Apr 09 21:57:50 ICT 2020 -VERSION_MINOR=0 -VERSION_CODE=372 +#Fri Apr 10 09:42:44 ICT 2020 +VERSION_MINOR=1 +VERSION_CODE=384 VERSION_MAJOR=1 diff --git a/json/release.json b/json/release.json index 00b32cf..e03007b 100644 --- a/json/release.json +++ b/json/release.json @@ -1,6 +1,6 @@ { - "versionCode": 327, - "versionName": "v1.0", - "changelog": ["rilisan pertama", "upto 87 channel"], - "downloadUrl": "https://github.com/hariimurti/NontonTV/releases/download/v1.0/net.harimurti.tv_v1.0_b327.apk" + "versionCode": 384, + "versionName": "v1.1", + "changelog": ["perbaikan ssl error (android 4.4)", "pemberitahuan pembaharuan", "perubahan lain"], + "downloadUrl": "https://github.com/hariimurti/NontonTV/releases/download/v1.1/net.harimurti.tv_v1.1_b384.apk" } \ No newline at end of file