Android Open Source - HexNanoController_Android Connect State Manager






From Project

Back to project page HexNanoController_Android.

License

The source code is released under:

Code license GNU GPL v2 http://www.gnu.org/licenses/gpl.html Content license CC BY-NC-SA 4.0 http://creativecommons.org/licenses/by-nc-sa/4.0/

If you think the Android project HexNanoController_Android 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

package com.hexairbot.hexmini.services;
/*from w  w  w. j a v  a  2  s.c  om*/
import java.util.ArrayList;

import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;

import com.hexairbot.hexmini.util.DebugHandler;

public class ConnectStateManager {

    static {
  System.loadLibrary("vmcipc");
    }

    private final static String TAG = "ConnectStateManager";

    public final static int CONNECTED = 1;
    public final static int DISCONNECTED = 0;
    public final static int PAUSED = 2;
    public final static int CONNECTING = 3;

    public final static String ACTION_CONNECT_STATE_CHANGED = "com.vmc.ipc.service.ConnectStateManager.stateChanged";
    public final static String EXTRA_STATE = "connect_state";
    public final static String EXTRA_INFO = "connect_changed_info";

    private Application appContext = null;
    private int currentState = 0;
    private ConnectStateChangedReceiver mConnectStateChangedReceiver = new ConnectStateChangedReceiver();
    private IpcProxy proxy = null;

    private ArrayList<OnIpcConnectChangedListener> connectChangedListeners = new ArrayList<OnIpcConnectChangedListener>();
    private LocalBroadcastManager mLocalBroadcastManager;
    /**
     * ??????????????????????????????????     */
    private static ConnectStateManager instance = null;

    private ConnectStateManager(Application app) {
  appContext = app;
  proxy = new IpcProxy(app);
  mLocalBroadcastManager = LocalBroadcastManager.getInstance(app);
    }

    public static ConnectStateManager getInstance(Application app) {
  if (instance == null) {
      instance = new ConnectStateManager(app);
  }
  return instance;
    }

    public void init() {
  IntentFilter filter = new IntentFilter();
  filter.addAction(ACTION_CONNECT_STATE_CHANGED);
  mLocalBroadcastManager.registerReceiver(mConnectStateChangedReceiver,
    filter);
  // proxy.connect();
    }

    public void destroy() {
  proxy.disconnect();
  mLocalBroadcastManager.unregisterReceiver(mConnectStateChangedReceiver);
    }

    public IpcProxy getIpcProxy() {
  return proxy;
    }

    private class ConnectStateChangedReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
      // TODO Auto-generated method stub
      int newState = intent.getIntExtra(EXTRA_STATE, 0);
      String info = intent.getStringExtra(EXTRA_INFO);
      setState(newState, info);
  }
    }

    public void addConnectChangedListener(OnIpcConnectChangedListener lis) {
  synchronized (connectChangedListeners) {
      connectChangedListeners.add(lis);
  }
    }

    public void removeConnectChangedListener(OnIpcConnectChangedListener lis) {
  synchronized (connectChangedListeners) {
      connectChangedListeners.remove(lis);
  }
    }

    public int getState() {
  return currentState;
    }

    public void connect(String address) {
  if (currentState == CONNECTING || currentState == CONNECTED) {
      DebugHandler.logd(TAG, "you are connect repeatedly.");
  }
  proxy.connect(address);
  currentState = CONNECTING;
    }

    public void pause() {
  setState(PAUSED, "paused by user");
  proxy.pause();
    }

    public void resume() {
  setState(CONNECTED, "resumed by user");
  proxy.resume();
    }

    public void disconnect() {
  proxy.disconnect();
    }

    private void setState(int newState, String info) {
  DebugHandler.logInsist(TAG, String.format(
    "connect state changed from %1$d to %2$d,because %3$s",
    currentState, newState, info));
  if (currentState == newState)
      return;
  else {
      if (newState == CONNECTED) {
    if (currentState == PAUSED) {
        onResumed();
    } else {
        onConnected();
    }
      } else if (newState == DISCONNECTED) {
    onDisConnected();
      } else if (newState == PAUSED) {
    onPaused();
      }
      currentState = newState;
  }
    }

    private void onConnected() {
  synchronized (connectChangedListeners) {
      if (connectChangedListeners.size() == 0)
    return;
      for (OnIpcConnectChangedListener lis : connectChangedListeners) {
    lis.OnIpcConnected();
      }
  }
    }

    private void onDisConnected() {
  synchronized (connectChangedListeners) {
      if (connectChangedListeners.size() == 0)
    return;
      for (OnIpcConnectChangedListener lis : connectChangedListeners) {
    lis.OnIpcDisConnected();
      }
  }
    }

    private void onPaused() {
  synchronized (connectChangedListeners) {
      if (connectChangedListeners.size() == 0)
    return;
      for (OnIpcConnectChangedListener lis : connectChangedListeners) {
    lis.onIpcPaused();
      }
  }
    }

    private void onResumed() {
  synchronized (connectChangedListeners) {
      if (connectChangedListeners.size() == 0)
    return;
      for (OnIpcConnectChangedListener lis : connectChangedListeners) {
    lis.onIpcResumed();
      }
  }
    }
}




