diff --git a/app/src/main/java/de/danoeh/antennapod/activity/CastEnabledActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/CastEnabledActivity.java index dd7cef428..d7996dd14 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/CastEnabledActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/CastEnabledActivity.java @@ -1,13 +1,18 @@ package de.danoeh.antennapod.activity; import android.content.SharedPreferences; +import android.media.AudioManager; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.Menu; +import com.google.android.gms.cast.ApplicationMetadata; + import de.danoeh.antennapod.R; +import de.danoeh.antennapod.core.cast.CastConsumer; +import de.danoeh.antennapod.core.cast.CastConsumerImpl; import de.danoeh.antennapod.core.cast.CastManager; import de.danoeh.antennapod.core.cast.SwitchableMediaRouteActionProvider; import de.danoeh.antennapod.core.preferences.UserPreferences; @@ -35,13 +40,16 @@ public abstract class CastEnabledActivity extends AppCompatActivity castUICounter = 0; mCastManager = CastManager.getInstance(); + mCastManager.addCastConsumer(castConsumer); isCastEnabled = UserPreferences.isCastEnabled(); + onCastConnectionChanged(mCastManager.isConnected()); } @Override protected void onDestroy() { PreferenceManager.getDefaultSharedPreferences(getApplicationContext()) .unregisterOnSharedPreferenceChangeListener(this); + mCastManager.removeCastConsumer(castConsumer); super.onDestroy(); } @@ -104,4 +112,24 @@ public abstract class CastEnabledActivity extends AppCompatActivity } } } + + CastConsumer castConsumer = new CastConsumerImpl() { + @Override + public void onApplicationConnected(ApplicationMetadata appMetadata, String sessionId, boolean wasLaunched) { + onCastConnectionChanged(true); + } + + @Override + public void onDisconnected() { + onCastConnectionChanged(false); + } + }; + + private void onCastConnectionChanged(boolean connected) { + if (connected) { + setVolumeControlStream(AudioManager.USE_DEFAULT_STREAM_TYPE); + } else { + setVolumeControlStream(AudioManager.STREAM_MUSIC); + } + } } 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 beaea0fb9..61831138f 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java @@ -7,7 +7,6 @@ import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; import android.database.DataSetObserver; -import android.media.AudioManager; import android.os.Build; import android.os.Bundle; import android.os.Handler; @@ -122,7 +121,6 @@ public class MainActivity extends CastEnabledActivity implements NavDrawerActivi super.onCreate(savedInstanceState); StorageUtils.checkStorageAvailability(this); setContentView(R.layout.main); - setVolumeControlStream(AudioManager.STREAM_MUSIC); toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); 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 dba4db334..83a746f9a 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java @@ -6,7 +6,6 @@ import android.content.SharedPreferences; import android.content.res.TypedArray; import android.graphics.Color; import android.graphics.PixelFormat; -import android.media.AudioManager; import android.net.Uri; import android.os.Build; import android.os.Bundle; @@ -207,8 +206,6 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements Log.d(TAG, "onCreate()"); StorageUtils.checkStorageAvailability(this); - //TODO we should most likely change this if casting - setVolumeControlStream(AudioManager.STREAM_MUSIC); orientation = getResources().getConfiguration().orientation; getWindow().setFormat(PixelFormat.TRANSPARENT);