Skip to content

Commit 2d5b1f4

Browse files
Iain McGinnissGerrit Code Review
authored andcommitted
Merge "Firebase Auth demo, to more comprehensively demonstrate the API surface"
2 parents f584ed4 + 8e88259 commit 2d5b1f4

27 files changed

+693
-18
lines changed

app/build.gradle

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
apply plugin: 'com.android.application'
2+
apply plugin: 'com.neenbedankt.android-apt'
23

34
android {
45
compileSdkVersion 23
@@ -32,8 +33,16 @@ dependencies {
3233
compile fileTree(dir: 'libs', include: ['*.jar'])
3334
compile 'com.android.support:appcompat-v7:23.2.1'
3435
compile 'com.android.support:recyclerview-v7:23.2.1'
36+
compile 'com.android.support:support-v4:23.2.1'
3537

3638
compile project(':library')
39+
40+
// The following dependencies are not required to use the Firebase UI library.
41+
// They are used to make some aspects of the demo app implementation simpler for
42+
// demonstrative purposes, and you may find them useful in your own apps; YMMV.
43+
compile 'com.jakewharton:butterknife:8.0.1'
44+
compile 'com.github.bumptech.glide:glide:3.7.0'
45+
apt 'com.jakewharton:butterknife-compiler:8.0.1'
3746
}
3847

3948
apply plugin: 'com.google.gms.google-services'

app/src/main/AndroidManifest.xml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
android:icon="@mipmap/ic_launcher"
1010
android:label="@string/app_name"
1111
android:theme="@style/AppTheme">
12-
<activity
13-
android:name=".ChatActivity"
14-
android:label="@string/name_chat" />
1512

1613
<activity android:name=".ChooserActivity">
1714
<intent-filter>
@@ -20,6 +17,20 @@
2017
<category android:name="android.intent.category.LAUNCHER" />
2118
</intent-filter>
2219
</activity>
20+
21+
<!-- Chat demo -->
22+
<activity
23+
android:name=".ChatActivity"
24+
android:label="@string/name_chat" />
25+
26+
<!-- Auth UI demo -->
27+
<activity
28+
android:name=".auth.AuthUiActivity"
29+
android:label="@string/name_auth_ui" />
30+
31+
<activity
32+
android:name=".auth.SignedInActivity"
33+
android:label="@string/name_auth_ui" />
2334
</application>
2435

2536
</manifest>

app/src/main/java/com/firebase/uidemo/ChooserActivity.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,55 @@
22

33
import android.content.Context;
44
import android.content.Intent;
5-
import android.support.v7.app.AppCompatActivity;
65
import android.os.Bundle;
6+
import android.support.v7.app.AppCompatActivity;
77
import android.view.LayoutInflater;
88
import android.view.View;
99
import android.view.ViewGroup;
10-
import android.widget.AdapterView;
1110
import android.widget.ArrayAdapter;
1211
import android.widget.ListView;
1312
import android.widget.TextView;
1413

15-
public class ChooserActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
14+
import com.firebase.uidemo.auth.AuthUiActivity;
15+
16+
import butterknife.BindView;
17+
import butterknife.ButterKnife;
18+
import butterknife.OnItemClick;
19+
20+
public class ChooserActivity extends AppCompatActivity {
1621

1722
private static final Class[] CLASSES = new Class[]{
18-
ChatActivity.class
23+
ChatActivity.class,
24+
AuthUiActivity.class,
1925
};
2026

2127
private static final int[] DESCRIPTION_NAMES = new int[] {
2228
R.string.name_chat,
29+
R.string.name_auth_ui
2330
};
2431

2532
private static final int[] DESCRIPTION_IDS = new int[] {
2633
R.string.desc_chat,
34+
R.string.desc_auth_ui
2735
};
2836

37+
@BindView(R.id.list_view)
38+
ListView mListView;
39+
2940
@Override
3041
protected void onCreate(Bundle savedInstanceState) {
3142
super.onCreate(savedInstanceState);
3243
setContentView(R.layout.activity_chooser);
44+
ButterKnife.bind(this);
3345

34-
// Set up ListView and Adapter
35-
ListView listView = (ListView) findViewById(R.id.list_view);
36-
37-
MyArrayAdapter adapter = new MyArrayAdapter(this, android.R.layout.simple_list_item_2, CLASSES);
38-
39-
listView.setAdapter(adapter);
40-
listView.setOnItemClickListener(this);
46+
mListView.setAdapter(new MyArrayAdapter(
47+
this,
48+
android.R.layout.simple_list_item_2,
49+
CLASSES));
4150
}
4251

43-
@Override
44-
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
52+
@OnItemClick(R.id.list_view)
53+
public void onItemClick(int position) {
4554
Class clicked = CLASSES[position];
4655
startActivity(new Intent(this, clicked));
4756
}
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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

Comments
 (0)