Skip to content

Commit cc0859c

Browse files
committed
Custom Authentification
#10 - layout, activity and broadcastreceiver
1 parent 5904d56 commit cc0859c

File tree

7 files changed

+298
-3
lines changed

7 files changed

+298
-3
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<activity android:name=".SettingsActivity"/>
4848
<activity android:name=".MapActivity"/>
4949
<activity android:name=".RxbusActivity"/>
50+
<activity android:name=".CustomAuthActivity"/>
5051

5152

5253
<!-- for googlemaps -->
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/**
2+
* Copyright Google Inc. All Rights Reserved.
3+
* <p/>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p/>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p/>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.eusecom.attendance;
17+
18+
import android.os.Bundle;
19+
import android.support.annotation.NonNull;
20+
import android.support.v7.app.AppCompatActivity;
21+
import android.util.Log;
22+
import android.view.View;
23+
import android.widget.TextView;
24+
import android.widget.Toast;
25+
26+
import com.google.android.gms.tasks.OnCompleteListener;
27+
import com.google.android.gms.tasks.OnSuccessListener;
28+
import com.google.android.gms.tasks.Task;
29+
import com.google.firebase.auth.AuthResult;
30+
import com.google.firebase.auth.FirebaseAuth;
31+
import com.google.firebase.auth.FirebaseUser;
32+
33+
/**
34+
* Demonstrate Firebase Authentication using a custom minted token. For more information, see:
35+
* https://firebase.google.com/docs/auth/android/custom-auth
36+
*/
37+
public class CustomAuthActivity extends AppCompatActivity implements View.OnClickListener {
38+
39+
private static final String TAG = "CustomAuthActivity";
40+
41+
// [START declare_auth]
42+
private FirebaseAuth mAuth;
43+
// [END declare_auth]
44+
45+
// [START declare_auth_listener]
46+
private FirebaseAuth.AuthStateListener mAuthListener;
47+
// [END declare_auth_listener]
48+
49+
private String mCustomToken;
50+
private TokenBroadcastReceiver mTokenReceiver;
51+
52+
@Override
53+
protected void onCreate(Bundle savedInstanceState) {
54+
super.onCreate(savedInstanceState);
55+
setContentView(R.layout.activity_custom);
56+
57+
// Button click listeners
58+
findViewById(R.id.button_sign_in).setOnClickListener(this);
59+
60+
// Create token receiver (for demo purposes only)
61+
mTokenReceiver = new TokenBroadcastReceiver() {
62+
@Override
63+
public void onNewToken(String token) {
64+
Log.d(TAG, "onNewToken:" + token);
65+
setCustomToken(token);
66+
}
67+
};
68+
69+
// [START initialize_auth]
70+
mAuth = FirebaseAuth.getInstance();
71+
// [END initialize_auth]
72+
73+
// [START auth_state_listener]
74+
mAuthListener = new FirebaseAuth.AuthStateListener() {
75+
@Override
76+
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
77+
FirebaseUser user = firebaseAuth.getCurrentUser();
78+
if (user != null) {
79+
// User is signed in
80+
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
81+
} else {
82+
// User is signed out
83+
Log.d(TAG, "onAuthStateChanged:signed_out");
84+
}
85+
// [START_EXCLUDE]
86+
updateUI(user);
87+
// [END_EXCLUDE]
88+
}
89+
};
90+
// [END auth_state_listener]
91+
92+
93+
94+
}//end of oncreate
95+
96+
// [START on_start_add_listener]
97+
@Override
98+
public void onStart() {
99+
super.onStart();
100+
mAuth.addAuthStateListener(mAuthListener);
101+
// [START_EXCLUDE]
102+
registerReceiver(mTokenReceiver, TokenBroadcastReceiver.getFilter());
103+
// [END_EXCLUDE]
104+
}
105+
// [END on_start_add_listener]
106+
107+
// [START on_stop_remove_listener]
108+
@Override
109+
public void onStop() {
110+
super.onStop();
111+
if (mAuthListener != null) {
112+
mAuth.removeAuthStateListener(mAuthListener);
113+
}
114+
// [START_EXCLUDE]
115+
unregisterReceiver(mTokenReceiver);
116+
// [END_EXCLUDE]
117+
}
118+
// [END on_stop_remove_listener]
119+
120+
private void startSignIn() {
121+
// Initiate sign in with custom token
122+
// [START sign_in_custom]
123+
124+
mCustomToken="ya29.ElsRBCFaBUX0npVERVcttGZUQJaJ3Ycy8ONDUjwbFZkn4WQkkKg-_Dtnbd9_1W1zj0I7f-eyWtCarTf0uzYsh3JsHT7wcpsgvwEPrUpCjqPzYXFTrnGb6isShIcT";
125+
126+
mAuth.signInWithCustomToken(mCustomToken)
127+
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
128+
@Override
129+
public void onComplete(@NonNull Task<AuthResult> task) {
130+
Log.d(TAG, "signInWithCustomToken:onComplete:" + task.isSuccessful());
131+
132+
// If sign in fails, display a message to the user. If sign in succeeds
133+
// the auth state listener will be notified and logic to handle the
134+
// signed in user can be handled in the listener.
135+
if (!task.isSuccessful()) {
136+
Log.w(TAG, "signInWithCustomToken", task.getException());
137+
Toast.makeText(CustomAuthActivity.this, "Authentication failed.",
138+
Toast.LENGTH_SHORT).show();
139+
}
140+
}
141+
});
142+
// [END sign_in_custom]
143+
}
144+
145+
private void updateUI(FirebaseUser user) {
146+
if (user != null) {
147+
((TextView) findViewById(R.id.text_sign_in_status)).setText(
148+
"User ID: " + user.getUid());
149+
} else {
150+
((TextView) findViewById(R.id.text_sign_in_status)).setText(
151+
"Error: sign in failed.");
152+
}
153+
}
154+
155+
private void setCustomToken(String token) {
156+
mCustomToken = token;
157+
158+
String status;
159+
if (mCustomToken != null) {
160+
status = "Token:" + mCustomToken;
161+
} else {
162+
status = "Token: null";
163+
}
164+
165+
// Enable/disable sign-in button and show the token
166+
findViewById(R.id.button_sign_in).setEnabled((mCustomToken != null));
167+
((TextView) findViewById(R.id.text_token_status)).setText(status);
168+
}
169+
170+
@Override
171+
public void onClick(View v) {
172+
int i = v.getId();
173+
if (i == R.id.button_sign_in) {
174+
startSignIn();
175+
176+
}
177+
}
178+
}

