123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- package com.epson.memcardacc;
- import android.app.Activity;
- import android.content.Intent;
- import android.media.MediaScannerConnection;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.ProgressBar;
- import android.widget.TextView;
- import com.epson.iprint.prtlogger.Analytics;
- import com.epson.iprint.prtlogger.CommonLog;
- import com.epson.mobilephone.common.wifidirect.WiFiDirectManager;
- import epson.common.Utils;
- import epson.print.CommonDefine;
- import epson.print.Util.EPLog;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.Locale;
- public class MemcardReadProgress extends Activity {
- public static final String KEY_IMAGE_LIST = "imageList";
- public static final String KEY_WRITE_FOLDER_NAME = "target_folder_name";
- public static final String LOG_TAG = "MemcardReadProgress";
- public static final String MEMCARD_STORAGE_TYPE = "memcard_storage_type";
- public static final int RESULT_COMPLETE = -2;
- public static final int RESULT_ERROR = 3;
- boolean discconectOnThreadFinish = false;
- private Button mCancelButton;
- private CopyTask mCopyTask;
- ProgressBar mEachProgress;
- private ArrayList<String> mImageList;
- private int mMemcardStorageType;
- private String mTargetFolderName;
- TextView mTextView;
- ProgressBar mTotalProgress;
- protected void onCreate(Bundle bundle) {
- super.onCreate(bundle);
- Utils.setFInishOnTOuchOutside(this, false);
- setContentView(R.layout.memcard_write_progress);
- Intent intent = getIntent();
- mImageList = intent.getStringArrayListExtra("imageList");
- mTargetFolderName = intent.getStringExtra("target_folder_name");
- mMemcardStorageType = intent.getIntExtra("memcard_storage_type", 1);
- mTotalProgress = (ProgressBar) findViewById(R.id.progress_percent);
- mTotalProgress.setVisibility(View.VISIBLE);
- mEachProgress = (ProgressBar) findViewById(R.id.progress_copies);
- mEachProgress.setVisibility(View.GONE);
- mTextView = (TextView) findViewById(R.id.percent);
- ((TextView) findViewById(R.id.copyingMessageText)).setText(getResources().getString(R.string.memcard_file_reading_message));
- Intent intent2 = getIntent();
- mImageList = intent2.getStringArrayListExtra("imageList");
- mTargetFolderName = intent2.getStringExtra("target_folder_name");
- mCancelButton = (Button) findViewById(R.id.cancel_button);
- mCancelButton.setOnClickListener(new View.OnClickListener() {
- public void onClick(View view) {
- MemcardReadProgress.this.actionCancel();
- }
- });
- mCopyTask = new CopyTask();
- mCopyTask.execute(new Void[0]);
- }
- protected void onStop() {
- EPLog.d(LOG_TAG, "onStop()");
- actionCancel();
- discconectOnThreadFinish = true;
- super.onStop();
- }
- public void actionCancel() {
- mCancelButton.setEnabled(false);
- mCopyTask.taskCancel();
- }
- public void setProgressText(int i, int i2) {
- mTextView.setText(String.format("%d/%d", new Object[]{Integer.valueOf(i), Integer.valueOf(i2)}));
- }
- public void setProgress(int i, int i2) {
- setProgressText(i, i2);
- mTotalProgress.setMax(i2);
- mTotalProgress.setProgress(i);
- }
- public void finishWithState(int i, @Nullable CommonLog commonLog) {
- if (this.discconectOnThreadFinish) {
- WiFiDirectManager.disconnect(this, WiFiDirectManager.DEVICE_TYPE_PRINTER, MemcardUtil.getPrinterIpAddress(this));
- }
- if (commonLog != null && commonLog.numberOfSheet > 0) {
- Analytics.sendCommonLog(this, commonLog);
- }
- setResult(i);
- finish();
- }
- class CopyTask extends AsyncTask<Void, Integer, Integer> {
- CifsAccess cifsAccess = CifsAccess.getInstance();
- private boolean mCanceling;
- private CommonLog mCommonLog = new CommonLog();
- CopyTask() {
- CommonLog commonLog = mCommonLog;
- commonLog.numberOfSheet = 0;
- commonLog.action = 8194;
- commonLog.printerName = Analytics.getDefaultPrinterName(MemcardReadProgress.this.getApplicationContext());
- }
- protected void onCancelled() {
- MemcardUtil.clearKeepScreenOn(MemcardReadProgress.this.getWindow());
- EPLog.i(MemcardReadProgress.LOG_TAG, "canceld in AsyncTask");
- MemcardReadProgress.this.finishWithState(0, mCommonLog);
- }
- protected void onPostExecute(Integer num) {
- MemcardUtil.clearKeepScreenOn(MemcardReadProgress.this.getWindow());
- MemcardReadProgress.this.finishWithState(num.intValue(), mCommonLog);
- }
- protected void onPreExecute() {
- mCanceling = false;
- MemcardUtil.keepScreenOn(MemcardReadProgress.this.getWindow());
- MemcardReadProgress memcardReadProgress = MemcardReadProgress.this;
- memcardReadProgress.setProgress(0, memcardReadProgress.mImageList.size());
- }
- protected void onProgressUpdate(Integer... numArr) {
- MemcardReadProgress.this.setProgress(numArr[0].intValue(), MemcardReadProgress.mImageList.size());
- }
- protected Integer doInBackground(Void... voidArr) {
- try {
- if (this.cifsAccess.initDefault(MemcardReadProgress.this, 1) == 0) {
- return 3;
- }
- if (this.cifsAccess.connectDefaultStorageWidthDefaultAthInfo(MemcardReadProgress.mMemcardStorageType) == 0) {
- cifsAccess.free();
- return 3;
- }
- int copyFile = copyFile();
- cifsAccess.disconnectStorage();
- mCommonLog.setConnectionType(MemcardReadProgress.this.getApplicationContext());
- Integer valueOf = Integer.valueOf(copyFile);
- cifsAccess.free();
- return valueOf;
- } finally {
- cifsAccess.free();
- }
- }
- private int copyFile() {
- Iterator it = MemcardReadProgress.mImageList.iterator();
- int i = 0;
- while (it.hasNext()) {
- if (isCancelled()) {
- return 0;
- }
- publishProgress(new Integer[]{Integer.valueOf(i)});
- String str = (String) it.next();
- String format = String.format(Locale.US, "%s/IMG_%04d.JPG", new Object[]{MemcardReadProgress.mTargetFolderName, Integer.valueOf(i)});
- i++;
- if (new File(format).exists()) {
- EPLog.i(MemcardReadProgress.LOG_TAG, "local file exists. skip");
- } else {
- EPLog.i(MemcardReadProgress.LOG_TAG, "copy " + str + " => " + format);
- int readFromPrinterMemcard = cifsAccess.readFromPrinterMemcard(str, format);
- if (readFromPrinterMemcard != 0) {
- EPLog.w(MemcardReadProgress.LOG_TAG, "cifsAccess.writeFromPrinterMemcard() return <" + readFromPrinterMemcard + ">");
- new File(format).delete();
- if (readFromPrinterMemcard == -2) {
- return 0;
- }
- return 3;
- }
- String replace = format.replace(MemcardConfig.BASE_DIRECTORY, CommonDefine.DEFAULT_DIR);
- if (new File(replace).exists()) {
- EPLog.i(MemcardReadProgress.LOG_TAG, "MediaScannerConnection.scanFile path = " + replace);
- MediaScannerConnection.scanFile(MemcardReadProgress.this, new String[]{replace}, (String[]) null, (MediaScannerConnection.OnScanCompletedListener) null);
- } else {
- EPLog.m307e(MemcardReadProgress.LOG_TAG, "Failed MediaScannerConnection.scanFile path = " + replace);
- }
- mCommonLog.numberOfSheet = i;
- }
- }
- return -2;
- }
- public void taskCancel() {
- if (!mCanceling) {
- mCanceling = true;
- cifsAccess.cancel();
- cancel(false);
- }
- }
- }
- }
|