Skip to content

Commit

Permalink
Version 2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxct committed Jul 21, 2020
1 parent 9e0779c commit cafd201
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 11 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
applicationId defaultApplicationId
minSdkVersion 25
targetSdkVersion 30
versionCode 10
versionName '2.0'
versionCode 11
versionName '2.1'
}
buildTypes {
debug {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
android:required="false" />
<uses-feature
android:name="android.software.leanback"
android:required="true" />
android:required="false" />

<application
android:keepScreenOn="true"
Expand Down
63 changes: 56 additions & 7 deletions app/src/main/java/space/linuxct/architv/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
package space.linuxct.architv;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaMetadata;
import android.media.session.MediaSession;
import android.media.session.PlaybackState;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {
private WebView mWebView;
WebView mWebView;
private String upstreamURL = BuildConfig.SERVER_URL;
private PowerManager.WakeLock wakeLock;
public MediaSession mSession;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mWebView = findViewById(R.id.backgroundview);
handleMediaSession();
handleAudioFocus();
handleWebView();

mWebView.setWebViewClient(new WebViewClient(){});

if (!mSession.isActive()) {
mSession.setActive(true);
}

}

private void handleWebView(){
mWebView = findViewById(R.id.backgroundview);
mWebView.setWebViewClient(new WebViewClient(){});
mWebView.setOnTouchListener(null);
mWebView.setEnabled(false);

Expand All @@ -35,8 +49,38 @@ public void onCreate(Bundle savedInstanceState) {

webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl(upstreamURL);
}

private void handleMediaSession(){
mSession = new MediaSession(this, "ArchiTV");
mSession.setCallback(new MediaSessionCallback(this));
mSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
PlaybackState state = new PlaybackState.Builder()
.setActions(PlaybackState.ACTION_PAUSE)
.setState(PlaybackState.STATE_PLAYING, 0, 1.0f)
.build();
mSession.setPlaybackState(state);
MediaMetadata.Builder metadataBuilder = new MediaMetadata.Builder();
metadataBuilder.putString(MediaMetadata.METADATA_KEY_TITLE, getString(R.string.title));
metadataBuilder.putString(MediaMetadata.METADATA_KEY_ARTIST, getString(R.string.desc));
mSession.setMetadata(metadataBuilder.build());
}

private void handleAudioFocus(){
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
AudioManager.OnAudioFocusChangeListener changedListener = new AudioManager.OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(int focusChange) {
if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
mSession.setActive(true);
}
else if (focusChange == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {
mSession.setActive(false);
}
}
};
audioManager.requestAudioFocus(changedListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
}

@Override
Expand All @@ -46,6 +90,9 @@ public void onResume() {
wakeLock.acquire();
}
}
if (!mSession.isActive()) {
mSession.setActive(true);
}
mWebView.resumeTimers();
mWebView.onResume();
super.onResume();
Expand All @@ -58,8 +105,7 @@ public void onPause() {
wakeLock.release();
}
}
mWebView.onPause();
mWebView.pauseTimers();

super.onPause();
}

Expand All @@ -72,6 +118,9 @@ protected void onDestroy(){
}
mWebView.destroy();
mWebView = null;
if (mSession.isActive()) {
mSession.setActive(false);
}
super.onDestroy();
}
}
21 changes: 21 additions & 0 deletions app/src/main/java/space/linuxct/architv/MediaSessionCallback.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package space.linuxct.architv;

import android.media.session.MediaSession;

public class MediaSessionCallback extends MediaSession.Callback {
private MainActivity mMainActivity;

public MediaSessionCallback(MainActivity mainActivity) {
mMainActivity = mainActivity;
}

@Override
public void onPause(){
mMainActivity.onDestroy();
}

@Override
public void onStop(){
mMainActivity.onDestroy();
}
}
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<resources>
<string name="app_name">ArchiTV</string>
<string name="title">ArchiTV for Archillect</string>
<string name="desc">Sounds of the Internet</string>
</resources>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.0'
classpath 'com.android.tools.build:gradle:4.0.1'
}
}

Expand Down

0 comments on commit cafd201

Please sign in to comment.