Skip to content

Commit

Permalink
app: added settings menu
Browse files Browse the repository at this point in the history
  • Loading branch information
hariimurti committed Apr 21, 2020
1 parent 18fbcd2 commit 42d1dc0
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 5 deletions.
19 changes: 17 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package="net.harimurti.tv">

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

<application
android:name=".App"
Expand All @@ -15,17 +16,31 @@
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true"
tools:ignore="AllowBackup,UnusedAttribute">
<activity android:name="net.harimurti.tv.MainActivity">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="net.harimurti.tv.PlayerActivity"

<activity
android:name=".PlayerActivity"
android:noHistory="true"
android:screenOrientation="landscape"
android:theme="@style/AppThemePlayer"
tools:ignore="LockedOrientationActivity" />

<receiver
android:name=".LaunchAtBootReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>

</manifest>
20 changes: 20 additions & 0 deletions app/src/main/java/net/harimurti/tv/LaunchAtBootReceiver.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
47 changes: 44 additions & 3 deletions app/src/main/java/net/harimurti/tv/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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),
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}
}
}
6 changes: 6 additions & 0 deletions app/src/main/java/net/harimurti/tv/PlayerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
33 changes: 33 additions & 0 deletions app/src/main/java/net/harimurti/tv/extra/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, "");
}
}
60 changes: 60 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,64 @@

</RelativeLayout>

<RelativeLayout
android:id="@+id/layout_settings"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_dark_transparent"
android:visibility="gone">

<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
app:cardCornerRadius="10dp"
app:cardElevation="5dp">

<LinearLayout
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical">

<TextView
style="@style/TextAppearance.AppCompat.Medium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/settings"
android:textAllCaps="true"
android:textStyle="bold" />

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/color_text" />

<Space
android:layout_width="match_parent"
android:layout_height="20dp"/>

<Switch
android:id="@+id/launch_at_boot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/launch_at_boot" />

<Space
android:layout_width="match_parent"
android:layout_height="5dp"/>

<Switch
android:id="@+id/open_last_watched"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/open_last_watched" />

</LinearLayout>

</androidx.cardview.widget.CardView>

</RelativeLayout>

</RelativeLayout>
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@
<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>

<!-- settings -->
<string name="settings">Pengaturan</string>
<string name="launch_at_boot">Jalankan app saat boot</string>
<string name="open_last_watched">Putar terakhir dilihat</string>
</resources>

0 comments on commit 42d1dc0

Please sign in to comment.