package com.epson.memcardacc;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;


import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;


import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.epson.memcardacc.ImageFileListAdapter;
import com.epson.memcardacc.MemcardTempAlertDialog;
import com.epson.mobilephone.common.wifidirect.WiFiDirectManager;

import epson.common.DialogProgress;
import epson.common.DialogProgressViewModel;
import epson.print.ActivityIACommon;

import epson.print.Util.EPLog;

import java.io.UnsupportedEncodingException;
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Locale;
import java.util.concurrent.LinkedBlockingQueue;

public class MemcardImageGrid extends ActivityIACommon implements DialogProgress.DialogButtonClick {
    static final int ACTIVITY_TYPE_CONFIRM_COPY = 1;
    private static final String DIALOG_FILE_READ_ERROR = "dialog_file_read_error";
    private static final String DIALOG_NO_IMAGE_SELECTED = "dialog_no_image_selected";
    private static final String DIALOG_OVER_SELECTED_NUMBER = "dialog_over_selected_number";
    private static final String DIALOG_PROGRESS = "dialog_progress";
    private static final String LOG_TAG = "MemcardImageGrid";
    public static final String MEMCARD_STORAGE_TYPE = "memcard_storage_type";
    static final int MESSAGE_EXEC_NEXT_TASK_OR_IDLE = 2;
    static final int MESSAGE_WAIT_TASK_FINISHED_AND_NEXT_TASK = 1;
    static final long NEXT_TASK_WAITE = 300;
    public static final int RESULT_BACK_TO_MEMCARD_TOP = 1;
    protected static final long TASK_WAITE_PRIOD = 1000;
    boolean discconectOnThreadFinish = false;
    private boolean isFinishRequested;
    boolean isKeepSimleApConnect = false;
    boolean isTryConnectSimpleAp = false;
    private MemcardBitmapCache mBitmapCache;
    private CifsAccess mCifsAccess;
    private CifsFileListLoader mCifsFileListLoader;
    private DirectoryCache mDirectoryCache;
    private GridView mGridView;
    /* access modifiers changed from: private */
    @SuppressLint({"HandlerLeak"})
    public Handler mHandler = new Handler() {
        public void handleMessage(Message message) {
            switch (message.what) {
                case 1:
                    if (MemcardImageGrid.mCifsFileListLoader != null) {
                        EPLog.i(MemcardImageGrid.LOG_TAG, "status :" + MemcardImageGrid.mCifsFileListLoader.getStatus());
                        switch (C09864.$SwitchMap$android$os$AsyncTask$Status[MemcardImageGrid.mCifsFileListLoader.getStatus().ordinal()]) {
                            case 1:
                            case 2:
                                MemcardImageGrid.mHandler.sendEmptyMessageDelayed(1, 1000);
                                return;
                            case 3:
                                break;
                            default:
                                EPLog.e(MemcardImageGrid.LOG_TAG, "unkown AsyncTask.getStatus() value");
                                return;
                        }
                    } else {
                        EPLog.i(MemcardImageGrid.LOG_TAG, "mCifsFileListLoader == null");
                    }
                    MemcardImageGrid.mHandler.sendEmptyMessageDelayed(2, 300);
                    return;
                case 2:
                    MemcardImageGrid.this.execNextTaskOrIdle();
                    return;
                default:
                    return;
            }
        }
    };
    private ImageFileListAdapter mImageFileListAdapter;
    private int mMemcardStorageType;
    private DialogProgressViewModel mModelDialog;
    private ProgressBar mProgressBar;
    private TextView mSelectedFileNumText;
    public boolean mStartDirectory;
    private LinkedBlockingQueue<String> mTaskQueue = new LinkedBlockingQueue<>();
    private int mThumbnailHeight;
    private int mThumbnailWidth;
    private String targetDir = null;

    public void onCancelDialog(String str) {
    }

    public void onNeutralClick(String str) {
    }

