GoogleApiClient identifier expected Android Studio error - android

I'm using Android Studio and I want to use Google API as a login. If the login is succesful, it can change to another activity. Besides that, I want to show the account name and profile picture in another activity, but I got this error:
error: identifier expected GoogleApiClient = new GoogleApiClient.Builder(this).enableAutoManage(this,this).addApi(Auth.GOOGLE_SIGN_IN_API, gso).build();
My code:
package com.example.alif.angkotcoba;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;
public class MainActivity extends AppCompatActivity implements
View.OnClickListener,GoogleApiClient.OnConnectionFailedListener {
private SignInButton SignIn;
private GoogleApiClient GoogleApiClient;
private static final int RC_SIGN_IN =9001;
private static final String TAG ="signInActivity";
GoogleSignInOptions gso=new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
GoogleApiClient = new GoogleApiClient.Builder(this).enableAutoManage(this,this).addApi(Auth.GOOGLE_SIGN_IN_API, gso).build();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SignIn = (SignInButton) findViewById(R.id.btngoogle);
SignIn.setOnClickListener(this);
}
public void gotoSecondActivity (View view) {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btngoogle:
signIn ();
break;
}
}
public void signIn ()
{
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(GoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
public void handleResult (GoogleSignInResult result) {
GoogleSignInAccount account = result.getSignInAccount();
String name = account.getDisplayName();
String email = account.getEmail();
String img_url = account.getPhotoUrl().toString();
}
#Override
public void onActivityResult(int requestcode,int resultcode,Intent data) {
super.onActivityResult(requestcode,resultcode,data);
if (requestcode==RC_SIGN_IN){
GoogleSignInResult result=Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
private void handleSignInResult(GoogleSignInResult result) {
Log.d(TAG,"handleSignInResult:"+ result.isSuccess());
if (result.isSuccess()){
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
}
#Override
public void onConnectionFailed(#NonNull ConnectionResult connectionResult) {
Log.d(TAG,"onConnectionFailed:"+connectionResult);
}
}

You need a variable name in the declaration. The problem is that you are trying to declare a variable with the same name as a class:
private GoogleApiClient GoogleApiClient;
That won't work. As far as the compiler is concerned, all you have on the left side of the assignment is the type name GoogleApiClient. Try something like this:
private GoogleApiClient googleApiClient; // note change of case!
googleApiClient = ...
Also, you are probably better off initializing anything that depends on this inside onCreate rather than when the variable is declared.

Related

Attempt to invoke virtual method 'void com.google.android.gms.common.SignInButton.setSize(int)' on a null object reference

package design.nxn.login_template_01;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import java.text.BreakIterator;
import java.util.Objects;
import design.nxn.login_template_01.Tools.CustomViewPager;
import design.nxn.login_template_01.Tools.ViewPagerAdapter;
public class LoginActivity extends BaseActivity implements GoogleApiClient.OnConnectionFailedListener, View.OnClickListener {
private static final String TAG = BaseActivity.class.getSimpleName();
private static final int RC_SIGN_IN = 420;
private GoogleApiClient mGoogleApiClient;
private ProgressDialog mProgressDialog;
private SignInButton btnSignIn;
private GoogleSignInClient mGoogleSignInClient;
private String personPhotoUrl;
GoogleSignInOptions gso;
BreakIterator mStatusTextView;
private static final int LOGIN_FRAGMENT = 0;
private static final int SIGNUP_FRAGMENT = 1;
private static final int RESET_PASSWORD_FRAGMENT = 2;
private Fragment newFragment;
private CustomViewPager viewPager;
private ViewPagerAdapter viewPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btnSignIn = (SignInButton)findViewById(R.id.sign_in_button);
initializeGPlusSettings();
initializeControls();
findViewById(R.id.sign_in_button).setOnClickListener(this);
viewPager = findViewById(R.id.viewpager);
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(viewPagerAdapter);
viewPager.setPagingEnabled(false);
changeFragment(LOGIN_FRAGMENT);
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
}
private void initializeControls() {
btnSignIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
});
}
public void onStart() {
super.onStart();
GoogleSignInAccount account =
GoogleSignIn.getLastSignedInAccount(this);
updateUI(account);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task =
GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
private void handleGPlusSignInResult(GoogleSignInResult result) {
Log.d(TAG,"handleSignInResult:" +result.isSuccess());
if (result.isSuccess()){
GoogleSignInAccount acct = result.getSignInAccount();
String personname = acct.getDisplayName();
personPhotoUrl = Objects.requireNonNull(acct.getPhotoUrl()).toString();
String personEmail = acct.getEmail();
Intent intent = new Intent(LoginActivity.this,SecondActivity.class);
intent.putExtra("personname",personname);
intent.putExtra("email",personEmail);
intent.putExtra("personphotoUrl",personPhotoUrl);
startActivity(intent);
finish();
}
}
private void handleSignInResult(Task<GoogleSignInAccount> task) {
try {
GoogleSignInAccount account = task.getResult(ApiException.class);
// Signed in successfully, show authenticated UI.
updateUI(account);
}
catch (ApiException e) {
// The ApiException status code indicates the detailed failure reason.
// Please refer to the GoogleSignInStatusCodes class reference for more information.
Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());
updateUI(null);
}
}
private void initializeGPlusSettings() {
// Configure sign-in to request the user's ID, email address, and basic
// profile. ID and basic profile are included in DEFAULT_SIGN_IN.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().build();
// Build a GoogleSignInClient with the options specified by gso.
mGoogleApiClient = new GoogleApiClient.Builder(this).enableAutoManage(this,this).addApi(Auth.GOOGLE_SIGN_IN_API,gso).build();
btnSignIn.setSize(SignInButton.SIZE_STANDARD);
}
private void changeFragment(int fragmentType) {
switch (fragmentType) {
case LOGIN_FRAGMENT:
viewPager.setCurrentItem(LOGIN_FRAGMENT);
break;
case SIGNUP_FRAGMENT:
viewPager.setCurrentItem(SIGNUP_FRAGMENT);
break;
case RESET_PASSWORD_FRAGMENT:
viewPager.setCurrentItem(RESET_PASSWORD_FRAGMENT);
break;
default:
viewPager.setCurrentItem(LOGIN_FRAGMENT);
break;
}
}
public void signUpClick(View view) {
changeFragment(SIGNUP_FRAGMENT);
}
public void signInClick(View view) {
changeFragment(LOGIN_FRAGMENT);
}
public void resetPasswordClick(View view) {
changeFragment(RESET_PASSWORD_FRAGMENT);
}
public void backClick(View view){
changeFragment(LOGIN_FRAGMENT);
}
#Override
public void onBackPressed() {
if (viewPager.getCurrentItem() == LOGIN_FRAGMENT)
super.onBackPressed();
else {
changeFragment(LOGIN_FRAGMENT);
}
}
public void logInButtonClicked() {
Toast.makeText(this, R.string.login_button_click, Toast.LENGTH_SHORT).show();
}
public void signUpButtonClicked() {
Toast.makeText(this, R.string.signup_button_click, Toast.LENGTH_SHORT).show();
}
public void resetPasswordButtonClicked() {
Toast.makeText(this, R.string.reset_password_button_clicked, Toast.LENGTH_SHORT).show();
}
#Override
public void onConnectionFailed(#NonNull ConnectionResult connectionResult)
{
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.sign_in_button:
signIn();
case R.id.logout:
signOut();
break;
}
}
private void signOut() {
mGoogleSignInClient.signOut()
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
// ...
}
});
}
private void revokeAccess() {
mGoogleSignInClient.revokeAccess()
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
// ...
}
});
}
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
private void updateUI(GoogleSignInAccount account) {
if (account != null) {
mStatusTextView.setText(getString(R.string.signed_in_fmt, account.getDisplayName()));
findViewById(R.id.sign_in_button).setVisibility(View.GONE);
findViewById(R.id.logout).setVisibility(View.VISIBLE);
}
else {
mStatusTextView.setText(String.valueOf(R.string.signed_out));
findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
findViewById(R.id.logout).setVisibility(View.GONE);
}
}
}
You are setting click here
switch (view.getId()) {
case R.id.sign_in_button:
signIn();
As per this code you need to use
btnSignIn.setOnClickListener(this);
Instead of
findViewById(R.id.sign_in_button).setOnClickListener(this);

How to pass an GoogleApiClient objet to another activity

In my loginactivity, I have an google Sign In button to LogIn.
I checked and works fine.
I had anothers buttons to log out and Revoke and worked fine too.
This is my code of the loggin screen.
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.OptionalPendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements
View.OnClickListener,
GoogleApiClient.OnConnectionFailedListener
{
private static final String TAG = MainActivity.class.getSimpleName();
private static final int RC_SIGN_IN = 007;
private GoogleApiClient mGoogleApiClient;
private ProgressDialog mProgressDialog;
private SignInButton btnSignIn;
List<String> profile=new ArrayList<>();
private MyGoogleApi_Singleton myGoogleApi_singleton;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSignIn = (SignInButton) findViewById(R.id.sign_in_button);
btnSignIn.setOnClickListener(this);
updateUI(false);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
/* Customizing G+ button
btnSignIn.setSize(SignInButton.SIZE_STANDARD);
btnSignIn.setScopes(gso.getScopeArray());*/
//Create a new objet to handle in all class
myGoogleApi_singleton= new MyGoogleApi_Singleton();
myGoogleApi_singleton.getInstance(mGoogleApiClient);
}
private void signIn()
{
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
private void signOut()
{
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>()
{
#Override
public void onResult(Status status)
{
updateUI(false);
}
});
}
private void revokeAccess()
{
Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>()
{
#Override
public void onResult(Status status)
{
updateUI(false);
}
});
}
private void handleSignInResult(GoogleSignInResult result)
{
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if (result.isSuccess())
{
// Signed in successfully, show authenticated UI.
GoogleSignInAccount acct = result.getSignInAccount();
String personName = acct.getDisplayName();
String personPhotoUrl = acct.getPhotoUrl().toString();
String email = acct.getEmail();
String idnumber= acct.getId();
Log.e(TAG, "Name: " + personName + ", email: " + email
+ ", Image: " + personPhotoUrl+ ", Id Number: "+ idnumber);
//Save the data into the arraylist
profile.add(idnumber);
profile.add(personName);
profile.add(email);
profile.add(personPhotoUrl);
//save into sharedpreferences
StringBuilder stringBuilder = new StringBuilder();
for (String s:profile)
{
stringBuilder.append(s);
stringBuilder.append(",");
}
SharedPreferences sharpref = getSharedPreferences("ProfileList",0);
SharedPreferences.Editor editor = sharpref.edit();
editor.putString("ProfileList", stringBuilder.toString());
editor.commit();
goIndexScreen();
} else
{
// Signed out, show unauthenticated UI.
updateUI(false);
}
}
#Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.sign_in_button:
signIn();
break;
default:
break;
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN)
{
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
#Override
public void onStart()
{
super.onStart();
OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
if (opr.isDone())
{
// If the user's cached credentials are valid, the OptionalPendingResult will be "done"
// and the GoogleSignInResult will be available instantly.
Log.d(TAG, "Got cached sign-in");
GoogleSignInResult result = opr.get();
handleSignInResult(result);
} else
{
// If the user has not previously signed in on this device or the sign-in has expired,
// this asynchronous branch will attempt to sign in the user silently. Cross-device
// single sign-on will occur in this branch.
showProgressDialog();
opr.setResultCallback(new ResultCallback<GoogleSignInResult>()
{
#Override
public void onResult(GoogleSignInResult googleSignInResult)
{
hideProgressDialog();
handleSignInResult(googleSignInResult);
}
});
}
}
#Override
public void onConnectionFailed(#NonNull ConnectionResult connectionResult)
{
// An unresolvable error has occurred and Google APIs (including Sign-In) will not
// be available.
Log.d(TAG, "onConnectionFailed:" + connectionResult);
}
//method to show a progress dialog during the SignIn
private void showProgressDialog()
{
if (mProgressDialog == null)
{
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(true);
}
mProgressDialog.show();
}
//method to hide the progress dialog
private void hideProgressDialog()
{
if (mProgressDialog != null && mProgressDialog.isShowing())
{
mProgressDialog.hide();
}
}
//method to show or hide the buttons
private void updateUI(boolean isSignedIn)
{
if (isSignedIn)
{
btnSignIn.setVisibility(View.GONE);
} else
{
btnSignIn.setVisibility(View.VISIBLE);
}
}
//method to go to next activity
private void goIndexScreen()
{
Intent intent=new Intent(this,Index.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
When the login is succes, the app jump to another activity.
I need to LogOut from this activity, but I need to a mGoogleApiClient from LoginActivity and I donĀ“t know how I could doing.
I have create a new class (singleton) here my code
import com.google.android.gms.common.api.GoogleApiClient;
class MyGoogleApi_Singleton {
private static final String TAG = "GoogleApiClient";
private static MyGoogleApi_Singleton instance = null;
private static GoogleApiClient mGoogleApiClient = null;
private MyGoogleApi_Singleton(Context context) {
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(context)
.enableAutoManage(this, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
}
public static MyGoogleApi_Singleton getInstance(Context context) {
if(instance == null) {
instance = new MyGoogleApi_Singleton(context);
}
return instance;
}
//methods SingIn,SignOut and Revoke
public void Login(GoogleApiClient bGoogleApiClient){
}
public void Logout(){
if(mGoogleApiClient.isConnected()) {
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
#Override
public void onResult(Status status) {
Log.d("LOGOUT-RESULT","LOGOUT");
}
});
}
}
public void Revoke() {
if ((mGoogleApiClient.isConnected())) {
Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
#Override
public void onResult(Status status) {
Log.d("REVOKE-RESULT","REVOKE");
}
});
}
}
#Override
public void onConnectionFailed(#NonNull ConnectionResult connectionResult) {
}
}
This is the code of Index activity
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.util.Log;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import java.util.ArrayList;
import java.util.List;
public class Index extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private ImageView ivprofile;
private TextView tvname;
private TextView tvemail;
private TextView tvidnumber;
private String picprofile;
private String name;
private String idnumber;
private String email;
final List<String> profile = new ArrayList<String>();
private View headerview;
private GoogleApiClient mGoogleApiClient;
private MyGoogleApi_Singleton myGoogleApiSingleton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_index);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//create a headerview to conect to header of left menu
headerview=navigationView.getHeaderView(0);
ivprofile=(ImageView)headerview.findViewById(R.id.imageProfile);
tvname=(TextView)headerview.findViewById(R.id.fullName);
tvemail=(TextView)headerview.findViewById(R.id.email);
tvidnumber=(TextView) headerview.findViewById(R.id.idNumber);
//Load file saved by sharedpreferences into a new arraylist
final SharedPreferences sharpref = getSharedPreferences("ProfileList",0);
String Items = sharpref.getString("ProfileList","");
String [] listItems = Items.split(",");
for (int i=0;i<listItems.length;i++){
profile.add(listItems[i]);
}
//get the profile
idnumber=profile.get(0);
name=profile.get(1);
email=profile.get(2);
picprofile=profile.get(3);
Log.d("ArrayPerfil", name+email+idnumber+picprofile);
tvname.setText(name);
tvidnumber.setText(idnumber);
tvemail.setText(email);
Glide.with(this).load(picprofile).into(ivprofile);
//get the mgoogleapiclient objet
myGoogleApiSingleton=new MyGoogleApi_Singleton();
mGoogleApiClient=myGoogleApiSingleton.get_GoogleApiClient();
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.index, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}else if (id == R.id.logout) {
myGoogleApiSingleton.getInstance(context).Logout();
goLoginScreen();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
//method to go to login screen
private void goLoginScreen() {
Intent intent=new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
As you told me, I created a Transparent Activity. I added the method handleSignInResult because is calling on the method OnActivityResult.
This is the code:
public class GoogleActivity extends AppCompatActivity {
public static final int RC_SIGN_IN = 1000;
private static final String ACTION = "calling_action";
public static Intent getIntent(Context context, int action, Intent actionIntent) {
Intent i = new Intent(context, GoogleActivity.class);
i.putExtra(ACTION, action);
i.putExtra(Intent.EXTRA_INTENT, actionIntent);
return i;
}
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
Intent actionIntent;
int action = getIntent().getIntExtra(ACTION, 0);
switch (action) {
case RC_SIGN_IN:
actionIntent = (Intent) getIntent().getExtras().get(Intent.EXTRA_INTENT);
if (actionIntent != null)
startActivityForResult(actionIntent, RC_SIGN_IN);
break;
case 0:
default:
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RC_SIGN_IN:
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
finish();
return;
}
}
private void handleSignInResult(GoogleSignInResult result)
{
if (result.isSuccess())
{
// Signed in successfully
Log.d("SIGNIN", "YOU ARE LOG IN");
} else
{
// Signed out,
Log.d("SIGNIN", "YOU ARE NOT LOG IN");
}
}
}
I wait for your help!
Thanks!
This is not a bad idea, in fact it is you should do. If you want to handle all actions and events from Google Auth, the easiest, elegant, reusable, and testeable way to do it is to wrap this logic in one or more clases dedicated to that.
If you have few actions you could encalsupate all of them in only one wrap class. For example you could create a class GoogleSingInWrapper and use the Singleton patter to make sure there is only one instance in your entire app.
public class GoogleSingInWrapper {
private static GoogleSingInWrapper instance;
private GoogleApiClient mGoogleApiClient;
private GoogleSingainWrapper() {
// Private constructor to deny the creation of this object through the constructor and prevent creating more then one instance
mGoogleApiClient = /*create your client here*/;
}
public static getInstance(/*params you need*/) {
if(instance == null)
instance = new GoogleSingInWrapper (/*params*/);
return instance;
}
public void login(/*params*/) {
// Login
}
// Other methods
}
So to get (and create an instance if doesn't exists yet) an instance of GoogleSingInWrapper you use:
GoogleSingInWrapper.gerInstance(/*params*/);
Now if you put all variables and logic in this class you can access them from where you want. The mGoogleApiClient must be in this wrapper.
You can now add all method you need such login, logout and revoke.
And use it as follows:
GoogleSingInWrapper.getInstance().login(/*params*/);
GoogleSingInWrapper.getInstance().logout(/*params*/);
GoogleSingInWrapper.getInstance().revoke(/*params*/);
You shouldn't use mGoogleApiClient directly, it should be encapsulated in GoogleSingInWrapper.
EDIT
When I say mGoogleApiClient should be private inside GoogleSingInWrapper it means that you should't have access to it outside the GoogleSingInWrapper class. If you create a GoogleSingInWrapper but you create a method called
public GoogleApiClient get_GoogleApiClient();
you problems keep existing, because you are still using this mGoogleApiClientin all activities. You don't want this, so you want to be decoupled of this object in all activities.
Following your edits, I'll type here more code, but the more free code I give you less you learn.
public class GoogleSingInWrapper {
private static GoogleSingInWrapper instance;
private GoogleApiClient mGoogleApiClient;
private GoogleSingainWrapper(Context context) {
// Private constructor to deny the creation of this object through the constructor and prevent creating more then one instance
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(context)
.enableAutoManage(this, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
}
public static getInstance(Context context) {
if(instance == null)
instance = new GoogleSingInWrapper (/*params*/);
return instance;
}
public void login(/*params*/) {
// Login
}
// Other methods
public void logout(){
if(mGoogleApiClient.isConnected()) {
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
#Override
public void onResult(Status status) {
Log.d("LOGOUT-RESULT","LOGOUT");
}
});
}
}
}
This should look like your GoogleSingInWrapper. And to call for example logout method, you should call as follows:
if (id == R.id.logout) {
GoogleSingInWrapper.getInstance(context).logout();
goLoginScreen();
}
Note the constructor is Private intentionally because you doesn't want to call new GoogleSingInWrapper. If you do that, you are creating multiple instances of this object ant you're breaking the Singleton pattern.
Also, you may note that some processes like login, needs an Activity because the results are posted in onActivityResult. In order to decouple you GoogleSingInWrapper from all your Activities you could create a dedicated Activity to manage all onActivityResults. This activity should be transparent and will be invisible for the user.
You can achieve that with this code:
public class GoogleActivity extends AppCompatActivity {
public static final int RC_SIGN_IN = 1000;
private static final String ACTION = "calling_action";
public static Intent getIntent(Context context, int action, Intent actionIntent) {
Intent i = new Intent(context, GoogleActivity.class);
i.putExtra(ACTION, action);
i.putExtra(Intent.EXTRA_INTENT, actionIntent);
return i;
}
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
Intent actionIntent;
int action = getIntent().getIntExtra(ACTION, 0);
switch (action) {
case RC_SIGN_IN:
actionIntent = (Intent) getIntent().getExtras().get(Intent.EXTRA_INTENT);
if (actionIntent != null)
startActivityForResult(actionIntent, RC_SIGN_IN);
break;
case 0:
default:
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RC_SIGN_IN:
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
finish();
return;
}
}
}
Then set the transparent theme in the manifest for this Activity:
<activity
android:name=".data.drive.GoogleActivity"
android:theme="#style/TransparentActivity"/>
And define your transparent style in your values folder:
<style name="TransparentActivity" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
Now you have to catch all pieces I've done and construct your final product in order to get it working.

