Skip to content

Commit 2157043

Browse files
committed
Autenticação com Google e E-mail
1 parent 9129d8f commit 2157043

File tree

8 files changed

+131
-67
lines changed

8 files changed

+131
-67
lines changed

app/build.gradle

+8-6
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ android {
2121

2222
dependencies {
2323
compile fileTree(dir: 'libs', include: ['*.jar'])
24-
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
25-
exclude group: 'com.android.support', module: 'support-annotations'
26-
})
24+
testCompile 'junit:junit:4.12'
25+
26+
compile 'com.google.firebase:firebase-core:10.2.6'
27+
compile 'com.google.firebase:firebase-database:10.2.6'
2728
compile 'com.android.support:appcompat-v7:25.3.1'
2829
compile 'com.google.android.gms:play-services-maps:10.2.6'
29-
compile 'com.google.firebase:firebase-auth:10.0.1'
30-
testCompile 'junit:junit:4.12'
30+
compile 'com.google.firebase:firebase-auth:10.2.6'
31+
compile 'com.google.android.gms:play-services-auth:10.2.6'
32+
compile 'com.firebaseui:firebase-ui-auth:0.6.1'
3133
}
3234

33-
35+
apply plugin: 'com.google.gms.google-services'

app/google-services.json

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
{
22
"project_info": {
3-
"project_number": "430361735377",
4-
"firebase_url": "https://friendlychat-7716b.firebaseio.com",
5-
"project_id": "friendlychat-7716b",
6-
"storage_bucket": "friendlychat-7716b.appspot.com"
3+
"project_number": "311676898802",
4+
"firebase_url": "https://safeway-ufabc.firebaseio.com",
5+
"project_id": "safeway-ufabc",
6+
"storage_bucket": "safeway-ufabc.appspot.com"
77
},
88
"client": [
99
{
1010
"client_info": {
11-
"mobilesdk_app_id": "1:430361735377:android:64e58dacc0483faa",
11+
"mobilesdk_app_id": "1:311676898802:android:86ceb04160e72675",
1212
"android_client_info": {
13-
"package_name": "com.google.firebase.udacity.friendlychat"
13+
"package_name": "br.edu.ufabc.safeway"
1414
}
1515
},
1616
"oauth_client": [
1717
{
18-
"client_id": "430361735377-2boqgn20agqm7gukmsauhu9n8g02vla4.apps.googleusercontent.com",
18+
"client_id": "311676898802-8tsavh6ne3d6n4ulg77l7fo7f28e5gt1.apps.googleusercontent.com",
1919
"client_type": 1,
2020
"android_info": {
21-
"package_name": "com.google.firebase.udacity.friendlychat",
21+
"package_name": "br.edu.ufabc.safeway",
2222
"certificate_hash": "a0a3682cea35b6e8d240584d724966343c305b17"
2323
}
2424
},
2525
{
26-
"client_id": "430361735377-6rme7lplf7sa88ivhe8317o9oh55s2qg.apps.googleusercontent.com",
26+
"client_id": "311676898802-2u34lknvaq6674auneve8h70joipk72a.apps.googleusercontent.com",
2727
"client_type": 3
2828
}
2929
],
3030
"api_key": [
3131
{
32-
"current_key": "AIzaSyBmbSYyl6JlI5yZKa6AN7ZilBehHVqr1pk"
32+
"current_key": "AIzaSyAQQNmyr9ZNMny81SmRLxBydpovGlDoJQQ"
3333
}
3434
],
3535
"services": {
@@ -40,7 +40,7 @@
4040
"status": 2,
4141
"other_platform_oauth_client": [
4242
{
43-
"client_id": "430361735377-6rme7lplf7sa88ivhe8317o9oh55s2qg.apps.googleusercontent.com",
43+
"client_id": "311676898802-2u34lknvaq6674auneve8h70joipk72a.apps.googleusercontent.com",
4444
"client_type": 3
4545
}
4646
]

app/src/androidTest/java/br/edu/ufabc/safeway/ExampleInstrumentedTest.java

-26
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,128 @@
11
package br.edu.ufabc.safeway;
22

3-
import android.support.v4.app.FragmentActivity;
3+
import android.content.Intent;
44
import android.os.Bundle;
5+
import android.support.annotation.NonNull;
6+
import android.support.v7.app.AppCompatActivity;
7+
import android.util.Log;
8+
import android.view.Menu;
9+
import android.view.MenuInflater;
10+
import android.view.MenuItem;
11+
import android.widget.Toast;
512

13+
import com.firebase.ui.auth.AuthUI;
614
import com.google.android.gms.maps.CameraUpdateFactory;
715
import com.google.android.gms.maps.GoogleMap;
816
import com.google.android.gms.maps.OnMapReadyCallback;
917
import com.google.android.gms.maps.SupportMapFragment;
1018
import com.google.android.gms.maps.model.LatLng;
1119
import com.google.android.gms.maps.model.MarkerOptions;
20+
import com.google.firebase.auth.FirebaseAuth;
21+
import com.google.firebase.auth.FirebaseUser;
22+
import com.google.firebase.database.ChildEventListener;
1223

13-
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
24+
public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {
25+
26+
private static final String TAG = "MapsActivity";
27+
public static final int RC_SIGN_IN = 1;
1428

1529
private GoogleMap mMap;
1630

31+
private String mUsername;
32+
33+
//Firebase instance
34+
private FirebaseAuth mAuth;
35+
private FirebaseAuth.AuthStateListener mAuthListener;
36+
private ChildEventListener mChildEventListener;
37+
1738
@Override
1839
protected void onCreate(Bundle savedInstanceState) {
1940
super.onCreate(savedInstanceState);
41+
startMap();
42+
43+
mAuth = FirebaseAuth.getInstance();
44+
mAuthListener = new FirebaseAuth.AuthStateListener() {
45+
@Override
46+
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
47+
FirebaseUser user = firebaseAuth.getCurrentUser();
48+
if (user != null) {
49+
// User is signed in
50+
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
51+
} else {
52+
// User is signed out
53+
Log.d(TAG, "onAuthStateChanged:signed_out");
54+
startActivityForResult(
55+
AuthUI.getInstance()
56+
.createSignInIntentBuilder()
57+
.setIsSmartLockEnabled(false)
58+
.setProviders(
59+
AuthUI.EMAIL_PROVIDER,
60+
AuthUI.GOOGLE_PROVIDER)
61+
.build(),
62+
RC_SIGN_IN);
63+
}
64+
}
65+
};
66+
}
67+
68+
public void onActivityResult(int requestCode, int resultCode, Intent data) {
69+
super.onActivityResult(requestCode, resultCode, data);
70+
if (requestCode == RC_SIGN_IN) {
71+
if (resultCode == RESULT_OK) {
72+
// Sign-in succeeded, set up the UI
73+
74+
Toast.makeText(this, "Bem vindo! "+mAuth.getCurrentUser().getDisplayName(), Toast.LENGTH_LONG).show();
75+
} else if (resultCode == RESULT_CANCELED) {
76+
// Sign in was canceled by the user, finish the activity
77+
Toast.makeText(this, "Sign in canceled", Toast.LENGTH_SHORT).show();
78+
finish();
79+
}
80+
}
81+
}
82+
83+
@Override
84+
public void onStart() {
85+
super.onStart();
86+
mAuth.addAuthStateListener(mAuthListener);
87+
}
88+
89+
@Override
90+
public void onStop() {
91+
super.onStop();
92+
if (mAuthListener != null) {
93+
mAuth.removeAuthStateListener(mAuthListener);
94+
}
95+
}
96+
97+
@Override
98+
public boolean onCreateOptionsMenu(Menu menu) {
99+
MenuInflater inflater = getMenuInflater();
100+
inflater.inflate(R.menu.main_menu, menu);
101+
return true;
102+
}
103+
104+
@Override
105+
public boolean onOptionsItemSelected(MenuItem item) {
106+
switch (item.getItemId()) {
107+
case R.id.sign_out_menu:
108+
AuthUI.getInstance().signOut(this);
109+
return true;
110+
default:
111+
return super.onOptionsItemSelected(item);
112+
}
113+
}
114+
115+
private void startMap(){
20116
setContentView(R.layout.activity_maps);
21117
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
22118
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
23119
.findFragmentById(R.id.map);
24120
mapFragment.getMapAsync(this);
25121
}
26-
27-
28122
/**
29123
* Manipulates the map once available.
30124
* This callback is triggered when the map is ready to be used.
31-
* This is where we can add markers or lines, add listeners or move the camera. In this case,
32-
* we just add a marker near Sydney, Australia.
125+
* This is where we can add markers or lines, add listeners or move the camera.
33126
* If Google Play services is not installed on the device, the user will be prompted to install
34127
* it inside the SupportMapFragment. This method will only be triggered once the user has
35128
* installed Google Play services and returned to the app.
@@ -43,4 +136,6 @@ public void onMapReady(GoogleMap googleMap) {
43136
mMap.addMarker(new MarkerOptions().position(ufabc_sa).title("UFABC Santo André"));
44137
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(ufabc_sa, 15));
45138
}
139+
140+
46141
}

app/src/main/res/menu/main_menu.xml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<menu xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto">
4+
<item
5+
android:id="@+id/sign_out_menu"
6+
android:title="@string/sign_out"
7+
app:showAsAction="never"/>
8+
</menu>

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<resources>
22
<string name="app_name">SafeWay</string>
3-
<string name="title_activity_maps">Map</string>
3+
<string name="title_activity_maps">SafeWay</string>
44
<string name="google_maps_key">AIzaSyABMpGvu83QNi0GSQesgXs2rBtlTRcQdlk</string>
5+
<string name="sign_out">Sair</string>
56
</resources>

app/src/test/java/br/edu/ufabc/safeway/ExampleUnitTest.java

-17
This file was deleted.

build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ buildscript {
77
dependencies {
88
classpath 'com.android.tools.build:gradle:2.3.3'
99
classpath 'com.google.gms:google-services:3.1.0'
10+
1011
// NOTE: Do not place your application dependencies here; they belong
1112
// in the individual module build.gradle files
1213
}

0 commit comments

Comments
 (0)