Java Source Code List

.FileHelper.java
.Input.java
.Output.java
.Serializable.java
com.hexairbot.hexmini.HelpActivity.java
com.hexairbot.hexmini.HexMiniApplication.java
com.hexairbot.hexmini.HudActivity.java
com.hexairbot.hexmini.HudViewControllerDelegate.java
com.hexairbot.hexmini.HudViewController.java
com.hexairbot.hexmini.SettingsDialogDelegate.java
com.hexairbot.hexmini.SettingsDialog.java
com.hexairbot.hexmini.SettingsViewControllerDelegate.java
com.hexairbot.hexmini.SettingsViewController.java
com.hexairbot.hexmini.ViewController.java
com.hexairbot.hexmini.adapter.SettingsViewAdapter.java
com.hexairbot.hexmini.ble.BleConnectinManagerDelegate.java
com.hexairbot.hexmini.ble.BleConnectinManager.java
com.hexairbot.hexmini.ble.BluetoothLeService.java
com.hexairbot.hexmini.gestures.EnhancedGestureDetector.java
com.hexairbot.hexmini.modal.ApplicationSettings.java
com.hexairbot.hexmini.modal.Channel.java
com.hexairbot.hexmini.modal.OSDCommon.java
com.hexairbot.hexmini.modal.Transmitter.java
com.hexairbot.hexmini.sensors.DeviceOrientationChangeDelegate.java
com.hexairbot.hexmini.sensors.DeviceOrientationManager.java
com.hexairbot.hexmini.sensors.DeviceSensorManagerWrapper.java
com.hexairbot.hexmini.sensors.SensorManagerWrapper.java
com.hexairbot.hexmini.services.ConnectStateManager.java
com.hexairbot.hexmini.services.IpcControlService.java
com.hexairbot.hexmini.services.IpcProxy.java
com.hexairbot.hexmini.services.NavData.java
com.hexairbot.hexmini.services.OnIpcConnectChangedListener.java
com.hexairbot.hexmini.services.VIConfig.java
com.hexairbot.hexmini.ui.Button.java
com.hexairbot.hexmini.ui.Image.java
com.hexairbot.hexmini.ui.Indicator.java
com.hexairbot.hexmini.ui.Sprite.java
com.hexairbot.hexmini.ui.Text.java
com.hexairbot.hexmini.ui.ToggleButton.java
com.hexairbot.hexmini.ui.UIRenderer.java
com.hexairbot.hexmini.ui.control.CustomSeekBar.java
com.hexairbot.hexmini.ui.control.ViewPagerIndicator.java
com.hexairbot.hexmini.ui.gl.GLSprite.java
com.hexairbot.hexmini.ui.joystick.AcceleratorJoystick.java
com.hexairbot.hexmini.ui.joystick.AnalogueJoystick.java
com.hexairbot.hexmini.ui.joystick.JoystickBase.java
com.hexairbot.hexmini.ui.joystick.JoystickFactory.java
com.hexairbot.hexmini.ui.joystick.JoystickListener.java
com.hexairbot.hexmini.util.DebugHandler.java
com.hexairbot.hexmini.util.FontUtils.java
com.hexairbot.hexmini.util.SystemUiHiderBase.java
com.hexairbot.hexmini.util.SystemUiHiderHoneycomb.java
com.hexairbot.hexmini.util.SystemUiHider.java
com.hexairbot.hexmini.util.SystemUtil.java
com.hexairbot.hexmini.util.TextureUtils.java
fix.android.opengl.GLES20.java