PAypal android future payment issues

i am trying to create a future payment flow into my android app. I already send to my server the OaUTH2 authentication in my method sendAuthorizationToServer; My question is, what and how should i do next, i know that i have to implement a http request to set the future payment but the documetns in paypal are using curl. what should i do next?
here is my paypal activity:
package studio.brunocasamassa.ajudaaqui.payment;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import com.google.firebase.database.DatabaseReference;
import com.paypal.android.sdk.payments.PayPalAuthorization;
import com.paypal.android.sdk.payments.PayPalConfiguration;
import com.paypal.android.sdk.payments.PayPalFuturePaymentActivity;
import com.paypal.android.sdk.payments.PayPalService;
import studio.brunocasamassa.ajudaaqui.R;
import studio.brunocasamassa.ajudaaqui.helper.Base64Decoder;
import studio.brunocasamassa.ajudaaqui.helper.FirebaseConfig;
/**
* Created by bruno on 02/07/2017.
*/
public class PaymentActivity extends AppCompatActivity implements View.OnClickListener{
private static final int REQUEST_CODE_FUTURE_PAYMENT = 123;
private static PayPalConfiguration config = new PayPalConfiguration()
// Start with mock environment. When ready, switch to sandbox (ENVIRONMENT_SANDBOX)
// or live (ENVIRONMENT_PRODUCTION)
.environment(PayPalConfiguration.ENVIRONMENT_NO_NETWORK)
.clientId(PayPalConfig.PAYPAL_CLIENT_ID)
// Minimally, you will need to set three merchant information properties.
// These should be the same values that you provided to PayPal when you registered your app.
.merchantName("Hipster Store")
.merchantPrivacyPolicyUri(Uri.parse("https://www.example.com/privacy"))
.merchantUserAgreementUri(Uri.parse("https://www.example.com/legal"));
private ImageButton buttonPay;
private TextView editTextAmount;
private String userKey = Base64Decoder.encoderBase64(FirebaseConfig.getFirebaseAuthentication().getCurrentUser().getEmail());
private DatabaseReference dbUser = FirebaseConfig.getFireBase().child("usuarios").child(userKey);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paypal);
buttonPay = (ImageButton) findViewById(R.id.buttonPay);
editTextAmount = (TextView) findViewById(R.id.editTextAmount);
buttonPay.setOnClickListener(this);
Intent intent = new Intent(this, PayPalService.class);
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
startService(intent);
}
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
PayPalAuthorization auth = data
.getParcelableExtra(PayPalFuturePaymentActivity.EXTRA_RESULT_AUTHORIZATION);
if (auth != null) {
String authorization_code = auth.getAuthorizationCode();
Log.i("FuturePaymentExample", authorization_code);
sendAuthorizationToServer(auth);
}
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.i("FuturePaymentExample", "The user canceled.");
} else if (resultCode == PayPalFuturePaymentActivity.RESULT_EXTRAS_INVALID) {
Log.i("FuturePaymentExample",
"Probably the attempt to previously start the PayPalService had an invalid PayPalConfiguration. Please see the docs.");
}
}
private void sendAuthorizationToServer(PayPalAuthorization auth) {
dbUser.child("auth").setValue(auth);
}
public void onFuturePaymentPressed(View pressed) {
Intent intent = new Intent(PaymentActivity.this, PayPalFuturePaymentActivity.class);
// send the same configuration for restart resiliency
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
startActivityForResult(intent, REQUEST_CODE_FUTURE_PAYMENT);
}
public void onFuturePaymentPurchasePressed(View pressed) {
// Get the Client Metadata ID from the SDK
String metadataId = PayPalConfiguration.getClientMetadataId(this);
// TODO: Send metadataId and transaction details to your server for processing with
// PayPal...
dbUser.child("metadata").setValue(metadataId);
}
#Override
public void onDestroy() {
stopService(new Intent(this, PayPalService.class));
super.onDestroy();
}
#Override
public void onClick(View v) {
onFuturePaymentPressed(v);
//onFuturePaymentPurchasePressed(v);
}
}
if someone knows, please tell me, i will be very gratefull
tks,

