have hardware buttons change media volume ONLY when not casting

This commit is contained in:
Domingos Lopes 2016-04-17 02:13:56 -04:00
parent 9939f8f461
commit 9d4969b0a7
3 changed files with 28 additions and 5 deletions

View File

@ -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);
}
}
}

View File

@ -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);

View File

@ -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);