MemcardReadProgress.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package com.epson.memcardacc;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.media.MediaScannerConnection;
  5. import android.os.AsyncTask;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.ProgressBar;
  10. import android.widget.TextView;
  11. import com.epson.iprint.prtlogger.Analytics;
  12. import com.epson.iprint.prtlogger.CommonLog;
  13. import com.epson.mobilephone.common.wifidirect.WiFiDirectManager;
  14. import epson.common.Utils;
  15. import epson.print.CommonDefine;
  16. import epson.print.Util.EPLog;
  17. import java.io.File;
  18. import java.util.ArrayList;
  19. import java.util.Iterator;
  20. import java.util.Locale;
  21. public class MemcardReadProgress extends Activity {
  22. public static final String KEY_IMAGE_LIST = "imageList";
  23. public static final String KEY_WRITE_FOLDER_NAME = "target_folder_name";
  24. public static final String LOG_TAG = "MemcardReadProgress";
  25. public static final String MEMCARD_STORAGE_TYPE = "memcard_storage_type";
  26. public static final int RESULT_COMPLETE = -2;
  27. public static final int RESULT_ERROR = 3;
  28. boolean discconectOnThreadFinish = false;
  29. private Button mCancelButton;
  30. private CopyTask mCopyTask;
  31. ProgressBar mEachProgress;
  32. private ArrayList<String> mImageList;
  33. private int mMemcardStorageType;
  34. private String mTargetFolderName;
  35. TextView mTextView;
  36. ProgressBar mTotalProgress;
  37. protected void onCreate(Bundle bundle) {
  38. super.onCreate(bundle);
  39. Utils.setFInishOnTOuchOutside(this, false);
  40. setContentView(R.layout.memcard_write_progress);
  41. Intent intent = getIntent();
  42. mImageList = intent.getStringArrayListExtra("imageList");
  43. mTargetFolderName = intent.getStringExtra("target_folder_name");
  44. mMemcardStorageType = intent.getIntExtra("memcard_storage_type", 1);
  45. mTotalProgress = (ProgressBar) findViewById(R.id.progress_percent);
  46. mTotalProgress.setVisibility(View.VISIBLE);
  47. mEachProgress = (ProgressBar) findViewById(R.id.progress_copies);
  48. mEachProgress.setVisibility(View.GONE);
  49. mTextView = (TextView) findViewById(R.id.percent);
  50. ((TextView) findViewById(R.id.copyingMessageText)).setText(getResources().getString(R.string.memcard_file_reading_message));
  51. Intent intent2 = getIntent();
  52. mImageList = intent2.getStringArrayListExtra("imageList");
  53. mTargetFolderName = intent2.getStringExtra("target_folder_name");
  54. mCancelButton = (Button) findViewById(R.id.cancel_button);
  55. mCancelButton.setOnClickListener(new View.OnClickListener() {
  56. public void onClick(View view) {
  57. MemcardReadProgress.this.actionCancel();
  58. }
  59. });
  60. mCopyTask = new CopyTask();
  61. mCopyTask.execute(new Void[0]);
  62. }
  63. protected void onStop() {
  64. EPLog.d(LOG_TAG, "onStop()");
  65. actionCancel();
  66. discconectOnThreadFinish = true;
  67. super.onStop();
  68. }
  69. public void actionCancel() {
  70. mCancelButton.setEnabled(false);
  71. mCopyTask.taskCancel();
  72. }
  73. public void setProgressText(int i, int i2) {
  74. mTextView.setText(String.format("%d/%d", new Object[]{Integer.valueOf(i), Integer.valueOf(i2)}));
  75. }
  76. public void setProgress(int i, int i2) {
  77. setProgressText(i, i2);
  78. mTotalProgress.setMax(i2);
  79. mTotalProgress.setProgress(i);
  80. }
  81. public void finishWithState(int i, @Nullable CommonLog commonLog) {
  82. if (this.discconectOnThreadFinish) {
  83. WiFiDirectManager.disconnect(this, WiFiDirectManager.DEVICE_TYPE_PRINTER, MemcardUtil.getPrinterIpAddress(this));
  84. }
  85. if (commonLog != null && commonLog.numberOfSheet > 0) {
  86. Analytics.sendCommonLog(this, commonLog);
  87. }
  88. setResult(i);
  89. finish();
  90. }
  91. class CopyTask extends AsyncTask<Void, Integer, Integer> {
  92. CifsAccess cifsAccess = CifsAccess.getInstance();
  93. private boolean mCanceling;
  94. private CommonLog mCommonLog = new CommonLog();
  95. CopyTask() {
  96. CommonLog commonLog = mCommonLog;
  97. commonLog.numberOfSheet = 0;
  98. commonLog.action = 8194;
  99. commonLog.printerName = Analytics.getDefaultPrinterName(MemcardReadProgress.this.getApplicationContext());
  100. }
  101. protected void onCancelled() {
  102. MemcardUtil.clearKeepScreenOn(MemcardReadProgress.this.getWindow());
  103. EPLog.i(MemcardReadProgress.LOG_TAG, "canceld in AsyncTask");
  104. MemcardReadProgress.this.finishWithState(0, mCommonLog);
  105. }
  106. protected void onPostExecute(Integer num) {
  107. MemcardUtil.clearKeepScreenOn(MemcardReadProgress.this.getWindow());
  108. MemcardReadProgress.this.finishWithState(num.intValue(), mCommonLog);
  109. }
  110. protected void onPreExecute() {
  111. mCanceling = false;
  112. MemcardUtil.keepScreenOn(MemcardReadProgress.this.getWindow());
  113. MemcardReadProgress memcardReadProgress = MemcardReadProgress.this;
  114. memcardReadProgress.setProgress(0, memcardReadProgress.mImageList.size());
  115. }
  116. protected void onProgressUpdate(Integer... numArr) {
  117. MemcardReadProgress.this.setProgress(numArr[0].intValue(), MemcardReadProgress.mImageList.size());
  118. }
  119. protected Integer doInBackground(Void... voidArr) {
  120. try {
  121. if (this.cifsAccess.initDefault(MemcardReadProgress.this, 1) == 0) {
  122. return 3;
  123. }
  124. if (this.cifsAccess.connectDefaultStorageWidthDefaultAthInfo(MemcardReadProgress.mMemcardStorageType) == 0) {
  125. cifsAccess.free();
  126. return 3;
  127. }
  128. int copyFile = copyFile();
  129. cifsAccess.disconnectStorage();
  130. mCommonLog.setConnectionType(MemcardReadProgress.this.getApplicationContext());
  131. Integer valueOf = Integer.valueOf(copyFile);
  132. cifsAccess.free();
  133. return valueOf;
  134. } finally {
  135. cifsAccess.free();
  136. }
  137. }
  138. private int copyFile() {
  139. Iterator it = MemcardReadProgress.mImageList.iterator();
  140. int i = 0;
  141. while (it.hasNext()) {
  142. if (isCancelled()) {
  143. return 0;
  144. }
  145. publishProgress(new Integer[]{Integer.valueOf(i)});
  146. String str = (String) it.next();
  147. String format = String.format(Locale.US, "%s/IMG_%04d.JPG", new Object[]{MemcardReadProgress.mTargetFolderName, Integer.valueOf(i)});
  148. i++;
  149. if (new File(format).exists()) {
  150. EPLog.i(MemcardReadProgress.LOG_TAG, "local file exists. skip");
  151. } else {
  152. EPLog.i(MemcardReadProgress.LOG_TAG, "copy " + str + " => " + format);
  153. int readFromPrinterMemcard = cifsAccess.readFromPrinterMemcard(str, format);
  154. if (readFromPrinterMemcard != 0) {
  155. EPLog.w(MemcardReadProgress.LOG_TAG, "cifsAccess.writeFromPrinterMemcard() return <" + readFromPrinterMemcard + ">");
  156. new File(format).delete();
  157. if (readFromPrinterMemcard == -2) {
  158. return 0;
  159. }
  160. return 3;
  161. }
  162. String replace = format.replace(MemcardConfig.BASE_DIRECTORY, CommonDefine.DEFAULT_DIR);
  163. if (new File(replace).exists()) {
  164. EPLog.i(MemcardReadProgress.LOG_TAG, "MediaScannerConnection.scanFile path = " + replace);
  165. MediaScannerConnection.scanFile(MemcardReadProgress.this, new String[]{replace}, (String[]) null, (MediaScannerConnection.OnScanCompletedListener) null);
  166. } else {
  167. EPLog.m307e(MemcardReadProgress.LOG_TAG, "Failed MediaScannerConnection.scanFile path = " + replace);
  168. }
  169. mCommonLog.numberOfSheet = i;
  170. }
  171. }
  172. return -2;
  173. }
  174. public void taskCancel() {
  175. if (!mCanceling) {
  176. mCanceling = true;
  177. cifsAccess.cancel();
  178. cancel(false);
  179. }
  180. }
  181. }
  182. }