Skip to content

Commit cc40af0

Browse files
committed
basic google sign in functionality
1 parent ae1c651 commit cc40af0

File tree

5 files changed

+245
-98
lines changed

5 files changed

+245
-98
lines changed

app/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ dependencies {
3232
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
3333
implementation 'com.google.firebase:firebase-analytics:17.2.0'
3434
implementation 'com.google.firebase:firebase-ml-vision:24.0.1'
35+
implementation 'com.google.firebase:firebase-auth:19.2.0'
36+
implementation 'com.google.android.gms:play-services-auth:17.0.0'
3537
}
3638

37-
apply plugin: 'com.google.gms.google-services'
39+
apply plugin: 'com.google.gms.google-services'

app/src/main/AndroidManifest.xml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,21 @@
99
android:roundIcon="@mipmap/ic_launcher_round"
1010
android:supportsRtl="true"
1111
android:theme="@style/AppTheme">
12-
<meta-data
13-
android:name="com.google.firebase.ml.vision.DEPENDENCIES"
14-
android:value="ocr" />
15-
<!-- To use multiple models: android:value="ocr,model2,model3" -->
16-
<activity
17-
android:name=".MainActivity"
18-
android:label="@string/app_name">
12+
<activity android:name=".SignInActivity">
1913
<intent-filter>
2014
<action android:name="android.intent.action.MAIN" />
2115

2216
<category android:name="android.intent.category.LAUNCHER" />
2317
</intent-filter>
2418
</activity>
19+
20+
<meta-data
21+
android:name="com.google.firebase.ml.vision.DEPENDENCIES"
22+
android:value="ocr" /> <!-- To use multiple models: android:value="ocr,model2,model3" -->
23+
<activity
24+
android:name=".MainActivity"
25+
android:label="@string/app_name">
26+
</activity>
2527
</application>
2628

2729
</manifest>
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package com.example.menumap;
2+
3+
import androidx.annotation.NonNull;
4+
import androidx.appcompat.app.AppCompatActivity;
5+
6+
import android.content.Intent;
7+
import android.os.Bundle;
8+
import android.util.Log;
9+
import android.view.View;
10+
11+
import com.google.android.gms.auth.api.signin.GoogleSignIn;
12+
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
13+
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
14+
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
15+
import com.google.android.gms.common.api.ApiException;
16+
import com.google.android.gms.tasks.OnCompleteListener;
17+
import com.google.android.gms.tasks.Task;
18+
import com.google.android.material.snackbar.Snackbar;
19+
import com.google.firebase.auth.AuthCredential;
20+
import com.google.firebase.auth.AuthResult;
21+
import com.google.firebase.auth.FirebaseAuth;
22+
import com.google.firebase.auth.FirebaseUser;
23+
import com.google.firebase.auth.GoogleAuthProvider;
24+
25+
public class SignInActivity extends AppCompatActivity implements View.OnClickListener {
26+
27+
GoogleSignInClient mGoogleSignInClient;
28+
private FirebaseAuth mAuth;
29+
30+
31+
@Override
32+
protected void onCreate(Bundle savedInstanceState) {
33+
super.onCreate(savedInstanceState);
34+
setContentView(R.layout.activity_sign_in);
35+
36+
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
37+
.requestIdToken(getString(R.string.default_web_client_id))
38+
.requestEmail()
39+
.build();
40+
41+
// Build a GoogleSignInClient with the options specified by gso.
42+
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
43+
44+
mAuth = FirebaseAuth.getInstance();
45+
46+
findViewById(R.id.sign_in_button).setOnClickListener(this);
47+
}
48+
49+
@Override
50+
protected void onStart() {
51+
super.onStart();
52+
// Check for existing Google Sign In account, if the user is already signed in
53+
// the GoogleSignInAccount will be non-null.
54+
FirebaseUser currentUser = mAuth.getCurrentUser();
55+
56+
if (currentUser != null) {
57+
goToMainActivity(currentUser);
58+
}
59+
}
60+
61+
@Override
62+
public void onClick(View v) {
63+
switch (v.getId()) {
64+
case R.id.sign_in_button:
65+
signIn();
66+
break;
67+
}
68+
}
69+
70+
private void signIn() {
71+
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
72+
startActivityForResult(signInIntent, 1);
73+
}
74+
75+
@Override
76+
public void onActivityResult(int requestCode, int resultCode, Intent data) {
77+
super.onActivityResult(requestCode, resultCode, data);
78+
79+
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
80+
if (requestCode == 1) {
81+
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
82+
try {
83+
// Google Sign In was successful, authenticate with Firebase
84+
GoogleSignInAccount account = task.getResult(ApiException.class);
85+
firebaseAuthWithGoogle(account);
86+
} catch (ApiException e) {
87+
// Google Sign In failed, update UI appropriately
88+
Log.w("Failed", "Google sign in failed", e);
89+
// ...
90+
}
91+
}
92+
}
93+
94+
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
95+
Log.d("Logging in", "firebaseAuthWithGoogle:" + acct.getId());
96+
97+
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
98+
mAuth.signInWithCredential(credential)
99+
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
100+
@Override
101+
public void onComplete(@NonNull Task<AuthResult> task) {
102+
if (task.isSuccessful()) {
103+
// Sign in success, update UI with the signed-in user's information
104+
Log.d("success", "signInWithCredential:success");
105+
FirebaseUser user = mAuth.getCurrentUser();
106+
107+
goToMainActivity(user);
108+
} else {
109+
// If sign in fails, display a message to the user.
110+
Log.w("failure", "signInWithCredentiasignInWithCredentiall:failure", task.getException());
111+
Snackbar.make(findViewById(R.id.signin_layout), "Authentication Failed.", Snackbar.LENGTH_SHORT).show();
112+
}
113+
114+
// ...
115+
}
116+
});
117+
}
118+
119+
private void goToMainActivity(FirebaseUser user){
120+
Intent intent = new Intent(this, MainActivity.class);
121+
intent.putExtra("user", user);
122+
startActivity(intent);
123+
}
124+
}

