Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. package com.top.mobigold;
  2.  
  3. import androidx.annotation.Nullable;
  4. import androidx.appcompat.app.AppCompatActivity;
  5.  
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.content.SharedPreferences;
  9. import android.os.Bundle;
  10. import android.os.Handler;
  11. import android.util.Log;
  12. import android.view.View;
  13.  
  14. import com.google.android.gms.auth.api.signin.GoogleSignIn;
  15. import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
  16. import com.google.android.gms.auth.api.signin.GoogleSignInClient;
  17. import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
  18. import com.google.android.gms.common.SignInButton;
  19. import com.google.android.gms.common.api.ApiException;
  20. import com.google.android.gms.tasks.Task;
  21.  
  22. public class MainActivity extends AppCompatActivity {
  23.  
  24. private GoogleSignInClient mGoogleSignInClient;
  25.  
  26. @Override
  27. protected void onCreate(@Nullable Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29.  
  30. SharedPreferences mPreferences = this.getSharedPreferences("first_time", Context.MODE_PRIVATE);
  31. boolean firstTime = mPreferences.getBoolean("firstTime", true);
  32.  
  33. if(firstTime){
  34. for (final int i: new int []{10, 30, 60, 120, 300}) {
  35. new Handler().postDelayed(new Runnable() {
  36. @Override
  37. public void run() {
  38. Log.w("Event11", "Event after " + i + " seconds");
  39. // YandexMetrica.reportEvent("Event after " + i + " seconds");
  40. }}, i * 1000);
  41. }
  42. }
  43.  
  44. SharedPreferences.Editor editor = mPreferences.edit();
  45. editor.putBoolean("firstTime", false);
  46. editor.apply();
  47.  
  48. GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
  49. if(account != null){
  50. Intent intent = new Intent(getApplicationContext(), WebActivity.class);
  51. intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
  52. startActivity(intent);
  53. Log.w("Cash", "finished");
  54.  
  55. }
  56. setContentView(R.layout.activity_main);
  57.  
  58. GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
  59. .requestEmail()
  60. .build();
  61.  
  62. mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
  63.  
  64. SignInButton signInButton = findViewById(R.id.sign_in_button);
  65. signInButton.setSize(SignInButton.SIZE_STANDARD);
  66. signInButton.setOnClickListener(new View.OnClickListener() {
  67. @Override
  68. public void onClick(View v) {
  69.  
  70. Intent signInIntent = mGoogleSignInClient.getSignInIntent();
  71. startActivityForResult(signInIntent, 9001);
  72. }
  73. });
  74. }
  75.  
  76. @Override
  77. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  78. super.onActivityResult(requestCode, resultCode, data);
  79. Log.w("Cash", "onActivityResult " + requestCode + " " + resultCode);
  80.  
  81. // Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
  82. if (requestCode == 9001) {
  83. // The Task returned from this call is always completed, no need to attach a listener.
  84. Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
  85. handleSignInResult(task);
  86. }
  87. }
  88.  
  89. private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
  90. try {
  91. GoogleSignInAccount account = completedTask.getResult(ApiException.class);
  92.  
  93. if(account != null){
  94. // Signed in successfully, show authenticated UI.
  95. Intent intent = new Intent(getApplicationContext(), MainActivity.class);
  96. intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
  97. startActivity(intent);
  98. }
  99.  
  100. } catch (ApiException e) {
  101. Log.w("Cash", "" + e.getStatusCode());
  102. }
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement