add cast button to important activities

This commit is contained in:
Domingos Lopes 2016-03-19 12:42:26 -04:00 committed by Domingos Lopes
parent afbae2a7ef
commit 6224f80c89
5 changed files with 113 additions and 4 deletions

View File

@ -0,0 +1,97 @@
package de.danoeh.antennapod.activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import com.google.android.libraries.cast.companionlibrary.cast.VideoCastManager;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.preferences.UserPreferences;
/**
* Activity that allows for showing the MediaRouter button whenever there's a cast device in the
* network.
*/
public abstract class CastEnabledActivity extends AppCompatActivity
implements SharedPreferences.OnSharedPreferenceChangeListener {
protected VideoCastManager mCastManager;
private int castUICounter;
protected MenuItem mMediaRouteMenuItem;
protected boolean isCastEnabled;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PreferenceManager.getDefaultSharedPreferences(this).
registerOnSharedPreferenceChangeListener(this);
castUICounter = 0;
mCastManager = VideoCastManager.getInstance();
isCastEnabled = UserPreferences.isCastEnabled();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.cast_enabled, menu);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
mMediaRouteMenuItem = menu.findItem(R.id.media_route_menu_item);
mMediaRouteMenuItem.setEnabled(isCastEnabled);
mMediaRouteMenuItem = mCastManager.addMediaRouterButton(menu, R.id.media_route_menu_item);
return true;
}
@Override
protected void onResume() {
super.onResume();
castUICounter++;
if (isCastEnabled) {
mCastManager.incrementUiCounter();
castUICounter++;
}
}
@Override
protected void onPause() {
super.onPause();
castUICounter--;
if (isCastEnabled) {
mCastManager.decrementUiCounter();
castUICounter--;
}
}
//This whole method might just be useless because it's assumed that the cast button
//won't show where the user actually has the power to change the preference.
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals(UserPreferences.PREF_CAST_ENABLED)) {
isCastEnabled = UserPreferences.isCastEnabled();
mMediaRouteMenuItem.setEnabled(isCastEnabled);
if (isCastEnabled) {
//Test if activity is resumed but without UI counter incremented
if (castUICounter==1) {
mCastManager.incrementUiCounter();
castUICounter++;
}
} else {
if (castUICounter > 1) {
mCastManager.decrementUiCounter();
castUICounter--;
}
//TODO disable any current casting (or possibly do it within the PlaybackService)
}
}
}
}

View File

@ -17,7 +17,6 @@ import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.util.TypedValue;
@ -70,7 +69,7 @@ import rx.schedulers.Schedulers;
/**
* The activity that is shown when the user launches the app.
*/
public class MainActivity extends AppCompatActivity implements NavDrawerActivity {
public class MainActivity extends CastEnabledActivity implements NavDrawerActivity {
private static final String TAG = "MainActivity";

View File

@ -12,7 +12,6 @@ import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
@ -58,7 +57,7 @@ import rx.schedulers.Schedulers;
* Provides general features which are both needed for playing audio and video
* files.
*/
public abstract class MediaplayerActivity extends AppCompatActivity implements OnSeekBarChangeListener {
public abstract class MediaplayerActivity extends CastEnabledActivity implements OnSeekBarChangeListener {
private static final String TAG = "MediaplayerActivity";
private static final String PREFS = "MediaPlayerActivityPreferences";
private static final String PREF_SHOW_TIME_LEFT = "showTimeLeft";

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/media_route_menu_item"
android:title="@string/media_route_menu_title"
android:enabled="false"
custom:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"
custom:showAsAction="always"/>
</menu>

View File

@ -616,4 +616,7 @@
<string name="proxy_host_invalid_error">Host is not a valid IP address or domain</string>
<string name="proxy_port_invalid_error">Port not valid</string>
<!-- Casting -->
<string name="media_route_menu_title">Play on&#8230;</string>
</resources>