diff --git a/app/src/main/java/de/danoeh/antennapod/activity/CastEnabledActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/CastEnabledActivity.java new file mode 100644 index 000000000..497247ad4 --- /dev/null +++ b/app/src/main/java/de/danoeh/antennapod/activity/CastEnabledActivity.java @@ -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) + } + } + } +} diff --git a/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java index 8599eb4f4..beaea0fb9 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java @@ -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"; diff --git a/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java index 4911f61bc..3f8cfc557 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java @@ -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"; diff --git a/app/src/main/res/menu/cast_enabled.xml b/app/src/main/res/menu/cast_enabled.xml new file mode 100644 index 000000000..f8135db2a --- /dev/null +++ b/app/src/main/res/menu/cast_enabled.xml @@ -0,0 +1,11 @@ + + + + + \ No newline at end of file diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index 8a3f32b5f..648379206 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -616,4 +616,7 @@ Host is not a valid IP address or domain Port not valid + + Play on… +