Android Open Source - miner Cmd Factory






From Project

Back to project page miner.

License

The source code is released under:

Apache License

If you think the Android project miner 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.kolomiyets.miner.bt.protocol;
// www.j  a v a  2  s  . co  m
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class CmdFactory {
  
  public static final String CMD_TERMINATION = "&#cmd;";
  public static final String KEY_NAME = "name";
  public static final String KEY_CODE = "code";
  public static final String KEY_MINER = "miner";
  public static final String KEY_SAPPER = "sapper";
  public static final String KEY_STATE = "state";
  public static final String KEY_FIELD_MATRIX = "field_matrix";
  
  public static CmdBase getCmd(JSONObject jCmd){
    CmdBase cmd = null;
    try {
      ECmdName name = ECmdName.fromString(jCmd.getString(KEY_NAME));
      switch (name) {
      case GAME_STATE:
        cmd = new CmdGameState(
            EGameState.fromInteger(jCmd.getInt(KEY_STATE)));
        break;
      case GAME_TEAM:
        cmd = new CmdGameTeam(
            jCmd.getInt(KEY_MINER),
            jCmd.getInt(KEY_SAPPER));
        break;
      case FIELD_STATE:
        JSONArray jArr = jCmd.getJSONArray(KEY_FIELD_MATRIX);
        ArrayList<Integer> arr = new ArrayList<Integer>(jArr.length());
        for(int i=0; i<jArr.length(); i++){
          arr.add(jArr.getInt(i));
        }
        cmd = new CmdFieldState(arr);
        break;
      case PING:
        cmd = new CmdPing();
        break;
      case RESPONSE:
        cmd = new CmdResponse(
            jCmd.getInt(KEY_CODE));
        break;
      default:
        cmd = new CmdBase(ECmdName.UNKNOWN){};
        break;
      }
    } catch (Exception e) {
      e.printStackTrace();
      cmd = new CmdBase(ECmdName.UNKNOWN){};
    }
    return cmd;
  }
  
  public static CmdBase getCmd(String sCmd){
    try {
      JSONObject jCmd = new JSONObject(sCmd);
      return getCmd(jCmd);
    } catch (JSONException e) {
      e.printStackTrace();
    }    
    return getCmd((JSONObject)null);
  }
  
  public static JSONObject toJson(CmdBase cmd) {
    JSONObject json = new JSONObject();
    try {
      json.put(KEY_NAME, cmd.name.toString());
      switch (cmd.name) {
      case GAME_STATE:
        json.put(KEY_STATE, ((CmdGameState)cmd).state.toInteger());
        break;
      case PING:
        break;
      case RESPONSE:
        json.put(KEY_CODE, ((CmdResponse)cmd).code);
        break;
      case GAME_TEAM:
        json.put(KEY_MINER, ((CmdGameTeam)cmd).miner);
        json.put(KEY_SAPPER, ((CmdGameTeam)cmd).sapper);
        break;
      case FIELD_STATE:
        JSONArray jArr = new JSONArray(
            ((CmdFieldState)cmd).stateMatrix);
        json.put(KEY_FIELD_MATRIX, jArr);
      default:
        break;
      }
    } catch (JSONException e) {
      e.printStackTrace();
    }
    return json;
  }
  
  public static String toString(CmdBase cmd){
    return toJson(cmd)+CMD_TERMINATION;
  }
}




Java Source Code List

com.kolomiyets.miner.MinerActivity.java
com.kolomiyets.miner.Miner.java
com.kolomiyets.miner.bt.BtStateMonitor.java
com.kolomiyets.miner.bt.ConnectionBase.java
com.kolomiyets.miner.bt.ConnectionManager.java
com.kolomiyets.miner.bt.ConnectionMaster.java
com.kolomiyets.miner.bt.ConnectionProcessor.java
com.kolomiyets.miner.bt.ConnectionSlave.java
com.kolomiyets.miner.bt.EConnectMethod.java
com.kolomiyets.miner.bt.IBtEnableCallback.java
com.kolomiyets.miner.bt.IRequestBtResult.java
com.kolomiyets.miner.bt.notification.BtListener.java
com.kolomiyets.miner.bt.notification.BtNotificationCmd.java
com.kolomiyets.miner.bt.notification.BtNotificationConnect.java
com.kolomiyets.miner.bt.notification.BtNotificationDiscoveryDevice.java
com.kolomiyets.miner.bt.notification.BtNotificationDiscoveryState.java
com.kolomiyets.miner.bt.notification.BtNotificationDiscovery.java
com.kolomiyets.miner.bt.notification.BtNotificationHandshake.java
com.kolomiyets.miner.bt.notification.BtNotificationPower.java
com.kolomiyets.miner.bt.notification.BtNotificationState.java
com.kolomiyets.miner.bt.notification.BtNotification.java
com.kolomiyets.miner.bt.notification.EConnectionSate.java
com.kolomiyets.miner.bt.notification.NotificationManager.java
com.kolomiyets.miner.bt.protocol.CmdBase.java
com.kolomiyets.miner.bt.protocol.CmdFactory.java
com.kolomiyets.miner.bt.protocol.CmdFieldState.java
com.kolomiyets.miner.bt.protocol.CmdGameState.java
com.kolomiyets.miner.bt.protocol.CmdGameTeam.java
com.kolomiyets.miner.bt.protocol.CmdHandshake.java
com.kolomiyets.miner.bt.protocol.CmdPing.java
com.kolomiyets.miner.bt.protocol.CmdResponse.java
com.kolomiyets.miner.bt.protocol.ECmdName.java
com.kolomiyets.miner.bt.protocol.EGameState.java
com.kolomiyets.miner.dialog.DialogProvider.java
com.kolomiyets.miner.dialog.EDialogType.java
com.kolomiyets.miner.dialog.IDialogResult.java
com.kolomiyets.miner.screen.PlayGroundBase.java
com.kolomiyets.miner.screen.PlayGroundMiner.java
com.kolomiyets.miner.screen.PlayGroundSapper.java
com.kolomiyets.miner.screen.ScreenBase.java
com.kolomiyets.miner.screen.ScreenConnectingGame.java
com.kolomiyets.miner.screen.ScreenCreateGame.java
com.kolomiyets.miner.screen.ScreenDevices.java
com.kolomiyets.miner.screen.ScreenManager.java
com.kolomiyets.miner.screen.ScreenSelectPlayer.java
com.kolomiyets.miner.view.GridCell.java