123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- package epson.print.gdconv;
- import android.content.Context;
- import android.os.AsyncTask;
- import androidx.annotation.NonNull;
- import androidx.annotation.Nullable;
- import com.epson.iprint.storage.Network;
- import com.epson.iprint.storage.gdrivev3.DriveWriter;
- import java.io.File;
- import java.lang.ref.WeakReference;
- import epson.common.ExternalFileUtils;
- import epson.common.Utils;
- import epson.print.CommonDefine;
- public class ConvertTask extends AsyncTask<String, ConvertStatus, Integer> {
- private static final String FOLDER_FOR_CONVERT = "Epson iPrint";
- private final WeakReference<Context> mContextReference;
- private File mOutFile;
- private final WeakReference<TaskCallback> mTaskCallbackWeakReference;
- public interface TaskCallback {
- void messageChange(ConvertStatus convertStatus);
- void notifyTaskEnd(int i);
- }
- @Nullable
- public synchronized File getOutFile() {
- return mOutFile;
- }
- public ConvertTask(@NonNull Context context, TaskCallback taskCallback) {
- mContextReference = new WeakReference<>(context.getApplicationContext());
- mTaskCallbackWeakReference = new WeakReference<>(taskCallback);
- }
- protected Integer doInBackground(String[] strArr) {
- if (strArr == null || strArr[0] == null) {
- return 101;
- }
- String str = strArr[0];
- Context context = (Context) mContextReference.get();
- if (context == null) {
- return 103;
- }
- new Network();
- if (!Utils.isConnecting(context)) {
- return 102;
- }
- try {
- DriveWriter createDriveWriter = DriveWriter.createDriveWriter(context);
- String mimeType = Utils.getMimeType(str);
- if (mimeType == null) {
- return 101;
- }
- publishProgress(new ConvertStatus[]{ConvertStatus.UPLOAD});
- File createOutFilename = createOutFilename(context, str);
- synchronized (this) {
- mOutFile = createOutFilename;
- }
- createDriveWriter.convertFileToPdf(str, mimeType, createDriveWriter.findOrCreateFolder("Epson iPrint"), createOutFilename, new DriveWriter.ConvertStatusNotifier() {
- public void changeStatus(ConvertStatus convertStatus) {
- publishProgress(new ConvertStatus[]{convertStatus});
- }
- });
- return 100;
- } catch (Exception unused) {
- return 101;
- }
- }
- @NonNull
- private File createOutFilename(@NonNull Context context, @NonNull String str) {
- String name = new File(str).getName();
- File tempCacheDir = ExternalFileUtils.getInstance(context).getTempCacheDir(ExternalFileUtils.TempCacheDirectory.GOOGLE_DRIVE_CONVERT);
- return new File(tempCacheDir, name + CommonDefine.FileType_PDF);
- }
- protected void onProgressUpdate(ConvertStatus... convertStatusArr) {
- TaskCallback taskCallback = (TaskCallback) mTaskCallbackWeakReference.get();
- if (taskCallback != null && convertStatusArr != null && convertStatusArr[0] != null) {
- taskCallback.messageChange(convertStatusArr[0]);
- }
- }
- private void doNotifyTaskEnd(int i) {
- TaskCallback taskCallback = (TaskCallback) mTaskCallbackWeakReference.get();
- if (taskCallback != null) {
- taskCallback.notifyTaskEnd(i);
- }
- }
- protected void onPostExecute(Integer num) {
- doNotifyTaskEnd(num != null ? num.intValue() : 101);
- }
- protected void onCancelled() {
- doNotifyTaskEnd(103);
- }
- }
|