    protected void onCreate(Bundle bundle) {
        mBitmapCache = MemcardBitmapCache.getInstance(this);
        MemcardBitmapCache memcardBitmapCache = mBitmapCache;
        if (memcardBitmapCache == null || !memcardBitmapCache.checkEnv()) {
            new MemcardTempAlertDialog().showAlertDialog(this, new MemcardTempAlertDialog.DialogCallback() {
                public void onPositiveCallback() {
                    MemcardImageGrid.this.finish();
                }
            });
        }
        mCifsAccess = CifsAccess.getInstance();
        mDirectoryCache = new DirectoryCache();
        mDirectoryCache.setCifsAccess(mCifsAccess);
        super.onCreate(bundle);
        mImageFileListAdapter = new ImageFileListAdapter();
        mImageFileListAdapter.setLayoutInflater(getLayoutInflater());
        mImageFileListAdapter.setUpFolderString(getResources().getString(R.string.memcard_folder_up));
        setContentView(R.layout.activity_memcard_image_grid);
        setActionBar("", true);
        mGridView = (GridView) findViewById(R.id.gridView1);
        mGridView.setAdapter(mImageFileListAdapter);
        mSelectedFileNumText = (TextView) findViewById(R.id.textSelectedImageNum);
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
                MemcardImageGrid.this.gridViewItemClicked(adapterView, view, i, j);
            }
        });
        mThumbnailWidth = getResources().getDimensionPixelSize(R.dimen.image_selector_grid_thumbnail_size);
        int i = mThumbnailWidth;
        mThumbnailHeight = i;
        mBitmapCache.setSize(i, mThumbnailHeight);
        mImageFileListAdapter.setImageSize(mThumbnailWidth, mThumbnailHeight);
        mGridView.getViewTreeObserver().addOnGlobalLayoutListener(new MyGridLayoutListener(mGridView, mImageFileListAdapter, getResources().getDimensionPixelSize(R.dimen.image_selector_grid_thumbnail_spacing)));
        updateSelectedNumber();
        mProgressBar = (ProgressBar) findViewById(R.id.progressBar1);
        mProgressBar.setVisibility(4);
        mMemcardStorageType = getIntent().getIntExtra("memcard_storage_type", 1);
        if (mMemcardStorageType <= 0) {
            mMemcardStorageType = 1;
        }
        mStartDirectory = true;
        targetDir = MemcardConfig.INIT_CIFS_DIRECTORY;
        mModelDialog = (DialogProgressViewModel) ViewModelProviders.m2of((FragmentActivity) this).get(DialogProgressViewModel.class);
        mModelDialog.getDialogJob().observe(this, new Observer() {
            public final void onChanged(Object obj) {
                MemcardImageGrid.lambda$onCreate$0(MemcardImageGrid.this, (Deque) obj);
            }
        });
    }

    public static /* synthetic */ void lambda$onCreate$0(MemcardImageGrid memcardImageGrid, Deque deque) {
        String[] checkQueue = memcardImageGrid.mModelDialog.checkQueue();
        if (checkQueue != null) {
            String str = checkQueue[0];
            String str2 = checkQueue[1];
            if (str2.equals("do_show")) {
                memcardImageGrid.showDialog(str);
            }
            if (str2.equals("do_dismiss")) {
                memcardImageGrid.dismissDialog(str);
            }
        }
    }

    private void dispTargetDirectory() {
        setTitle(mImageFileListAdapter.getTargetDirectoryName());
    }

    protected void onStop() {
        mHandler.removeMessages(1);
        if (!this.isKeepSimleApConnect) {
            CifsFileListLoader cifsFileListLoader = mCifsFileListLoader;
            if (cifsFileListLoader != null && cifsFileListLoader.getStatus() == AsyncTask.Status.RUNNING) {
                discconectOnThreadFinish = true;
            }
            if (!this.discconectOnThreadFinish) {
                EPLog.d(LOG_TAG, "discconect at onStop()");
                WiFiDirectManager.disconnect(this, WiFiDirectManager.DEVICE_TYPE_PRINTER, MemcardUtil.getPrinterIpAddress(this));
            }
        }
        cancelFileListLoadTask();
        super.onStop();
    }

    protected void onRestart() {
        super.onRestart();
        targetDir = mImageFileListAdapter.getTargetDirectoryName();
    }

    protected void onResume() {
        super.onResume();
        if (!WiFiDirectManager.isNeedConnect(this, WiFiDirectManager.DEVICE_TYPE_PRINTER)) {
            isTryConnectSimpleAp = false;
        } else if (!this.isTryConnectSimpleAp) {
            isTryConnectSimpleAp = true;
            if (WiFiDirectManager.reconnect(this, WiFiDirectManager.DEVICE_TYPE_PRINTER, -1)) {
                return;
            }
        }
        isKeepSimleApConnect = false;
        discconectOnThreadFinish = false;
        if (!this.isFinishRequested) {
            chengeTargetDirectory(this.targetDir);
        }
    }

    protected void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        if (i2 == 1 && i2 == 1) {
            setResult(1);
            finishMemCardImageGridActivity();
        }
    }

    private void gridViewItemClicked(AdapterView<?> adapterView, View view, int i, long j) {
        String directoryName = mImageFileListAdapter.getDirectoryName(i);
        if (directoryName != null) {
            EPLog.i(LOG_TAG, "item clicked : dir_name => " + directoryName);
            chengeTargetDirectory(directoryName);
        } else if (mImageFileListAdapter.itemSelected(i)) {
            updateSelectedNumber();
        } else {
            localShowDialog(DIALOG_OVER_SELECTED_NUMBER);
        }
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        int selectedFileNum = mImageFileListAdapter.getSelectedFileNum();
        getMenuInflater().inflate(R.menu.menu_image_selector, menu);
        menu.findItem(R.id.action_go_preview).setEnabled(selectedFileNum > 0);
        if (selectedFileNum <= 0) {
            setTitle(mImageFileListAdapter.getTargetDirectoryName());
        } else {
            setTitle(String.format(getResources().getString(R.string.photo_selected), new Object[]{Integer.valueOf(selectedFileNum)}));
        }
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem menuItem) {
        if (menuItem.getItemId() != R.id.action_go_preview) {
            return super.onOptionsItemSelected(menuItem);
        }
        goNext();
        return true;
    }

    public void updateSelectedNumber() {
        supportInvalidateOptionsMenu();
    }

    public void next_button_clicked(View view) {
        goNext();
    }

    private void goNext() {
        showProgressDialog();
        if (mImageFileListAdapter.getSelectedFileNum() <= 0) {
            dismissProgressDialog();
            localShowDialog(DIALOG_NO_IMAGE_SELECTED);
            return;
        }
        cancelFileListLoadTask();
        Intent intent = new Intent(this, ConfirmReadMemcard.class);
        intent.putStringArrayListExtra(ConfirmReadMemcard.KEY_IMAGE_LIST, mImageFileListAdapter.getAllSelectedFile());
        intent.putExtra("memcard_storage_type", mMemcardStorageType);
        startActivityForResult(intent, 1);
        dismissProgressDialog();
        isKeepSimleApConnect = true;
    }

    public void onBackPressed() {
        finishMemCardImageGridActivity();
    }

    private void finishMemCardImageGridActivity() {
        mHandler.removeMessages(1);
        cancelFileListLoadTask();
        finish();
        isFinishRequested = true;
    }

    private void localShowDialog(String str) {
        mModelDialog.doShow(str);
    }

    private void chengeTargetDirectory(String str) {
        EPLog.i(LOG_TAG, "chengeTargetDirectory() start");
        showProgressDialog();
        mTaskQueue.clear();
        cancelFileListLoadTask();
        try {
            mTaskQueue.put(str);
            mHandler.sendEmptyMessage(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
            EPLog.e(LOG_TAG, "queue.put() error in chengeTargetDirectory()");
            dismissProgressDialog();
        }
    }

    protected void execNextTaskOrIdle() {
        if (!mTaskQueue.isEmpty()) {
            EPLog.i(LOG_TAG, "checkQueue() queue not empty");
            mCifsFileListLoader = new CifsFileListLoader();
            String str = "";
            while (!mTaskQueue.isEmpty()) {
                str = mTaskQueue.poll();
            }
            mCifsFileListLoader.execute(new String[]{str});
            return;
        }
        dismissProgressDialog();
    }

    private void cancelFileListLoadTask() {
        CifsFileListLoader cifsFileListLoader = mCifsFileListLoader;
        if (cifsFileListLoader != null) {
            cifsFileListLoader.cancelTask();
        }
    }

    private void showProgressDialog() {
        mModelDialog.doShow(DIALOG_PROGRESS);
    }

    private void dismissProgressDialog() {
        mModelDialog.doDismiss(DIALOG_PROGRESS);
    }

    /* JADX WARNING: Removed duplicated region for block: B:23:0x0049  */
    /* JADX WARNING: Removed duplicated region for block: B:24:0x004b  */
    /* JADX WARNING: Removed duplicated region for block: B:25:0x0069  */
    /* JADX WARNING: Removed duplicated region for block: B:26:0x0084  */
    /* JADX WARNING: Removed duplicated region for block: B:27:0x0099  */
    /* JADX WARNING: Removed duplicated region for block: B:29:0x00ad  */
    /* JADX WARNING: Removed duplicated region for block: B:31:? A[RETURN, SYNTHETIC] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private void showDialog(java.lang.String r11) {
        /*
            r10 = this;
            int r0 = r11.hashCode()
            r1 = -903480314(0xffffffffca25fc06, float:-2719489.5)
            r2 = 0
            if (r0 == r1) goto L_0x0038
            r1 = -842795367(0xffffffffcdc3f699, float:-4.10964768E8)
            if (r0 == r1) goto L_0x002e
            r1 = -195189813(0xfffffffff45da3cb, float:-7.024046E31)
            if (r0 == r1) goto L_0x0024
            r1 = -21731516(0xfffffffffeb46744, float:-1.1989861E38)
            if (r0 == r1) goto L_0x001a
            goto L_0x0042
        L_0x001a:
            java.lang.String r0 = "dialog_progress"
            boolean r0 = r11.equals(r0)
            if (r0 == 0) goto L_0x0042
            r0 = 0
            goto L_0x0043
        L_0x0024:
            java.lang.String r0 = "dialog_file_read_error"
            boolean r0 = r11.equals(r0)
            if (r0 == 0) goto L_0x0042
            r0 = 3
            goto L_0x0043
        L_0x002e:
            java.lang.String r0 = "dialog_over_selected_number"
            boolean r0 = r11.equals(r0)
            if (r0 == 0) goto L_0x0042
            r0 = 1
            goto L_0x0043
        L_0x0038:
            java.lang.String r0 = "dialog_no_image_selected"
            boolean r0 = r11.equals(r0)
            if (r0 == 0) goto L_0x0042
            r0 = 2
            goto L_0x0043
        L_0x0042:
            r0 = -1
        L_0x0043:
            r1 = 2131625202(0x7f0e04f2, float:1.8877605E38)
            switch(r0) {
                case 0: goto L_0x0099;
                case 1: goto L_0x0084;
                case 2: goto L_0x0069;
                case 3: goto L_0x004b;
                default: goto L_0x0049;
            }
        L_0x0049:
            r0 = 0
            goto L_0x00ab
        L_0x004b:
            r4 = 2
            r0 = 2131624887(0x7f0e03b7, float:1.8876966E38)
            java.lang.String r5 = r10.getString(r0)
            r6 = 0
            r0 = 2131625078(0x7f0e0476, float:1.8877354E38)
            java.lang.String r7 = r10.getString(r0)
            r8 = 0
            r0 = 2131625211(0x7f0e04fb, float:1.8877624E38)
            java.lang.String r9 = r10.getString(r0)
            r3 = r11
            epson.common.DialogProgress r0 = epson.common.DialogProgress.newInstance(r3, r4, r5, r6, r7, r8, r9)
            goto L_0x00ab
        L_0x0069:
            r2 = 2
            r0 = 2131624905(0x7f0e03c9, float:1.8877003E38)
            java.lang.String r3 = r10.getString(r0)
            r0 = 2131624906(0x7f0e03ca, float:1.8877005E38)
            java.lang.String r4 = r10.getString(r0)
            java.lang.String r5 = r10.getString(r1)
            r6 = 0
            r7 = 0
            r1 = r11
            epson.common.DialogProgress r0 = epson.common.DialogProgress.newInstance(r1, r2, r3, r4, r5, r6, r7)
            goto L_0x00ab
        L_0x0084:
            r2 = 2
            r0 = 2131624871(0x7f0e03a7, float:1.8876934E38)
            java.lang.String r3 = r10.getString(r0)
            r4 = 0
            r5 = 0
            r6 = 0
            java.lang.String r7 = r10.getString(r1)
            r1 = r11
            epson.common.DialogProgress r0 = epson.common.DialogProgress.newInstance(r1, r2, r3, r4, r5, r6, r7)
            goto L_0x00ab
        L_0x0099:
            android.content.res.Resources r0 = r10.getResources()
            r1 = 2131624888(0x7f0e03b8, float:1.8876968E38)
            java.lang.String r0 = r0.getString(r1)
            epson.common.DialogProgress r0 = epson.common.DialogProgress.newInstance(r11, r2, r0)
            r0.setCancelable(r2)
        L_0x00ab:
            if (r0 == 0) goto L_0x00b4
            android.support.v4.app.FragmentManager r1 = r10.getSupportFragmentManager()
            r0.show(r1, r11)
        L_0x00b4:
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: com.epson.memcardacc.MemcardImageGrid.showDialog(java.lang.String):void");
    }

    private void dismissDialog(String str) {
        DialogFragment dialogFragment = (DialogFragment) getSupportFragmentManager().findFragmentByTag(str);
        if (dialogFragment != null) {
            dialogFragment.dismiss();
        }
    }

    /* renamed from: com.epson.memcardacc.MemcardImageGrid$4 */
    static /* synthetic */ class C09864 {
        static final /* synthetic */ int[] $SwitchMap$android$os$AsyncTask$Status = new int[AsyncTask.Status.values().length];

        /* JADX WARNING: Can't wrap try/catch for region: R(8:0|1|2|3|4|5|6|8) */
        /* JADX WARNING: Failed to process nested try/catch */
        /* JADX WARNING: Missing exception handler attribute for start block: B:3:0x0014 */
        /* JADX WARNING: Missing exception handler attribute for start block: B:5:0x001f */
        static {
            /*
                android.os.AsyncTask$Status[] r0 = android.os.AsyncTask.Status.values()
                int r0 = r0.length
                int[] r0 = new int[r0]
                $SwitchMap$android$os$AsyncTask$Status = r0
                int[] r0 = $SwitchMap$android$os$AsyncTask$Status     // Catch:{ NoSuchFieldError -> 0x0014 }
                android.os.AsyncTask$Status r1 = android.os.AsyncTask.Status.PENDING     // Catch:{ NoSuchFieldError -> 0x0014 }
                int r1 = r1.ordinal()     // Catch:{ NoSuchFieldError -> 0x0014 }
                r2 = 1
                r0[r1] = r2     // Catch:{ NoSuchFieldError -> 0x0014 }
            L_0x0014:
                int[] r0 = $SwitchMap$android$os$AsyncTask$Status     // Catch:{ NoSuchFieldError -> 0x001f }
                android.os.AsyncTask$Status r1 = android.os.AsyncTask.Status.RUNNING     // Catch:{ NoSuchFieldError -> 0x001f }
                int r1 = r1.ordinal()     // Catch:{ NoSuchFieldError -> 0x001f }
                r2 = 2
                r0[r1] = r2     // Catch:{ NoSuchFieldError -> 0x001f }
            L_0x001f:
                int[] r0 = $SwitchMap$android$os$AsyncTask$Status     // Catch:{ NoSuchFieldError -> 0x002a }
                android.os.AsyncTask$Status r1 = android.os.AsyncTask.Status.FINISHED     // Catch:{ NoSuchFieldError -> 0x002a }
                int r1 = r1.ordinal()     // Catch:{ NoSuchFieldError -> 0x002a }
                r2 = 3
                r0[r1] = r2     // Catch:{ NoSuchFieldError -> 0x002a }
            L_0x002a:
                return
            */
            throw new UnsupportedOperationException("Method not decompiled: com.epson.memcardacc.MemcardImageGrid.C09864.<clinit>():void");
        }
    }

    private void selectFileList(LinkedList<CifsFileInfo> linkedList) {
        Iterator it = linkedList.iterator();
        while (it.hasNext()) {
            CifsFileInfo cifsFileInfo = (CifsFileInfo) it.next();
            if (!cifsFileInfo.isReadable() || (!cifsFileInfo.isDirectory() && !hasExtensionJpg(cifsFileInfo.mFileName))) {
                it.remove();
            }
        }
    }

    private boolean hasExtensionJpg(String str) {
        return Boolean.valueOf(str.toLowerCase(Locale.US).endsWith(".jpg")).booleanValue();
    }

    public void onPositiveClick(String str) {
        if (((str.hashCode() == -195189813 && str.equals(DIALOG_FILE_READ_ERROR)) ? (char) 0 : 65535) == 0) {
            finishMemCardImageGridActivity();
        }
    }

    public void onNegativeClick(String str) {
        if (((str.hashCode() == -195189813 && str.equals(DIALOG_FILE_READ_ERROR)) ? (char) 0 : 65535) == 0) {
            mStartDirectory = true;
            chengeTargetDirectory(MemcardConfig.INIT_CIFS_DIRECTORY);
        }
    }

    private static class MyGridLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener {
        ImageFileListAdapter mAdapter;
        private GridView mGridView;
        private int mHorizontalSpacing;

        public MyGridLayoutListener(GridView gridView, ImageFileListAdapter imageFileListAdapter, int i) {
            mGridView = gridView;
            mAdapter = imageFileListAdapter;
            mHorizontalSpacing = i;
        }

        @TargetApi(16)
        public void onGlobalLayout() {
            int numColumns = mGridView.getNumColumns();
            if (numColumns <= 0) {
                return;
            }
            if (Build.VERSION.SDK_INT >= 16) {
                setNewVerticalParams();
                mGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                return;
            }
            int width = mGridView.getWidth();
            int i = mHorizontalSpacing;
            int i2 = ((width + i) / numColumns) - i;
            mAdapter.setImageSize(i2, i2);
            mGridView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        }

        @TargetApi(16)
        private void setNewVerticalParams() {
            mGridView.setVerticalSpacing(mGridView.getHorizontalSpacing());
            int columnWidth = mGridView.getColumnWidth();
            mAdapter.setImageSize(columnWidth, columnWidth);
        }
    }

    class CifsFileListLoader extends AsyncTask<String, Integer, Integer> {
        private static final int PROGRESS_UPDATE_DIRCTORY = 1;
        private static final int PROGRESS_UPDATE_THUMBNAIL = 2;
        private static final int RESULT_FILE_LIST_CANNOT_GET = 1;
        private static final int RESULT_OK = 0;
        boolean mAdapterUpdateWaitDone;
        String mBaseDir;
        boolean mCanceling;
        LinkedList<CifsFileInfo> mFileList;

        CifsFileListLoader() {
        }

        protected void onPreExecute() {
            EPLog.i(MemcardImageGrid.LOG_TAG, "doInBackground() onPreExecute().");
        }

        private boolean waiteAdapterUpdateAndCancelCheck() {
            if (mCanceling) {
                return false;
            }
            while (!mAdapterUpdateWaitDone) {
                try {
                    Thread.sleep(200);
                    if (mCanceling) {
                        return false;
                    }
                } catch (InterruptedException unused) {
                    return false;
                }
            }
            return true;
        }

        protected synchronized Integer doInBackground(String... strArr) {
            EPLog.i(MemcardImageGrid.LOG_TAG, "doInBackground() started");
            if (MemcardImageGrid.mCifsAccess.initDefault(MemcardImageGrid.this, 1) == 0) {
                return null;
            }
            if (MemcardImageGrid.mCifsAccess.connectDefaultStorageWidthDefaultAthInfo(MemcardImageGrid.mMemcardStorageType) == 0) {
                MemcardImageGrid.mCifsAccess.free();
                return null;
            }
            try {
                String str = strArr[0];
                LinkedList<CifsFileInfo> fileList = MemcardImageGrid.mDirectoryCache.getFileList(str);
                if (fileList == null) {
                    if (MemcardImageGrid.mStartDirectory) {
                        str = "";
                        fileList = MemcardImageGrid.mDirectoryCache.getFileList(str);
                        if (fileList == null) {
                            if (MemcardImageGrid.mCifsAccess.getFreeUnit() < 0) {
                                MemcardImageGrid.mCifsAccess.disconnectStorage();
                                MemcardImageGrid.mCifsAccess.free();
                                return 1;
                            }
                            fileList = new LinkedList<>();
                        }
                    } else {
                        fileList = new LinkedList<>();
                    }
                }
                MemcardImageGrid.mStartDirectory = false;
                MemcardImageGrid.this.selectFileList(fileList);
                mBaseDir = str;
                mFileList = fileList;
                mAdapterUpdateWaitDone = false;
                publishProgress(new Integer[]{1});
                if (waiteAdapterUpdateAndCancelCheck()) {
                    EPLog.i(MemcardImageGrid.LOG_TAG, "doInBackground() next createThumbnail");
                    createThumbnail(MemcardImageGrid.mCifsAccess);
                }
                EPLog.i(MemcardImageGrid.LOG_TAG, "doInBackground() disconnectStorage()");
                MemcardImageGrid.mCifsAccess.disconnectStorage();
                if (MemcardImageGrid.this.discconectOnThreadFinish) {
                    EPLog.d(MemcardImageGrid.LOG_TAG, "disconnectOnThreadFinish");
                    WiFiDirectManager.disconnect(MemcardImageGrid.this, WiFiDirectManager.DEVICE_TYPE_PRINTER, MemcardUtil.getPrinterIpAddress(MemcardImageGrid.this));
                }
                EPLog.i(MemcardImageGrid.LOG_TAG, "doInBackground() end");
                MemcardImageGrid.mCifsAccess.free();
                return 0;
            } catch (UnsupportedEncodingException e) {
                try {
                    e.printStackTrace();
                    EPLog.i(MemcardImageGrid.LOG_TAG, "exception in doInBackground()");
                    return null;
                } finally {
                    MemcardImageGrid.mCifsAccess.free();
                }
            }
        }

        public void cancelTask() {
            if (!mCanceling) {
                EPLog.i(MemcardImageGrid.LOG_TAG, "task canceling...");
                mCanceling = true;
                MemcardImageGrid.mCifsAccess.cancel();
            }
        }

        protected void onProgressUpdate(Integer... numArr) {
            if (numArr[0].intValue() == 1) {
                MemcardImageGrid.mImageFileListAdapter.setFileList(mBaseDir, mFileList);
                MemcardImageGrid.mImageFileListAdapter.notifyDataSetChanged();
                mAdapterUpdateWaitDone = true;
                MemcardImageGrid.this.updateSelectedNumber();
                MemcardImageGrid.this.dismissProgressDialog();
                return;
            }
            MemcardImageGrid.mImageFileListAdapter.notifyDataSetChanged();
        }

        protected void onPostExecute(Integer num) {
            EPLog.i(MemcardImageGrid.LOG_TAG, "doInBackground() done.");
            if (num == null) {
                MemcardImageGrid.this.dismissProgressDialog();
                MemcardImageGrid.this.localShowDialog(MemcardImageGrid.DIALOG_FILE_READ_ERROR);
                return;
            }
            switch (num.intValue()) {
                case 0:
                    MemcardImageGrid.mImageFileListAdapter.notifyDataSetChanged();
                    return;
                case 1:
                    MemcardImageGrid.this.localShowDialog(MemcardImageGrid.DIALOG_FILE_READ_ERROR);
                    return;
                default:
                    return;
            }
        }

        private int createThumbnail(CifsAccess cifsAccess) throws UnsupportedEncodingException {
            MemcardImageGrid.mBitmapCache.setCifsAccess(cifsAccess);
            Iterator<ImageFileListAdapter.FileInfoWithCheck> fileInfoIterator = MemcardImageGrid.mImageFileListAdapter.getFileInfoIterator();
            while (fileInfoIterator.hasNext()) {
                if (mCanceling) {
                    return 0;
                }
                ImageFileListAdapter.FileInfoWithCheck next = fileInfoIterator.next();
                if (next.needThumbNail()) {
                    if (next.isDirectory()) {
                        next.setThumbNail((Bitmap) null);
                    } else {
                        String fileName = next.getFileName();
                        EPLog.i(MemcardImageGrid.LOG_TAG, "name <" + fileName + ">");
                        next.setThumbNail(MemcardImageGrid.mBitmapCache.cacheOrMakeThumbnailBitmap(fileName, next.getFullPath()));
                    }
                    publishProgress(new Integer[]{2});
                }
            }
            return 1;
        }
    }
}
