1
1
package com .pulkit4tech .privy ;
2
2
3
+ import android .content .Context ;
3
4
import android .content .Intent ;
5
+ import android .content .SharedPreferences ;
4
6
import android .content .pm .PackageManager ;
5
7
import android .support .annotation .NonNull ;
6
8
import android .support .design .widget .CoordinatorLayout ;
19
21
import android .util .Log ;
20
22
import android .view .Menu ;
21
23
import android .view .MenuItem ;
22
- import android .widget .Toast ;
23
-
24
+ import android .view .View ;
25
+ import android .widget .TextView ;
26
+
27
+ import com .google .android .gms .auth .api .Auth ;
28
+ import com .google .android .gms .auth .api .signin .GoogleSignInAccount ;
29
+ import com .google .android .gms .auth .api .signin .GoogleSignInOptions ;
30
+ import com .google .android .gms .auth .api .signin .GoogleSignInResult ;
31
+ import com .google .android .gms .common .ConnectionResult ;
32
+ import com .google .android .gms .common .api .GoogleApiClient ;
33
+ import com .google .android .gms .common .api .ResultCallback ;
34
+ import com .google .android .gms .common .api .Status ;
24
35
import com .google .android .gms .location .places .Place ;
25
36
import com .google .android .gms .location .places .ui .PlacePicker ;
26
37
import com .pulkit4tech .privy .data .json .Location ;
32
43
import java .util .ArrayList ;
33
44
34
45
import static com .pulkit4tech .privy .constants .Constants .DEBUG ;
46
+ import static com .pulkit4tech .privy .constants .Constants .LOG_PREF ;
35
47
import static com .pulkit4tech .privy .constants .Constants .MY_PERMISSIONS_REQUEST_FINE_LOCATIONS ;
36
48
import static com .pulkit4tech .privy .constants .Constants .PLACE_PICKER_REQUEST ;
49
+ import static com .pulkit4tech .privy .constants .Constants .RC_SIGN_IN ;
50
+ import static com .pulkit4tech .privy .constants .Constants .RC_SIGN_IN_NEW_PRIVY_REQUEST ;
37
51
38
- public class MainActivity extends AppCompatActivity implements NavigationView .OnNavigationItemSelectedListener {
52
+ public class MainActivity extends AppCompatActivity implements NavigationView .OnNavigationItemSelectedListener , GoogleApiClient . OnConnectionFailedListener {
39
53
40
54
private NavigationView navigationView ;
55
+ private GoogleApiClient mGoogleApiClient ;
56
+ private Context mContext ;
57
+ private FloatingActionButton fab ;
58
+ private SharedPreferences mSharedPreferences ;
59
+ private String NAME = "user_name" ;
60
+ private String EMAIL = "email_id" ;
61
+ private String LOGGED_IN = "logged_in" ;
62
+ // private ImageView profileImg;
63
+ private TextView userName , emailId ;
41
64
42
65
@ Override
43
66
protected void onCreate (Bundle savedInstanceState ) {
44
67
super .onCreate (savedInstanceState );
45
68
setContentView (R .layout .activity_main );
69
+ mContext = this ;
70
+ mSharedPreferences = getSharedPreferences (LOG_PREF , MODE_PRIVATE );
46
71
47
72
if (savedInstanceState == null ) {
48
73
setUpInfo ();
@@ -55,28 +80,96 @@ protected void onResume() {
55
80
}
56
81
57
82
private void setUpInfo () {
58
- Toolbar toolbar = (Toolbar ) findViewById (R .id .toolbar );
59
- setSupportActionBar (toolbar );
83
+ setGoogleApiClientInfo ();
84
+ setUpFab ();
85
+ setUpNavigationDrawer ();
86
+
87
+ if (checkIfLoggedIn ()) {
88
+ fab .hide ();
89
+ }
60
90
61
- FloatingActionButton fab = (FloatingActionButton ) findViewById (R .id .fab );
62
- // fab.setOnClickListener(new View.OnClickListener() {
63
- // @Override
64
- // public void onClick(View view) {
65
- // Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
66
- // .setAction("Action", null);
67
- // }
68
- // });
91
+ navigationView .getMenu ().getItem (0 ).setChecked (true );
92
+ loadMapFragment ();
93
+ }
69
94
95
+ private void setUpNavigationDrawer () {
96
+ Toolbar toolbar = (Toolbar ) findViewById (R .id .toolbar );
97
+ setSupportActionBar (toolbar );
70
98
DrawerLayout drawer = (DrawerLayout ) findViewById (R .id .drawer_layout );
71
99
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle (
72
100
this , drawer , toolbar , R .string .navigation_drawer_open , R .string .navigation_drawer_close );
73
101
drawer .setDrawerListener (toggle );
74
102
toggle .syncState ();
75
-
76
103
navigationView = (NavigationView ) findViewById (R .id .nav_view );
77
104
navigationView .setNavigationItemSelectedListener (this );
78
- navigationView .getMenu ().getItem (0 ).setChecked (true );
79
- loadMapFragment ();
105
+ setUpNavigationHeader ();
106
+ }
107
+
108
+ private void setUpNavigationHeader () {
109
+ View nav_head = navigationView .getHeaderView (0 );
110
+ // profileImg = (ImageView) nav_head.findViewById(R.id.profile_pic);
111
+ userName = (TextView ) nav_head .findViewById (R .id .user_name );
112
+ emailId = (TextView ) nav_head .findViewById (R .id .email_id );
113
+
114
+ setUpNavigationHeaderValue ();
115
+ }
116
+
117
+ private void setUpNavigationHeaderValue () {
118
+ // TODO : Set Profile Image
119
+ userName .setText (mSharedPreferences .getString (NAME , getResources ().getString (R .string .sign_in )));
120
+ emailId .setText (mSharedPreferences .getString (EMAIL , "" ));
121
+ }
122
+
123
+ private void setUpFab () {
124
+ fab = (FloatingActionButton ) findViewById (R .id .fab );
125
+ fab .setOnClickListener (new View .OnClickListener () {
126
+ @ Override
127
+ public void onClick (View view ) {
128
+ startGoogleSignInActivity (RC_SIGN_IN );
129
+ }
130
+ });
131
+ }
132
+
133
+ private void startGoogleSignInActivity (int resultcode ) {
134
+ Intent signInIntent = Auth .GoogleSignInApi .getSignInIntent (mGoogleApiClient );
135
+ startActivityForResult (signInIntent , resultcode );
136
+ }
137
+
138
+ private void signOut () {
139
+ clearSharedPreference ();
140
+ setUpNavigationHeaderValue ();
141
+
142
+ Auth .GoogleSignInApi .signOut (mGoogleApiClient ).setResultCallback (new ResultCallback <Status >() {
143
+ @ Override
144
+ public void onResult (@ NonNull Status status ) {
145
+ if (status .isSuccess ()) {
146
+ snackMsg ("Signed Out!!" );
147
+ } else {
148
+ snackMsg ("Some error while signing out!" );
149
+ Log .d (DEBUG , "Sign out error : " + status .toString ());
150
+ }
151
+ }
152
+ });
153
+ }
154
+
155
+ private boolean checkIfLoggedIn () {
156
+ return mSharedPreferences .getBoolean (LOGGED_IN , false );
157
+ }
158
+
159
+ private void setGoogleApiClientInfo () {
160
+
161
+ // Testing Google Sign in
162
+ GoogleSignInOptions gso = new GoogleSignInOptions .Builder (GoogleSignInOptions .DEFAULT_SIGN_IN )
163
+ .requestEmail ()
164
+ .requestProfile ()
165
+ .build ();
166
+
167
+ // Build a GoogleApiClient with access to the Google Sign-In API and the
168
+ // options specified by gso.
169
+ mGoogleApiClient = new GoogleApiClient .Builder (mContext )
170
+ .enableAutoManage (this , this )
171
+ .addApi (Auth .GOOGLE_SIGN_IN_API , gso )
172
+ .build ();
80
173
}
81
174
82
175
@ Override
@@ -91,6 +184,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
91
184
switch (requestCode ) {
92
185
case MY_PERMISSIONS_REQUEST_FINE_LOCATIONS :
93
186
if (grantResults .length > 0 && grantResults [0 ] == PackageManager .PERMISSION_GRANTED ) {
187
+ loadMapFragment ();
94
188
snackMsg ("Permission granted!!" );
95
189
} else {
96
190
loadFragment (new NoLocationPermission ());
@@ -147,14 +241,23 @@ public boolean onNavigationItemSelected(MenuItem item) {
147
241
148
242
if (id == R .id .nav_nearby_privy ) {
149
243
loadMapFragment ();
244
+ closeDrawer ();
150
245
} else if (id == R .id .nav_request_new ) {
151
246
loadAddNewPrivyActivity ();
247
+ closeDrawer ();
248
+ } else if (id == R .id .nav_sign_in_up ) {
249
+ if (checkIfLoggedIn ())
250
+ signOut ();
251
+ else
252
+ startGoogleSignInActivity (RC_SIGN_IN );
152
253
}
153
254
//TODO : Add other conditions
255
+ return true ;
256
+ }
154
257
258
+ private void closeDrawer () {
155
259
DrawerLayout drawer = (DrawerLayout ) findViewById (R .id .drawer_layout );
156
260
drawer .closeDrawer (GravityCompat .START );
157
- return true ;
158
261
}
159
262
160
263
@ Override
@@ -174,6 +277,64 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
174
277
navigationView .getMenu ().getItem (0 ).setChecked (true );
175
278
loadMapFragment ();
176
279
}
280
+
281
+ // Google Sign in Callback
282
+ if (requestCode == RC_SIGN_IN ) {
283
+ GoogleSignInResult result = Auth .GoogleSignInApi .getSignInResultFromIntent (data );
284
+ handleSignInResult (result );
285
+ }
286
+
287
+ if (requestCode == RC_SIGN_IN_NEW_PRIVY_REQUEST ) {
288
+ GoogleSignInResult result = Auth .GoogleSignInApi .getSignInResultFromIntent (data );
289
+ handleSignInResultAndLaunchNewRequestPrivy (result );
290
+ }
291
+ }
292
+
293
+ private void handleSignInResultAndLaunchNewRequestPrivy (GoogleSignInResult result ) {
294
+ if (result .isSuccess ()) {
295
+ // Signed in successfully, show authenticated UI.
296
+ GoogleSignInAccount acct = result .getSignInAccount ();
297
+ addLoginInfo (acct );
298
+
299
+ //Launch NewRequestPrivy
300
+ loadAddNewPrivyActivity ();
301
+
302
+ // Hide Fab button
303
+ fab .hide ();
304
+ } else {
305
+ clearSharedPreference ();
306
+ snackMsg ("Please Sign in to request new Privy" );
307
+ }
308
+ setUpNavigationHeaderValue ();
309
+ }
310
+
311
+ private void handleSignInResult (GoogleSignInResult result ) {
312
+ Log .d (DEBUG , "handleSignInResult:" + result .isSuccess ());
313
+ if (result .isSuccess ()) {
314
+ // Signed in successfully, show authenticated UI.
315
+ GoogleSignInAccount acct = result .getSignInAccount ();
316
+ addLoginInfo (acct );
317
+ // Hide Fab button
318
+ fab .hide ();
319
+ } else {
320
+ clearSharedPreference ();
321
+ snackMsg ("Please Sign in to request new Privy" );
322
+ }
323
+ setUpNavigationHeaderValue ();
324
+ }
325
+
326
+ private void addLoginInfo (GoogleSignInAccount acct ) {
327
+ // TODO : add Profile Picture
328
+ SharedPreferences .Editor editor = mSharedPreferences .edit ();
329
+ editor .putBoolean (LOGGED_IN , true );
330
+ editor .putString (NAME , acct .getDisplayName ());
331
+ editor .putString (EMAIL , acct .getEmail ());
332
+ editor .commit ();
333
+ }
334
+
335
+ private void clearSharedPreference () {
336
+ SharedPreferences .Editor editor = mSharedPreferences .edit ();
337
+ editor .clear ().commit ();
177
338
}
178
339
179
340
private void requestNewPrivy (Place placeToAdd ) {
@@ -188,16 +349,22 @@ private void requestNewPrivy(Place placeToAdd) {
188
349
ArrayList <String > types = new ArrayList <String >();
189
350
types .add ("establishment" );
190
351
postdata .setTypes (types );
352
+ postdata .setAddress (mSharedPreferences .getString (EMAIL , "" ));
191
353
postdata .setLanguage ("en" );
192
354
new NetworkRequest (this , postdata ).postRequest ();
193
355
}
194
356
195
357
private void loadAddNewPrivyActivity () {
196
- PlacePicker .IntentBuilder builder = new PlacePicker .IntentBuilder ();
197
- try {
198
- startActivityForResult (builder .build (this ), PLACE_PICKER_REQUEST );
199
- } catch (Exception e ) {
200
- Log .d (DEBUG , e .toString ());
358
+ if (checkIfLoggedIn ()) {
359
+ PlacePicker .IntentBuilder builder = new PlacePicker .IntentBuilder ();
360
+ try {
361
+ startActivityForResult (builder .build (this ), PLACE_PICKER_REQUEST );
362
+ } catch (Exception e ) {
363
+ Log .d (DEBUG , e .toString ());
364
+ }
365
+ } else {
366
+ startGoogleSignInActivity (RC_SIGN_IN_NEW_PRIVY_REQUEST );
367
+ snackMsg ("Please Sign In to add new Privy" );
201
368
}
202
369
}
203
370
@@ -220,4 +387,12 @@ public void snackMsg(String msg) {
220
387
private boolean checkLocationEnabledPermission () {
221
388
return ActivityCompat .checkSelfPermission (this , android .Manifest .permission .ACCESS_FINE_LOCATION ) == PackageManager .PERMISSION_GRANTED ;
222
389
}
390
+
391
+ @ Override
392
+ public void onConnectionFailed (@ NonNull ConnectionResult connectionResult ) {
393
+ snackMsg ("Some Error while Connecting to Google Service!!" );
394
+ Log .d (DEBUG , "OnConnectionFailed: " + connectionResult .toString ());
395
+ }
396
+
397
+
223
398
}
0 commit comments