app/src/main/java/com/example/menumap/ui/dashboard/DashboardFragment.java

Lines changed: 90 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -74,96 +74,96 @@ public void onClick(View view) {
7474
return root;
7575
}
7676

77-
private FirebaseVisionImage imageFromBitmap(Bitmap bitmap) {
78-
// [START image_from_bitmap]
79-
Image image = (Image) bitmap;
80-
int rotation = getRotationCompensation(
81-
CameraCharacteristics.LENS_FACING_FRONT, MainActivity.this, this);
82-
return FirebaseVisionImage.fromMediaImage(image, rotation);
83-
// [END image_from_bitmap]
84-
}
85-
86-
@Override
87-
public void onActivityResult(int requestCode, int resultCode, Intent data) {
88-
if(requestCode == 1 && resultCode == -1 && data != null){
89-
Log.d("check", "in onActivityResult");
90-
Bundle extras = data.getExtras();
91-
if(extras != null) {
92-
Bitmap bm = (Bitmap) extras.get("data");
93-
this.mImageView.setImageBitmap(bm);
94-
95-
FirebaseVisionImage image = imageFromBitmap(bm);
96-
97-
98-
Task<FirebaseVisionText> result = mDetector.processImage(image)
99-
.addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
100-
@Override
101-
public void onSuccess(FirebaseVisionText result) {
102-
String resultText = result.getText();
103-
Log.d("result", resultText);
104-
}
105-
})
106-
.addOnFailureListener(new OnFailureListener() {
107-
@Override
108-
public void onFailure(@NonNull Exception e) {
109-
e.printStackTrace();
110-
}
111-
});
112-
113-
}
114-
}
115-
}
116-
private static final SparseIntArray ORIENTATIONS = new SparseIntArray();
117-
static {
118-
ORIENTATIONS.append(Surface.ROTATION_0, 90);
119-
ORIENTATIONS.append(Surface.ROTATION_90, 0);
120-
ORIENTATIONS.append(Surface.ROTATION_180, 270);
121-
ORIENTATIONS.append(Surface.ROTATION_270, 180);
122-
}
123-
124-
/**
125-
* Get the angle by which an image must be rotated given the device's current
126-
* orientation.
127-
*/
128-
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
129-
private int getRotationCompensation(String cameraId, Activity activity, Context context)
130-
throws CameraAccessException {
131-
// Get the device's current rotation relative to its "native" orientation.
132-
// Then, from the ORIENTATIONS table, look up the angle the image must be
133-
// rotated to compensate for the device's rotation.
134-
int deviceRotation = activity.getWindowManager().getDefaultDisplay().getRotation();
135-
int rotationCompensation = ORIENTATIONS.get(deviceRotation);
136-
137-
// On most devices, the sensor orientation is 90 degrees, but for some
138-
// devices it is 270 degrees. For devices with a sensor orientation of
139-
// 270, rotate the image an additional 180 ((270 + 270) % 360) degrees.
140-
CameraManager cameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
141-
int sensorOrientation = cameraManager
142-
.getCameraCharacteristics(cameraId)
143-
.get(CameraCharacteristics.SENSOR_ORIENTATION);
144-
rotationCompensation = (rotationCompensation + sensorOrientation + 270) % 360;
145-
146-
// Return the corresponding FirebaseVisionImageMetadata rotation value.
147-
int result;
148-
switch (rotationCompensation) {
149-
case 0:
150-
result = FirebaseVisionImageMetadata.ROTATION_0;
151-
break;
152-
case 90:
153-
result = FirebaseVisionImageMetadata.ROTATION_90;
154-
break;
155-
case 180:
156-
result = FirebaseVisionImageMetadata.ROTATION_180;
157-
break;
158-
case 270:
159-
result = FirebaseVisionImageMetadata.ROTATION_270;
160-
break;
161-
default:
162-
result = FirebaseVisionImageMetadata.ROTATION_0;
163-
Log.e("Rotation", "Bad rotation value: " + rotationCompensation);
164-
}
165-
return result;
166-
}
77+
// private FirebaseVisionImage imageFromBitmap(Bitmap bitmap) {
78+
// // [START image_from_bitmap]
79+
// Image image = (Image) bitmap;
80+
// int rotation = getRotationCompensation(
81+
// CameraCharacteristics.LENS_FACING_FRONT, MainActivity.this, this);
82+
// return FirebaseVisionImage.fromMediaImage(image, rotation);
83+
// // [END image_from_bitmap]
84+
// }
85+
//
86+
// @Override
87+
// public void onActivityResult(int requestCode, int resultCode, Intent data) {
88+
// if(requestCode == 1 && resultCode == -1 && data != null){
89+
// Log.d("check", "in onActivityResult");
90+
// Bundle extras = data.getExtras();
91+
// if(extras != null) {
92+
// Bitmap bm = (Bitmap) extras.get("data");
93+
// this.mImageView.setImageBitmap(bm);
94+
//
95+
// FirebaseVisionImage image = imageFromBitmap(bm);
96+
//
97+
//
98+
// Task<FirebaseVisionText> result = mDetector.processImage(image)
99+
// .addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
100+
// @Override
101+
// public void onSuccess(FirebaseVisionText result) {
102+
// String resultText = result.getText();
103+
// Log.d("result", resultText);
104+
// }
105+
// })
106+
// .addOnFailureListener(new OnFailureListener() {
107+
// @Override
108+
// public void onFailure(@NonNull Exception e) {
109+
// e.printStackTrace();
110+
// }
111+
// });
112+
//
113+
// }
114+
// }
115+
// }
116+
// private static final SparseIntArray ORIENTATIONS = new SparseIntArray();
117+
// static {
118+
// ORIENTATIONS.append(Surface.ROTATION_0, 90);
119+
// ORIENTATIONS.append(Surface.ROTATION_90, 0);
120+
// ORIENTATIONS.append(Surface.ROTATION_180, 270);
121+
// ORIENTATIONS.append(Surface.ROTATION_270, 180);
122+
// }
123+
//
124+
// /**
125+
// * Get the angle by which an image must be rotated given the device's current
126+
// * orientation.
127+
// */
128+
// @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
129+
// private int getRotationCompensation(String cameraId, Activity activity, Context context)
130+
// throws CameraAccessException {
131+
// // Get the device's current rotation relative to its "native" orientation.
132+
// // Then, from the ORIENTATIONS table, look up the angle the image must be
133+
// // rotated to compensate for the device's rotation.
134+
// int deviceRotation = activity.getWindowManager().getDefaultDisplay().getRotation();
135+
// int rotationCompensation = ORIENTATIONS.get(deviceRotation);
136+
//
137+
// // On most devices, the sensor orientation is 90 degrees, but for some
138+
// // devices it is 270 degrees. For devices with a sensor orientation of
139+
// // 270, rotate the image an additional 180 ((270 + 270) % 360) degrees.
140+
// CameraManager cameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
141+
// int sensorOrientation = cameraManager
142+
// .getCameraCharacteristics(cameraId)
143+
// .get(CameraCharacteristics.SENSOR_ORIENTATION);
144+
// rotationCompensation = (rotationCompensation + sensorOrientation + 270) % 360;
145+
//
146+
// // Return the corresponding FirebaseVisionImageMetadata rotation value.
147+
// int result;
148+
// switch (rotationCompensation) {
149+
// case 0:
150+
// result = FirebaseVisionImageMetadata.ROTATION_0;
151+
// break;
152+
// case 90:
153+
// result = FirebaseVisionImageMetadata.ROTATION_90;
154+
// break;
155+
// case 180:
156+
// result = FirebaseVisionImageMetadata.ROTATION_180;
157+
// break;
158+
// case 270:
159+
// result = FirebaseVisionImageMetadata.ROTATION_270;
160+
// break;
161+
// default:
162+
// result = FirebaseVisionImageMetadata.ROTATION_0;
163+
// Log.e("Rotation", "Bad rotation value: " + rotationCompensation);
164+
// }
165+
// return result;
166+
// }
167167

168168

169169
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:id="@+id/signin_layout"
6+
android:layout_width="match_parent"
7+
android:layout_height="match_parent"
8+
tools:context=".SignInActivity">
9+
10+
<com.google.android.gms.common.SignInButton
11+
android:id="@+id/sign_in_button"
12+
android:layout_width="wrap_content"
13+
android:layout_height="wrap_content"
14+
app:layout_constraintBottom_toBottomOf="parent"
15+
app:layout_constraintEnd_toEndOf="parent"
16+
app:layout_constraintStart_toStartOf="parent"
17+
app:layout_constraintTop_toTopOf="parent" />
18+
19+
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)