StorageProcessUploadActivity.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. package epson.print.storage;
  2. import android.content.Intent;
  3. import android.os.AsyncTask;
  4. import android.os.Bundle;
  5. import android.support.p000v4.media.session.PlaybackStateCompat;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.RadioGroup;
  11. import android.widget.TextView;
  12. import com.epson.iprint.storage.gdrivev3.GoogleV3UploadClient;
  13. import com.epson.iprint.storage.gdrivev3.GoogleV3UploadSignInActivity;
  14. import java.io.File;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. import java.util.concurrent.locks.Condition;
  18. import java.util.concurrent.locks.ReentrantLock;
  19. import epson.print.CommonDefine;
  20. import epson.print.ScanFileNumber;
  21. import epson.print.Util.EPLog;
  22. import epson.print.Util.Utils;
  23. import epson.scan.lib.libHaru;
  24. import epson.server.utils.MyUtility;
  25. public class StorageProcessUploadActivity extends StorageProcessActivity {
  26. final String UPLOAD_EDITING_FILENAME = "Upload.Editing.Filename";
  27. List<String> mUploadFileLocalPathList;
  28. String mUploadFilename;
  29. private void onUpdateProcessView() {
  30. }
  31. protected void onCreate(Bundle bundle) {
  32. String string;
  33. super.onCreate(bundle);
  34. setContentView(R.layout.folder_content_layout_for_upload);
  35. setActionBar(getStorageClient().getStorageServiceName(this), true);
  36. final Button button = (Button) findViewById(R.id.save_button);
  37. button.setText(getString(R.string.upload_btn_label));
  38. button.setOnClickListener(new View.OnClickListener() {
  39. public void onClick(View view) {
  40. String filename = getFilename();
  41. if (!getStorageClient().isValidUploadName(filename)) {
  42. showErrorDialog(R.string.str_error_Filename);
  43. } else if (isConnected()) {
  44. button.setEnabled(false);
  45. UploadTask uploadTask = new UploadTask();
  46. uploadTask.localPaths = new ArrayList(StorageProcessUploadActivity.mUploadFileLocalPathList);
  47. uploadTask.uploadFilename = filename;
  48. uploadTask.fileType = getFiletype();
  49. uploadTask.execute(new Void[0]);
  50. } else {
  51. showConnectionError();
  52. }
  53. }
  54. });
  55. bindClearButton(R.id.upload_dialog_file_name, R.id.clear_name_btn, new OnVisibilityListener() {
  56. public void onVisibilityChanged(int i) {
  57. setEnabledSaveButton(i == 0);
  58. }
  59. });
  60. mUploadFilename = generateUploadFilename(0);
  61. if (!(bundle == null || (string = bundle.getString("Upload.Editing.Filename")) == null)) {
  62. mUploadFilename = string;
  63. }
  64. setFilename(mUploadFilename);
  65. mUploadFileLocalPathList = getIntent().getStringArrayListExtra(StorageProcessActivity.EXTRA_UPLOADFILE_LIST);
  66. if (getStorageClient() instanceof GoogleV3UploadClient) {
  67. startActivity(GoogleV3UploadSignInActivity.getPlayServiceCheckIntent(this));
  68. }
  69. }
  70. protected void onSaveInstanceState(Bundle bundle) {
  71. super.onSaveInstanceState(bundle);
  72. mUploadFilename = getFilename();
  73. bundle.putString("Upload.Editing.Filename", mUploadFilename);
  74. }
  75. protected void onRestoreInstanceState(Bundle bundle) {
  76. super.onRestoreInstanceState(bundle);
  77. mUploadFilename = bundle.getString("Upload.Editing.Filename");
  78. }
  79. private void onSignInStatus(SignInStatus signInStatus) {
  80. updateUploadSection(signInStatus);
  81. updateSaveButton(signInStatus);
  82. }
  83. class UploadTask extends AsyncTask<Void, Void, Void> {
  84. boolean canceled;
  85. StorageServiceClient.UploadFileType fileType;
  86. List<String> localPaths;
  87. private volatile StorageServiceClient.ProcessError taskError;
  88. String uploadFilename;
  89. boolean uploaded;
  90. StorageServiceClient.Uploader uploader;
  91. UploadTask() {
  92. }
  93. protected Void doInBackground(Void... voidArr) {
  94. final ReentrantLock reentrantLock;
  95. final OverlayProgress overlayProgress = new OverlayProgress();
  96. overlayProgress.show(getString(R.string.uploading_notification), new CancelRequestCallback() {
  97. public void onCancelRequest(Intent intent) {
  98. if (uploader != null && uploader.isCancelable()) {
  99. uploader.cancel();
  100. }
  101. canceled = true;
  102. }
  103. });
  104. String str = "";
  105. if (StorageServiceClient.UploadFileType.PDF.equals(fileType)) {
  106. String str2 = new File(localPaths.get(0)).getParent() + CommonDefine.SLASH + uploadFilename + CommonDefine.FileType_PDF;
  107. new libHaru().createPDF((ArrayList) localPaths, str2);
  108. localPaths.clear();
  109. localPaths.add(str2);
  110. str = CommonDefine.FileType_PDF;
  111. } else if (StorageServiceClient.UploadFileType.JPEG.equals(fileType)) {
  112. str = ".jpg";
  113. }
  114. boolean z = !this.uploadFilename.equals(generateUploadFilename(0));
  115. int size = localPaths.size();
  116. int i = 0;
  117. while (true) {
  118. if (i >= size) {
  119. break;
  120. }
  121. String str3 = localPaths.get(i);
  122. String str4 = uploadFilename + str;
  123. if (size > 1) {
  124. if (z) {
  125. str4 = String.format("%s%03d%s", new Object[]{this.uploadFilename, Integer.valueOf(i + 1), str});
  126. } else {
  127. str4 = String.format("%s%s", new Object[]{generateUploadFilename(i), str});
  128. }
  129. overlayProgress.changeMessage(getString(R.string.uploading_notification) + (i + 1) + CommonDefine.SLASH + size);
  130. }
  131. reentrantLock = new ReentrantLock();
  132. final Condition newCondition = reentrantLock.newCondition();
  133. try {
  134. reentrantLock.lock();
  135. uploaded = false;
  136. taskError = StorageServiceClient.ProcessError.NONE;
  137. uploader = getStorageClient().getUploader(StorageProcessUploadActivity.this, fileType, str3, str4);
  138. uploader.start(new StorageServiceClient.UploadCompletion() {
  139. public void onUploadComplete(String str, String str2, StorageServiceClient.ProcessError processError) {
  140. try {
  141. reentrantLock.lock();
  142. StorageServiceClient.ProcessError unused = taskError = processError;
  143. uploaded = true;
  144. newCondition.signal();
  145. } catch (Exception e) {
  146. EPLog.w("Storage", "onUploadComplete() exception <" + e.toString() + ">");
  147. } catch (Throwable th) {
  148. reentrantLock.unlock();
  149. throw th;
  150. }
  151. reentrantLock.unlock();
  152. }
  153. });
  154. while (!uploaded) {
  155. newCondition.await();
  156. }
  157. if (!canceled && taskError == StorageServiceClient.ProcessError.NONE) {
  158. reentrantLock.unlock();
  159. i++;
  160. }
  161. } catch (Exception e) {
  162. e.printStackTrace();
  163. taskError = StorageServiceClient.ProcessError.ERROR;
  164. } catch (Throwable th) {
  165. reentrantLock.unlock();
  166. throw th;
  167. }
  168. }
  169. if (canceled) {
  170. taskError = StorageServiceClient.ProcessError.CANCELED;
  171. }
  172. reentrantLock.unlock();
  173. runOnUiThread(new Runnable() {
  174. public void run() {
  175. if (taskError == StorageServiceClient.ProcessError.NONE) {
  176. overlayProgress.dismiss();
  177. finish();
  178. return;
  179. }
  180. setEnabledSaveButton(true);
  181. if (taskError == StorageServiceClient.ProcessError.ERROR) {
  182. showErrorDialog(R.string.upload_error_mes);
  183. }
  184. overlayProgress.dismiss();
  185. }
  186. });
  187. return null;
  188. }
  189. }
  190. private void updateUploadSection(SignInStatus signInStatus) {
  191. ViewGroup viewGroup = (ViewGroup) findViewById(R.id.upload_file_name_file_type_section);
  192. if (signInStatus == SignInStatus.SIGNED_IN) {
  193. viewGroup.setVisibility(View.VISIBLE);
  194. ((RadioGroup) findViewById(R.id.file_type_for_upload)).check(R.id.pdf_type);
  195. List<String> list = mUploadFileLocalPathList;
  196. if (list != null && list.size() > 0) {
  197. setFilename(mUploadFilename);
  198. }
  199. ((TextView) findViewById(R.id.file_size)).setText(getTotalFileSize(mUploadFileLocalPathList));
  200. return;
  201. }
  202. viewGroup.setVisibility(4);
  203. }
  204. private void updateSaveButton(SignInStatus signInStatus) {
  205. setEnabledSaveButton(signInStatus == SignInStatus.SIGNED_IN);
  206. }
  207. private void setEnabledSaveButton(boolean z) {
  208. Button button = (Button) findViewById(R.id.save_button);
  209. if (z) {
  210. button.setEnabled(true);
  211. } else {
  212. button.setEnabled(false);
  213. }
  214. }
  215. private String getFilename() {
  216. return ((TextView) findViewById(R.id.upload_dialog_file_name)).getText().toString();
  217. }
  218. private void setFilename(String str) {
  219. ((EditText) findViewById(R.id.upload_dialog_file_name)).setText(str);
  220. }
  221. private StorageServiceClient.UploadFileType getFiletype() {
  222. return ((RadioGroup) findViewById(R.id.file_type_for_upload)).getCheckedRadioButtonId() == R.id.pdf_type ? StorageServiceClient.UploadFileType.PDF : StorageServiceClient.UploadFileType.JPEG;
  223. }
  224. private String generateUploadFilename(int i) {
  225. int count = ScanFileNumber.getCount(this);
  226. return "EPSON" + new Utils().editNumber(count + i);
  227. }
  228. private String getTotalFileSize(List<String> list) {
  229. Long l = 0L;
  230. for (String file : list) {
  231. l = Long.valueOf(l.longValue() + new File(file).length());
  232. }
  233. double longValue = (l.longValue() / PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID);
  234. if (longValue > 500.0d) {
  235. return MyUtility.mathRound(longValue / 1024.0d, 2) + "MB";
  236. }
  237. return MyUtility.mathRound(longValue, 2) + "KB";
  238. }
  239. }