Android Open Source - HexNanoController_Android Ipc Control Service






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;
//www.j  ava  2  s  .  c  o  m
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.support.v4.content.LocalBroadcastManager;

import com.hexairbot.hexmini.util.DebugHandler;

public class IpcControlService extends Service {

    public final static String TAG = "IpcControllService";

    public final static String COMMAD_PARM = "command";
    public final static int COMMAND_CONNECT = 1000;
    public final static int COMMAND_DISCONNET = 2000;
    public final static int COMMAND_PAUSE = 3000;
    public final static int COMMAND_RESUME = 4000;

    private Binder localBinder = new LocalBinder();
    private ConnectStateManager mConnectStateManager = null;
    // private NavData preNavData = new NavData();
    // private String [] preNavData;
    private Map<String, String> preNavData = new HashMap<String, String>();

    private Object navDataUpdateLock = new Object();
    private boolean stopNavDataUpdateThread = false;

    private WakeLock wakeLock;

    public final static String ACTION_NAVDATA_IPCREADY = "action_navdata_onipcready";
    public final static String ACTION_NAVDATA_CAMERAREADYCHANGED = "action_navdata_onCameraReadyChanged";
    public final static String ACTION_NAVDATA_RECORDREADYCHANGED = "action_navdata_onRecordReadyChangedd";
    public final static String ACTION_NAVDATA_BATTERYSTATECHANGED = "com.vmc.intent.action.action_navdata_onBatteryStateChanged";
    public final static String ACTION_NAVDATA_RECORDCHANGED = "action_navdata_onRecordChanged";

    public static final String EXTRA_STATE_IPCREADY = "extra_state_ipcready";
    public static final String EXTRA_STATE_RECORD = "extra_state_record";
    public static final String EXTRA_BATTERY_LEVEL = "extra_battery_level";
    public static final String EXTRA_STATE_RECORD_RAREADY = "extra_state_record_raready";
    public static final String EXTRA_STATE_CAMERA_READY = "extra_state_camera_ready";

    private Map<String, Intent> intentMap;
    private boolean mThreadStarted = false;

    @Override
    public void onCreate() {
  super.onCreate();

  mConnectStateManager = ConnectStateManager.getInstance(this
    .getApplication());
  mConnectStateManager.init();
  if (!DebugHandler.showServerSelect) {
      mConnectStateManager.connect("rtmp://192.168.1.1/live/stream");
//      mConnectStateManager.connect("rtmp://10.0.12.191/live/stream");
  }
  mConnectStateManager
    .addConnectChangedListener(mOnIpcConnectChangedListener);

  // Preventing device from sleep
  PowerManager service = (PowerManager) getSystemService(POWER_SERVICE);
  wakeLock = service.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK,
    "DimWakeLock");
  wakeLock.acquire();

