package com.epson.iprint.storage.gdrivev3;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;



import android.support.p000v4.app.Fragment;
import android.support.p000v4.app.FragmentManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import com.epson.iprint.prtlogger.PrintLog;
import com.epson.iprint.storage.ConfirmCancelDialog;
import com.epson.iprint.storage.LocalProgressDialog;
import com.epson.iprint.storage.OneButtonDialogFragment;
import com.epson.iprint.storage.StorageProcessDownloadActivity;
import com.epson.iprint.storage.gdrivev3.GoogleDownloadContract;
import com.epson.iprint.storage.gdrivev3.IprintGoogleSignIn;
import epson.print.ActivityIACommon;
import epson.print.R;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;

public class ListAndDownloadActivity extends ActivityIACommon implements ActivityWrapper, GoogleDownloadContract.View, OneButtonDialogFragment.DialogCallback, ConfirmCancelDialog.DialogCancelListener {
    private static final String DIALOG_TAG_CONFIRM_CANCEL = "confirm-cancel";
    private static final String DIALOG_TAG_DOWNLOAD_FAILED = "download_failed";
    private static final String DIALOG_TAG_OFFLINE_ERROR = "offline_error";
    private static final String DIALOG_TAG_SIGN_IN_FAILED = "sign-in_failed";
    private static final String FRAGMENT_TAG_PROGRESS = "fragment-progress";
    private static final int REQUEST_CODE_CHECK_PLAY_SERVICE = 11;
    private static final int REQUEST_CODE_DOWNLOAD_PREVIEW_END = 12;
    private static final int REQUEST_CODE_SIGN_IN = 10;
    private static final int REQUEST_CODE_SIGN_IN_ADD_SCOPE = 13;
    private boolean mActivityForeground;
    private FileListAdapter mFileListAdapter;
    private LinkedList<Runnable> mForegroundRunnableList;
    private GoogleDownloadContract.UserActionListener mGoogleDownloadPresenter;
    private boolean mIsDownloadInterruption = false;
    private ListView mListView;
    /* access modifiers changed from: private */
    public int mPosition = 0;
    private TextView mSignInMessage;
    private boolean mSignInOutButtonEnabled;
    private int mSignInOutButtonType;
    private IprintGoogleSignIn.StartActivityResultCallback mSignInStartActivityCallback;

    @NonNull
    public Activity getActivity() {
        return this;
    }

    @NonNull
    public ActivityWrapper getActivityWrapper() {
        return this;
    }

    public int getPlayServiceRequestCode() {
        return 11;
    }

