Demo entry 2779950

LocalControlFragment

   

Submitted by Esau Garcia on Sep 29, 2015 at 22:41
Language: Java. Code size: 3.4 kB.

package io.egalabs.nxtremotecontroller.fragment;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import io.egalabs.nxtremotecontroller.R;
import io.egalabs.nxtremotecontroller.activity.MainActivity;
import io.egalabs.nxtremotecontroller.bluetooth.Connector;
import io.egalabs.nxtremotecontroller.constants.EventsConstants;
import io.egalabs.nxtremotecontroller.widget.Joystick;

public class LocalControlFragment extends BaseFragment {

	protected MainActivity mMainActivity;
	protected RelativeLayout mRelativeLayout;
	protected Joystick mJoystick;

	public final LocalControlFragment setActivity (MainActivity activity) {
		super.setActivity(activity);
		mMainActivity = activity;
		return this;
	}

	/*
	 * Returns the fragment view to render it.
	 */
	@Override
	public final View onCreateView (LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
		View view =  inflater.inflate(R.layout.local_control_fragment_layout, parent, false);
		mRelativeLayout = (RelativeLayout) view.findViewById(R.id.relative_layout);
		mJoystick = (Joystick) view.findViewById(R.id.joystick);
		mJoystick.setOnChangePositionListener(new Joystick.OnChangePosition() {
			@Override
			public final void onChange (float x, float y) {
				double module = mJoystick.getModule();
				double maxModule = mJoystick.getMaximumModule();
				double[] power = Connector.coordsToPower(x, y, module, maxModule);
				mMainActivity.mConnector.motorABC(0, power[1], power[0], false, false);
			}
		});
		return view;
	}

	@Override
	public void onMessage (String tag, Object... arguments) {
		switch (tag) {
			case EventsConstants.TAG_BLUETOOTH_STATE_CHANGED:
				String btState = (String) arguments[0];
				switch (btState) {
					case EventsConstants.VALUE_BLUETOOTH_STATE_ENABLING:
					case EventsConstants.VALUE_BLUETOOTH_STATE_ENABLED:
						break;
					case EventsConstants.VALUE_BLUETOOTH_STATE_DISABLING:
					case EventsConstants.VALUE_BLUETOOTH_STATE_DISABLED:
						mMainActivity.checkItem(2);
						break;
				}
				break;
			case EventsConstants.TAG_SNACKBAR_SHOW:
				Snackbar.make(mRelativeLayout, (int) arguments[0], (int) arguments[1]).show();
				break;
			case EventsConstants.TAG_HANDLER_MESSAGE:
				int what = (int) arguments[0];
				int state = (int) arguments[1];
				int error = (int) arguments[2];
				if (what == EventsConstants.WHAT_ERROR_CHANGE) {
					if (Connector.connectionClosed(state, error)) {
						mMainActivity.mQueuedMessage = R.string.snackbar_control_closed;
					} else if (Connector.connectionLost(state, error)) {
						mMainActivity.mQueuedMessage = R.string.snackbar_control_lost;
					} else if (Connector.connectionSocketFailed(state, error)) {
						mMainActivity.mQueuedMessage = R.string.snackbar_control_socket_error;
					} else if (Connector.connectionRequestFailed(state, error)) {
						mMainActivity.mQueuedMessage = R.string.snackbar_control_request_failed;
					} else if (Connector.connectionUnexpectedFailed(state, error)) {
						mMainActivity.mQueuedMessage = R.string.snackbar_control_unexpected_error;
					}
					mMainActivity.checkItem(2);
				}
				break;
		}
	}

}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).