44import android .content .Context ;
55import android .content .DialogInterface ;
66import android .content .Intent ;
7+ import android .net .Uri ;
78import android .os .Bundle ;
9+ import android .support .annotation .NonNull ;
810import android .support .design .widget .FloatingActionButton ;
911import android .support .design .widget .Snackbar ;
1012import android .support .design .widget .TextInputEditText ;
1416import android .support .v7 .widget .LinearLayoutManager ;
1517import android .support .v7 .widget .RecyclerView ;
1618import android .support .v7 .widget .Toolbar ;
19+ import android .util .Log ;
1720import android .view .LayoutInflater ;
1821import android .view .Menu ;
1922import android .view .MenuItem ;
2023import android .view .View ;
2124import android .widget .DatePicker ;
2225import android .support .v7 .widget .TooltipCompat ;
26+ import android .widget .Toast ;
27+
28+ import com .google .android .gms .auth .api .Auth ;
29+ import com .google .android .gms .auth .api .signin .GoogleSignInAccount ;
30+ import com .google .android .gms .auth .api .signin .GoogleSignInApi ;
31+ import com .google .android .gms .auth .api .signin .GoogleSignInOptions ;
32+ import com .google .android .gms .auth .api .signin .GoogleSignInResult ;
33+ import com .google .android .gms .common .ConnectionResult ;
34+ import com .google .android .gms .common .SignInButton ;
35+ import com .google .android .gms .common .api .GoogleApi ;
36+ import com .google .android .gms .common .api .GoogleApiClient ;
37+ import com .google .android .gms .tasks .OnCompleteListener ;
38+ import com .google .android .gms .tasks .Task ;
39+ import com .google .firebase .auth .AuthCredential ;
40+ import com .google .firebase .auth .AuthResult ;
41+ import com .google .firebase .auth .FirebaseAuth ;
42+ import com .google .firebase .auth .FirebaseUser ;
43+ import com .google .firebase .auth .GoogleAuthProvider ;
2344
2445import java .text .ParseException ;
2546import java .text .SimpleDateFormat ;
3051
3152import static java .lang .System .out ;
3253
33- public class MainActivity extends AppCompatActivity {
54+ public class MainActivity extends AppCompatActivity implements GoogleApiClient . OnConnectionFailedListener {
3455 final Context context = this ;
3556 private View view ;
3657 private RecyclerView mRecyclerView ;
@@ -39,7 +60,9 @@ public class MainActivity extends AppCompatActivity {
3960 private List <TaskItem > taskItems = new ArrayList <>();
4061 private String [] taskTest = new String []{"wow" , "test" };
4162 private Date dueDateTest ;
42- private int testInt ;
63+ private int testInt , RC_SIGN_IN ;
64+ private FirebaseAuth mAuth ;
65+ private GoogleApiClient mGoogleApiClient ;
4366
4467 @ Override
4568 protected void onCreate (Bundle savedInstanceState ) {
@@ -94,9 +117,97 @@ public void run() {
94117 1000 );
95118 }
96119 });
120+ // Firebase
121+ RC_SIGN_IN = 9001 ;
122+ mAuth = FirebaseAuth .getInstance ();
123+ GoogleSignInOptions gso = new GoogleSignInOptions .Builder (GoogleSignInOptions .DEFAULT_SIGN_IN )
124+ .requestIdToken ("713563449638-sc1up855asm687s55f8qi4bdrh0u18tc.apps.googleusercontent.com" )
125+ .requestEmail ()
126+ .build ();
127+ mGoogleApiClient = new GoogleApiClient .Builder (this )
128+ .enableAutoManage (this , this )
129+ .addApi (Auth .GOOGLE_SIGN_IN_API , gso )
130+ .build ();
97131 // Tooltips
98132 TooltipCompat .setTooltipText (fab , "New Task" );
99133 }
134+
135+ @ Override
136+ public void onStart () {
137+ super .onStart ();
138+ FirebaseUser currentUser = mAuth .getCurrentUser ();
139+ if (currentUser == null ) {
140+ out .println ("Not logged in" );
141+ final AlertDialog .Builder signInDialogBuilder = new AlertDialog .Builder (context );
142+ LayoutInflater inflater = this .getLayoutInflater ();
143+ View signInDialogView = inflater .inflate (R .layout .dialog_sign_in , null );
144+ signInDialogBuilder .setView (signInDialogView );
145+ signInDialogBuilder .setTitle ("Sign in" );
146+ final SignInButton signInButton = (SignInButton ) signInDialogView .findViewById (R .id .signInDialogGoogle );
147+ final AlertDialog signInDialog = signInDialogBuilder .create ();
148+ signInButton .setOnClickListener (new View .OnClickListener () {
149+ @ Override
150+ public void onClick (View view ) {
151+ Intent signInIntent = Auth .GoogleSignInApi .getSignInIntent (mGoogleApiClient );
152+ startActivityForResult (signInIntent , RC_SIGN_IN );
153+ out .println ("TEST" );
154+ signInDialog .dismiss ();
155+ }
156+ });
157+ signInDialog .show ();
158+ } else {
159+ // Logged in
160+ // TODO: Add database
161+ Log .d ("Tag" , "Successfully logged in!" );
162+ }
163+ }
164+
165+ @ Override
166+ public void onActivityResult (int requestCode , int resultCode , Intent data ) {
167+ super .onActivityResult (requestCode , resultCode , data );
168+ if (requestCode == RC_SIGN_IN ) {
169+ GoogleSignInResult result = Auth .GoogleSignInApi .getSignInResultFromIntent (data );
170+ if (result .isSuccess ()) {
171+ GoogleSignInAccount account = result .getSignInAccount ();
172+ firebaseGoogle (account );
173+ } else {
174+ // Somehow it failed
175+ Log .d ("tag" , "failed" );
176+ }
177+ }
178+ }
179+
180+ private void firebaseGoogle (GoogleSignInAccount acct ) {
181+ AuthCredential credential = GoogleAuthProvider .getCredential (acct .getIdToken (), null );
182+ mAuth .signInWithCredential (credential )
183+ .addOnCompleteListener (this , new OnCompleteListener <AuthResult >() {
184+ @ Override
185+ public void onComplete (@ NonNull Task <AuthResult > task ) {
186+ if (task .isSuccessful ()) {
187+ // Sign in success, update UI with the signed-in user's information
188+ Log .d ("Tag" , "signInWithCredential:success" );
189+ FirebaseUser user = mAuth .getCurrentUser ();
190+ // AlertDialog.Builder builder = new AlertDialog.Builder(context);
191+ // builder.setMessage(user.toString())
192+ // .setPositiveButton("Close", new DialogInterface.OnClickListener() {
193+ // @Override
194+ // public void onClick(DialogInterface dialogInterface, int i) {
195+ // dialogInterface.dismiss();
196+ // }
197+ // });
198+ // AlertDialog dialog = builder.create();
199+ // dialog.show();
200+ } else {
201+ // If sign in fails, display a message to the user.
202+ Log .w ("Tag" , "signInWithCredential:failure" , task .getException ());
203+ Toast .makeText (MainActivity .this , "Authentication failed." ,
204+ Toast .LENGTH_SHORT ).show ();
205+ }
206+
207+ }
208+ });
209+ }
210+
100211 public void share () {
101212 final AlertDialog .Builder shareDialogBuilder = new AlertDialog .Builder (context );
102213 LayoutInflater inflater = this .getLayoutInflater ();
@@ -127,11 +238,14 @@ public void onClick(DialogInterface dialogInterface, int i) {
127238 shareIntent .putExtra (Intent .EXTRA_TEXT , desc .getText ().toString ());
128239 shareIntent .setType ("text/plain" );
129240 startActivity (Intent .createChooser (shareIntent , getResources ().getText (R .string .share_intent_value )));
130- };
241+ }
242+
243+ ;
131244 });
132245 AlertDialog shareDialog = shareDialogBuilder .create ();
133246 shareDialog .show ();
134247 }
248+
135249 public void newTask () {
136250 testInt ++;
137251 final AlertDialog .Builder newTaskDialogBuilder = new AlertDialog .Builder (context );
@@ -228,7 +342,28 @@ public boolean onOptionsItemSelected(MenuItem item) {
228342 startActivity (prefsIntent );
229343 break ;
230344 case R .id .action_about :
231- Snackbar .make (view , "Coming soon!" , Snackbar .LENGTH_SHORT ).show ();
345+ String aboutDialogText = getString (R .string .about_dialog_text );
346+ AlertDialog .Builder aboutDialogBuilder = new AlertDialog .Builder (context );
347+ aboutDialogBuilder .setTitle ("About this app" );
348+ aboutDialogBuilder .setMessage (aboutDialogText );
349+ aboutDialogBuilder .setPositiveButton ("Close" , new DialogInterface .OnClickListener () {
350+ @ Override
351+ public void onClick (DialogInterface dialogInterface , int i ) {
352+ dialogInterface .dismiss ();
353+ }
354+ });
355+ aboutDialogBuilder .setNeutralButton ("Visit Source Code" , new DialogInterface .OnClickListener () {
356+ @ Override
357+ public void onClick (DialogInterface dialogInterface , int i ) {
358+ String githubUrl = "https://github.com/Chan4077/StudyBuddy" ;
359+ Intent githubIntent = new Intent ();
360+ githubIntent .setAction (Intent .ACTION_VIEW );
361+ githubIntent .setData (Uri .parse (githubUrl ));
362+ startActivity (githubIntent );
363+ }
364+ });
365+ AlertDialog aboutDialog = aboutDialogBuilder .create ();
366+ aboutDialog .show ();
232367 break ;
233368 case R .id .action_share :
234369 share ();
@@ -238,4 +373,8 @@ public boolean onOptionsItemSelected(MenuItem item) {
238373 return super .onOptionsItemSelected (item );
239374 }
240375
376+ @ Override
377+ public void onConnectionFailed (@ NonNull ConnectionResult connectionResult ) {
378+ Log .d ("Tag" , "onConnectionFailed:" + connectionResult );
379+ }
241380}
0 commit comments