com.wit.and.examples.dialogs.screen.ScreenDialogsHoloDark.java Source code

Java tutorial

Introduction

Here is the source code for com.wit.and.examples.dialogs.screen.ScreenDialogsHoloDark.java

Source

/*
 * =================================================================================
 * Copyright (C) 2013 Martin Albedinsky [Wolf-ITechnologies]
 * =================================================================================
 * Licensed under the Apache License, Version 2.0 or later (further "License" only);
 * ---------------------------------------------------------------------------------
 * You may use this file only in compliance with the License. More details and copy
 * of this License you may obtain at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 * You can redistribute, modify or publish any part of the code written in this
 * file but as it is described in the License, the software distributed under the 
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES or CONDITIONS OF
 * ANY KIND.
 * 
 * See the License for the specific language governing permissions and limitations
 * under the License.
 * =================================================================================
 */
package com.wit.and.examples.dialogs.screen;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Toast;

import com.wit.and.dialog.Dialog;
import com.wit.and.dialog.EditDialog;
import com.wit.and.dialog.IDialog;
import com.wit.and.dialog.IEditDialog;
import com.wit.and.dialog.ILoginDialog;
import com.wit.and.dialog.LoginDialog;
import com.wit.and.dialog.ProgressDialog;
import com.wit.and.dialog.R;
import com.wit.and.dialog.internal.BaseDialog;
import com.wit.and.dialog.manage.DialogManager;
import com.wit.and.dialog.manage.DialogOptions;
import com.wit.and.examples.dialogs.async.AuthTask;
import com.wit.and.examples.dialogs.async.LoadingDialogTask;
import com.wit.and.examples.dialogs.async.ProgressDialogTask;
import com.wit.and.examples.dialogs.async.ProgressTask;
import com.wit.and.examples.dialogs.factory.DialogsFactory;
import com.wit.and.examples.dialogs.factory.DialogsJoinFactory;
import com.wit.and.examples.dialogs.model.DialogExample;
import com.wit.and.examples.dialogs.widget.DialogsListAdapter;

/**
 * @author Martin Albedinsky
 */
