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