From 42d1dc0746a2f6ce2e0814d407dcc9e627ec7326 Mon Sep 17 00:00:00 2001 From: Harimurti Date: Wed, 22 Apr 2020 06:55:46 +0700 Subject: [PATCH] app: added settings menu --- app/src/main/AndroidManifest.xml | 19 +++++- .../harimurti/tv/LaunchAtBootReceiver.java | 20 +++++++ .../java/net/harimurti/tv/MainActivity.java | 47 ++++++++++++++- .../java/net/harimurti/tv/PlayerActivity.java | 6 ++ .../net/harimurti/tv/extra/Preferences.java | 33 ++++++++++ app/src/main/res/layout/activity_main.xml | 60 +++++++++++++++++++ app/src/main/res/values/strings.xml | 5 ++ 7 files changed, 185 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/net/harimurti/tv/LaunchAtBootReceiver.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 793f661..af063ac 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -4,6 +4,7 @@ package="net.harimurti.tv"> + - + + - + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/net/harimurti/tv/LaunchAtBootReceiver.java b/app/src/main/java/net/harimurti/tv/LaunchAtBootReceiver.java new file mode 100644 index 0000000..af216be --- /dev/null +++ b/app/src/main/java/net/harimurti/tv/LaunchAtBootReceiver.java @@ -0,0 +1,20 @@ +package net.harimurti.tv; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; + +import net.harimurti.tv.extra.Preferences; + +public class LaunchAtBootReceiver extends BroadcastReceiver { + + @Override + public void onReceive(Context context, Intent intent) { + Preferences preferences = new Preferences(); + if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED) && preferences.isLaunchAtBoot()) { + Intent i = new Intent(context, MainActivity.class); + i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + context.startActivity(i); + } + } +} diff --git a/app/src/main/java/net/harimurti/tv/MainActivity.java b/app/src/main/java/net/harimurti/tv/MainActivity.java index 0371843..24ea45a 100644 --- a/app/src/main/java/net/harimurti/tv/MainActivity.java +++ b/app/src/main/java/net/harimurti/tv/MainActivity.java @@ -6,12 +6,16 @@ import android.annotation.SuppressLint; import android.app.DownloadManager; +import android.content.Intent; import android.net.Uri; import android.os.Build; import android.os.Build.VERSION_CODES; import android.os.Bundle; import android.util.Log; +import android.view.KeyEvent; import android.view.View; +import android.widget.RelativeLayout; +import android.widget.Switch; import android.widget.TextView; import com.android.volley.Request; @@ -37,7 +41,7 @@ import java.security.NoSuchAlgorithmException; public class MainActivity extends AppCompatActivity { - private View layoutStatus, layoutSpin, layoutText; + private View layoutSettings, layoutStatus, layoutSpin, layoutText; private TextView tvStatus, tvRetry; private StringRequest playlist; @@ -59,6 +63,20 @@ protected void onCreate(Bundle savedInstanceState) { layoutText = findViewById(R.id.layout_text); tvStatus = findViewById(R.id.text_status); tvRetry = findViewById(R.id.text_retry); + layoutSettings = findViewById(R.id.layout_settings); + layoutSettings.setOnClickListener(view -> { + layoutSettings.setVisibility(View.GONE); + }); + Switch swLaunch = findViewById(R.id.launch_at_boot); + swLaunch.setChecked(preferences.isLaunchAtBoot()); + swLaunch.setOnClickListener(view -> { + preferences.setLaunchAtBoot(swLaunch.isChecked()); + }); + Switch swOpenLast = findViewById(R.id.open_last_watched); + swOpenLast.setChecked(preferences.isOpenLastWatched()); + swOpenLast.setOnClickListener(view -> { + preferences.setOpenLastWatched(swOpenLast.isChecked()); + }); playlist = new StringRequest(Request.Method.GET, getString(R.string.json_playlist), @@ -119,6 +137,13 @@ protected void onCreate(Bundle savedInstanceState) { if (!preferences.isCheckedUpdate()) { volley.add(update); } + + String streamUrl = preferences.getLastWatched(); + if (preferences.isOpenLastWatched() && !streamUrl.equals("") && PlayerActivity.isFirst) { + Intent intent = new Intent(this, PlayerActivity.class); + intent.putExtra("channel_url", streamUrl); + this.startActivity(intent); + } } private void ShowLayoutMessage(int visibility, boolean isMessage) { @@ -174,9 +199,25 @@ private void DownloadFile(String url) { } } + @Override + public boolean onKeyUp(int keyCode, KeyEvent event) { + if (keyCode == KeyEvent.KEYCODE_MENU) { + layoutSettings.setVisibility(View.VISIBLE); + return true; + } + else { + return super.onKeyUp(keyCode, event); + } + } + @Override public void onBackPressed() { - //super.onBackPressed(); - this.finish(); + if (layoutSettings.getVisibility() == View.VISIBLE) { + layoutSettings.setVisibility(View.GONE); + } + else { + //super.onBackPressed(); + this.finish(); + } } } diff --git a/app/src/main/java/net/harimurti/tv/PlayerActivity.java b/app/src/main/java/net/harimurti/tv/PlayerActivity.java index 6f6da74..7a91a74 100644 --- a/app/src/main/java/net/harimurti/tv/PlayerActivity.java +++ b/app/src/main/java/net/harimurti/tv/PlayerActivity.java @@ -24,8 +24,10 @@ import net.harimurti.tv.extra.AsyncSleep; import net.harimurti.tv.extra.Network; +import net.harimurti.tv.extra.Preferences; public class PlayerActivity extends AppCompatActivity { + public static boolean isFirst = true; private SimpleExoPlayer player; private MediaSource mediaSource; private View layoutStatus, layoutSpin, layoutText; @@ -36,6 +38,9 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_player); + isFirst = false; + Preferences preferences = new Preferences(); + // hide navigation bar getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN); @@ -72,6 +77,7 @@ protected void onCreate(Bundle savedInstanceState) { public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { if (playbackState == Player.STATE_READY) { ShowLayoutMessage(View.GONE, false); + preferences.setLastWatched(url); } if (playbackState == Player.STATE_BUFFERING) { ShowLayoutMessage(View.VISIBLE, false); diff --git a/app/src/main/java/net/harimurti/tv/extra/Preferences.java b/app/src/main/java/net/harimurti/tv/extra/Preferences.java index 0d71f12..5cff93c 100644 --- a/app/src/main/java/net/harimurti/tv/extra/Preferences.java +++ b/app/src/main/java/net/harimurti/tv/extra/Preferences.java @@ -10,6 +10,9 @@ public class Preferences { private static final String LAST_CHECK_UPDATE = "LAST_CHECK_UPDATE"; + private static final String LAST_WATCHED = "LAST_WATCHED"; + private static final String OPEN_LAST_WATCHED = "OPEN_LAST_WATCHED"; + private static final String LAUNCH_AT_BOOT = "LAUNCH_AT_BOOT"; private SharedPreferences preferences; private SharedPreferences.Editor editor; @@ -44,4 +47,34 @@ public boolean isCheckedUpdate() { return false; } } + + public void setLaunchAtBoot(boolean value) { + editor = preferences.edit(); + editor.putBoolean(LAUNCH_AT_BOOT, value); + editor.apply(); + } + + public boolean isLaunchAtBoot() { + return preferences.getBoolean(LAUNCH_AT_BOOT, false); + } + + public void setOpenLastWatched(boolean value) { + editor = preferences.edit(); + editor.putBoolean(OPEN_LAST_WATCHED, value); + editor.apply(); + } + + public boolean isOpenLastWatched() { + return preferences.getBoolean(OPEN_LAST_WATCHED, false); + } + + public void setLastWatched(String value) { + editor = preferences.edit(); + editor.putString(LAST_WATCHED, value); + editor.apply(); + } + + public String getLastWatched() { + return preferences.getString(LAST_WATCHED, ""); + } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index e64e586..420fea4 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -71,4 +71,64 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0115e00..7a61065 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -20,4 +20,9 @@ Versi\n• Terpasang : %s (%d)\n• Terkini : %s (%d)\n\nPerubahan : \n• %s \n• lain-lain + + + Pengaturan + Jalankan app saat boot + Putar terakhir dilihat