hku.fyp14017.blencode.content.bricks.BroadcastReceiverBrick.java Source code

Java tutorial

Introduction

Here is the source code for hku.fyp14017.blencode.content.bricks.BroadcastReceiverBrick.java

Source

/*
 * Catroid: An on-device visual programming system for Android devices
 * Copyright (C) 2010-2014 The Catrobat Team
 * (<http://developer.fyp14017.hku/credits>)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * An additional term exception under section 7 of the GNU Affero
 * General Public License, version 3, is available at
 * http://developer.fyp14017.hku/license_additional_term
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.hku/licenses/>.
 */
package hku.fyp14017.blencode.content.bricks;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;

import com.badlogic.gdx.scenes.scene2d.actions.SequenceAction;

import hku.fyp14017.blencode.R;
import hku.fyp14017.blencode.common.MessageContainer;
import hku.fyp14017.blencode.content.BroadcastMessage;
import hku.fyp14017.blencode.content.BroadcastScript;
import hku.fyp14017.blencode.content.Script;
import hku.fyp14017.blencode.content.Sprite;
import hku.fyp14017.blencode.ui.dialogs.BrickTextDialog;

import java.util.List;

import hku.fyp14017.blencode.common.MessageContainer;

public class BroadcastReceiverBrick extends ScriptBrick implements BroadcastMessage {
    private static final long serialVersionUID = 1L;

    private BroadcastScript receiveScript;
    private transient String broadcastMessage;

    public BroadcastReceiverBrick(String broadcastMessage) {
        this.broadcastMessage = broadcastMessage;
    }

    public BroadcastReceiverBrick(BroadcastScript receiveScript) {
        this.receiveScript = receiveScript;
    }

    @Override
    public Brick copyBrickForSprite(Sprite sprite) {
        BroadcastReceiverBrick copyBrick = (BroadcastReceiverBrick) clone();
        copyBrick.receiveScript = receiveScript;
        return copyBrick;
    }

    @Override
    public String brickTutorial() {
        return "Used to receive messages/notifications sent by other components of the app, "
                + "depending on which the application can perform further actions.";
    }

    @Override
    public Brick clone() {
        return new BroadcastReceiverBrick(new BroadcastScript(getBroadcastMessage()));
    }

    @Override
    public int getRequiredResources() {
        return NO_RESOURCES;
    }

    @Override
    public String getBroadcastMessage() {
        if (receiveScript == null) {
            return broadcastMessage;
        }
        return receiveScript.getBroadcastMessage();
    }

    @Override
    public View getView(final Context context, int brickId, BaseAdapter baseAdapter) {
        if (animationState) {
            return view;
        }
        if (view == null) {
            alphaValue = 255;
        }
        if (receiveScript == null) {
            receiveScript = new BroadcastScript(broadcastMessage);
            MessageContainer.addMessage(getBroadcastMessage());
        }

        view = View.inflate(context, hku.fyp14017.blencode.R.layout.brick_broadcast_receive, null);
        view = getViewWithAlpha(alphaValue);
        setCheckboxView(hku.fyp14017.blencode.R.id.brick_broadcast_receive_checkbox);

        // XXX method moved to to DragAndDropListView since it is not working on 2.x
        //      checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        //
        //         @Override
        //         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        //            checked = isChecked;
        //            if (!checked) {
        //               for (Brick currentBrick : adapter.getCheckedBricks()) {
        //                  currentBrick.setCheckedBoolean(false);
        //               }
        //            }
        //            adapter.handleCheck(brickInstance, checked);
        //         }
        //      });

        final Spinner broadcastSpinner = (Spinner) view
                .findViewById(hku.fyp14017.blencode.R.id.brick_broadcast_receive_spinner);
        broadcastSpinner.setFocusableInTouchMode(false);
        broadcastSpinner.setFocusable(false);
        if (!(checkbox.getVisibility() == View.VISIBLE)) {
            broadcastSpinner.setClickable(true);
            broadcastSpinner.setEnabled(true);
        } else {
            broadcastSpinner.setClickable(false);
            broadcastSpinner.setEnabled(false);
        }

        broadcastSpinner.setAdapter(MessageContainer.getMessageAdapter(context));
        broadcastSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String selectedMessage = broadcastSpinner.getSelectedItem().toString();
                if (selectedMessage
                        .equals(context.getString(hku.fyp14017.blencode.R.string.new_broadcast_message))) {
                    showNewMessageDialog(broadcastSpinner);
                } else {
                    receiveScript.setBroadcastMessage(selectedMessage);
                    broadcastMessage = selectedMessage;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });

        setSpinnerSelection(broadcastSpinner);
        return view;
    }

    @Override
    public View getPrototypeView(Context context) {
        View prototypeView = View.inflate(context, hku.fyp14017.blencode.R.layout.brick_broadcast_receive, null);
        Spinner broadcastReceiverSpinner = (Spinner) prototypeView
                .findViewById(hku.fyp14017.blencode.R.id.brick_broadcast_receive_spinner);
        broadcastReceiverSpinner.setFocusableInTouchMode(false);
        broadcastReceiverSpinner.setFocusable(false);
        SpinnerAdapter broadcastReceiverSpinnerAdapter = MessageContainer.getMessageAdapter(context);
        broadcastReceiverSpinner.setAdapter(broadcastReceiverSpinnerAdapter);
        setSpinnerSelection(broadcastReceiverSpinner);
        return prototypeView;
    }

    @Override
    public View getViewWithAlpha(int alphaValue) {

        if (view != null) {

            View layout = view.findViewById(hku.fyp14017.blencode.R.id.brick_broadcast_receive_layout);
            Drawable background = layout.getBackground();
            background.setAlpha(alphaValue);
            this.alphaValue = (alphaValue);

        }
        return view;
    }

    @Override
    public Script getScriptSafe() {
        return receiveScript;
    }

    private void setSpinnerSelection(Spinner spinner) {
        int position = MessageContainer.getPositionOfMessageInAdapter(spinner.getContext(), getBroadcastMessage());
        spinner.setSelection(position, true);
    }

    // TODO: BroadcastBrick and BroadcastReceiverBrick contain this identical method.
    private void showNewMessageDialog(final Spinner spinner) {
        final Context context = spinner.getContext();
        BrickTextDialog editDialog = new BrickTextDialog() {

            @Override
            protected void initialize() {
                inputTitle.setText(hku.fyp14017.blencode.R.string.dialog_new_broadcast_message_name);
            }

            @Override
            protected boolean handleOkButton() {
                String newMessage = (input.getText().toString()).trim();
                if (newMessage.isEmpty() || newMessage
                        .equals(context.getString(hku.fyp14017.blencode.R.string.new_broadcast_message))) {
                    dismiss();
                    return false;
                }

                receiveScript.setBroadcastMessage(newMessage);
                broadcastMessage = newMessage;
                MessageContainer.addMessage(newMessage);
                setSpinnerSelection(spinner);
                return true;
            }

            @Override
            public void onDismiss(DialogInterface dialog) {
                setSpinnerSelection(spinner);
                super.onDismiss(dialog);
            }

            @Override
            protected String getTitle() {
                return getString(hku.fyp14017.blencode.R.string.dialog_new_broadcast_message_title);
            }
        };

        editDialog.show(((FragmentActivity) context).getSupportFragmentManager(), "dialog_broadcast_brick");
    }

    @Override
    public List<SequenceAction> addActionToSequence(Sprite sprite, SequenceAction sequence) {
        return null;
    }
}