Android Open Source - nxt-remote-controller Base Receiver






From Project

Back to project page nxt-remote-controller.

License

The source code is released under:

MIT License

If you think the Android project nxt-remote-controller listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *  Copyright (c) 2014 EgaTuts & Esa Garca - All Rights Reserved                 *
 *                                                                                 *
 *  Open-source code licensed under the MIT License (the "License").               *
 *                                                                                 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy   *
 *  of this software and associated documentation files (the "Software"), to deal  *
 *  in the Software without restriction, including without limitation the rights   *
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell      *
 *  copies of the Software, and to permit persons to whom the Software is          *
 *  furnished to do so, subject to the following conditions:                       *
 *                                                                                 *
 *  The above copyright notice and this permission notice shall be included in     *
 *  all copies or substantial portions of the Software.                            *
 *                                                                                 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR     *
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,       *
 *  FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE    *
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER         *
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  *
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN      *
 *  THE SOFTWARE.                                                                  *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*w ww. j a va 2 s  .c  o  m*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *  You can find the entire project at:                                                                                                                        *
 *                                                                                                                                                             *
 *    https://github.com/Egatuts/nxt-remote-controller                                                                                                         *
 *                                                                                                                                                             *
 *  And the corresponding file at:                                                                                                                             *
 *                                                                                                                                                             *
 *    https://github.com/Egatuts/nxt-remote-controller/blob/master/Android%20App/app/src/main/java/git/egatuts/nxtremotecontroller/receiver/BaseReceiver.java  *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package git.egatuts.nxtremotecontroller.receiver;

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.os.Build;

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

import git.egatuts.nxtremotecontroller.activity.BaseActivity;
import git.egatuts.nxtremotecontroller.activity.MainActivity;
import git.egatuts.nxtremotecontroller.fragment.BaseFragment;
import git.egatuts.nxtremotecontroller.listener.BaseListener;

/*
 *  Base class to extend BroadcastReceiver using BaseListener.
 */
public abstract class BaseReceiver extends BroadcastReceiver {

