Demo entry 2779809

BaseReceiver.java

   

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

package io.egalabs.nxtremotecontroller.bluetooth.receiver;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import io.egalabs.nxtremotecontroller.bluetooth.listener.BaseListener;

/*
 * Base class for any broadcast receiver.
 */
public abstract class BaseReceiver extends BroadcastReceiver {

	/*
	 * The activity, which stores the mActivity data, and the listener which contains all the available callbacks.
	 */
	protected Activity mActivity;
	protected BaseListener mBaseListener;

	/*
	 * Declares which callbacks are available and should be called at runtime.
	 */
	public HashMap<String, Boolean> BROADCAST_CALLBACKS_STATES = new HashMap<String, Boolean>() {
		{
			put("ON_CONNECTION", false);
			put("ON_DISCOVER_FINISH", false);
			put("ON_DISCOVER_START", false);
			put("ON_LOCAL_NAME_CHANGE", false);
			put("ON_SCAN_MODE_CHANGE", false);
			put("ON_STATE_CHANGE", false);

			put("ON_LOW_LEVEL_CONNECT", false);
			put("ON_LOW_LEVEL_DISCONNECT", false);
			put("ON_LOW_LEVEL_DISCONNECT_REQUEST", false);
			put("ON_BOND_STATE_CHANGE", false);
			put("ON_DEVICE_CLASS_CHANGE", false);
			put("ON_DEVICE_FOUND", false);
			put("ON_REMOTE_NAME_CHANGE", false);

			put("ON_CONNECTIVITY_CHANGE", false);
		}
	};

	/*
	 * Declares all the method names of the callbacks.
	 */
	public static final HashMap<String, String> BROADCAST_CALLBACKS_METHODS = new HashMap<String, String>() {
		{
			put("ON_CONNECTION", "onConnectionChange");
			put("ON_DISCOVER_FINISH", "onDiscoveryFinish");
			put("ON_DISCOVER_START", "onDiscoveryStart");
			put("ON_LOCAL_NAME_CHANGE", "onLocalNameChange");
			put("ON_SCAN_MODE_CHANGE", "onScanModeChange");
			put("ON_STATE_CHANGE", "onStateChange");

			put("ON_LOW_LEVEL_CONNECT", "onLowLevelConnect");
			put("ON_LOW_LEVEL_DISCONNECT", "onLowLevelDisconnect");
			put("ON_LOW_LEVEL_DISCONNECT_REQUEST", "onLowLevelDisconnectRequest");
			put("ON_BOND_STATE_CHANGE", "onBondStateChange");
			put("ON_DEVICE_CLASS_CHANGE", "onDeviceClassChange");
			put("ON_DEVICE_FOUND", "onDeviceFound");
			put("ON_REMOTE_NAME_CHANGE", "onRemoteNameChange");

			put("ON_CONNECTIVITY_CHANGE", "onConnectivityChange");
		}
	};

	/*
	 * Declares all values that identify an action received by the broadcast.
	 */
	public static final HashMap<String, String> BROADCAST_ACTIONS = new HashMap<String, String>() {
		{
			put("ON_CONNECTION", BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
			put("ON_DISCOVER_FINISH", BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
			put("ON_DISCOVER_START", BluetoothAdapter.ACTION_DISCOVERY_STARTED);
			put("ON_LOCAL_NAME_CHANGE", BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED);
			put("ON_SCAN_MODE_CHANGE", BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
			put("ON_STATE_CHANGE", BluetoothAdapter.ACTION_STATE_CHANGED);

			put("ON_LOW_LEVEL_CONNECT", BluetoothDevice.ACTION_ACL_CONNECTED);
			put("ON_LOW_LEVEL_DISCONNECT", BluetoothDevice.ACTION_ACL_DISCONNECTED);
			put("ON_LOW_LEVEL_DISCONNECT_REQUEST", BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
			put("ON_BOND_STATE_CHANGE", BluetoothDevice.ACTION_BOND_STATE_CHANGED);
			put("ON_DEVICE_CLASS_CHANGE", BluetoothDevice.ACTION_CLASS_CHANGED);
			put("ON_DEVICE_FOUND", BluetoothDevice.ACTION_FOUND);
			put("ON_REMOTE_NAME_CHANGE", BluetoothDevice.ACTION_NAME_CHANGED);

			put("ON_CONNECTIVITY_CHANGE", ConnectivityManager.CONNECTIVITY_ACTION);
		}
	};

	/*
	 * Constructors.
	 */
	public BaseReceiver () {}

	public BaseReceiver (Activity activity) {
		mActivity = activity;
	}

	public BaseReceiver (BaseListener listener) {
		mBaseListener = listener;
	}

	public BaseReceiver (Activity activity, BaseListener listener) {
		mActivity = activity;
		mBaseListener = listener;
	}

	/*
	 * Getters and setters.
	 */
	public void setActivity (Activity activity) {
		mActivity = activity;
	}

	public void setListener (BaseListener listener) {
		mBaseListener = listener;
	}

	public Activity getActivity () {
		return mActivity;
	}

	public BaseListener getListener () {
		return mBaseListener;
	}

	/*
	 * Register and unregister methods.
	 */
	public final void register (boolean state) {
		if (state) {
			mActivity.registerReceiver(this, this.getIntentFilter());
		} else {
			mActivity.unregisterReceiver(this);
		}
	}

	/*
	 * When an intent is received from the broadcast, check the action to get the identifier.
	 * Using the identifier check if the callback should be called (BROADCAST_CALLBACKS_STATES) and
	 * call it if true using the correct callback name (BROADCAST_CALLBACKS_METHODS).
	 */
	@Override
	public void onReceive (Context context, Intent intent) {

		String mReceivedAction = intent.getAction();
		Iterator< Map.Entry<String, String> > mActionsIterator = BaseReceiver.BROADCAST_ACTIONS.entrySet().iterator();

		Map.Entry<String, String> mPair;
		String mBroadcastAction;
		String mActionIdentifier;
		String mListenerMethod;
		Class<? extends BaseListener> mDefinitionClass;
		Method mMethod;

		while ( mActionsIterator.hasNext() ) {
			mPair = mActionsIterator.next();
			mBroadcastAction = mPair.getValue();
			mActionIdentifier = mPair.getKey();

			if (mBroadcastAction.equals(mReceivedAction) && this.BROADCAST_CALLBACKS_STATES.get(mActionIdentifier)) {
				mListenerMethod = BROADCAST_CALLBACKS_METHODS.get(mActionIdentifier);

				try {
					mDefinitionClass = mBaseListener.getClass();
					mMethod = mDefinitionClass.getMethod(mListenerMethod, Context.class, Intent.class);
					mMethod.invoke(mBaseListener, context, intent);
				} catch (NoClassDefFoundError | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
					//e.printStackTrace();
				}
			}
		}
	}

	/*
	 * MUST declare methods.
	 */
	public abstract IntentFilter getIntentFilter ();

	public abstract Object getIntentData (Intent intent);

}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).