Merge pull request #3440 from orionlee/avoid_cast_init_unless_enabled
Avoid Cast initialization unless it is enabled
This commit is contained in:
commit
e3ef12e413
|
@ -29,17 +29,34 @@ public abstract class CastEnabledActivity extends AppCompatActivity
|
||||||
implements SharedPreferences.OnSharedPreferenceChangeListener {
|
implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||||
public static final String TAG = "CastEnabledActivity";
|
public static final String TAG = "CastEnabledActivity";
|
||||||
|
|
||||||
protected CastManager castManager;
|
private CastConsumer castConsumer;
|
||||||
protected SwitchableMediaRouteActionProvider mediaRouteActionProvider;
|
private CastManager castManager;
|
||||||
|
|
||||||
|
private SwitchableMediaRouteActionProvider mediaRouteActionProvider;
|
||||||
private final CastButtonVisibilityManager castButtonVisibilityManager = new CastButtonVisibilityManager();
|
private final CastButtonVisibilityManager castButtonVisibilityManager = new CastButtonVisibilityManager();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
|
if (!CastManager.isInitialized()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).
|
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).
|
||||||
registerOnSharedPreferenceChangeListener(this);
|
registerOnSharedPreferenceChangeListener(this);
|
||||||
|
|
||||||
|
castConsumer = new DefaultCastConsumer() {
|
||||||
|
@Override
|
||||||
|
public void onApplicationConnected(ApplicationMetadata appMetadata, String sessionId, boolean wasLaunched) {
|
||||||
|
onCastConnectionChanged(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDisconnected() {
|
||||||
|
onCastConnectionChanged(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
castManager = CastManager.getInstance();
|
castManager = CastManager.getInstance();
|
||||||
castManager.addCastConsumer(castConsumer);
|
castManager.addCastConsumer(castConsumer);
|
||||||
castButtonVisibilityManager.setPrefEnabled(UserPreferences.isCastEnabled());
|
castButtonVisibilityManager.setPrefEnabled(UserPreferences.isCastEnabled());
|
||||||
|
@ -48,6 +65,10 @@ public abstract class CastEnabledActivity extends AppCompatActivity
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onDestroy() {
|
protected void onDestroy() {
|
||||||
|
if (!CastManager.isInitialized()) {
|
||||||
|
super.onDestroy();
|
||||||
|
return;
|
||||||
|
}
|
||||||
PreferenceManager.getDefaultSharedPreferences(getApplicationContext())
|
PreferenceManager.getDefaultSharedPreferences(getApplicationContext())
|
||||||
.unregisterOnSharedPreferenceChangeListener(this);
|
.unregisterOnSharedPreferenceChangeListener(this);
|
||||||
castManager.removeCastConsumer(castConsumer);
|
castManager.removeCastConsumer(castConsumer);
|
||||||
|
@ -58,6 +79,9 @@ public abstract class CastEnabledActivity extends AppCompatActivity
|
||||||
@CallSuper
|
@CallSuper
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
super.onCreateOptionsMenu(menu);
|
super.onCreateOptionsMenu(menu);
|
||||||
|
if (!CastManager.isInitialized()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
getMenuInflater().inflate(R.menu.cast_enabled, menu);
|
getMenuInflater().inflate(R.menu.cast_enabled, menu);
|
||||||
castButtonVisibilityManager.setMenu(menu);
|
castButtonVisibilityManager.setMenu(menu);
|
||||||
return true;
|
return true;
|
||||||
|
@ -67,6 +91,10 @@ public abstract class CastEnabledActivity extends AppCompatActivity
|
||||||
@CallSuper
|
@CallSuper
|
||||||
public boolean onPrepareOptionsMenu(Menu menu) {
|
public boolean onPrepareOptionsMenu(Menu menu) {
|
||||||
super.onPrepareOptionsMenu(menu);
|
super.onPrepareOptionsMenu(menu);
|
||||||
|
if (!CastManager.isInitialized()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
MenuItem mediaRouteButton = menu.findItem(R.id.media_route_menu_item);
|
MenuItem mediaRouteButton = menu.findItem(R.id.media_route_menu_item);
|
||||||
if (mediaRouteButton == null) {
|
if (mediaRouteButton == null) {
|
||||||
Log.wtf(TAG, "MediaRoute item could not be found on the menu!", new Exception());
|
Log.wtf(TAG, "MediaRoute item could not be found on the menu!", new Exception());
|
||||||
|
@ -83,15 +111,22 @@ public abstract class CastEnabledActivity extends AppCompatActivity
|
||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
|
if (!CastManager.isInitialized()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
castButtonVisibilityManager.setResumed(true);
|
castButtonVisibilityManager.setResumed(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPause() {
|
protected void onPause() {
|
||||||
super.onPause();
|
super.onPause();
|
||||||
|
if (!CastManager.isInitialized()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
castButtonVisibilityManager.setResumed(false);
|
castButtonVisibilityManager.setResumed(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
||||||
if (UserPreferences.PREF_CAST_ENABLED.equals(key)) {
|
if (UserPreferences.PREF_CAST_ENABLED.equals(key)) {
|
||||||
|
@ -105,18 +140,6 @@ public abstract class CastEnabledActivity extends AppCompatActivity
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CastConsumer castConsumer = new DefaultCastConsumer() {
|
|
||||||
@Override
|
|
||||||
public void onApplicationConnected(ApplicationMetadata appMetadata, String sessionId, boolean wasLaunched) {
|
|
||||||
onCastConnectionChanged(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDisconnected() {
|
|
||||||
onCastConnectionChanged(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
private void onCastConnectionChanged(boolean connected) {
|
private void onCastConnectionChanged(boolean connected) {
|
||||||
if (connected) {
|
if (connected) {
|
||||||
castButtonVisibilityManager.onConnected();
|
castButtonVisibilityManager.onConnected();
|
||||||
|
@ -133,6 +156,9 @@ public abstract class CastEnabledActivity extends AppCompatActivity
|
||||||
* @param showAsAction refer to {@link MenuItem#setShowAsAction(int)}
|
* @param showAsAction refer to {@link MenuItem#setShowAsAction(int)}
|
||||||
*/
|
*/
|
||||||
public final void requestCastButton(int showAsAction) {
|
public final void requestCastButton(int showAsAction) {
|
||||||
|
if (!CastManager.isInitialized()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
castButtonVisibilityManager.requestCastButton(showAsAction);
|
castButtonVisibilityManager.requestCastButton(showAsAction);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,13 @@
|
||||||
package de.danoeh.antennapod.preferences;
|
package de.danoeh.antennapod.preferences;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
import android.support.v7.app.AlertDialog;
|
||||||
|
|
||||||
import com.google.android.gms.common.ConnectionResult;
|
import com.google.android.gms.common.ConnectionResult;
|
||||||
import com.google.android.gms.common.GoogleApiAvailability;
|
import com.google.android.gms.common.GoogleApiAvailability;
|
||||||
|
|
||||||
|
import de.danoeh.antennapod.R;
|
||||||
import de.danoeh.antennapod.core.preferences.UserPreferences;
|
import de.danoeh.antennapod.core.preferences.UserPreferences;
|
||||||
import de.danoeh.antennapod.fragment.preferences.PlaybackPreferencesFragment;
|
import de.danoeh.antennapod.fragment.preferences.PlaybackPreferencesFragment;
|
||||||
|
|
||||||
|
@ -18,6 +23,7 @@ public class PreferenceControllerFlavorHelper {
|
||||||
final int googlePlayServicesCheck = GoogleApiAvailability.getInstance()
|
final int googlePlayServicesCheck = GoogleApiAvailability.getInstance()
|
||||||
.isGooglePlayServicesAvailable(ui.getActivity());
|
.isGooglePlayServicesAvailable(ui.getActivity());
|
||||||
if (googlePlayServicesCheck == ConnectionResult.SUCCESS) {
|
if (googlePlayServicesCheck == ConnectionResult.SUCCESS) {
|
||||||
|
displayRestartRequiredDialog(ui.requireContext());
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
GoogleApiAvailability.getInstance()
|
GoogleApiAvailability.getInstance()
|
||||||
|
@ -29,4 +35,13 @@ public class PreferenceControllerFlavorHelper {
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void displayRestartRequiredDialog(@NonNull Context context) {
|
||||||
|
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
|
||||||
|
dialog.setTitle(android.R.string.dialog_alert_title);
|
||||||
|
dialog.setMessage(R.string.pref_restart_required);
|
||||||
|
dialog.setPositiveButton(android.R.string.ok, null);
|
||||||
|
dialog.setCancelable(false);
|
||||||
|
dialog.show();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package de.danoeh.antennapod.core;
|
package de.danoeh.antennapod.core;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import de.danoeh.antennapod.core.cast.CastManager;
|
import de.danoeh.antennapod.core.cast.CastManager;
|
||||||
import de.danoeh.antennapod.core.preferences.PlaybackPreferences;
|
import de.danoeh.antennapod.core.preferences.PlaybackPreferences;
|
||||||
|
@ -15,6 +16,8 @@ import de.danoeh.antennapod.core.util.exception.RxJavaErrorHandlerSetup;
|
||||||
* Apps using the core module of AntennaPod should register implementations of all interfaces here.
|
* Apps using the core module of AntennaPod should register implementations of all interfaces here.
|
||||||
*/
|
*/
|
||||||
public class ClientConfig {
|
public class ClientConfig {
|
||||||
|
private static final String TAG = "ClientConfig";
|
||||||
|
|
||||||
private ClientConfig(){}
|
private ClientConfig(){}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -44,7 +47,15 @@ public class ClientConfig {
|
||||||
UserPreferences.init(context);
|
UserPreferences.init(context);
|
||||||
PlaybackPreferences.init(context);
|
PlaybackPreferences.init(context);
|
||||||
NetworkUtils.init(context);
|
NetworkUtils.init(context);
|
||||||
CastManager.init(context);
|
// Don't initialize Cast-related logic unless it is enabled, to avoid the unnecessary
|
||||||
|
// Google Play Service usage.
|
||||||
|
// Down side: when the user decides to enable casting, AntennaPod needs to be restarted
|
||||||
|
// for it to take effect.
|
||||||
|
if (UserPreferences.isCastEnabled()) {
|
||||||
|
CastManager.init(context);
|
||||||
|
} else {
|
||||||
|
Log.v(TAG, "Cast is disabled. All Cast-related initialization will be skipped.");
|
||||||
|
}
|
||||||
SleepTimerPreferences.init(context);
|
SleepTimerPreferences.init(context);
|
||||||
RxJavaErrorHandlerSetup.setupRxJavaErrorHandler();
|
RxJavaErrorHandlerSetup.setupRxJavaErrorHandler();
|
||||||
initialized = true;
|
initialized = true;
|
||||||
|
|
|
@ -163,6 +163,10 @@ public class CastManager extends BaseCastManager implements OnFailedListener {
|
||||||
return INSTANCE;
|
return INSTANCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean isInitialized() {
|
||||||
|
return INSTANCE != null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the active {@link RemoteMediaPlayer} instance. Since there are a number of media
|
* Returns the active {@link RemoteMediaPlayer} instance. Since there are a number of media
|
||||||
* control APIs that this library do not provide a wrapper for, client applications can call
|
* control APIs that this library do not provide a wrapper for, client applications can call
|
||||||
|
|
|
@ -56,11 +56,18 @@ public class PlaybackServiceFlavorHelper {
|
||||||
|
|
||||||
PlaybackServiceFlavorHelper(Context context, PlaybackService.FlavorHelperCallback callback) {
|
PlaybackServiceFlavorHelper(Context context, PlaybackService.FlavorHelperCallback callback) {
|
||||||
this.callback = callback;
|
this.callback = callback;
|
||||||
|
if (!CastManager.isInitialized()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
mediaRouter = MediaRouter.getInstance(context.getApplicationContext());
|
mediaRouter = MediaRouter.getInstance(context.getApplicationContext());
|
||||||
setCastConsumer(context);
|
setCastConsumer(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
void initializeMediaPlayer(Context context) {
|
void initializeMediaPlayer(Context context) {
|
||||||
|
if (!CastManager.isInitialized()) {
|
||||||
|
callback.setMediaPlayer(new LocalPSMP(context, callback.getMediaPlayerCallback()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
castManager = CastManager.getInstance();
|
castManager = CastManager.getInstance();
|
||||||
castManager.addCastConsumer(castConsumer);
|
castManager.addCastConsumer(castConsumer);
|
||||||
boolean isCasting = castManager.isConnected();
|
boolean isCasting = castManager.isConnected();
|
||||||
|
@ -77,10 +84,16 @@ public class PlaybackServiceFlavorHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
void removeCastConsumer() {
|
void removeCastConsumer() {
|
||||||
|
if (!CastManager.isInitialized()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
castManager.removeCastConsumer(castConsumer);
|
castManager.removeCastConsumer(castConsumer);
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean castDisconnect(boolean castDisconnect) {
|
boolean castDisconnect(boolean castDisconnect) {
|
||||||
|
if (!CastManager.isInitialized()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
if (castDisconnect) {
|
if (castDisconnect) {
|
||||||
castManager.disconnect();
|
castManager.disconnect();
|
||||||
}
|
}
|
||||||
|
@ -88,6 +101,9 @@ public class PlaybackServiceFlavorHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean onMediaPlayerInfo(Context context, int code, @StringRes int resourceId) {
|
boolean onMediaPlayerInfo(Context context, int code, @StringRes int resourceId) {
|
||||||
|
if (!CastManager.isInitialized()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
switch (code) {
|
switch (code) {
|
||||||
case RemotePSMP.CAST_ERROR:
|
case RemotePSMP.CAST_ERROR:
|
||||||
callback.sendNotificationBroadcast(PlaybackService.NOTIFICATION_TYPE_SHOW_TOAST, resourceId);
|
callback.sendNotificationBroadcast(PlaybackService.NOTIFICATION_TYPE_SHOW_TOAST, resourceId);
|
||||||
|
@ -218,6 +234,9 @@ public class PlaybackServiceFlavorHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
void registerWifiBroadcastReceiver() {
|
void registerWifiBroadcastReceiver() {
|
||||||
|
if (!CastManager.isInitialized()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (wifiBroadcastReceiver != null) {
|
if (wifiBroadcastReceiver != null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -243,6 +262,9 @@ public class PlaybackServiceFlavorHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
void unregisterWifiBroadcastReceiver() {
|
void unregisterWifiBroadcastReceiver() {
|
||||||
|
if (!CastManager.isInitialized()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (wifiBroadcastReceiver != null) {
|
if (wifiBroadcastReceiver != null) {
|
||||||
callback.unregisterReceiver(wifiBroadcastReceiver);
|
callback.unregisterReceiver(wifiBroadcastReceiver);
|
||||||
wifiBroadcastReceiver = null;
|
wifiBroadcastReceiver = null;
|
||||||
|
@ -250,6 +272,9 @@ public class PlaybackServiceFlavorHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean onSharedPreference(String key) {
|
boolean onSharedPreference(String key) {
|
||||||
|
if (!CastManager.isInitialized()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
if (UserPreferences.PREF_CAST_ENABLED.equals(key)) {
|
if (UserPreferences.PREF_CAST_ENABLED.equals(key)) {
|
||||||
if (!UserPreferences.isCastEnabled()) {
|
if (!UserPreferences.isCastEnabled()) {
|
||||||
if (castManager.isConnecting() || castManager.isConnected()) {
|
if (castManager.isConnecting() || castManager.isConnected()) {
|
||||||
|
@ -263,6 +288,9 @@ public class PlaybackServiceFlavorHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
void sessionStateAddActionForWear(PlaybackStateCompat.Builder sessionState, String actionName, CharSequence name, int icon) {
|
void sessionStateAddActionForWear(PlaybackStateCompat.Builder sessionState, String actionName, CharSequence name, int icon) {
|
||||||
|
if (!CastManager.isInitialized()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
PlaybackStateCompat.CustomAction.Builder actionBuilder =
|
PlaybackStateCompat.CustomAction.Builder actionBuilder =
|
||||||
new PlaybackStateCompat.CustomAction.Builder(actionName, name, icon);
|
new PlaybackStateCompat.CustomAction.Builder(actionName, name, icon);
|
||||||
Bundle actionExtras = new Bundle();
|
Bundle actionExtras = new Bundle();
|
||||||
|
@ -273,6 +301,9 @@ public class PlaybackServiceFlavorHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
void mediaSessionSetExtraForWear(MediaSessionCompat mediaSession) {
|
void mediaSessionSetExtraForWear(MediaSessionCompat mediaSession) {
|
||||||
|
if (!CastManager.isInitialized()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
Bundle sessionExtras = new Bundle();
|
Bundle sessionExtras = new Bundle();
|
||||||
sessionExtras.putBoolean(MediaControlConstants.EXTRA_RESERVE_SLOT_SKIP_TO_PREVIOUS, true);
|
sessionExtras.putBoolean(MediaControlConstants.EXTRA_RESERVE_SLOT_SKIP_TO_PREVIOUS, true);
|
||||||
sessionExtras.putBoolean(MediaControlConstants.EXTRA_RESERVE_SLOT_SKIP_TO_NEXT, true);
|
sessionExtras.putBoolean(MediaControlConstants.EXTRA_RESERVE_SLOT_SKIP_TO_NEXT, true);
|
||||||
|
|
Loading…
Reference in New Issue