  protected BaseActivity activity;
  protected BaseListener listener;

  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_ACTIVITY_RESTART",             false);
    }
  };

  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_ACTIVITY_RESTART",             "onAppNeedsRestart");
    }
  };

  public static final HashMap<String, String> BROADCAST_ACTIONS = new HashMap<String, String>() {
    {
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        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_ACTIVITY_RESTART",             MainActivity.ACTION_RESTART_APP);
    }
  };

  /*
   *  Constructors. Saves the Context and the listener.
   */
  public BaseReceiver (BaseActivity activity) {
    this.activity = activity;
  }

  public BaseReceiver (BaseFragment fragment) {
    this.activity = fragment.getBaseActivity();
  }

  public BaseReceiver (Context context) {
    this.activity = (BaseActivity) context;
  }

  public BaseReceiver (BaseActivity activity, BaseListener listener) {
    this.activity = activity;
    this.listener = listener;
  }

  public BaseReceiver (BaseFragment fragment, BaseListener listener) {
    this.activity = fragment.getBaseActivity();
    this.listener = listener;
  }

  public BaseReceiver (Context context, BaseListener listener) {
    this.activity = (BaseActivity) context;
    this.listener = listener;
  }

  /*
   *  Registers the BroadcastReceiver.
   */
  public void registerReceiver () {
    this.activity.registerReceiver(this, this.getIntentFilter());
  }

  /*
   *  Unregisters the BroadcastReceiver.
   */
  public void unregisterReceiver () {
    this.activity.unregisterReceiver(this);
  }

  /*
   *  Getter and setter for the listener.
   */
  public void setListener (BaseListener listener) {
    this.listener = listener;
  }

  public BaseListener getListener () {
    return this.listener;
  }

  /*
   *  Inherited abstract method from BroadcastReceiver class.
   *  Whenever it receives a new Intent it does a loop for each BaseReceiver#BROADCAST_ACTIONS.
   *  When the received action equals a registered action from the previous loop checks if it's enabled in BaseReceiver#BROADCAST_CALLBACK_STATES.
   *  If that's true it uses Java Reflection to invoke the method of the listener corresponding to the received action declared in BaseReceiver#BROADCAST_CALLBACK_METHODS.
   */
  @Override
  public void onReceive (Context context, Intent intent) {

    /*
     *  We create some variables.
     */
    String action = intent.getAction();
    Iterator< Map.Entry<String, String> > actionsIterator = BaseReceiver.BROADCAST_ACTIONS.entrySet().iterator();
    Map.Entry<String, String> pair;
    String realAction;
    String idAction;
    String listenerMethod;
    Class<? extends BaseListener> defClass;
    Method method;

    /*
     *  We do a loop for each possible action and compare to the received one.
     */
    while (actionsIterator.hasNext()) {
      pair = actionsIterator.next();
      realAction = pair.getValue();
      idAction = pair.getKey();

      /*
       *  If the received action corresponds to the onw of the loop and the one of the loop
       *  is enabled in BROADCAST_CALLBACK_STATES we process it and execute the
       *  corresponding listener method.
       */
      if (realAction.equals(action) && this.BROADCAST_CALLBACKS_STATES.get(idAction)) {
        listenerMethod = BROADCAST_CALLBACKS_METHODS.get(idAction);

        /*
         *  Using Java Reflection we try to execute that listener method.
         */
        try {

          /*
           *  Remember that the listener must extends our BaseListener.
           */
          defClass = this.listener.getClass();
          method = defClass.getMethod(listenerMethod, new Class[] { Context.class, Intent.class });
          method.invoke(this.listener, context, intent);
        } catch (NoClassDefFoundError | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
          e.printStackTrace();
        }
      }
    }
  }

  /*
   *  Returns the IntentFilter used when registering the receiver.
   */
  public abstract IntentFilter getIntentFilter ();

  /*
   *  Returns the most relevant data of the intent (e.g. the bluetooth device when found a new one, etc).
   */
  public abstract Object getIntentData (Intent intent);

}




Java Source Code List

