package com.epson.iprint.storage;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import com.epson.iprint.storage.StorageActivity;
import com.epson.iprint.storage.box.BoxNetSignInActivity;
import com.epson.iprint.storage.dropbox.DropboxV2SignInActivity;
import com.epson.iprint.storage.evernote.EvernoteSignInActivity;
import com.epson.iprint.storage.gdrivev3.GoogleV3UploadSignInActivity;
import com.epson.iprint.storage.onedrive.OneDriveSignInActivity;
import epson.print.R;

public abstract class StorageSignInActivity extends StorageActivity {
    private static final String EXTRA_SERVER_NAME = "StorageSignInActivity.ServerName";
    private final int DEFAULT_INPUT_MAX_LENGTH = 256;
    /* access modifiers changed from: private */
    public BasicSignIn mBasicSignIn;
    private StorageActivity.OverlayProgress mProgressUntilOnDestroy;

    public interface BasicSignIn {
        boolean signInBackground(String str, String str2);
    }

    private int getMaxLengthUsername() {
        return 256;
    }

    public abstract BasicSignIn getBasicSignIn();

    protected int getMaxLengthPassword() {
        return 256;
    }

    public /* bridge */ /* synthetic */ boolean isConnected() {
        return super.isConnected();
    }

    private static Class<?> getActivityClass(String str) {
        if (str.equals(StorageServiceClient.STORAGE_GOOGLEDRIVE)) {
            return GoogleV3UploadSignInActivity.class;
        }
        if (str.equals(StorageServiceClient.STORAGE_DROPBOX)) {
            return DropboxV2SignInActivity.class;
        }
        if (str.equals(StorageServiceClient.STORAGE_BOX)) {
            return BoxNetSignInActivity.class;
        }
        if (str.equals(StorageServiceClient.STORAGE_EVERNOTE)) {
            return EvernoteSignInActivity.class;
        }
        if (str.equals(StorageServiceClient.STORAGE_ONEDRIVE)) {
            return OneDriveSignInActivity.class;
        }
        return null;
    }

    public static Intent getStartIntent(Context context, String str, String str2) {
        Intent intent = new Intent(context, getActivityClass(str));
        intent.putExtra(EXTRA_SERVER_NAME, str2);
        return intent;
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        BasicSignIn basicSignIn = getBasicSignIn();
        if (basicSignIn != null) {
            onInitBasicSignIn(basicSignIn);
        }
    }

    protected void onDestroy() {
        StorageActivity.OverlayProgress overlayProgress = this.mProgressUntilOnDestroy;
        if (overlayProgress != null) {
            overlayProgress.dismiss();
            this.mProgressUntilOnDestroy = null;
        }
        super.onDestroy();
    }

    protected void onInitBasicSignIn(BasicSignIn basicSignIn) {
        this.mBasicSignIn = basicSignIn;
        setContentView((int) R.layout.login_screen_layout);
        setActionBar(getIntent().getStringExtra(EXTRA_SERVER_NAME), true);
        setInputMaxLength((EditText) findViewById(R.id.login_username_content), getMaxLengthUsername());
        setInputMaxLength((EditText) findViewById(R.id.login_password_content), getMaxLengthPassword());
        bindClearButton(R.id.login_username_content, R.id.clear_id_btn);
        bindClearButton(R.id.login_password_content, R.id.clear_password_btn);
    }

    protected void showLoginError() {
        showErrorDialog((int) R.string.authenticate_error_mes);
    }

    protected void showLoginErrorAndFinish() {
        showErrorDialogAndFinish(getString(R.string.authenticate_error_mes));
    }

    class BasicSignInTask extends AsyncTask<Void, Void, Boolean> {
        private String mPassword;
        private String mUserName;
        StorageActivity.OverlayProgress progress;

        BasicSignInTask() {
            this.mUserName = ((EditText) StorageSignInActivity.this.findViewById(R.id.login_username_content)).getText().toString();
            this.mPassword = ((EditText) StorageSignInActivity.this.findViewById(R.id.login_password_content)).getText().toString();
        }

        protected Boolean doInBackground(Void... voidArr) {
            this.progress = new StorageActivity.OverlayProgress();
            this.progress.show();
            return Boolean.valueOf(StorageSignInActivity.this.mBasicSignIn.signInBackground(this.mUserName, this.mPassword));
        }

        protected void onPostExecute(Boolean bool) {
            if (!bool.booleanValue()) {
                StorageSignInActivity.this.showLoginError();
                this.progress.dismiss();
                return;
            }
            this.progress.dismiss();
            StorageSignInActivity.this.finish();
        }
    }

    public boolean onOptionsItemSelected(MenuItem menuItem) {
        if (menuItem.getItemId() != R.id.menu_signin) {
            return super.onOptionsItemSelected(menuItem);
        }
        new BasicSignInTask().execute(new Void[0]);
        return true;
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.C2138menu.menu_sign_inout, menu);
        menu.removeItem(R.id.menu_signout);
        return super.onCreateOptionsMenu(menu);
    }
}
