|
| 1 | +/* |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | + * express or implied. See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.firebase.uidemo.auth; |
| 16 | + |
| 17 | +import android.app.Activity; |
| 18 | +import android.content.Context; |
| 19 | +import android.content.Intent; |
| 20 | +import android.os.Bundle; |
| 21 | +import android.support.annotation.MainThread; |
| 22 | +import android.support.annotation.StringRes; |
| 23 | +import android.support.annotation.StyleRes; |
| 24 | +import android.support.design.widget.Snackbar; |
| 25 | +import android.view.View; |
| 26 | +import android.widget.Button; |
| 27 | +import android.widget.CheckBox; |
| 28 | +import android.widget.RadioButton; |
| 29 | + |
| 30 | +import com.firebase.ui.auth.AuthUI; |
| 31 | +import com.firebase.uidemo.R; |
| 32 | +import com.google.firebase.auth.FirebaseAuth; |
| 33 | + |
| 34 | +import java.util.ArrayList; |
| 35 | + |
| 36 | +import butterknife.BindView; |
| 37 | +import butterknife.ButterKnife; |
| 38 | +import butterknife.OnClick; |
| 39 | + |
| 40 | +public class AuthUiActivity extends Activity { |
| 41 | + |
| 42 | + private static final String GOOGLE_TOS_URL = |
| 43 | + "https://www.google.com/policies/terms/"; |
| 44 | + private static final String FIREBASE_TOS_URL = |
| 45 | + "https://www.firebase.com/terms/terms-of-service.html"; |
| 46 | + |
| 47 | + private static final int RC_SIGN_IN = 100; |
| 48 | + |
| 49 | + @BindView(R.id.green_theme) |
| 50 | + RadioButton mUseGreenTheme; |
| 51 | + |
| 52 | + @BindView(R.id.blue_theme) |
| 53 | + RadioButton mUseBlueTheme; |
| 54 | + |
| 55 | + @BindView(R.id.email_provider) |
| 56 | + CheckBox mUseEmailProvider; |
| 57 | + |
| 58 | + @BindView(R.id.google_provider) |
| 59 | + CheckBox mUseGoogleProvider; |
| 60 | + |
| 61 | + @BindView(R.id.facebook_provider) |
| 62 | + CheckBox mUseFacebookProvider; |
| 63 | + |
| 64 | + @BindView(R.id.google_tos) |
| 65 | + RadioButton mUseGoogleTos; |
| 66 | + |
| 67 | + @BindView(R.id.firebase_tos) |
| 68 | + RadioButton mUseFirebaseTos; |
| 69 | + |
| 70 | + @BindView(R.id.sign_in) |
| 71 | + Button mSignIn; |
| 72 | + |
| 73 | + @BindView(android.R.id.content) |
| 74 | + View mRootView; |
| 75 | + |
| 76 | + @Override |
| 77 | + public void onCreate(Bundle savedInstanceState) { |
| 78 | + super.onCreate(savedInstanceState); |
| 79 | + |
| 80 | + FirebaseAuth auth = FirebaseAuth.getInstance(); |
| 81 | + if (auth.getCurrentUser() != null) { |
| 82 | + startActivity(SignedInActivity.createIntent(this)); |
| 83 | + finish(); |
| 84 | + } |
| 85 | + |
| 86 | + setContentView(R.layout.launch_layout); |
| 87 | + ButterKnife.bind(this); |
| 88 | + } |
| 89 | + |
| 90 | + @OnClick(R.id.sign_in) |
| 91 | + public void signIn(View view) { |
| 92 | + startActivityForResult( |
| 93 | + new AuthUI.SignInIntentBuilder(this) |
| 94 | + .setTheme(getSelectedTheme()) |
| 95 | + .setProviders(getSelectedProviders()) |
| 96 | + .setTosUrl(getSelectedTosUrl()) |
| 97 | + .build(), |
| 98 | + RC_SIGN_IN); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 103 | + super.onActivityResult(requestCode, resultCode, data); |
| 104 | + if (requestCode == RC_SIGN_IN) { |
| 105 | + handleSignInResponse(resultCode, data); |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + showSnackbar(R.string.unknown_response); |
| 110 | + } |
| 111 | + |
| 112 | + @MainThread |
| 113 | + private void handleSignInResponse(int resultCode, Intent data) { |
| 114 | + if (resultCode == RESULT_OK) { |
| 115 | + startActivity(SignedInActivity.createIntent(this)); |
| 116 | + finish(); |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + if (resultCode == RESULT_CANCELED) { |
| 121 | + showSnackbar(R.string.sign_in_cancelled); |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + showSnackbar(R.string.unknown_sign_in_response); |
| 126 | + } |
| 127 | + |
| 128 | + @MainThread |
| 129 | + @StyleRes |
| 130 | + private int getSelectedTheme() { |
| 131 | + // this can happen prior to initial layout inflation and binding, |
| 132 | + // in which case the radio button references will be null |
| 133 | + if (mUseBlueTheme == null || mUseBlueTheme.isChecked()) { |
| 134 | + return R.style.BlueTheme; |
| 135 | + } |
| 136 | + |
| 137 | + return R.style.GreenTheme; |
| 138 | + } |
| 139 | + |
| 140 | + @MainThread |
| 141 | + private String[] getSelectedProviders() { |
| 142 | + ArrayList<String> selectedProviders = new ArrayList<>(); |
| 143 | + |
| 144 | + if (mUseEmailProvider.isChecked()) { |
| 145 | + selectedProviders.add(AuthUI.EMAIL_PROVIDER); |
| 146 | + } |
| 147 | + |
| 148 | + if (mUseFacebookProvider.isChecked()) { |
| 149 | + selectedProviders.add(AuthUI.FACEBOOK_PROVIDER); |
| 150 | + } |
| 151 | + |
| 152 | + if (mUseGoogleProvider.isChecked()) { |
| 153 | + selectedProviders.add(AuthUI.GOOGLE_PROVIDER); |
| 154 | + } |
| 155 | + |
| 156 | + return selectedProviders.toArray(new String[selectedProviders.size()]); |
| 157 | + } |
| 158 | + |
| 159 | + @MainThread |
| 160 | + private String getSelectedTosUrl() { |
| 161 | + if (mUseGoogleTos.isChecked()) { |
| 162 | + return GOOGLE_TOS_URL; |
| 163 | + } |
| 164 | + |
| 165 | + return FIREBASE_TOS_URL; |
| 166 | + } |
| 167 | + |
| 168 | + @MainThread |
| 169 | + private void showSnackbar(@StringRes int errorMessageRes) { |
| 170 | + Snackbar.make(mRootView, errorMessageRes, Snackbar.LENGTH_LONG) |
| 171 | + .show(); |
| 172 | + } |
| 173 | + |
| 174 | + public static Intent createIntent(Context context) { |
| 175 | + Intent in = new Intent(); |
| 176 | + in.setClass(context, AuthUiActivity.class); |
| 177 | + return in; |
| 178 | + } |
| 179 | +} |
0 commit comments