Authentication with Firebase Auth UI doesnt work

I am following the instructions in Firebase in a weekend
When I start the app, I am asked to choose an account. But when I choose the account, the same layer is showing up again and again. Happens on emulator and real device.
In the course it looks like this:
Do you have any tips?
EDIT:
MainActivity.java:
package de.marcofriedmann.testlabor:
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import com.firebase.ui.auth.AuthUI;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
private FirebaseAuth mFirebaseAuth;
private FirebaseAuth.AuthStateListener mAuthStateListener;
// Choose an arbitrary request code value
private static final int RC_SIGN_IN = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
// initialize Firebase
mFirebaseAuth = FirebaseAuth.getInstance();
mAuthStateListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is logged in
String username = firebaseAuth.getCurrentUser().getDisplayName();
TextView welcomeText = (TextView) findViewById(R.id.welcome);
welcomeText.setText(username);
} else {
// User is logged out
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setIsSmartLockEnabled(false)
.setProviders(Arrays.asList(
new AuthUI.IdpConfig.Builder(AuthUI.GOOGLE_PROVIDER).build()))
.build(),
RC_SIGN_IN);
}
}
};
}
#Override
protected void onPause() {
super.onPause();
mFirebaseAuth.removeAuthStateListener(mAuthStateListener);
}
#Override
protected void onResume() {
super.onResume();
mFirebaseAuth.addAuthStateListener(mAuthStateListener);
}
}

