From f4fd3b12e6beb4e5c49bb8e6afa53b7e85a524da Mon Sep 17 00:00:00 2001 From: aquilescanta Date: Mon, 17 Dec 2018 13:52:56 +0000 Subject: [PATCH] Handle failure to get Cast context more gracefully Issue:#4160 Issue:#4743 PiperOrigin-RevId: 225813243 --- .../exoplayer2/castdemo/MainActivity.java | 24 ++++++++++++++- .../cast_context_error_message_layout.xml | 29 +++++++++++++++++++ demos/cast/src/main/res/values/strings.xml | 2 ++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 demos/cast/src/main/res/layout/cast_context_error_message_layout.xml diff --git a/demos/cast/src/main/java/com/google/android/exoplayer2/castdemo/MainActivity.java b/demos/cast/src/main/java/com/google/android/exoplayer2/castdemo/MainActivity.java index 5278776070..9d4c3ec87f 100644 --- a/demos/cast/src/main/java/com/google/android/exoplayer2/castdemo/MainActivity.java +++ b/demos/cast/src/main/java/com/google/android/exoplayer2/castdemo/MainActivity.java @@ -41,6 +41,7 @@ import com.google.android.exoplayer2.ui.PlayerView; import com.google.android.gms.cast.CastMediaControlIntent; import com.google.android.gms.cast.framework.CastButtonFactory; import com.google.android.gms.cast.framework.CastContext; +import com.google.android.gms.dynamite.DynamiteModule; /** * An activity that plays video using {@link SimpleExoPlayer} and supports casting using ExoPlayer's @@ -68,7 +69,20 @@ public class MainActivity extends AppCompatActivity public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Getting the cast context later than onStart can cause device discovery not to take place. - castContext = CastContext.getSharedInstance(this); + try { + castContext = CastContext.getSharedInstance(this); + } catch (RuntimeException e) { + Throwable cause = e.getCause(); + while (cause != null) { + if (cause instanceof DynamiteModule.LoadingException) { + setContentView(R.layout.cast_context_error_message_layout); + return; + } + cause = cause.getCause(); + } + // Unknown error. We propagate it. + throw e; + } setContentView(R.layout.main_activity); @@ -98,6 +112,10 @@ public class MainActivity extends AppCompatActivity @Override public void onResume() { super.onResume(); + if (castContext == null) { + // There is no Cast context to work with. Do nothing. + return; + } String applicationId = castContext.getCastOptions().getReceiverApplicationId(); switch (applicationId) { case CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID: @@ -118,6 +136,10 @@ public class MainActivity extends AppCompatActivity @Override public void onPause() { super.onPause(); + if (castContext == null) { + // Nothing to release. + return; + } mediaQueueListAdapter.notifyItemRangeRemoved(0, mediaQueueListAdapter.getItemCount()); mediaQueueList.setAdapter(null); playerManager.release(); diff --git a/demos/cast/src/main/res/layout/cast_context_error_message_layout.xml b/demos/cast/src/main/res/layout/cast_context_error_message_layout.xml new file mode 100644 index 0000000000..6d3260de38 --- /dev/null +++ b/demos/cast/src/main/res/layout/cast_context_error_message_layout.xml @@ -0,0 +1,29 @@ + + + + + + + diff --git a/demos/cast/src/main/res/values/strings.xml b/demos/cast/src/main/res/values/strings.xml index 3505c40400..58f5233412 100644 --- a/demos/cast/src/main/res/values/strings.xml +++ b/demos/cast/src/main/res/values/strings.xml @@ -22,4 +22,6 @@ Add samples + Failed to get Cast context. Try updating Google Play Services and restart the app. +