diff --git a/app/src/play/java/de/danoeh/antennapod/activity/CastEnabledActivity.java b/app/src/play/java/de/danoeh/antennapod/activity/CastEnabledActivity.java index 87304b3d6..caca8a6e3 100644 --- a/app/src/play/java/de/danoeh/antennapod/activity/CastEnabledActivity.java +++ b/app/src/play/java/de/danoeh/antennapod/activity/CastEnabledActivity.java @@ -29,17 +29,34 @@ public abstract class CastEnabledActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener { public static final String TAG = "CastEnabledActivity"; - protected CastManager castManager; - protected SwitchableMediaRouteActionProvider mediaRouteActionProvider; + private CastConsumer castConsumer; + private CastManager castManager; + + private SwitchableMediaRouteActionProvider mediaRouteActionProvider; private final CastButtonVisibilityManager castButtonVisibilityManager = new CastButtonVisibilityManager(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + if (!CastManager.isInitialized()) { + return; + } + PreferenceManager.getDefaultSharedPreferences(getApplicationContext()). 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.addCastConsumer(castConsumer); castButtonVisibilityManager.setPrefEnabled(UserPreferences.isCastEnabled()); @@ -48,6 +65,10 @@ public abstract class CastEnabledActivity extends AppCompatActivity @Override protected void onDestroy() { + if (!CastManager.isInitialized()) { + super.onDestroy(); + return; + } PreferenceManager.getDefaultSharedPreferences(getApplicationContext()) .unregisterOnSharedPreferenceChangeListener(this); castManager.removeCastConsumer(castConsumer); @@ -58,6 +79,9 @@ public abstract class CastEnabledActivity extends AppCompatActivity @CallSuper public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); + if (!CastManager.isInitialized()) { + return true; + } getMenuInflater().inflate(R.menu.cast_enabled, menu); castButtonVisibilityManager.setMenu(menu); return true; @@ -67,6 +91,10 @@ public abstract class CastEnabledActivity extends AppCompatActivity @CallSuper public boolean onPrepareOptionsMenu(Menu menu) { super.onPrepareOptionsMenu(menu); + if (!CastManager.isInitialized()) { + return true; + } + MenuItem mediaRouteButton = menu.findItem(R.id.media_route_menu_item); if (mediaRouteButton == null) { 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 protected void onResume() { super.onResume(); + if (!CastManager.isInitialized()) { + return; + } castButtonVisibilityManager.setResumed(true); } @Override protected void onPause() { super.onPause(); + if (!CastManager.isInitialized()) { + return; + } castButtonVisibilityManager.setResumed(false); } + @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String 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) { if (connected) { castButtonVisibilityManager.onConnected(); @@ -133,6 +156,9 @@ public abstract class CastEnabledActivity extends AppCompatActivity * @param showAsAction refer to {@link MenuItem#setShowAsAction(int)} */ public final void requestCastButton(int showAsAction) { + if (!CastManager.isInitialized()) { + return; + } castButtonVisibilityManager.requestCastButton(showAsAction); } diff --git a/app/src/play/java/de/danoeh/antennapod/preferences/PreferenceControllerFlavorHelper.java b/app/src/play/java/de/danoeh/antennapod/preferences/PreferenceControllerFlavorHelper.java index c9d52df0c..0e69da61e 100644 --- a/app/src/play/java/de/danoeh/antennapod/preferences/PreferenceControllerFlavorHelper.java +++ b/app/src/play/java/de/danoeh/antennapod/preferences/PreferenceControllerFlavorHelper.java @@ -1,8 +1,13 @@ 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.GoogleApiAvailability; +import de.danoeh.antennapod.R; import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.fragment.preferences.PlaybackPreferencesFragment; @@ -18,6 +23,7 @@ public class PreferenceControllerFlavorHelper { final int googlePlayServicesCheck = GoogleApiAvailability.getInstance() .isGooglePlayServicesAvailable(ui.getActivity()); if (googlePlayServicesCheck == ConnectionResult.SUCCESS) { + displayRestartRequiredDialog(ui.requireContext()); return true; } else { GoogleApiAvailability.getInstance() @@ -29,4 +35,13 @@ public class PreferenceControllerFlavorHelper { 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(); + } } diff --git a/core/src/play/java/de/danoeh/antennapod/core/ClientConfig.java b/core/src/play/java/de/danoeh/antennapod/core/ClientConfig.java index 244fb0254..800222ada 100644 --- a/core/src/play/java/de/danoeh/antennapod/core/ClientConfig.java +++ b/core/src/play/java/de/danoeh/antennapod/core/ClientConfig.java @@ -1,6 +1,7 @@ package de.danoeh.antennapod.core; import android.content.Context; +import android.util.Log; import de.danoeh.antennapod.core.cast.CastManager; 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. */ public class ClientConfig { + private static final String TAG = "ClientConfig"; + private ClientConfig(){} /** @@ -44,7 +47,15 @@ public class ClientConfig { UserPreferences.init(context); PlaybackPreferences.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); RxJavaErrorHandlerSetup.setupRxJavaErrorHandler(); initialized = true; diff --git a/core/src/play/java/de/danoeh/antennapod/core/cast/CastManager.java b/core/src/play/java/de/danoeh/antennapod/core/cast/CastManager.java index 5198a76bd..414a7840c 100644 --- a/core/src/play/java/de/danoeh/antennapod/core/cast/CastManager.java +++ b/core/src/play/java/de/danoeh/antennapod/core/cast/CastManager.java @@ -163,6 +163,10 @@ public class CastManager extends BaseCastManager implements OnFailedListener { return INSTANCE; } + public static boolean isInitialized() { + return INSTANCE != null; + } + /** * 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 diff --git a/core/src/play/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceFlavorHelper.java b/core/src/play/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceFlavorHelper.java index 7ab1be380..79c71f164 100644 --- a/core/src/play/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceFlavorHelper.java +++ b/core/src/play/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceFlavorHelper.java @@ -56,11 +56,18 @@ public class PlaybackServiceFlavorHelper { PlaybackServiceFlavorHelper(Context context, PlaybackService.FlavorHelperCallback callback) { this.callback = callback; + if (!CastManager.isInitialized()) { + return; + } mediaRouter = MediaRouter.getInstance(context.getApplicationContext()); setCastConsumer(context); } void initializeMediaPlayer(Context context) { + if (!CastManager.isInitialized()) { + callback.setMediaPlayer(new LocalPSMP(context, callback.getMediaPlayerCallback())); + return; + } castManager = CastManager.getInstance(); castManager.addCastConsumer(castConsumer); boolean isCasting = castManager.isConnected(); @@ -77,10 +84,16 @@ public class PlaybackServiceFlavorHelper { } void removeCastConsumer() { + if (!CastManager.isInitialized()) { + return; + } castManager.removeCastConsumer(castConsumer); } boolean castDisconnect(boolean castDisconnect) { + if (!CastManager.isInitialized()) { + return false; + } if (castDisconnect) { castManager.disconnect(); } @@ -88,6 +101,9 @@ public class PlaybackServiceFlavorHelper { } boolean onMediaPlayerInfo(Context context, int code, @StringRes int resourceId) { + if (!CastManager.isInitialized()) { + return false; + } switch (code) { case RemotePSMP.CAST_ERROR: callback.sendNotificationBroadcast(PlaybackService.NOTIFICATION_TYPE_SHOW_TOAST, resourceId); @@ -218,6 +234,9 @@ public class PlaybackServiceFlavorHelper { } void registerWifiBroadcastReceiver() { + if (!CastManager.isInitialized()) { + return; + } if (wifiBroadcastReceiver != null) { return; } @@ -243,6 +262,9 @@ public class PlaybackServiceFlavorHelper { } void unregisterWifiBroadcastReceiver() { + if (!CastManager.isInitialized()) { + return; + } if (wifiBroadcastReceiver != null) { callback.unregisterReceiver(wifiBroadcastReceiver); wifiBroadcastReceiver = null; @@ -250,6 +272,9 @@ public class PlaybackServiceFlavorHelper { } boolean onSharedPreference(String key) { + if (!CastManager.isInitialized()) { + return false; + } if (UserPreferences.PREF_CAST_ENABLED.equals(key)) { if (!UserPreferences.isCastEnabled()) { if (castManager.isConnecting() || castManager.isConnected()) { @@ -263,6 +288,9 @@ public class PlaybackServiceFlavorHelper { } void sessionStateAddActionForWear(PlaybackStateCompat.Builder sessionState, String actionName, CharSequence name, int icon) { + if (!CastManager.isInitialized()) { + return; + } PlaybackStateCompat.CustomAction.Builder actionBuilder = new PlaybackStateCompat.CustomAction.Builder(actionName, name, icon); Bundle actionExtras = new Bundle(); @@ -273,6 +301,9 @@ public class PlaybackServiceFlavorHelper { } void mediaSessionSetExtraForWear(MediaSessionCompat mediaSession) { + if (!CastManager.isInitialized()) { + return; + } Bundle sessionExtras = new Bundle(); sessionExtras.putBoolean(MediaControlConstants.EXTRA_RESERVE_SLOT_SKIP_TO_PREVIOUS, true); sessionExtras.putBoolean(MediaControlConstants.EXTRA_RESERVE_SLOT_SKIP_TO_NEXT, true);