Im new to Programming and im trying to implement google signin in my app but ive run into an error and im not sure whats wrong

This is my first time asking a question on stackoverflow so sorry in advance if im doing something wrong.
from previous research it seems the error has to do with a missing curly bracket or something but i cant seem to find the error.
this is my error
Error:(61, 9) error: illegal start of expression
Error:(61, 17) error: illegal start of expression
Error:(61, 28) error: ';' expected
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
the error is at
//Start sign in
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
and my code is
package com.madchallenge2016edwindaniel.upbirdwatchers;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;
import android.view.View;
import com.google.android.gms.common.ConnectionResult;
public class LoginActivity extends AppCompatActivity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener{
private GoogleApiClient mGoogleApiClient;
private SignInButton mSignInButton;
private static final int RC_SIGN_IN = 9001;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//Background
getWindow().setBackgroundDrawableResource(R.drawable.login_background);
// Configure sign-in to request the user's ID, email address, and basic
// profile. ID and basic profile are included in DEFAULT_SIGN_IN.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
// Build a GoogleApiClient with access to the Google Sign-In API and the
// options specified by gso.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
mSignInButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sign_in_button:
signIn();
break;
// ...
}
}
});
//Start sign in
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
}
#Override
public void onConnected(Bundle bundle) {
}
#Override
public void onConnectionSuspended(int i) {
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}
A } is missing before private void signIn() { to close the method onCreate.
In order to avoid this kind of typo, always indent your code correctly:
public void foo() {
// correct
}
public void foo() {
// incorrect
}
public void foo() {
// incorrect
}

Categories

Resources