GoogleV3UploadSignInActivity.java 7.1 KB

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