public class ScreenDialogsHoloDark extends ScreenDialogs
        implements ProgressTask.OnProgressTaskListener, AuthTask.OnAuthTaskListener {

    /**
     * Log TAG.
     */
    private static final String TAG = ScreenDialogsHoloDark.class.getSimpleName();

    private static ProgressTask sProgressDialogTask = null;
    private static ProgressTask sLoadingDialogTask = null;

    private static AuthTask sAuthTask = null;

    private final DialogManager DIALOG_MANAGER = getDialogManager();

    // Listener callback for LoginDialog.
    private final ILoginDialog.OnAuthenticationListener AUTH_LISTENER = new ILoginDialog.OnAuthenticationListener() {
        @Override
        public void onAuthenticationError(int errorCode, ILoginDialog loginDialog) {
            switch (errorCode) {
            case LoginDialog.ERROR_INVALID_PASSWORD:
                loginDialog.setPasswordError("Invalid password");
                break;
            case LoginDialog.ERROR_INVALID_USER_NAME:
                loginDialog.setUsernameError("Invalid username");
                break;
            }
        }

        @Override
        public void onAuthenticationSubmit(LoginDialog.Credentials credentials, ILoginDialog loginDialog) {
            // Simulate service call.
            if (sAuthTask == null) {
                sAuthTask = new AuthTask();
                sAuthTask.dispatchAttachAuthListener(ScreenDialogsHoloDark.this);
                sAuthTask.execute(credentials);
            }
        }
    };

    // Listener callback for EditDialog.
    private final IEditDialog.OnEditListener EDIT_LISTENER = new IEditDialog.OnEditListener() {
        @Override
        public void onEditSubmit(IEditDialog dialog) {
            // Here can be provided some check of edit value.
            String value = null;

            EditText edit = dialog.getCurrentEdit();
            if (edit.getText() != null) {
                value = edit.getText().toString();
            }

            if (value == null || !value.matches("^user$")) {
                dialog.setEditError("Incorrect username value");
            } else {
                DIALOG_MANAGER.dismissDialog(dialog.getDialogID());
                Toast.makeText(ScreenDialogsHoloDark.this, "Saved", Toast.LENGTH_SHORT).show();
            }
        }
    };

    @Override
    public void onDialogRestored(DialogFragment dialog, int dialogID) {
        Log.d(TAG, "onDialogRestored(" + dialog.getTag() + ", " + dialogID + ")");

        DialogsFactory.FactoryDialog factoryDialog = DialogsFactory.FactoryDialog.parseDialog(dialogID);
        if (factoryDialog != null) {
            // Get the currently running progress task.
            ProgressTask currProgressTask = (sProgressDialogTask != null) ? sProgressDialogTask
                    : (sLoadingDialogTask != null ? sLoadingDialogTask : null);

            // Handle restored progress dialogs.
            switch (factoryDialog) {
            case DIALOG_LOADING:
            case DIALOG_PROGRESS:
            case DIALOG_PROGRESS_NO_PROGRESS_INDICATOR:
            case DIALOG_PROGRESS_NO_TIME_INDICATOR:
                if (currProgressTask != null) {
                    currProgressTask.dispatchAttachProgressDialog((ProgressDialog) dialog);
                } else {
                    DIALOG_MANAGER.dismissDialog(dialog);
                }
                break;
            case DIALOG_LOGIN:
                ((LoginDialog) dialog).setOnAuthenticationListener(AUTH_LISTENER);
                break;
            case DIALOG_EDIT:
                ((EditDialog) dialog).setOnEditListener(EDIT_LISTENER);
                break;
            }
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // Id of item is id of dialog from dialog factory.
        final int dialogID = (int) id;
        boolean handled = false;

        DialogsFactory.FactoryDialog factoryDialog = DialogsFactory.FactoryDialog.parseDialog(dialogID);
        if (factoryDialog != null) {
            switch (factoryDialog) {
            case DIALOG_LOADING:
                if (sLoadingDialogTask == null) {
                    this.runProgressTask(LoadingDialogTask.TASK_ID, dialogID);
                }
                handled = true;
                break;
            case DIALOG_PROGRESS:
            case DIALOG_PROGRESS_NO_PROGRESS_INDICATOR:
            case DIALOG_PROGRESS_NO_TIME_INDICATOR:
                if (sProgressDialogTask == null) {
                    this.runProgressTask(ProgressDialogTask.TASK_ID, dialogID);
                }
                handled = true;
                break;
            case DIALOG_LOGIN:
                // Show login dialog with authentication listener.
                DIALOG_MANAGER.showDialog(dialogID, new DialogManager.ListenerOptions().listener(AUTH_LISTENER));
                handled = true;
                break;
            case DIALOG_EDIT:
                // Show edit dialog with edit listener.
                DIALOG_MANAGER.showDialog(dialogID, new DialogManager.ListenerOptions().listener(EDIT_LISTENER));
                handled = true;
                break;
            }
        }

        if (!handled) {
            // Fill options for dialogs from joined library.
            // This can be helpful if some dialog options depends on
            // the current context (there are no fixed and can't be saved)
            // for example in resources.
            // For example, if you would like to show to user how many messages,
            // he just received, that is dynamic value which can be passed to
            // dialog from activity context by dialog options.
            DialogOptions options = new DialogOptions(getResources());
            switch (dialogID) {
            case DialogsJoinFactory.DIALOG:
                final int msgCount = 12;
                options.message("You have just received " + Integer.toString(msgCount) + " new messages.");
                DIALOG_MANAGER.showDialog(dialogID, options);
                break;
            case DialogsJoinFactory.DIALOG_ALERT:
                options.title("Warning");
                options.message("Your Android device is exposed to risk. Would You like to proceed?");
                options.buttonsType(Dialog.Buttons.PAIR);
                options.negative(R.string.And_Dialog_Button_Close);
                options.positive(R.string.And_Dialog_Button_Proceed);
                options.icon(R.drawable.and_dialog_ic_alert);
                DIALOG_MANAGER.showDialog(dialogID, options);
                break;
            case DialogsJoinFactory.DIALOG_WEB:
                options.message("http://github.com");
                DIALOG_MANAGER.showDialog(dialogID, options);
                break;
            default:
                super.onItemClick(parent, view, position, id);
            }
        }
    }

    private void runProgressTask(int taskID, int dialogID) {
        final DialogManager.IDialogFactory factory = DIALOG_MANAGER.getDialogFactory();
        ProgressDialog progressDialog = (ProgressDialog) factory.createDialogInstance(dialogID, null);

        // Show progress dialog.
        DIALOG_MANAGER.showDialog(progressDialog, factory.getDialogTag(dialogID));

        // Create and start progress task.
        ProgressTask progressTask = null;
        switch (taskID) {
        case LoadingDialogTask.TASK_ID:
            sLoadingDialogTask = new LoadingDialogTask();
            progressTask = sLoadingDialogTask;
            break;
        case ProgressDialogTask.TASK_ID:
            sProgressDialogTask = new ProgressDialogTask();
            progressTask = sProgressDialogTask;
            break;
        }

        if (progressTask != null) {
            // Set up and run.
            if (dialogID == DialogsFactory.FactoryDialog.DIALOG_PROGRESS.id) {
                progressTask.setShowImagesProgress(true);
            }

            progressTask.dispatchAttachProgressDialog(progressDialog);
            progressTask.dispatchAttachTaskListener(this);
            progressTask.execute();
        }
    }

    @Override
    public void onDialogButtonClick(IDialog dialog, BaseDialog.DialogButton button) {
        super.onDialogButtonClick(dialog, button);
        switch (button) {
        case NEUTRAL:
            DialogsFactory.FactoryDialog factoryDialog = DialogsFactory.FactoryDialog
                    .parseDialog(dialog.getDialogID());
            if (factoryDialog != null) {
                // Handle restored progress dialogs.
                switch (factoryDialog) {
                case DIALOG_PROGRESS_NO_TIME_INDICATOR:
                    if (sProgressDialogTask != null) {
                        sProgressDialogTask.cancel();
                    }
                    break;
                }
            }
            break;
        }
    }

    @Override
    public void onDialogCancelled(IDialog dialog) {
        super.onDialogCancelled(dialog);

    }

    @Override
    public void onAuthorizationError(int error) {
        DialogFragment dialog = DIALOG_MANAGER.getTopVisibleDialog();
        if (dialog != null && dialog instanceof ILoginDialog) {
            switch (error) {
            case AuthTask.ERROR_WRONG_CREDENTIALS:
                ((ILoginDialog) dialog).setUsernameError("Wrong credentials");
                break;
            }
        }
        sAuthTask = null;
    }

    @Override
    public void onAuthorizationSuccess() {
        DIALOG_MANAGER.dismissDialog(DialogsFactory.FactoryDialog.DIALOG_LOGIN.id);
        Toast.makeText(this, "Login successful", Toast.LENGTH_SHORT).show();
        sAuthTask = null;
    }

    @Override
    public void onProgressTaskCancelled(int taskID, ProgressDialog progressDialog) {
        DIALOG_MANAGER.dismissDialog(progressDialog);
        switch (taskID) {
        case ProgressDialogTask.TASK_ID:
            sProgressDialogTask = null;
            break;
        case LoadingDialogTask.TASK_ID:
            sLoadingDialogTask = null;
            break;
        }
    }

    @Override
    public void onProgressTaskFinished(int taskID, ProgressDialog progressDialog) {
        // Perform same operations as when the task was cancelled.
        onProgressTaskCancelled(taskID, progressDialog);
    }

    private static int sCount = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Bind and set adapter.
        setListAdapter(DialogExample.bindDialogsAdapter(new DialogsListAdapter(this)));

        // Set up dialog manager.
        DIALOG_MANAGER.setDialogFactory(new DialogsFactory(this));

        if (savedInstanceState != null) {
            if (sProgressDialogTask != null) {
                sProgressDialogTask.dispatchAttachTaskListener(this);
            } else if (sLoadingDialogTask != null) {
                sLoadingDialogTask.dispatchAttachTaskListener(this);
            }
            if (sAuthTask != null) {
                sAuthTask.dispatchAttachAuthListener(this);
            }

        }
        Log.d(TAG, "onCreate() instances(" + ++sCount + ") created");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (sProgressDialogTask != null) {
            sProgressDialogTask.dispatchDetachTaskListener();
        } else if (sLoadingDialogTask != null) {
            sLoadingDialogTask.dispatchDetachTaskListener();
        }
        if (sAuthTask != null) {
            sAuthTask.dispatchDetachAuthListener();
        }
        Log.d(TAG, "onDestroy() instances (" + --sCount + ") remains");
    }
}