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

Java tutorial

Introduction

Here is the source code for hku.fyp14017.blencode.content.bricks.BroadcastBrick.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.content.res.ColorStateList;
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.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

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.Sprite;
import hku.fyp14017.blencode.content.actions.ExtendedActions;
import hku.fyp14017.blencode.ui.dialogs.BrickTextDialog;

import java.util.List;

import hku.fyp14017.blencode.common.MessageContainer;

public class BroadcastBrick extends BrickBaseType implements BroadcastMessage {
    private static final long serialVersionUID = 1L;

    protected String broadcastMessage;
    protected transient AdapterView<?> adapterView;

    protected Object readResolve() {
        MessageContainer.addMessage(broadcastMessage);
        return this;
    }

    public BroadcastBrick(String broadcastMessage) {
        this.broadcastMessage = broadcastMessage;
        MessageContainer.addMessage(broadcastMessage);
    }

    @Override
    public String brickTutorial() {
        return "Used to send a message or notify other components of the application that a specific action has taken place."
                + "\n\n The component can receive this broadcast by using When I Receive? brick and perform some actions depending on "
                + "the message/notification. ";
    }

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

    @Override
    public Brick clone() {
        return new BroadcastBrick(broadcastMessage);
    }

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

    @Override
    public String getBroadcastMessage() {
        return broadcastMessage;
    }

    @Override
    public View getView(final Context context, int brickId, BaseAdapter baseAdapter) {
        if (animationState) {
            return view;
        }
        if (view == null) {
            alphaValue = 255;
        }
        view = View.inflate(context, hku.fyp14017.blencode.R.layout.brick_broadcast, null);
        view = getViewWithAlpha(alphaValue);
        setCheckboxView(hku.fyp14017.blencode.R.id.brick_broadcast_checkbox);

        checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                checked = isChecked;
                adapter.handleCheck(BroadcastBrick.this, isChecked);
            }
        });

        final Spinner broadcastSpinner = (Spinner) view
                .findViewById(hku.fyp14017.blencode.R.id.brick_broadcast_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 {
                    broadcastMessage = selectedMessage;
                    adapterView = parent;
                }
            }

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

        setSpinnerSelection(broadcastSpinner);
        return view;
    }

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

    @Override
    public View getViewWithAlpha(int alphaValue) {

        if (view != null) {

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

            TextView textBroadcastLabel = (TextView) view
                    .findViewById(hku.fyp14017.blencode.R.id.brick_broadcast_label);
            textBroadcastLabel.setTextColor(textBroadcastLabel.getTextColors().withAlpha(alphaValue));
            Spinner broadcastSpinner = (Spinner) view
                    .findViewById(hku.fyp14017.blencode.R.id.brick_broadcast_spinner);
            ColorStateList color = textBroadcastLabel.getTextColors().withAlpha(alphaValue);
            broadcastSpinner.getBackground().setAlpha(alphaValue);
            if (adapterView != null) {
                ((TextView) adapterView.getChildAt(0)).setTextColor(color);
            }

            this.alphaValue = (alphaValue);

        }

        return view;
    }

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

    // TODO: BroadcastBrick and BroadcastReceiverBrick contain this identical method.
    protected 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;
                }

                broadcastMessage = newMessage;
                MessageContainer.addMessage(broadcastMessage);
                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) {
        sequence.addAction(ExtendedActions.broadcast(sprite, broadcastMessage));
        return null;
    }
}