app/src/main/java/com/eusecom/attendance/MyAdapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ public void onClick(View v, int pos, boolean isLongClick) {
219219
case 6:
220220

221221
// View v at position pos is clicked.
222-
//Intent i6 = new Intent(mContext, RxjavaActivity.class);
223-
//v.getContext().startActivity(i6);
222+
Intent i6 = new Intent(mContext, CustomAuthActivity.class);
223+
v.getContext().startActivity(i6);
224224

225225
break;
226226

app/src/main/java/com/eusecom/attendance/RxbusActivity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.eusecom.attendance;
22

3+
/**
4+
* send events by https://github.com/kaushikgopal/RxJava-Android-Samples
5+
* send pojo EventRxBus by https://quangson8128.github.io/2016/06/23/rxbus-android/
6+
*/
7+
38
import android.os.Bundle;
49
import android.support.v4.app.Fragment;
510
import android.support.v4.app.FragmentActivity;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.eusecom.attendance;
18+
19+
import android.content.BroadcastReceiver;
20+
import android.content.Context;
21+
import android.content.Intent;
22+
import android.content.IntentFilter;
23+
import android.util.Log;
24+
25+
/**
26+
* Receiver to capture tokens broadcast via ADB and insert them into the
27+
* running application to facilitate easy testing of custom authentication.
28+
*/
29+
public abstract class TokenBroadcastReceiver extends BroadcastReceiver {
30+
31+
private static final String TAG = "TokenBroadcastReceiver";
32+
33+
public static final String ACTION_TOKEN = "com.eusecom.attendance.ACTION_TOKEN";
34+
public static final String EXTRA_KEY_TOKEN = "key_token";
35+
36+
@Override
37+
public void onReceive(Context context, Intent intent) {
38+
Log.d(TAG, "onReceive:" + intent);
39+
40+
if (ACTION_TOKEN.equals(intent.getAction())) {
41+
String token = intent.getExtras().getString(EXTRA_KEY_TOKEN);
42+
onNewToken(token);
43+
}
44+
}
45+
46+
public static IntentFilter getFilter() {
47+
IntentFilter filter = new IntentFilter(ACTION_TOKEN);
48+
return filter;
49+
}
50+
51+
public abstract void onNewToken(String token);
52+
53+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
android:orientation="vertical"
7+
android:paddingBottom="@dimen/activity_vertical_margin"
8+
android:paddingLeft="@dimen/activity_horizontal_margin"
9+
android:paddingRight="@dimen/activity_horizontal_margin"
10+
android:paddingTop="@dimen/activity_vertical_margin"
11+
tools:context=".CustomAuthActivity">
12+
13+
<ImageView
14+
android:id="@+id/icon"
15+
android:contentDescription="@string/desc_firebase_lockup"
16+
android:layout_gravity="center_horizontal"
17+
android:src="@drawable/firebase_lockup_400"
18+
android:layout_width="wrap_content"
19+
android:layout_height="wrap_content" />
20+
21+
<TextView
22+
style="@style/TextAppearance.AppCompat.Medium"
23+
android:layout_width="wrap_content"
24+
android:layout_height="wrap_content"
25+
android:layout_marginBottom="8dp"
26+
android:text="@string/custom_token" />
27+
28+
<TextView
29+
android:id="@+id/text_token_status"
30+
android:layout_width="match_parent"
31+
android:layout_height="wrap_content"
32+
android:ellipsize="end"
33+
android:maxLines="1"
34+
android:singleLine="true"
35+
android:text="@string/token_null" />
36+
37+
<TextView
38+
style="@style/TextAppearance.AppCompat.Medium"
39+
android:layout_width="wrap_content"
40+
android:layout_height="wrap_content"
41+
android:layout_marginBottom="8dp"
42+
android:layout_marginTop="32dp"
43+
android:text="@string/firebase_user_management" />
44+
45+
<TextView
46+
android:id="@+id/text_sign_in_status"
47+
android:layout_width="match_parent"
48+
android:layout_height="wrap_content"
49+
android:text="@string/signed_out" />
50+
51+
<Button
52+
android:id="@+id/button_sign_in"
53+
android:layout_width="@dimen/field_width_standard"
54+
android:layout_height="wrap_content"
55+
android:enabled="true"
56+
android:text="@string/sign_in" />
57+
58+
</LinearLayout>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
<item >Google Maps</item>
119119
<item >Settings</item>
120120
<item >Demo Rx Bus</item>
121-
<item >Free</item>
121+
<item >Custom Auth</item>
122122
</string-array>
123123

124124
<string name="app_name1">Slider Menu 1</string>

0 commit comments

Comments
 (0)