Skip to content

Commit

Permalink
app: add updater
Browse files Browse the repository at this point in the history
  • Loading branch information
hariimurti committed Apr 10, 2020
1 parent 71ac948 commit 67ded62
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 12 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand Down
20 changes: 20 additions & 0 deletions app/src/main/java/net/harimurti/tv/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package net.harimurti.tv;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;

public class App extends Application {
@SuppressLint("StaticFieldLeak")
protected static Context context = null;

@Override
public void onCreate() {
super.onCreate();
context = this;
}

public static Context getContext() {
return context;
}
}
61 changes: 56 additions & 5 deletions app/src/main/java/net/harimurti/tv/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package net.harimurti.tv;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import android.annotation.SuppressLint;
import android.app.DownloadManager;
import android.net.Uri;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
Expand All @@ -23,8 +27,10 @@

import net.harimurti.tv.adapter.ViewPagerAdapter;
import net.harimurti.tv.data.Playlist;
import net.harimurti.tv.data.Release;
import net.harimurti.tv.extra.AsyncSleep;
import net.harimurti.tv.extra.Network;
import net.harimurti.tv.extra.Preferences;
import net.harimurti.tv.extra.TLSSocketFactory;

import java.security.KeyManagementException;
Expand All @@ -34,14 +40,17 @@ public class MainActivity extends AppCompatActivity {
private View layoutStatus, layoutSpin, layoutText;
private TextView tvStatus, tvRetry;

private StringRequest request;
private StringRequest playlist;
private RequestQueue volley;

@SuppressLint("DefaultLocale")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Preferences preferences = new Preferences();

// define some view
TabLayout tabLayout = findViewById(R.id.tab_layout);
ViewPager2 viewPager = findViewById(R.id.view_pager);
Expand All @@ -51,7 +60,7 @@ protected void onCreate(Bundle savedInstanceState) {
tvStatus = findViewById(R.id.text_status);
tvRetry = findViewById(R.id.text_retry);

request = new StringRequest(Request.Method.GET,
playlist = new StringRequest(Request.Method.GET,
getString(R.string.json_playlist),
response -> {
try {
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/net/harimurti/tv/data/Release.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package net.harimurti.tv.data;

import java.util.List;

public class Release
{
public int versionCode;
public String versionName;
public List<String> changelog;
public String downloadUrl;
}
47 changes: 47 additions & 0 deletions app/src/main/java/net/harimurti/tv/extra/Preferences.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
11 changes: 11 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
<resources>
<string name="app_name">Nonton TV</string>

<!-- some text -->
<string name="something_went_wrong">Ada sesuatu yang salah!!!</string>
<string name="source_offline">Siaran tidak dapat diakses!!!</string>
<string name="text_auto_retry">akan dicoba ulang otomatis dalam 5</string>
<string name="text_auto_retry_second">akan dicoba ulang otomatis dalam %d</string>
<string name="text_auto_retry_now">akan dicoba ulang otomatis sekarang</string>
<string name="no_network">Tidak ada koneksi internet!!!</string>

<!-- json link -->
<string name="json_playlist">https://github.com/hariimurti/NontonTV/raw/master/json/playlist.json</string>
<string name="json_release">https://github.com/hariimurti/NontonTV/raw/master/json/release.json</string>

<!-- alert update -->
<string name="alert_new_update">Pembaharuan!!!</string>
<string name="alert_download">Unduh</string>
<string name="alert_close">Tutup</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>
</resources>
6 changes: 3 additions & 3 deletions app/version.prop
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions json/release.json
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit 67ded62

Please sign in to comment.