ListAndDownloadActivity.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. package com.epson.iprint.storage.gdrivev3;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.graphics.Typeface;
  5. import android.os.Build;
  6. import android.os.Bundle;
  7. import android.support.p000v4.app.Fragment;
  8. import android.support.p000v4.app.FragmentManager;
  9. import android.view.Menu;
  10. import android.view.MenuItem;
  11. import android.view.View;
  12. import android.widget.AdapterView;
  13. import android.widget.ListView;
  14. import android.widget.TextView;
  15. import com.epson.iprint.prtlogger.PrintLog;
  16. import com.epson.iprint.storage.ConfirmCancelDialog;
  17. import com.epson.iprint.storage.LocalProgressDialog;
  18. import com.epson.iprint.storage.OneButtonDialogFragment;
  19. import com.epson.iprint.storage.StorageProcessDownloadActivity;
  20. import com.epson.iprint.storage.gdrivev3.GoogleDownloadContract;
  21. import com.epson.iprint.storage.gdrivev3.IprintGoogleSignIn;
  22. import epson.print.ActivityIACommon;
  23. import epson.print.R;
  24. import java.util.ArrayList;
  25. import java.util.Iterator;
  26. import java.util.LinkedList;
  27. public class ListAndDownloadActivity extends ActivityIACommon implements ActivityWrapper, GoogleDownloadContract.View, OneButtonDialogFragment.DialogCallback, ConfirmCancelDialog.DialogCancelListener {
  28. private static final String DIALOG_TAG_CONFIRM_CANCEL = "confirm-cancel";
  29. private static final String DIALOG_TAG_DOWNLOAD_FAILED = "download_failed";
  30. private static final String DIALOG_TAG_OFFLINE_ERROR = "offline_error";
  31. private static final String DIALOG_TAG_SIGN_IN_FAILED = "sign-in_failed";
  32. private static final String FRAGMENT_TAG_PROGRESS = "fragment-progress";
  33. private static final int REQUEST_CODE_CHECK_PLAY_SERVICE = 11;
  34. private static final int REQUEST_CODE_DOWNLOAD_PREVIEW_END = 12;
  35. private static final int REQUEST_CODE_SIGN_IN = 10;
  36. private static final int REQUEST_CODE_SIGN_IN_ADD_SCOPE = 13;
  37. private boolean mActivityForeground;
  38. private FileListAdapter mFileListAdapter;
  39. private LinkedList<Runnable> mForegroundRunnableList;
  40. private GoogleDownloadContract.UserActionListener mGoogleDownloadPresenter;
  41. private boolean mIsDownloadInterruption = false;
  42. private ListView mListView;
  43. private int mPosition = 0;
  44. private TextView mSignInMessage;
  45. private boolean mSignInOutButtonEnabled;
  46. private int mSignInOutButtonType;
  47. private IprintGoogleSignIn.StartActivityResultCallback mSignInStartActivityCallback;
  48. @NonNull
  49. public Activity getActivity() {
  50. return this;
  51. }
  52. @NonNull
  53. public ActivityWrapper getActivityWrapper() {
  54. return this;
  55. }
  56. public int getPlayServiceRequestCode() {
  57. return 11;
  58. }
  59. public int getSingInAddScopeRequestCode() {
  60. return 13;
  61. }
  62. protected void onCreate(Bundle bundle) {
  63. super.onCreate(bundle);
  64. setContentView((int) R.layout.activity_list_and_download);
  65. setActionBar(getString(R.string.google_drive), true);
  66. this.mGoogleDownloadPresenter = Injection.provideDownloadPresenter(this);
  67. this.mSignInOutButtonEnabled = true;
  68. this.mForegroundRunnableList = new LinkedList<>();
  69. this.mListView = (ListView) findViewById(16908298);
  70. this.mListView.setCacheColorHint(0);
  71. this.mFileListAdapter = new FileListAdapter(getApplicationContext());
  72. this.mListView.setAdapter(this.mFileListAdapter);
  73. this.mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  74. public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
  75. int unused = ListAndDownloadActivity.this.mPosition = i;
  76. ListAndDownloadActivity.this.localOnItemClick(i);
  77. }
  78. });
  79. this.mSignInMessage = (TextView) findViewById(16908292);
  80. this.mSignInMessage.setText(R.string.sign_in_request);
  81. this.mSignInMessage.setTypeface((Typeface) null, 1);
  82. this.mSignInMessage.setTextSize(18.0f);
  83. if (bundle != null) {
  84. clearFragments();
  85. }
  86. this.mGoogleDownloadPresenter.initView();
  87. }
  88. private void clearFragments() {
  89. removeProgressIfExists();
  90. deleteAllDialog();
  91. }
  92. private void deleteAllDialog() {
  93. for (String dismissDialog : new String[]{DIALOG_TAG_SIGN_IN_FAILED, DIALOG_TAG_DOWNLOAD_FAILED, DIALOG_TAG_OFFLINE_ERROR, DIALOG_TAG_CONFIRM_CANCEL}) {
  94. dismissDialog(dismissDialog);
  95. }
  96. }
  97. private void dismissDialog(@NonNull final String str) {
  98. if (!this.mActivityForeground) {
  99. this.mForegroundRunnableList.addLast(new Runnable() {
  100. public void run() {
  101. ListAndDownloadActivity.this.dismissDialog(str);
  102. }
  103. });
  104. return;
  105. }
  106. Fragment findFragmentByTag = getSupportFragmentManager().findFragmentByTag(str);
  107. if (findFragmentByTag != null) {
  108. ((DialogFragment) findFragmentByTag).dismiss();
  109. }
  110. }
  111. private void localOnItemClick(int i) {
  112. OnlineFile driveItem = this.mFileListAdapter.getDriveItem(i);
  113. if (driveItem != null) {
  114. this.mGoogleDownloadPresenter.itemSelected(driveItem);
  115. }
  116. }
  117. public boolean onOptionsItemSelected(MenuItem menuItem) {
  118. switch (menuItem.getItemId()) {
  119. case R.id.menu_signin /*2131231253*/:
  120. this.mGoogleDownloadPresenter.signInOrDisconnectClicked();
  121. return true;
  122. case R.id.menu_signout /*2131231254*/:
  123. this.mGoogleDownloadPresenter.signInOrDisconnectClicked();
  124. return true;
  125. default:
  126. return super.onOptionsItemSelected(menuItem);
  127. }
  128. }
  129. public boolean onCreateOptionsMenu(Menu menu) {
  130. getMenuInflater().inflate(R.C2138menu.menu_sign_inout, menu);
  131. if (this.mSignInOutButtonType == 0) {
  132. menu.removeItem(R.id.menu_signout);
  133. menu.findItem(R.id.menu_signin).setEnabled(this.mSignInOutButtonEnabled);
  134. return true;
  135. }
  136. menu.removeItem(R.id.menu_signin);
  137. menu.findItem(R.id.menu_signout).setEnabled(this.mSignInOutButtonEnabled);
  138. return true;
  139. }
  140. protected void onActivityResult(int i, int i2, Intent intent) {
  141. switch (i) {
  142. case 10:
  143. IprintGoogleSignIn.StartActivityResultCallback startActivityResultCallback = this.mSignInStartActivityCallback;
  144. if (startActivityResultCallback != null) {
  145. startActivityResultCallback.onActivityResult(i2, intent);
  146. this.mSignInStartActivityCallback = null;
  147. return;
  148. }
  149. return;
  150. case 11:
  151. return;
  152. case 12:
  153. this.mGoogleDownloadPresenter.onPreviewActivityEnd();
  154. return;
  155. case 13:
  156. this.mGoogleDownloadPresenter.onAddScopeCompleted(i2 == -1);
  157. return;
  158. default:
  159. return;
  160. }
  161. }
  162. protected void onPostResume() {
  163. super.onPostResume();
  164. this.mActivityForeground = true;
  165. processForegroundList();
  166. this.mGoogleDownloadPresenter.activityOnResume();
  167. }
  168. private void processForegroundList() {
  169. Iterator it = this.mForegroundRunnableList.iterator();
  170. while (it.hasNext()) {
  171. it.remove();
  172. ((Runnable) it.next()).run();
  173. }
  174. }
  175. protected void onPause() {
  176. super.onPause();
  177. this.mGoogleDownloadPresenter.activityOnPause();
  178. this.mActivityForeground = false;
  179. }
  180. public void onBackPressed() {
  181. this.mGoogleDownloadPresenter.backKeyPressed();
  182. }
  183. public void requestSignIn(@NonNull Intent intent, @NonNull IprintGoogleSignIn.StartActivityResultCallback startActivityResultCallback) {
  184. this.mSignInStartActivityCallback = startActivityResultCallback;
  185. startActivityForResult(intent, 10);
  186. }
  187. public void setSignInButtonEnabled(boolean z) {
  188. invalidateOptionsMenu();
  189. }
  190. public void changeSignInOutButton(int i) {
  191. int i2 = 0;
  192. this.mSignInMessage.setVisibility(i == 0 ? 0 : 8);
  193. ListView listView = this.mListView;
  194. if (i == 0) {
  195. i2 = 8;
  196. }
  197. listView.setVisibility(i2);
  198. this.mSignInOutButtonType = i;
  199. invalidateOptionsMenu();
  200. }
  201. private void showErrorDialog(final int i, final String str) {
  202. if (!this.mActivityForeground) {
  203. this.mForegroundRunnableList.addLast(new Runnable() {
  204. public void run() {
  205. ListAndDownloadActivity.this.showErrorDialog(i, str);
  206. }
  207. });
  208. } else {
  209. OneButtonDialogFragment.newInstance(i, str).show(getSupportFragmentManager(), str);
  210. }
  211. }
  212. public void showSignInFailDialog() {
  213. showErrorDialog(R.string.authenticate_error_mes, DIALOG_TAG_SIGN_IN_FAILED);
  214. }
  215. public void showDownloadErrorDialog() {
  216. showErrorDialog(R.string.download_error_mes, DIALOG_TAG_DOWNLOAD_FAILED);
  217. }
  218. public void showOfflineErrorDialog() {
  219. showErrorDialog(R.string.network_error_mes, DIALOG_TAG_OFFLINE_ERROR);
  220. }
  221. public void clearListItems() {
  222. this.mFileListAdapter.clearItems();
  223. }
  224. public void showProgress() {
  225. this.mSignInOutButtonEnabled = false;
  226. invalidateOptionsMenu();
  227. showProgressFragment(false, 0);
  228. }
  229. private void showProgressFragment(final boolean z, final int i) {
  230. if (!this.mActivityForeground) {
  231. this.mForegroundRunnableList.addLast(new Runnable() {
  232. public void run() {
  233. ListAndDownloadActivity.this.showProgressFragment(z, i);
  234. }
  235. });
  236. return;
  237. }
  238. FragmentManager supportFragmentManager = getSupportFragmentManager();
  239. supportFragmentManager.beginTransaction().add(16908290, LocalProgressDialog.newInstance(z, i, DIALOG_TAG_CONFIRM_CANCEL), FRAGMENT_TAG_PROGRESS).commit();
  240. }
  241. public void showDownloadProgress() {
  242. this.mSignInOutButtonEnabled = false;
  243. invalidateOptionsMenu();
  244. showProgressFragment(true, R.string.downloading_notification);
  245. }
  246. public void dismissProgress() {
  247. dismissDialog(DIALOG_TAG_CONFIRM_CANCEL);
  248. removeProgressIfExists();
  249. this.mSignInOutButtonEnabled = true;
  250. invalidateOptionsMenu();
  251. }
  252. public void listFiles(@Nullable ArrayList<OnlineFile> arrayList) {
  253. if (arrayList == null) {
  254. clearListItems();
  255. showErrorDialog(R.string.unknow_error_mes, DIALOG_TAG_DOWNLOAD_FAILED);
  256. return;
  257. }
  258. this.mFileListAdapter.setDriveFile(arrayList);
  259. }
  260. public void startPreviewActivity(String str) {
  261. startActivityForResult(StorageProcessDownloadActivity.getPreviewIntent(str, getApplicationContext(), new PrintLog()), 12);
  262. }
  263. public void finishActivity() {
  264. finish();
  265. }
  266. public void buttonPressed(@Nullable String str) {
  267. if (str != null) {
  268. char c = 65535;
  269. int hashCode = str.hashCode();
  270. if (hashCode != -683930452) {
  271. if (hashCode != 156934100) {
  272. if (hashCode == 366991495 && str.equals(DIALOG_TAG_SIGN_IN_FAILED)) {
  273. c = 0;
  274. }
  275. } else if (str.equals(DIALOG_TAG_DOWNLOAD_FAILED)) {
  276. c = 1;
  277. }
  278. } else if (str.equals(DIALOG_TAG_OFFLINE_ERROR)) {
  279. c = 2;
  280. }
  281. switch (c) {
  282. case 0:
  283. return;
  284. case 1:
  285. return;
  286. case 2:
  287. startActivity(new Intent("android.settings.WIRELESS_SETTINGS"));
  288. finish();
  289. return;
  290. default:
  291. return;
  292. }
  293. }
  294. }
  295. public void cancel() {
  296. this.mGoogleDownloadPresenter.cancelDownload();
  297. }
  298. protected void onStop() {
  299. super.onStop();
  300. if (!this.mGoogleDownloadPresenter.isDownloading() || Build.VERSION.SDK_INT < 29) {
  301. this.mIsDownloadInterruption = false;
  302. return;
  303. }
  304. this.mIsDownloadInterruption = true;
  305. cancel();
  306. }
  307. protected void onRestart() {
  308. super.onRestart();
  309. if (this.mIsDownloadInterruption) {
  310. localOnItemClick(this.mPosition);
  311. }
  312. this.mIsDownloadInterruption = false;
  313. }
  314. private void removeProgressIfExists() {
  315. if (!this.mActivityForeground) {
  316. this.mForegroundRunnableList.addLast(new Runnable() {
  317. public void run() {
  318. ListAndDownloadActivity.this.removeProgressIfExists();
  319. }
  320. });
  321. return;
  322. }
  323. FragmentManager supportFragmentManager = getSupportFragmentManager();
  324. Fragment findFragmentByTag = supportFragmentManager.findFragmentByTag(FRAGMENT_TAG_PROGRESS);
  325. if (findFragmentByTag != null) {
  326. supportFragmentManager.beginTransaction().remove(findFragmentByTag).commit();
  327. }
  328. }
  329. }