1
1
package br .edu .ufabc .safeway ;
2
2
3
- import android .support . v4 . app . FragmentActivity ;
3
+ import android .content . Intent ;
4
4
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 ;
5
12
13
+ import com .firebase .ui .auth .AuthUI ;
6
14
import com .google .android .gms .maps .CameraUpdateFactory ;
7
15
import com .google .android .gms .maps .GoogleMap ;
8
16
import com .google .android .gms .maps .OnMapReadyCallback ;
9
17
import com .google .android .gms .maps .SupportMapFragment ;
10
18
import com .google .android .gms .maps .model .LatLng ;
11
19
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 ;
12
23
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 ;
14
28
15
29
private GoogleMap mMap ;
16
30
31
+ private String mUsername ;
32
+
33
+ //Firebase instance
34
+ private FirebaseAuth mAuth ;
35
+ private FirebaseAuth .AuthStateListener mAuthListener ;
36
+ private ChildEventListener mChildEventListener ;
37
+
17
38
@ Override
18
39
protected void onCreate (Bundle savedInstanceState ) {
19
40
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 (){
20
116
setContentView (R .layout .activity_maps );
21
117
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
22
118
SupportMapFragment mapFragment = (SupportMapFragment ) getSupportFragmentManager ()
23
119
.findFragmentById (R .id .map );
24
120
mapFragment .getMapAsync (this );
25
121
}
26
-
27
-
28
122
/**
29
123
* Manipulates the map once available.
30
124
* 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.
33
126
* If Google Play services is not installed on the device, the user will be prompted to install
34
127
* it inside the SupportMapFragment. This method will only be triggered once the user has
35
128
* installed Google Play services and returned to the app.
@@ -43,4 +136,6 @@ public void onMapReady(GoogleMap googleMap) {
43
136
mMap .addMarker (new MarkerOptions ().position (ufabc_sa ).title ("UFABC Santo André" ));
44
137
mMap .moveCamera (CameraUpdateFactory .newLatLngZoom (ufabc_sa , 15 ));
45
138
}
139
+
140
+
46
141
}
0 commit comments