GoogleV3UploadSignInActivity.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package com.epson.iprint.storage.gdrivev3;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import androidx.appcompat.app.AppCompatActivity;
  7. import com.epson.iprint.storage.Network;
  8. import com.epson.iprint.storage.OneButtonDialogFragment;
  9. import com.epson.iprint.storage.gdrivev3.IprintGoogleSignIn;
  10. import com.epson.iprint.storage.gdrivev3.PlayServiceDialogManager;
  11. import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
  12. import epson.print.R;
  13. import java.util.Iterator;
  14. import java.util.LinkedList;
  15. public class GoogleV3UploadSignInActivity extends AppCompatActivity implements ActivityWrapper, PlayServiceDialogManager.SingInCancelNotifier, OneButtonDialogFragment.DialogCallback, IprintGoogleSignIn.SignInListener {
  16. private static final String DIALOG_TAG_OFFLINE_ERROR = "offline_error";
  17. private static final String DIALOG_TAG_SIGN_IN_FAILED = "sign-in_failed";
  18. private static final String PARAM_KEY_CHECK_SERVICE_ONLY = "check-service_only";
  19. private static final String PARAM_KEY_FOR_DISCONNECT = "for-disconnect";
  20. private static final int REQUEST_CODE_CHECK_PLAY_SERVICE = 11;
  21. private static final int REQUEST_CODE_SIGN_IN = 10;
  22. private static final int REQUEST_CODE_SIGN_IN_ADD_SCOPE = 12;
  23. private boolean mActivityForeground;
  24. private boolean mCheckServiceOnly;
  25. private LinkedList<Runnable> mForegroundRunnableList;
  26. private IprintGoogleSignIn mIprintGoogleSignIn;
  27. private Network mMisc;
  28. private PlayServiceDialogManager mPlayServiceDialogManager;
  29. private IprintGoogleSignIn.StartActivityResultCallback mSignInStartActivityCallback;
  30. private boolean mStartForDisconnect;
  31. private boolean mWaitSingInProcess;
  32. @NonNull
  33. public Activity getActivity() {
  34. return this;
  35. }
  36. public int getPlayServiceRequestCode() {
  37. return 11;
  38. }
  39. public int getSingInAddScopeRequestCode() {
  40. return 12;
  41. }
  42. @NonNull
  43. public static Intent getDisconnectIntent(@NonNull Context context) {
  44. Intent intent = new Intent(context, GoogleV3UploadSignInActivity.class);
  45. intent.putExtra(PARAM_KEY_FOR_DISCONNECT, true);
  46. return intent;
  47. }
  48. @NonNull
  49. public static Intent getPlayServiceCheckIntent(@NonNull Context context) {
  50. Intent intent = new Intent(context, GoogleV3UploadSignInActivity.class);
  51. intent.putExtra(PARAM_KEY_CHECK_SERVICE_ONLY, true);
  52. return intent;
  53. }
  54. protected void onCreate(Bundle bundle) {
  55. super.onCreate(bundle);
  56. this.mPlayServiceDialogManager = new PlayServiceDialogManager();
  57. this.mMisc = new Network();
  58. this.mIprintGoogleSignIn = new IprintGoogleSignIn();
  59. this.mWaitSingInProcess = false;
  60. this.mActivityForeground = false;
  61. this.mForegroundRunnableList = new LinkedList<>();
  62. Intent intent = getIntent();
  63. this.mStartForDisconnect = intent.getBooleanExtra(PARAM_KEY_FOR_DISCONNECT, false);
  64. this.mCheckServiceOnly = intent.getBooleanExtra(PARAM_KEY_CHECK_SERVICE_ONLY, false);
  65. }
  66. private void showErrorDialog(final int i, final String str) {
  67. if (!this.mActivityForeground) {
  68. this.mForegroundRunnableList.addLast(new Runnable() {
  69. public void run() {
  70. GoogleV3UploadSignInActivity.this.showErrorDialog(i, str);
  71. }
  72. });
  73. } else {
  74. OneButtonDialogFragment.newInstance(i, str).show(getSupportFragmentManager(), str);
  75. }
  76. }
  77. protected void onPostResume() {
  78. super.onPostResume();
  79. this.mMisc.selectSimpleAp(this, false);
  80. this.mActivityForeground = true;
  81. processForegroundList();
  82. if (!this.mMisc.isOnline(this)) {
  83. showErrorDialog(R.string.network_error_mes, DIALOG_TAG_OFFLINE_ERROR);
  84. } else if (!this.mPlayServiceDialogManager.checkPlayService(this, this)) {
  85. if (this.mCheckServiceOnly) {
  86. finishWithResult(true);
  87. } else if (!this.mWaitSingInProcess) {
  88. this.mWaitSingInProcess = true;
  89. if (this.mStartForDisconnect) {
  90. this.mIprintGoogleSignIn.disconnectAccount(this, this);
  91. } else {
  92. this.mIprintGoogleSignIn.startSignIn(this, this, IprintGoogleSignIn.OperationType.UPLOAD);
  93. }
  94. }
  95. }
  96. }
  97. private void processForegroundList() {
  98. Iterator it = this.mForegroundRunnableList.iterator();
  99. while (it.hasNext()) {
  100. it.remove();
  101. ((Runnable) it.next()).run();
  102. }
  103. }
  104. protected void onPause() {
  105. super.onPause();
  106. this.mPlayServiceDialogManager.onActivityPaused();
  107. this.mActivityForeground = false;
  108. }
  109. public void requestSignIn(@NonNull Intent intent, @NonNull IprintGoogleSignIn.StartActivityResultCallback startActivityResultCallback) {
  110. this.mSignInStartActivityCallback = startActivityResultCallback;
  111. startActivityForResult(intent, 10);
  112. }
  113. public void playServiceInstallCancel() {
  114. if (this.mCheckServiceOnly) {
  115. finishWithResult(false);
  116. } else {
  117. showErrorDialog(R.string.authenticate_error_mes, DIALOG_TAG_SIGN_IN_FAILED);
  118. }
  119. }
  120. public void buttonPressed(@Nullable String str) {
  121. if (str != null) {
  122. char c = 65535;
  123. int hashCode = str.hashCode();
  124. if (hashCode != -683930452) {
  125. if (hashCode == 366991495 && str.equals(DIALOG_TAG_SIGN_IN_FAILED)) {
  126. c = 0;
  127. }
  128. } else if (str.equals(DIALOG_TAG_OFFLINE_ERROR)) {
  129. c = 1;
  130. }
  131. switch (c) {
  132. case 0:
  133. finishWithResult(false);
  134. return;
  135. case 1:
  136. startActivity(new Intent("android.settings.WIRELESS_SETTINGS"));
  137. finishWithResult(false);
  138. return;
  139. default:
  140. return;
  141. }
  142. }
  143. }
  144. protected void onActivityResult(int i, int i2, Intent intent) {
  145. switch (i) {
  146. case 10:
  147. IprintGoogleSignIn.StartActivityResultCallback startActivityResultCallback = this.mSignInStartActivityCallback;
  148. if (startActivityResultCallback != null) {
  149. startActivityResultCallback.onActivityResult(i2, intent);
  150. this.mSignInStartActivityCallback = null;
  151. return;
  152. }
  153. return;
  154. case 11:
  155. return;
  156. case 12:
  157. if (i2 == -1) {
  158. finishWithResult(true);
  159. return;
  160. } else {
  161. showErrorDialog(R.string.authenticate_error_mes, DIALOG_TAG_SIGN_IN_FAILED);
  162. return;
  163. }
  164. default:
  165. return;
  166. }
  167. }
  168. public void onSignInComplete(boolean z, GoogleSignInAccount googleSignInAccount) {
  169. if (z) {
  170. finishWithResult(true);
  171. } else {
  172. showErrorDialog(R.string.authenticate_error_mes, DIALOG_TAG_SIGN_IN_FAILED);
  173. }
  174. }
  175. public void onDisconnectCompleted() {
  176. finishWithResult(true);
  177. }
  178. private void finishWithResult(boolean z) {
  179. GoogleV3UploadClient.setSingInValue(z);
  180. setResult(z ? -1 : 0);
  181. finish();
  182. }
  183. }