Skip to content
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.

Commit

Permalink
Display current show on menu, bugfixes, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
markhaehnel committed Feb 5, 2016
1 parent 3b70696 commit b731c74
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 170 deletions.
Binary file modified app/app-release.apk
Binary file not shown.
13 changes: 9 additions & 4 deletions app/app.iml
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,27 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/appcompat-v7/23.1.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-v4/23.1.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.devbrackets.android/exomedia/2.5.2/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.google.android.exoplayer/exoplayer/r1.5.3/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.devbrackets.android/exomedia/2.5.3/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.google.android.exoplayer/exoplayer/r1.5.4/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/instant-run-support" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/pre-dexed" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/restart-dex" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/transforms" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Android API 23 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="support-v4-23.1.1" level="project" />
<orderEntry type="library" exported="" name="exomedia-2.5.2" level="project" />
<orderEntry type="library" exported="" name="exoplayer-r1.5.3" level="project" />
<orderEntry type="library" exported="" name="exomedia-2.5.3" level="project" />
<orderEntry type="library" exported="" name="support-annotations-23.1.1" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-23.1.1" level="project" />
<orderEntry type="library" exported="" name="exoplayer-r1.5.4" level="project" />
</component>
</module>
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ android {
defaultConfig {
applicationId "de.markhaehnel.rbtv.rocketbeanstv"
minSdkVersion 17
targetSdkVersion 17
targetSdkVersion 23
versionCode 8
versionName "2.4"
}
Expand All @@ -21,5 +21,5 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.devbrackets.android:exomedia:2.5.2'
compile 'com.devbrackets.android:exomedia:2.5.3'
}
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
android:name="android.hardware.touchscreen"
android:required="false" />

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ protected JSONObject doInBackground(Context... contexts) {

try {
String response = HttpRequest.get("http://api.twitch.tv/api/channels/rocketbeanstv/access_token").body();
JSONObject json = new JSONObject(response);
return json;
return new JSONObject(response);
} catch (JSONException e) {
e.printStackTrace();
return new JSONObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.json.JSONObject;

public class GetCurrentShow extends AsyncTask<Void, String, String> {
String lastVisibleShow = "";
protected String doInBackground(Void... voids) {
while (true) {
try {
Expand All @@ -26,8 +25,7 @@ protected String doInBackground(Void... voids) {
protected void onProgressUpdate(String... strings) {
String currentShow = strings[0];
if (currentShow.length() != 0) {
lastVisibleShow = currentShow;
MainActivity.getInstance().showCurrentShow(currentShow);
MainActivity.getInstance().setCurrentShow(currentShow);
}
}
}
174 changes: 97 additions & 77 deletions app/src/main/java/de/markhaehnel/rbtv/rocketbeanstv/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,74 +29,47 @@

public class MainActivity extends AppCompatActivity implements MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener, MediaPlayer.OnInfoListener {

private AudioManager audioManager;
private ComponentName mediaButtonReceiver;
private EMVideoView videoView;

AudioManager am;
ComponentName mbr;
EMVideoView emVideoView;
boolean showGetterIsRunning = false;
String currentShow = "Keine Informationen";

AlphaAnimation fadeIn;
AlphaAnimation fadeOut;
AlphaAnimation fadeOutShow;

public static MainActivity ins;
private static MainActivity ins;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ins = this;

setContentView(R.layout.activity_main);
emVideoView = (EMVideoView)findViewById(R.id.exomediaplayer);
videoView = (EMVideoView)findViewById(R.id.exomediaplayer);

if (isOnline()) {
emVideoView.setOnPreparedListener(this);
emVideoView.setOnErrorListener(this);
emVideoView.setOnInfoListener(this);

am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mbr = new ComponentName(getPackageName(), MediaButtonReceiver.class.getName());
setupListeners();

audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mediaButtonReceiver = new ComponentName(getPackageName(), MediaButtonReceiver.class.getName());

setupAnimations();

int result = am.requestAudioFocus(focusListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
int result = audioManager.requestAudioFocus(focusListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);

if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
am.registerMediaButtonEventReceiver(mbr);
try {
JSONObject json = new GetAccesToken().execute(this).get();
if (json.length() != 0) {
String token = json.getString("token");
String sig = json.getString("sig");
if (token.length() != 0 && sig.length() != 0) {
String url = "http://usher.twitch.tv/api/channel/hls/rocketbeanstv.m3u8?player=twitchweb&token=" + token + "&sig=" + sig + "&allow_audio_only=true&allow_source=true&type=any&p=" + Math.round(Math.random() * 10000);
Uri theUri = Uri.parse(url);
emVideoView.setVideoURI(Uri.parse(url));
} else {
showMessage(R.string.error_twitchError);
}
} else {
showMessage(R.string.error_twitchError);
}

} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
showMessage(R.string.error_twitchError);
}
audioManager.registerMediaButtonEventReceiver(mediaButtonReceiver);
preparePlayer();
}
} else {
showMessage(R.string.error_noInternet);
}
}

public static MainActivity getInstance() {
return ins;
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
Expand All @@ -107,6 +80,9 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
case KeyEvent.KEYCODE_HOME:
System.exit(0);
return true;
case KeyEvent.KEYCODE_MENU:
this.showCurrentShow();
return true;
}
return false;
}
Expand All @@ -117,19 +93,6 @@ public void onPrepared(MediaPlayer mp) {
emVideoView.start();
}

@Override
public void onPause() {
emVideoView.pause();
super.onPause();
}

@Override
public void onResume() {

emVideoView.start();
super.onResume();
}

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
showMessage(R.string.error_unknown);
Expand All @@ -148,20 +111,56 @@ public boolean onInfo(MediaPlayer mp, int what, int extra) {
if (!showGetterIsRunning) new GetCurrentShow().execute();
break;
}

return false;
}

public void togglePlayState() {
@Override
protected void onPause() {
videoView.pause();
super.onPause();
}

@Override
protected void onResume() {
videoView.start();
super.onResume();
}

@Override
protected void onStop() {
System.exit(0);
super.onStop();
}

protected static MainActivity getInstance() {
return ins;
}

protected void setCurrentShow(String showName) {
if (!showName.equals(currentShow)) {
currentShow = showName;
showCurrentShow();
}
}

protected void togglePlayState() {
ImageView pauseView = (ImageView)findViewById(R.id.pauseImage);
if (emVideoView.isPlaying()) {
emVideoView.pause();
if (videoView.isPlaying()) {
videoView.pause();
pauseView.startAnimation(fadeIn);
pauseView.setVisibility(View.VISIBLE);
} else {
emVideoView.start();
videoView.start();
pauseView.startAnimation(fadeOut);
pauseView.setVisibility(View.INVISIBLE);
};
}
}

private void setupListeners() {
videoView.setOnPreparedListener(this);
videoView.setOnErrorListener(this);
videoView.setOnInfoListener(this);
}

private void setupAnimations() {
Expand All @@ -176,6 +175,30 @@ private void setupAnimations() {
fadeOutShow.setDuration(500);
}

private void preparePlayer() {
try {
JSONObject json = new GetAccesToken().execute(this).get();
if (json.length() != 0) {
String token = json.getString("token");
String sig = json.getString("sig");
if (token.length() != 0 && sig.length() != 0) {
String url = "http://usher.twitch.tv/api/channel/hls/rocketbeanstv.m3u8?player=twitchweb&token=" + token + "&sig=" + sig + "&allow_audio_only=true&allow_source=true&type=any&p=" + Math.round(Math.random() * 10000);
videoView.setVideoURI(Uri.parse(url));
} else {
showMessage(R.string.error_twitchError);
}
} else {
showMessage(R.string.error_twitchError);
}

} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
showMessage(R.string.error_twitchError);
}
}

private void showMessage(int resourceId) {
AlertDialog.Builder ad = new AlertDialog.Builder(this);
ad.setCancelable(false);
Expand All @@ -190,50 +213,47 @@ public void onClick(DialogInterface dialog, int which) {
ad.create().show();
}

public boolean isOnline() {
private void showCurrentShow() {
TableLayout containerCurrentShow = (TableLayout)findViewById(R.id.containerCurrentShow);
TextView textCurrentShow = (TextView)findViewById(R.id.textCurrentShow);
textCurrentShow.setText(currentShow);

AnimationSet animation = new AnimationSet(true);
animation.addAnimation(fadeIn);
animation.addAnimation(fadeOutShow);

containerCurrentShow.startAnimation(animation);
}

private boolean isOnline() {
Runtime runtime = Runtime.getRuntime();
try {

Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
int exitValue = ipProcess.waitFor();
return (exitValue == 0);

} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}

return false;
}

public void showCurrentShow(String showTitle) {
TableLayout containerCurrentShow = (TableLayout)findViewById(R.id.containerCurrentShow);
TextView textCurrentShow = (TextView)findViewById(R.id.textCurrentShow);
textCurrentShow.setText(showTitle);

AnimationSet animation = new AnimationSet(true);
animation.addAnimation(fadeIn);
animation.addAnimation(fadeOutShow);

containerCurrentShow.setAnimation(animation);
}

AudioManager.OnAudioFocusChangeListener focusListener = new AudioManager.OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focus) {
switch (focus) {
case AudioManager.AUDIOFOCUS_GAIN:
emVideoView.start();
videoView.start();
break;

case AudioManager.AUDIOFOCUS_LOSS:
am.unregisterMediaButtonEventReceiver(mbr);
audioManager.unregisterMediaButtonEventReceiver(mediaButtonReceiver);
System.exit(0);
break;

case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
emVideoView.pause();
videoView.pause();
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class MediaButtonReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
KeyEvent key = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
KeyEvent key = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
int keycode = key.getKeyCode();
if (key.getAction() == KeyEvent.ACTION_DOWN) {
switch (keycode) {
Expand Down
Loading

0 comments on commit b731c74

Please sign in to comment.