  initIntent();
    }

    private void initIntent() {
  intentMap = new HashMap<String, Intent>();
  intentMap.put(ACTION_NAVDATA_IPCREADY, new Intent(
    ACTION_NAVDATA_IPCREADY));
  intentMap.put(ACTION_NAVDATA_BATTERYSTATECHANGED, new Intent(
    ACTION_NAVDATA_BATTERYSTATECHANGED));
  intentMap.put(ACTION_NAVDATA_CAMERAREADYCHANGED, new Intent(
    ACTION_NAVDATA_CAMERAREADYCHANGED));
  intentMap.put(ACTION_NAVDATA_RECORDCHANGED, new Intent(
    ACTION_NAVDATA_RECORDCHANGED));
  intentMap.put(ACTION_NAVDATA_RECORDREADYCHANGED, new Intent(
    ACTION_NAVDATA_RECORDREADYCHANGED));
    }

    public ConnectStateManager getConnectStateManager() {
  if (mConnectStateManager == null)
      throw new IllegalArgumentException(
        "there is no ConnectStateManager!");
  return mConnectStateManager;
    }

    @Override
    public IBinder onBind(Intent arg0) {
  // TODO Auto-generated method stub
  return localBinder;
    }

    public class LocalBinder extends Binder {
  public IpcControlService getService() {
      return IpcControlService.this;
  }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
  // TODO Auto-generated method stub
  if (intent != null) {

  }
  return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
  if (mConnectStateManager != null) {
      mConnectStateManager
        .removeConnectChangedListener(mOnIpcConnectChangedListener);
      mConnectStateManager.destroy();
  }

  if (wakeLock != null && wakeLock.isHeld()) {
      wakeLock.release();
  }

  stopNavDataUpdateThread();
  super.onDestroy();
    }

    private void stopNavDataUpdateThread() {
  stopNavDataUpdateThread = true;
  synchronized (navDataUpdateLock) {
      navDataUpdateLock.notify();
  }
  try {
      navDataUpdateThread.join(2000);
  } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  }
    }

    public static Map transStringToMap(String[] mapString) {
  // Map map = new HashMap();
  if (mapString == null)
      return null;
  java.util.StringTokenizer items;
  Map<String, String> map = new HashMap<String, String>();

  int i;
  for (i = 0; i < mapString.length; i++) {
      items = new StringTokenizer(mapString[i], ",");
      map.put(items.nextToken(),
        items.hasMoreTokens() ? ((String) (items.nextToken()))
          : null);
  }
  return map;
    }

    private Thread navDataUpdateThread = new Thread() {
  @Override
  public void run() {
      DebugHandler.logInsist(TAG, "start updateNavDataThread ------");
      while (!stopNavDataUpdateThread) {
    if (mConnectStateManager != null) {
        IpcProxy ipc = mConnectStateManager.getIpcProxy();
        ipc.doUpdateNavData();
        // NavData currentNavData = ipc.getNavData();
        Map<String, String> currentNavData = transStringToMap(ipc
          .getNavData());
        /*
         * if(currentNavData.initialized != preNavData.initialized)
         * { onIpcReady(currentNavData.initialized); }
         * if(currentNavData.batteryStatus !=
         * preNavData.batteryStatus) {
         * onBatteryStateChanged(currentNavData.batteryStatus); }
         * if(currentNavData.cameraReady != preNavData.cameraReady)
         * { onCameraReadyChanged(currentNavData.cameraReady); }
         * if(currentNavData.recordReady != preNavData.recordReady)
         * { onRecordReadyChanged(currentNavData.recordReady); }
         * if(currentNavData.recording != preNavData.recording) {
         * onRecordChanged(currentNavData.recording); }
         */
        // preNavData.copyFrom(currentNavData);
        if (currentNavData != null) {
      String cur = currentNavData.get("battery");
      String old = preNavData.get("battery");
      if (cur == null) {
          cur = "";
      }
      if (old == null) {
          old = "";
      }
      if (!cur.equals(old)) {
          onBatteryStateChanged(Integer.parseInt(cur));
      }

      preNavData = currentNavData;
        }
    }

    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
        return;
    }

    if (mConnectStateManager != null
      && mConnectStateManager.getState() == ConnectStateManager.PAUSED
      && !stopNavDataUpdateThread) {
        synchronized (navDataUpdateLock) {
      try {
          navDataUpdateLock.wait();
      } catch (InterruptedException e) {
          e.printStackTrace();
      }
        }
    }
      }
      DebugHandler.logInsist(TAG, "end updateNavDataThread ------");
  }
    };

    private void onIpcReady(boolean isReady) {
  Intent intent = intentMap.get(ACTION_NAVDATA_IPCREADY);
  intent.putExtra(EXTRA_STATE_IPCREADY, isReady);
  LocalBroadcastManager.getInstance(getApplicationContext())
    .sendBroadcast(intent);
    }

    private void onCameraReadyChanged(boolean isReady) {
  Intent intent = intentMap.get(ACTION_NAVDATA_CAMERAREADYCHANGED);
  intent.putExtra(EXTRA_STATE_CAMERA_READY, isReady);
  LocalBroadcastManager.getInstance(getApplicationContext())
    .sendBroadcast(intent);
    }

    private void onRecordReadyChanged(boolean isReady) {
  Intent intent = intentMap.get(ACTION_NAVDATA_RECORDREADYCHANGED);
  intent.putExtra(EXTRA_STATE_RECORD_RAREADY, isReady);
  LocalBroadcastManager.getInstance(getApplicationContext())
    .sendBroadcast(intent);
    }

    private void onBatteryStateChanged(int level) {
  Intent intent = intentMap.get(ACTION_NAVDATA_BATTERYSTATECHANGED);
  intent.putExtra(EXTRA_BATTERY_LEVEL, level);
  LocalBroadcastManager.getInstance(getApplicationContext())
    .sendBroadcast(intent);
    }

    private void onRecordChanged(boolean isProgress) {
  Intent intent = intentMap.get(ACTION_NAVDATA_RECORDCHANGED);
  intent.putExtra(EXTRA_STATE_RECORD, isProgress);
  LocalBroadcastManager.getInstance(getApplicationContext())
    .sendBroadcast(intent);
    }

    private OnIpcConnectChangedListener mOnIpcConnectChangedListener = new OnIpcConnectChangedListener() {

  @Override
  public void OnIpcConnected() {
      if (!mThreadStarted) {
    navDataUpdateThread.start();
    mThreadStarted = true;
      }
  }

  @Override
  public void OnIpcDisConnected() {
      synchronized (navDataUpdateLock) {
    navDataUpdateLock.notify();
      }
  }

  @Override
  public void onIpcPaused() {
      // TODO Auto-generated method stub
      if (wakeLock != null && wakeLock.isHeld()) {
    wakeLock.release();
      }
  }

  @Override
  public void onIpcResumed() {
      // TODO Auto-generated method stub
      synchronized (navDataUpdateLock) {
    navDataUpdateLock.notify();
      }

      if (wakeLock != null && !wakeLock.isHeld()) {
    wakeLock.acquire();
      }
  }

    };
}




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