|
1 | 1 | package com.firebase.ui.auth;
|
2 | 2 |
|
3 | 3 | import android.content.Context;
|
| 4 | +import android.content.DialogInterface; |
4 | 5 | import android.content.Intent;
|
| 6 | +import android.net.ConnectivityManager; |
5 | 7 | import android.os.Bundle;
|
| 8 | +import android.util.Log; |
6 | 9 |
|
7 | 10 | import com.firebase.ui.auth.ui.ActivityHelper;
|
8 | 11 | import com.firebase.ui.auth.ui.AppCompatBase;
|
9 | 12 | import com.firebase.ui.auth.ui.ExtraConstants;
|
10 | 13 | import com.firebase.ui.auth.ui.FlowParameters;
|
| 14 | +import com.firebase.ui.auth.util.PlayServicesHelper; |
11 | 15 | import com.firebase.ui.auth.util.signincontainer.SignInDelegate;
|
12 | 16 |
|
13 | 17 | public class KickoffActivity extends AppCompatBase {
|
| 18 | + private static final String TAG = "KickoffActivity"; |
| 19 | + private static final String IS_WAITING_FOR_PLAY_SERVICES = "is_waiting_for_play_services"; |
| 20 | + private static final int RC_PLAY_SERVICES = 1; |
| 21 | + |
| 22 | + private boolean mIsWaitingForPlayServices = false; |
| 23 | + |
| 24 | + public static Intent createIntent(Context context, FlowParameters flowParams) { |
| 25 | + return ActivityHelper.createBaseIntent(context, KickoffActivity.class, flowParams); |
| 26 | + } |
| 27 | + |
14 | 28 | @Override
|
15 | 29 | protected void onCreate(Bundle savedInstance) {
|
16 | 30 | super.onCreate(savedInstance);
|
17 |
| - if (savedInstance == null) { |
18 |
| - SignInDelegate.delegate(this, mActivityHelper.getFlowParams()); |
| 31 | + if (savedInstance == null || savedInstance.getBoolean(IS_WAITING_FOR_PLAY_SERVICES)) { |
| 32 | + if (isOffline()) { |
| 33 | + Log.d(TAG, "No network connection"); |
| 34 | + finish(ErrorCodes.NO_NETWORK, |
| 35 | + IdpResponse.getErrorCodeIntent(ErrorCodes.NO_NETWORK)); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + boolean isPlayServicesAvailable = PlayServicesHelper.makePlayServicesAvailable( |
| 40 | + this, |
| 41 | + RC_PLAY_SERVICES, |
| 42 | + new DialogInterface.OnCancelListener() { |
| 43 | + @Override |
| 44 | + public void onCancel(DialogInterface dialog) { |
| 45 | + finish(ResultCodes.CANCELED, |
| 46 | + IdpResponse.getErrorCodeIntent( |
| 47 | + ErrorCodes.UNKNOWN_ERROR)); |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + if (isPlayServicesAvailable) { |
| 52 | + SignInDelegate.delegate(KickoffActivity.this, mActivityHelper.getFlowParams()); |
| 53 | + } else { |
| 54 | + mIsWaitingForPlayServices = true; |
| 55 | + } |
19 | 56 | }
|
20 | 57 | }
|
21 | 58 |
|
22 | 59 | @Override
|
23 | 60 | public void onSaveInstanceState(Bundle outState) {
|
24 | 61 | // It doesn't matter what we put here, we just don't want outState to be empty
|
25 | 62 | outState.putBoolean(ExtraConstants.HAS_EXISTING_INSTANCE, true);
|
| 63 | + outState.putBoolean(IS_WAITING_FOR_PLAY_SERVICES, mIsWaitingForPlayServices); |
26 | 64 | super.onSaveInstanceState(outState);
|
27 | 65 | }
|
28 | 66 |
|
29 | 67 | @Override
|
30 | 68 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
31 | 69 | super.onActivityResult(requestCode, resultCode, data);
|
32 |
| - SignInDelegate delegate = SignInDelegate.getInstance(this); |
33 |
| - if (delegate != null) { |
34 |
| - delegate.onActivityResult(requestCode, resultCode, data); |
| 70 | + if (requestCode == RC_PLAY_SERVICES) { |
| 71 | + if (resultCode == ResultCodes.OK) { |
| 72 | + SignInDelegate.delegate(KickoffActivity.this, mActivityHelper.getFlowParams()); |
| 73 | + } else { |
| 74 | + finish(ResultCodes.CANCELED, |
| 75 | + IdpResponse.getErrorCodeIntent(ErrorCodes.UNKNOWN_ERROR)); |
| 76 | + } |
| 77 | + } else { |
| 78 | + SignInDelegate delegate = SignInDelegate.getInstance(this); |
| 79 | + if (delegate != null) delegate.onActivityResult(requestCode, resultCode, data); |
35 | 80 | }
|
36 | 81 | }
|
37 | 82 |
|
38 |
| - public static Intent createIntent(Context context, FlowParameters flowParams) { |
39 |
| - return ActivityHelper.createBaseIntent(context, KickoffActivity.class, flowParams); |
| 83 | + /** |
| 84 | + * Check if there is an active or soon-to-be-active network connection. |
| 85 | + * |
| 86 | + * @return true if there is no network connection, false otherwise. |
| 87 | + */ |
| 88 | + private boolean isOffline() { |
| 89 | + ConnectivityManager manager = |
| 90 | + (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); |
| 91 | + |
| 92 | + return !(manager != null |
| 93 | + && manager.getActiveNetworkInfo() != null |
| 94 | + && manager.getActiveNetworkInfo().isConnectedOrConnecting()); |
40 | 95 | }
|
41 | 96 | }
|
0 commit comments