    public int getSingInAddScopeRequestCode() {
        return 13;
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView((int) R.layout.activity_list_and_download);
        setActionBar(getString(R.string.google_drive), true);
        this.mGoogleDownloadPresenter = Injection.provideDownloadPresenter(this);
        this.mSignInOutButtonEnabled = true;
        this.mForegroundRunnableList = new LinkedList<>();
        this.mListView = (ListView) findViewById(16908298);
        this.mListView.setCacheColorHint(0);
        this.mFileListAdapter = new FileListAdapter(getApplicationContext());
        this.mListView.setAdapter(this.mFileListAdapter);
        this.mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
                int unused = ListAndDownloadActivity.this.mPosition = i;
                ListAndDownloadActivity.this.localOnItemClick(i);
            }
        });
        this.mSignInMessage = (TextView) findViewById(16908292);
        this.mSignInMessage.setText(R.string.sign_in_request);
        this.mSignInMessage.setTypeface((Typeface) null, 1);
        this.mSignInMessage.setTextSize(18.0f);
        if (bundle != null) {
            clearFragments();
        }
        this.mGoogleDownloadPresenter.initView();
    }

    private void clearFragments() {
        removeProgressIfExists();
        deleteAllDialog();
    }

    private void deleteAllDialog() {
        for (String dismissDialog : new String[]{DIALOG_TAG_SIGN_IN_FAILED, DIALOG_TAG_DOWNLOAD_FAILED, DIALOG_TAG_OFFLINE_ERROR, DIALOG_TAG_CONFIRM_CANCEL}) {
            dismissDialog(dismissDialog);
        }
    }

    /* access modifiers changed from: private */
    public void dismissDialog(@NonNull final String str) {
        if (!this.mActivityForeground) {
            this.mForegroundRunnableList.addLast(new Runnable() {
                public void run() {
                    ListAndDownloadActivity.this.dismissDialog(str);
                }
            });
            return;
        }
        Fragment findFragmentByTag = getSupportFragmentManager().findFragmentByTag(str);
        if (findFragmentByTag != null) {
            ((DialogFragment) findFragmentByTag).dismiss();
        }
    }

    /* access modifiers changed from: private */
    public void localOnItemClick(int i) {
        OnlineFile driveItem = this.mFileListAdapter.getDriveItem(i);
        if (driveItem != null) {
            this.mGoogleDownloadPresenter.itemSelected(driveItem);
        }
    }

    public boolean onOptionsItemSelected(MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case R.id.menu_signin /*2131231253*/:
                this.mGoogleDownloadPresenter.signInOrDisconnectClicked();
                return true;
            case R.id.menu_signout /*2131231254*/:
                this.mGoogleDownloadPresenter.signInOrDisconnectClicked();
                return true;
            default:
                return super.onOptionsItemSelected(menuItem);
        }
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.C2138menu.menu_sign_inout, menu);
        if (this.mSignInOutButtonType == 0) {
            menu.removeItem(R.id.menu_signout);
            menu.findItem(R.id.menu_signin).setEnabled(this.mSignInOutButtonEnabled);
            return true;
        }
        menu.removeItem(R.id.menu_signin);
        menu.findItem(R.id.menu_signout).setEnabled(this.mSignInOutButtonEnabled);
        return true;
    }

    protected void onActivityResult(int i, int i2, Intent intent) {
        switch (i) {
            case 10:
                IprintGoogleSignIn.StartActivityResultCallback startActivityResultCallback = this.mSignInStartActivityCallback;
                if (startActivityResultCallback != null) {
                    startActivityResultCallback.onActivityResult(i2, intent);
                    this.mSignInStartActivityCallback = null;
                    return;
                }
                return;
            case 11:
                return;
            case 12:
                this.mGoogleDownloadPresenter.onPreviewActivityEnd();
                return;
            case 13:
                this.mGoogleDownloadPresenter.onAddScopeCompleted(i2 == -1);
                return;
            default:
                return;
        }
    }

    protected void onPostResume() {
        super.onPostResume();
        this.mActivityForeground = true;
        processForegroundList();
        this.mGoogleDownloadPresenter.activityOnResume();
    }

    private void processForegroundList() {
        Iterator it = this.mForegroundRunnableList.iterator();
        while (it.hasNext()) {
            it.remove();
            ((Runnable) it.next()).run();
        }
    }

    protected void onPause() {
        super.onPause();
        this.mGoogleDownloadPresenter.activityOnPause();
        this.mActivityForeground = false;
    }

    public void onBackPressed() {
        this.mGoogleDownloadPresenter.backKeyPressed();
    }

    public void requestSignIn(@NonNull Intent intent, @NonNull IprintGoogleSignIn.StartActivityResultCallback startActivityResultCallback) {
        this.mSignInStartActivityCallback = startActivityResultCallback;
        startActivityForResult(intent, 10);
    }

    public void setSignInButtonEnabled(boolean z) {
        invalidateOptionsMenu();
    }

    public void changeSignInOutButton(int i) {
        int i2 = 0;
        this.mSignInMessage.setVisibility(i == 0 ? 0 : 8);
        ListView listView = this.mListView;
        if (i == 0) {
            i2 = 8;
        }
        listView.setVisibility(i2);
        this.mSignInOutButtonType = i;
        invalidateOptionsMenu();
    }

    /* access modifiers changed from: private */
    public void showErrorDialog(final int i, final String str) {
        if (!this.mActivityForeground) {
            this.mForegroundRunnableList.addLast(new Runnable() {
                public void run() {
                    ListAndDownloadActivity.this.showErrorDialog(i, str);
                }
            });
        } else {
            OneButtonDialogFragment.newInstance(i, str).show(getSupportFragmentManager(), str);
        }
    }

    public void showSignInFailDialog() {
        showErrorDialog(R.string.authenticate_error_mes, DIALOG_TAG_SIGN_IN_FAILED);
    }

    public void showDownloadErrorDialog() {
        showErrorDialog(R.string.download_error_mes, DIALOG_TAG_DOWNLOAD_FAILED);
    }

    public void showOfflineErrorDialog() {
        showErrorDialog(R.string.network_error_mes, DIALOG_TAG_OFFLINE_ERROR);
    }

    public void clearListItems() {
        this.mFileListAdapter.clearItems();
    }

    public void showProgress() {
        this.mSignInOutButtonEnabled = false;
        invalidateOptionsMenu();
        showProgressFragment(false, 0);
    }

    /* access modifiers changed from: private */
    public void showProgressFragment(final boolean z, final int i) {
        if (!this.mActivityForeground) {
            this.mForegroundRunnableList.addLast(new Runnable() {
                public void run() {
                    ListAndDownloadActivity.this.showProgressFragment(z, i);
                }
            });
            return;
        }
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        supportFragmentManager.beginTransaction().add(16908290, LocalProgressDialog.newInstance(z, i, DIALOG_TAG_CONFIRM_CANCEL), FRAGMENT_TAG_PROGRESS).commit();
    }

    public void showDownloadProgress() {
        this.mSignInOutButtonEnabled = false;
        invalidateOptionsMenu();
        showProgressFragment(true, R.string.downloading_notification);
    }

    public void dismissProgress() {
        dismissDialog(DIALOG_TAG_CONFIRM_CANCEL);
        removeProgressIfExists();
        this.mSignInOutButtonEnabled = true;
        invalidateOptionsMenu();
    }

    public void listFiles(@Nullable ArrayList<OnlineFile> arrayList) {
        if (arrayList == null) {
            clearListItems();
            showErrorDialog(R.string.unknow_error_mes, DIALOG_TAG_DOWNLOAD_FAILED);
            return;
        }
        this.mFileListAdapter.setDriveFile(arrayList);
    }

    public void startPreviewActivity(String str) {
        startActivityForResult(StorageProcessDownloadActivity.getPreviewIntent(str, getApplicationContext(), new PrintLog()), 12);
    }

    public void finishActivity() {
        finish();
    }

    public void buttonPressed(@Nullable String str) {
        if (str != null) {
            char c = 65535;
            int hashCode = str.hashCode();
            if (hashCode != -683930452) {
                if (hashCode != 156934100) {
                    if (hashCode == 366991495 && str.equals(DIALOG_TAG_SIGN_IN_FAILED)) {
                        c = 0;
                    }
                } else if (str.equals(DIALOG_TAG_DOWNLOAD_FAILED)) {
                    c = 1;
                }
            } else if (str.equals(DIALOG_TAG_OFFLINE_ERROR)) {
                c = 2;
            }
            switch (c) {
                case 0:
                    return;
                case 1:
                    return;
                case 2:
                    startActivity(new Intent("android.settings.WIRELESS_SETTINGS"));
                    finish();
                    return;
                default:
                    return;
            }
        }
    }

    public void cancel() {
        this.mGoogleDownloadPresenter.cancelDownload();
    }

    protected void onStop() {
        super.onStop();
        if (!this.mGoogleDownloadPresenter.isDownloading() || Build.VERSION.SDK_INT < 29) {
            this.mIsDownloadInterruption = false;
            return;
        }
        this.mIsDownloadInterruption = true;
        cancel();
    }

    protected void onRestart() {
        super.onRestart();
        if (this.mIsDownloadInterruption) {
            localOnItemClick(this.mPosition);
        }
        this.mIsDownloadInterruption = false;
    }

    /* access modifiers changed from: private */
    public void removeProgressIfExists() {
        if (!this.mActivityForeground) {
            this.mForegroundRunnableList.addLast(new Runnable() {
                public void run() {
                    ListAndDownloadActivity.this.removeProgressIfExists();
                }
            });
            return;
        }
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        Fragment findFragmentByTag = supportFragmentManager.findFragmentByTag(FRAGMENT_TAG_PROGRESS);
        if (findFragmentByTag != null) {
            supportFragmentManager.beginTransaction().remove(findFragmentByTag).commit();
        }
    }
}
