Skip to content

Commit 96e903a

Browse files
committed
feat(): Add firebase
- Also add AppUpdater - Add permissions required by AppUpdater
1 parent 50d50d5 commit 96e903a

18 files changed

+337
-35
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Welcome to StudyBuddy! This project is currently a work in progress and is unfortunately delayed by other events.
33

44
## About
5-
This app started when someone told me that since I was excellent in coding (example [MyFirstGame](https://github.com/Chan4077/MyFirstGamw)) and this app is meant for those who would like a study calendar.
5+
This app started when someone told me that since I was excellent in coding (example [MyFirstGame](https://github.com/Chan4077/MyFirstGame)) and this app is meant for those who would like a study calendar.
66

77
## Current status
88
Currently, the project is in **BETA**.

app/build.gradle

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
apply plugin: 'com.android.application'
2-
apply plugin: 'com.google.gms.google-services'
32

43
android {
54
compileSdkVersion 26
@@ -31,4 +30,10 @@ dependencies {
3130
compile 'com.android.support:design:26.0.1'
3231
compile 'com.android.support:cardview-v7:26.0.1'
3332
testCompile 'junit:junit:4.12'
33+
compile 'com.google.firebase:firebase-storage:11.0.4'
34+
compile 'com.google.firebase:firebase-database:11.0.4'
35+
compile 'com.google.firebase:firebase-auth:11.0.4'
36+
compile 'com.google.android.gms:play-services-auth:11.0.4'
37+
compile 'com.github.javiersantos:AppUpdater:2.6.3'
3438
}
39+
apply plugin: 'com.google.gms.google-services'

app/google-services.json

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{
22
"project_info": {
33
"project_number": "713563449638",
4-
"project_id": "studybuddy-e5f46"
4+
"firebase_url": "https://studybuddy-e5f46.firebaseio.com",
5+
"project_id": "studybuddy-e5f46",
6+
"storage_bucket": "studybuddy-e5f46.appspot.com"
57
},
68
"client": [
79
{
@@ -12,6 +14,10 @@
1214
}
1315
},
1416
"oauth_client": [
17+
{
18+
"client_id": "713563449638-sc1up855asm687s55f8qi4bdrh0u18tc.apps.googleusercontent.com",
19+
"client_type": 3
20+
},
1521
{
1622
"client_id": "713563449638-ec59ooakmtdjps3595st07klier9mshs.apps.googleusercontent.com",
1723
"client_type": 1,
@@ -35,11 +41,16 @@
3541
"status": 1
3642
},
3743
"appinvite_service": {
38-
"status": 1,
39-
"other_platform_oauth_client": []
44+
"status": 2,
45+
"other_platform_oauth_client": [
46+
{
47+
"client_id": "713563449638-sc1up855asm687s55f8qi4bdrh0u18tc.apps.googleusercontent.com",
48+
"client_type": 3
49+
}
50+
]
4051
},
4152
"ads_service": {
42-
"status": 1
53+
"status": 2
4354
}
4455
}
4556
}

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.edricchan.studybuddy">
4-
4+
<!-- Required by AppUpdater -->
5+
<uses-permission android:name="android.permission.INTERNET"/>
6+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
57
<application
68
android:allowBackup="true"
79
android:icon="@mipmap/ic_launcher"

app/src/main/java/com/edricchan/studybuddy/MainActivity.java

Lines changed: 143 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import android.content.Context;
55
import android.content.DialogInterface;
66
import android.content.Intent;
7+
import android.net.Uri;
78
import android.os.Bundle;
9+
import android.support.annotation.NonNull;
810
import android.support.design.widget.FloatingActionButton;
911
import android.support.design.widget.Snackbar;
1012
import android.support.design.widget.TextInputEditText;
@@ -14,12 +16,31 @@
1416
import android.support.v7.widget.LinearLayoutManager;
1517
import android.support.v7.widget.RecyclerView;
1618
import android.support.v7.widget.Toolbar;
19+
import android.util.Log;
1720
import android.view.LayoutInflater;
1821
import android.view.Menu;
1922
import android.view.MenuItem;
2023
import android.view.View;
2124
import android.widget.DatePicker;
2225
import 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

2445
import java.text.ParseException;
2546
import java.text.SimpleDateFormat;
@@ -30,7 +51,7 @@
3051

3152
import 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

Comments
 (0)