package epson.print.storage;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.Random;

import epson.print.ActivityIACommon;


class StorageActivity extends ActivityIACommon {

    public interface CancelRequestCallback {
        void onCancelRequest(Intent intent);
    }

    StorageActivity() {
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        StorageSecureStore.initSharedSecureStore(getApplicationContext());
    }

    public class OverlayProgress {
        BroadcastReceiver mCancelReceiver;
        int mRequestCode = new Random().nextInt(Integer.MAX_VALUE);

        public OverlayProgress() {
            setCancelReceiver((BroadcastReceiver) null);
        }

        public void show() {
            show((String) null, (CancelRequestCallback) null);
        }

        private void show(String str) {
            show(str, (CancelRequestCallback) null);
        }

        private void show(String str, CancelRequestCallback cancelRequestCallback) {
            show(str, cancelRequestCallback, true);
        }

        private void show(String str, CancelRequestCallback cancelRequestCallback, boolean z) {
            Intent intent = new Intent(StorageActivity.this, StorageWaitingActivity.class);
            intent.putExtra(StorageWaitingActivity.EXTRA_REQUEST_CODE, mRequestCode);
            if (str != null) {
                intent.putExtra(StorageWaitingActivity.EXTRA_MESSAGE, str);
            }
            if (cancelRequestCallback != null) {
                intent.putExtra(StorageWaitingActivity.EXTRA_CANCEL_ENABLED, true);
                registerCancelRequestReceiver(cancelRequestCallback);
            }
            StorageActivity.this.startActivity(intent);
            while (z && !StorageWaitingActivity.bShowing) {
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        private void changeMessage(String str) {
            Intent intent = new Intent(StorageWaitingActivity.CHANGE_ACTION);
            intent.putExtra(StorageWaitingActivity.EXTRA_REQUEST_CODE, mRequestCode);
            intent.putExtra(StorageWaitingActivity.EXTRA_MESSAGE, str);
            StorageActivity.this.sendBroadcast(intent);
        }

        private void registerCancelCallback(CancelRequestCallback cancelRequestCallback) {
            Intent intent = new Intent(StorageWaitingActivity.REGISTER_ACTION);
            intent.putExtra(StorageWaitingActivity.EXTRA_REQUEST_CODE, mRequestCode);
            intent.putExtra(StorageWaitingActivity.EXTRA_CANCEL_ENABLED, true);
            registerCancelRequestReceiver(cancelRequestCallback);
            StorageActivity.this.sendBroadcast(intent);
        }

        public void dismiss() {
            Intent intent = new Intent(StorageWaitingActivity.DISMISS_ACTION);
            intent.putExtra(StorageWaitingActivity.EXTRA_REQUEST_CODE, mRequestCode);
            StorageActivity.this.sendBroadcast(intent);
            setCancelReceiver((BroadcastReceiver) null);
        }

        private void setCancelReceiver(BroadcastReceiver broadcastReceiver) {
            BroadcastReceiver broadcastReceiver2 = mCancelReceiver;
            if (broadcastReceiver2 != null) {
                StorageActivity.this.unregisterReceiver(broadcastReceiver2);
            }
            mCancelReceiver = broadcastReceiver;
        }

        private void registerCancelRequestReceiver(final CancelRequestCallback cancelRequestCallback) {
            C09221 r0 = new BroadcastReceiver() {
                public void onReceive(Context context, Intent intent) {
                    String action = intent.getAction();
                    int intExtra = intent.getIntExtra(StorageWaitingActivity.EXTRA_REQUEST_CODE, 0);
                    if (action.equals(StorageWaitingActivity.CANCEL_REQUEST_ACTION) && OverlayProgress.mRequestCode == intExtra) {
                        cancelRequestCallback.onCancelRequest(intent);
                    }
                }
            };
            StorageActivity.this.registerReceiver(r0, new IntentFilter(StorageWaitingActivity.CANCEL_REQUEST_ACTION));
            setCancelReceiver(r0);
        }
    }

    protected void showErrorDialog(String str) {
        showErrorDialog(str, (Intent) null);
    }

    private void showErrorDialog(int i) {
        showErrorDialog(getString(i));
    }

    private void showConnectionError() {
        showErrorDialog(getString(R.string.network_error_mes), new Intent("android.settings.WIRELESS_SETTINGS"));
    }

    private void showErrorDialog(String str, final Intent intent) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(str);
        builder.setCancelable(false);
        builder.setPositiveButton(getString(R.string.str_ok), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.cancel();
                Intent intent = intent;
                if (intent != null) {
                    StorageActivity.this.startActivity(intent);
                    StorageActivity.this.finish();
                }
            }
        });
        builder.create().show();
    }

    private void showErrorDialogAndFinish(String str) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(str);
        builder.setCancelable(false);
        builder.setPositiveButton(getString(R.string.str_ok), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.cancel();
                StorageActivity.this.finish();
            }
        });
        builder.create().show();
    }

    abstract class OnVisibilityListener {
        public abstract void onVisibilityChanged(int i);

        OnVisibilityListener() {
        }
    }

    private void bindClearButton(int i, int i2) {
        bindClearButton(i, i2, (OnVisibilityListener) null);
    }

    private void bindClearButton(int i, int i2, final OnVisibilityListener onVisibilityListener) {
        final EditText editText = (EditText) findViewById(i);
        final Button button = (Button) findViewById(i2);
        editText.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            public void afterTextChanged(Editable editable) {
                if (editText.getText().toString().length() > 0) {
                    button.setVisibility(View.VISIBLE);
                } else {
                    button.setVisibility(View.GONE);
                }
                OnVisibilityListener onVisibilityListener = onVisibilityListener;
                if (onVisibilityListener != null) {
                    onVisibilityListener.onVisibilityChanged(button.getVisibility());
                }
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                editText.getText().clear();
            }
        });
        if (editText.getText().toString().length() > 0) {
            button.setVisibility(View.VISIBLE);
        }
    }

    public boolean isConnected() {
        NetworkInfo activeNetworkInfo = ((ConnectivityManager) getBaseContext().getSystemService("connectivity")).getActiveNetworkInfo();
        if (activeNetworkInfo != null) {
            return activeNetworkInfo.isConnectedOrConnecting();
        }
        return false;
    }

    private void setInputMaxLength(EditText editText, int i) {
        editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(i)});
    }

    private void setTitle(String str) {
        super.setTitle(str);
    }
}
