package epson.print.storage; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.support.p000v4.media.session.PlaybackStateCompat; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.RadioGroup; import android.widget.TextView; import com.epson.iprint.storage.gdrivev3.GoogleV3UploadClient; import com.epson.iprint.storage.gdrivev3.GoogleV3UploadSignInActivity; import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; import epson.print.CommonDefine; import epson.print.ScanFileNumber; import epson.print.Util.EPLog; import epson.print.Util.Utils; import epson.scan.lib.libHaru; import epson.server.utils.MyUtility; public class StorageProcessUploadActivity extends StorageProcessActivity { final String UPLOAD_EDITING_FILENAME = "Upload.Editing.Filename"; List mUploadFileLocalPathList; String mUploadFilename; private void onUpdateProcessView() { } protected void onCreate(Bundle bundle) { String string; super.onCreate(bundle); setContentView(R.layout.folder_content_layout_for_upload); setActionBar(getStorageClient().getStorageServiceName(this), true); final Button button = (Button) findViewById(R.id.save_button); button.setText(getString(R.string.upload_btn_label)); button.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { String filename = StorageProcessUploadActivity.this.getFilename(); if (!StorageProcessUploadActivity.this.getStorageClient().isValidUploadName(filename)) { StorageProcessUploadActivity.this.showErrorDialog(R.string.str_error_Filename); } else if (StorageProcessUploadActivity.this.isConnected()) { button.setEnabled(false); UploadTask uploadTask = new UploadTask(); uploadTask.localPaths = new ArrayList(StorageProcessUploadActivity.mUploadFileLocalPathList); uploadTask.uploadFilename = filename; uploadTask.fileType = StorageProcessUploadActivity.this.getFiletype(); uploadTask.execute(new Void[0]); } else { StorageProcessUploadActivity.this.showConnectionError(); } } }); bindClearButton(R.id.upload_dialog_file_name, R.id.clear_name_btn, new OnVisibilityListener() { public void onVisibilityChanged(int i) { StorageProcessUploadActivity.this.setEnabledSaveButton(i == 0); } }); mUploadFilename = generateUploadFilename(0); if (!(bundle == null || (string = bundle.getString("Upload.Editing.Filename")) == null)) { mUploadFilename = string; } setFilename(mUploadFilename); mUploadFileLocalPathList = getIntent().getStringArrayListExtra(StorageProcessActivity.EXTRA_UPLOADFILE_LIST); if (getStorageClient() instanceof GoogleV3UploadClient) { startActivity(GoogleV3UploadSignInActivity.getPlayServiceCheckIntent(this)); } } protected void onSaveInstanceState(Bundle bundle) { super.onSaveInstanceState(bundle); mUploadFilename = getFilename(); bundle.putString("Upload.Editing.Filename", mUploadFilename); } protected void onRestoreInstanceState(Bundle bundle) { super.onRestoreInstanceState(bundle); mUploadFilename = bundle.getString("Upload.Editing.Filename"); } private void onSignInStatus(SignInStatus signInStatus) { updateUploadSection(signInStatus); updateSaveButton(signInStatus); } class UploadTask extends AsyncTask { boolean canceled; StorageServiceClient.UploadFileType fileType; List localPaths; private volatile StorageServiceClient.ProcessError taskError; String uploadFilename; boolean uploaded; StorageServiceClient.Uploader uploader; UploadTask() { } protected Void doInBackground(Void... voidArr) { final ReentrantLock reentrantLock; final OverlayProgress overlayProgress = new OverlayProgress(); overlayProgress.show(StorageProcessUploadActivity.this.getString(R.string.uploading_notification), new CancelRequestCallback() { public void onCancelRequest(Intent intent) { if (UploadTask.this.uploader != null && UploadTask.this.uploader.isCancelable()) { UploadTask.this.uploader.cancel(); } UploadTask.this.canceled = true; } }); String str = ""; if (StorageServiceClient.UploadFileType.PDF.equals(fileType)) { String str2 = new File(localPaths.get(0)).getParent() + CommonDefine.SLASH + uploadFilename + CommonDefine.FileType_PDF; new libHaru().createPDF((ArrayList) localPaths, str2); localPaths.clear(); localPaths.add(str2); str = CommonDefine.FileType_PDF; } else if (StorageServiceClient.UploadFileType.JPEG.equals(fileType)) { str = ".jpg"; } boolean z = !this.uploadFilename.equals(StorageProcessUploadActivity.this.generateUploadFilename(0)); int size = localPaths.size(); int i = 0; while (true) { if (i >= size) { break; } String str3 = localPaths.get(i); String str4 = uploadFilename + str; if (size > 1) { if (z) { str4 = String.format("%s%03d%s", new Object[]{this.uploadFilename, Integer.valueOf(i + 1), str}); } else { str4 = String.format("%s%s", new Object[]{StorageProcessUploadActivity.this.generateUploadFilename(i), str}); } overlayProgress.changeMessage(StorageProcessUploadActivity.this.getString(R.string.uploading_notification) + (i + 1) + CommonDefine.SLASH + size); } reentrantLock = new ReentrantLock(); final Condition newCondition = reentrantLock.newCondition(); try { reentrantLock.lock(); uploaded = false; taskError = StorageServiceClient.ProcessError.NONE; uploader = StorageProcessUploadActivity.this.getStorageClient().getUploader(StorageProcessUploadActivity.this, fileType, str3, str4); uploader.start(new StorageServiceClient.UploadCompletion() { public void onUploadComplete(String str, String str2, StorageServiceClient.ProcessError processError) { try { reentrantLock.lock(); StorageServiceClient.ProcessError unused = UploadTask.this.taskError = processError; UploadTask.this.uploaded = true; newCondition.signal(); } catch (Exception e) { EPLog.w("Storage", "onUploadComplete() exception <" + e.toString() + ">"); } catch (Throwable th) { reentrantLock.unlock(); throw th; } reentrantLock.unlock(); } }); while (!uploaded) { newCondition.await(); } if (!canceled && taskError == StorageServiceClient.ProcessError.NONE) { reentrantLock.unlock(); i++; } } catch (Exception e) { e.printStackTrace(); taskError = StorageServiceClient.ProcessError.ERROR; } catch (Throwable th) { reentrantLock.unlock(); throw th; } } if (canceled) { taskError = StorageServiceClient.ProcessError.CANCELED; } reentrantLock.unlock(); StorageProcessUploadActivity.this.runOnUiThread(new Runnable() { public void run() { if (UploadTask.this.taskError == StorageServiceClient.ProcessError.NONE) { overlayProgress.dismiss(); StorageProcessUploadActivity.this.finish(); return; } StorageProcessUploadActivity.this.setEnabledSaveButton(true); if (UploadTask.this.taskError == StorageServiceClient.ProcessError.ERROR) { StorageProcessUploadActivity.this.showErrorDialog(R.string.upload_error_mes); } overlayProgress.dismiss(); } }); return null; } } private void updateUploadSection(SignInStatus signInStatus) { ViewGroup viewGroup = (ViewGroup) findViewById(R.id.upload_file_name_file_type_section); if (signInStatus == SignInStatus.SIGNED_IN) { viewGroup.setVisibility(View.VISIBLE); ((RadioGroup) findViewById(R.id.file_type_for_upload)).check(R.id.pdf_type); List list = mUploadFileLocalPathList; if (list != null && list.size() > 0) { setFilename(mUploadFilename); } ((TextView) findViewById(R.id.file_size)).setText(getTotalFileSize(mUploadFileLocalPathList)); return; } viewGroup.setVisibility(4); } private void updateSaveButton(SignInStatus signInStatus) { setEnabledSaveButton(signInStatus == SignInStatus.SIGNED_IN); } private void setEnabledSaveButton(boolean z) { Button button = (Button) findViewById(R.id.save_button); if (z) { button.setEnabled(true); } else { button.setEnabled(false); } } private String getFilename() { return ((TextView) findViewById(R.id.upload_dialog_file_name)).getText().toString(); } private void setFilename(String str) { ((EditText) findViewById(R.id.upload_dialog_file_name)).setText(str); } private StorageServiceClient.UploadFileType getFiletype() { return ((RadioGroup) findViewById(R.id.file_type_for_upload)).getCheckedRadioButtonId() == R.id.pdf_type ? StorageServiceClient.UploadFileType.PDF : StorageServiceClient.UploadFileType.JPEG; } private String generateUploadFilename(int i) { int count = ScanFileNumber.getCount(this); return "EPSON" + new Utils().editNumber(count + i); } private String getTotalFileSize(List list) { Long l = 0L; for (String file : list) { l = Long.valueOf(l.longValue() + new File(file).length()); } double longValue = (l.longValue() / PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID); if (longValue > 500.0d) { return MyUtility.mathRound(longValue / 1024.0d, 2) + "MB"; } return MyUtility.mathRound(longValue, 2) + "KB"; } }