package com.epson.cameracopy.p006ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import epson.print.ActivityIACommon;
import epson.print.ActivityRequestPermissions;

import java.io.File;
import java.lang.ref.WeakReference;
import java.util.ArrayList;

/* renamed from: com.epson.cameracopy.ui.FolderSelectActivity */
public class FolderSelectActivity extends ActivityIACommon implements View.OnClickListener {
    private static final int MSG_UPDATE_LIST_VIEW = 901;
    private static final int REQEST_RUNTIMEPERMMISSION = 1;
    private static WeakReference<Activity> mActivity;
    /* access modifiers changed from: private */
    public static FolderSelectActivity mFolderSelectActivity;
    /* access modifiers changed from: private */
    public static Handler mHandler = new Handler() {
        public void handleMessage(Message message) {
            if (FolderSelectActivity.mFolderSelectActivity != null) {
                FolderSelectActivity.mFolderSelectActivity.executeMessage(message);
            }
        }
    };
    AdapterView.OnItemClickListener listViewListener = new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
            FolderSelectActivity.this.mFolderInformation.UpdateFolder(i);
            FolderSelectActivity.mHandler.sendEmptyMessage(FolderSelectActivity.MSG_UPDATE_LIST_VIEW);
        }
    };
    /* access modifiers changed from: private */
    public FolderInformation mFolderInformation = null;
    private LinearLayout mFolderSelectBase = null;
    private LinearLayout mFolderSelectCurrent = null;
    private TextView mFolderSelectText = null;
    private ItemDataAdapter mItemDataAdapter = null;
    private ArrayList<ItemData> mItemDataList = null;
    private ListView mListView = null;
    private MenuControl mMenuControl = null;
    private String mRootFolder = null;

    public void onActionbarMenuNewClicked(View view) {
    }

    public void onClick(View view) {
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView((int) R.layout.activity_folder_select);
        setActionBar((int) R.string.folder_selector_title, true);
        mActivity = new WeakReference<>(this);
        mFolderSelectActivity = (FolderSelectActivity) mActivity.get();
        this.mFolderSelectBase = (LinearLayout) findViewById(R.id.folder_select_base);
        this.mFolderSelectCurrent = (LinearLayout) findViewById(R.id.folder_select_current);
        this.mFolderSelectText = (TextView) findViewById(R.id.folder_select_text);
        this.mMenuControl = new MenuControl(this, 1);
        this.mRootFolder = Environment.getExternalStorageDirectory().getAbsolutePath();
        this.mItemDataList = new ArrayList<>();
        this.mItemDataAdapter = new ItemDataAdapter(this, 0, this.mItemDataList);
        updateListView();
        if (ActivityRequestPermissions.isRuntimePermissionSupported()) {
            String[] strArr = {"android.permission.WRITE_EXTERNAL_STORAGE"};
            ActivityRequestPermissions.Permission permission = new ActivityRequestPermissions.Permission(strArr[0], new String[]{getString(R.string.permission_dialog_title), getString(R.string.permission_dialog_title)}, new String[]{ActivityRequestPermissions.DialogParameter.setMessage2((Context) this, getString(R.string.permission_dialog_message_storage)), ActivityRequestPermissions.DialogParameter.setMessage3A((Context) this, getString(R.string.permission_dialog_message_storage), getString(R.string.permission_function_storage))});
            if (!ActivityRequestPermissions.checkPermission(this, strArr)) {
                ActivityRequestPermissions.requestPermission((Activity) this, permission, 1);
                return;
            }
        }
        initFolder();
    }

    private void initFolder() {
        try {
            this.mFolderInformation = new FolderInformation(this.mRootFolder, 0, this.mItemDataAdapter, this.mItemDataList, new CurrentFolderName(this.mFolderSelectCurrent, this.mFolderSelectText), this.mMenuControl);
            this.mFolderInformation.UpdateFolder();
        } catch (NullPointerException unused) {
            setResult(1, (Intent) null);
            finish();
        }
    }

    protected void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        if (i == 1) {
            if (i2 != -1) {
                setResult(0);
                finish();
                return;
            }
            initFolder();
        }
    }

    /* access modifiers changed from: private */
    public void executeMessage(Message message) {
        if (message.what == MSG_UPDATE_LIST_VIEW) {
            updateListView();
        }
    }

    private void updateListView() {
        int i = 0;
        while (true) {
            if (i >= this.mFolderSelectBase.getChildCount()) {
                break;
            } else if (this.mFolderSelectBase.getChildAt(i).equals(this.mListView)) {
                this.mFolderSelectBase.removeView(this.mListView);
                break;
            } else {
                i++;
            }
        }
        this.mListView = new ListView(this);
        this.mListView.setAdapter(this.mItemDataAdapter);
        this.mListView.setOnItemClickListener(this.listViewListener);
        this.mFolderSelectBase.addView(this.mListView);
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        this.mMenuControl.CreateMenu(menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem menuItem) {
        if (menuItem.getItemId() != R.id.f337ok) {
            return super.onOptionsItemSelected(menuItem);
        }
        onActionbarMenuidOkClicked((View) null);
        return true;
    }

    public void onActionbarMenuidOkClicked(View view) {
        File GetCurrentFolder = this.mFolderInformation.GetCurrentFolder();
        Intent intent = new Intent();
        intent.putExtra("SELECT_Folder", GetCurrentFolder.getAbsolutePath());
        setResult(-1, intent);
        finish();
    }

    public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
        super.onCreateContextMenu(contextMenu, view, contextMenuInfo);
        ListView listView = (ListView) view;
        if (this.mFolderInformation.IsFolder()) {
            contextMenu.add(0, 0, 0, "選択");
            contextMenu.add(0, 1, 0, "削除");
        }
    }

    public boolean onContextItemSelected(MenuItem menuItem) {
        menuItem.getItemId();
        return true;
    }
}