.OldPadControllerFragment.java
com.andexert.library.ApplicationTest.java
com.andexert.library.RippleView.java
com.andexert.rippleeffect.ApplicationTest.java
com.andexert.rippleeffect.CustomAdapter.java
com.andexert.rippleeffect.MainActivity.java
com.andexert.rippleeffect.OnTapListener.java
com.gc.materialdesign.utils.Utils.java
com.gc.materialdesign.views.ButtonFlat.java
com.gc.materialdesign.views.ButtonFloatSmall.java
com.gc.materialdesign.views.ButtonFloat.java
com.gc.materialdesign.views.ButtonIcon.java
com.gc.materialdesign.views.ButtonRectangle.java
com.gc.materialdesign.views.Button.java
com.gc.materialdesign.views.Card.java
com.gc.materialdesign.views.CheckBox.java
com.gc.materialdesign.views.CustomView.java
com.gc.materialdesign.views.LayoutRipple.java
com.gc.materialdesign.views.ProgressBarCircularIndeterminate.java
com.gc.materialdesign.views.ProgressBarDeterminate.java
com.gc.materialdesign.views.ProgressBarIndeterminateDeterminate.java
com.gc.materialdesign.views.ProgressBarIndeterminate.java
com.gc.materialdesign.views.RippleView.java
com.gc.materialdesign.views.ScrollView.java
com.gc.materialdesign.views.Slider.java
com.gc.materialdesign.views.Switch.java
com.gc.materialdesign.widgets.ColorSelector.java
com.gc.materialdesign.widgets.Dialog.java
com.gc.materialdesign.widgets.SnackBar.java
git.egatuts.nxtremotecontroller.ApplicationTest.java
git.egatuts.nxtremotecontroller.GlobalUtils.java
git.egatuts.nxtremotecontroller.activity.ActivityPendingTransition.java
git.egatuts.nxtremotecontroller.activity.BaseActivity.java
git.egatuts.nxtremotecontroller.activity.ControllerActivity.java
git.egatuts.nxtremotecontroller.activity.DefaultActivityPendingTransition.java
git.egatuts.nxtremotecontroller.activity.MainActivity.java
git.egatuts.nxtremotecontroller.activity.SettingsActivity.java
git.egatuts.nxtremotecontroller.bluetooth.BluetoothConstants.java
git.egatuts.nxtremotecontroller.bluetooth.BluetoothUtils.java
git.egatuts.nxtremotecontroller.bluetooth.NXTConnector.java
git.egatuts.nxtremotecontroller.device.PairedDeviceAdapter.java
git.egatuts.nxtremotecontroller.device.PairedDeviceItemClickListener.java
git.egatuts.nxtremotecontroller.device.PairedDeviceViewHolder.java
git.egatuts.nxtremotecontroller.device.PairedDevice.java
git.egatuts.nxtremotecontroller.exception.SocketCreationException.java
git.egatuts.nxtremotecontroller.fragment.ActivityBaseFragment.java
git.egatuts.nxtremotecontroller.fragment.BaseFragment.java
git.egatuts.nxtremotecontroller.fragment.BluetoothFragment.java
git.egatuts.nxtremotecontroller.fragment.ControllerBaseFragment.java
git.egatuts.nxtremotecontroller.fragment.DefaultFragmentPendingTransition.java
git.egatuts.nxtremotecontroller.fragment.FragmentPendingTransition.java
git.egatuts.nxtremotecontroller.fragment.HomeFragment.java
git.egatuts.nxtremotecontroller.fragment.LocalControllerFragment.java
git.egatuts.nxtremotecontroller.fragment.OnlineControllerFragment.java
git.egatuts.nxtremotecontroller.fragment.ScanFragment.java
git.egatuts.nxtremotecontroller.fragment.SettingsFragment.java
git.egatuts.nxtremotecontroller.listener.AnimationEndListener.java
git.egatuts.nxtremotecontroller.listener.AppKillerListener.java
git.egatuts.nxtremotecontroller.listener.BaseListener.java
git.egatuts.nxtremotecontroller.listener.BluetoothDiscoveryListener.java
git.egatuts.nxtremotecontroller.listener.BluetoothEnableListener.java
git.egatuts.nxtremotecontroller.listener.BluetoothPairingListener.java
git.egatuts.nxtremotecontroller.navigation.DrawerItemViewHolder.java
git.egatuts.nxtremotecontroller.navigation.DrawerItem.java
git.egatuts.nxtremotecontroller.navigation.NavigationDrawerAdapter.java
git.egatuts.nxtremotecontroller.navigation.NavigationDrawerCallback.java
git.egatuts.nxtremotecontroller.navigation.NavigationDrawerFragment.java
git.egatuts.nxtremotecontroller.preference.PreferencesUtils.java
git.egatuts.nxtremotecontroller.receiver.AppKillerReceiver.java
git.egatuts.nxtremotecontroller.receiver.BaseReceiver.java
git.egatuts.nxtremotecontroller.receiver.BluetoothDiscoveryReceiver.java
git.egatuts.nxtremotecontroller.receiver.BluetoothEnableReceiver.java
git.egatuts.nxtremotecontroller.receiver.BluetoothPairingReceiver.java
git.egatuts.nxtremotecontroller.thread.BaseThread.java
git.egatuts.nxtremotecontroller.thread.ConnectThread.java
git.egatuts.nxtremotecontroller.thread.ConnectedThread.java
git.egatuts.nxtremotecontroller.views.BaseIndeterminateProgressDialog.java
git.egatuts.nxtremotecontroller.views.BaseProgressDialog.java
git.egatuts.nxtremotecontroller.views.JoystickView.java
git.egatuts.nxtremotecontroller.views.LongIndeterminateProgressDialog.java
git.egatuts.nxtremotecontroller.views.ShortIndeterminateProgressDialog.java