Back to project page Android-Apps.
The source code is released under:
Apache License
If you think the Android project Android-Apps listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.kniezrec.xbmcgear.connection; //from w w w .jav a 2 s . c o m import android.util.Log; import org.json.JSONException; import org.json.JSONObject; public final class JSONRequestFactory { public final static int REQ_PLAYER_PLAYPAUSE = 800; public final static int REQ_PLAYER_SEEK_FOR = 804; public final static int REQ_PLAYER_SEEK_BAC = 805; public final static int REQ_ACTIVE_PLAYERS = 812; public final static int REQ_GET_PROPERTIES = 813; public final static int REQ_GET_ITEMS = 815; public final static int REQ_VOLUME = 816; public final static int REQ_GET_ITEM = 818; /* * Request codes from GEAR */ private final static int REQ_GOTO_PLAYLIST = 700; private final static int REQ_PLAYER_STOP = 801; private final static int REQ_PLAYER_NEXT = 802; private final static int REQ_PLAYER_PREV = 803; private final static int REQ_MOVE_UP = 806; private final static int REQ_MOVE_DOWN = 807; private final static int REQ_MOVE_LEFT = 808; private final static int REQ_MOVE_RIGHT = 809; private final static int REQ_SELECT = 810; public final static int REQ_SHUTDOWN = 811; private final static int REQ_BACK = 814; private final static int REQ_HOME = 817; private final static int REQ_CONTEXT_MENU = 819; public final static int REQ_GET_VERSION = 820; private static final String TAG = "RequestAction"; /* * Hardcoded json requests - it's not necessary to create JSON dynamically because I need only * restricted set of options in GEAR */ public final static String GET_MAC_ADDR = "{\"jsonrpc\": \"2.0\", \"method\": \"XBMC.GetInfoLabels\", \"params\": { \"labels\": [ \"Network.MacAddress\"] }, \"id\": 1}"; private final static String PlayPause = "{\"jsonrpc\":\"2.0\",\"method\":\"Player.PlayPause\",\"id\":1,\"params\":{\"playerid\":0}}"; private final static String Stop = "{\"jsonrpc\":\"2.0\",\"method\":\"Player.Stop\",\"id\":1,\"params\":{\"playerid\":0}}"; private final static String GoToPos = "{\"jsonrpc\":\"2.0\",\"method\":\"Player.GoTo\",\"id\":1,\"params\":{\"playerid\":0}}"; private final static String GoToNext = "{\"jsonrpc\":\"2.0\",\"method\":\"Player.GoTo\",\"id\":1,\"params\":{\"playerid\":0,\"to\":\"next\"}}"; private final static String GoToPrev = "{\"jsonrpc\":\"2.0\",\"method\":\"Player.GoTo\",\"id\":1,\"params\":{\"playerid\":0,\"to\":\"previous\"}}"; private final static String SetSpeedInc = "{\"jsonrpc\":\"2.0\",\"method\":\"Player.SetSpeed\",\"id\":1,\"params\":{\"playerid\":0,\"speed\":\"increment\"}}"; private final static String SetSpeedDec = "{\"jsonrpc\":\"2.0\",\"method\":\"Player.SetSpeed\",\"id\":1,\"params\":{\"playerid\":0,\"speed\":\"decrement\"}}"; private final static String MoveUp = "{\"jsonrpc\": \"2.0\", \"method\": \"Input.Up\", \"id\": 1}"; private final static String MoveDown = "{\"jsonrpc\": \"2.0\", \"method\": \"Input.Down\", \"id\": 1}"; private final static String MoveLeft = "{\"jsonrpc\": \"2.0\", \"method\": \"Input.Left\", \"id\": 1}"; private final static String MoveRight = "{\"jsonrpc\": \"2.0\", \"method\": \"Input.Right\", \"id\": 1}"; private final static String Select = "{\"jsonrpc\": \"2.0\", \"method\": \"Input.Select\", \"id\": 1}"; private final static String ContextMenu = "{\"jsonrpc\": \"2.0\", \"method\": \"Input.ContextMenu\", \"id\": 1}"; private final static String Shutdown = "{\"jsonrpc\": \"2.0\", \"method\": \"System.Shutdown\", \"id\": 1}"; private final static String ActivePlayers = "{\"jsonrpc\": \"2.0\", \"method\": \"Player.GetActivePlayers\", \"id\": 1}"; private final static String GetProperties = "{\"jsonrpc\":\"2.0\",\"method\":\"Player.GetProperties\",\"id\":1,\"params\":{\"playerid\":0,\"properties\":[\"playlistid\",\"speed\",\"position\",\"totaltime\",\"time\"]}}"; private final static String Back = "{\"jsonrpc\": \"2.0\", \"method\": \"Input.Back\", \"id\": 1}"; private final static String GetItems = "{\"jsonrpc\":\"2.0\",\"method\":\"Playlist.GetItems\",\"id\":1,\"params\":{\"playlistid\":0,\"properties\":[\"title\",\"album\",\"artist\",\"duration\"]}}"; private final static String GoHome = "{\"jsonrpc\": \"2.0\", \"method\": \"Input.Home\", \"id\": 1}"; private final static String GetItem = "{\"jsonrpc\":\"2.0\",\"method\":\"Player.GetItem\",\"id\":1,\"params\":{\"playerid\":0,\"properties\":[\"title\",\"season\",\"episode\",\"plot\",\"runtime\",\"showtitle\"]}}"; private final static String SetVolume = "{\"jsonrpc\":\"2.0\",\"method\":\"Application.SetVolume\",\"id\":1,\"params\":{}}"; private final static String GetParams = "{\"jsonrpc\": \"2.0\", \"method\": \"Application.GetProperties\", \"params\": { \"properties\": [ \"name\", \"version\", \"volume\"] }, \"id\": 1}"; public static JSONRPCRequest processQuery(int queryRequest) { JSONObject json = null; JSONRPCRequest request = new JSONRPCRequest(); try { switch (queryRequest) { case REQ_HOME: json = new JSONObject(GoHome); break; case REQ_CONTEXT_MENU: json = new JSONObject(ContextMenu); break; case REQ_PLAYER_PLAYPAUSE: json = new JSONObject(PlayPause); break; case REQ_PLAYER_STOP: json = new JSONObject(Stop); break; case REQ_PLAYER_NEXT: json = new JSONObject(GoToNext); break; case REQ_PLAYER_PREV: json = new JSONObject(GoToPrev); break; case REQ_PLAYER_SEEK_FOR: json = new JSONObject(SetSpeedInc); break; case REQ_PLAYER_SEEK_BAC: json = new JSONObject(SetSpeedDec); break; case REQ_MOVE_UP: json = new JSONObject(MoveUp); break; case REQ_MOVE_DOWN: json = new JSONObject(MoveDown); break; case REQ_MOVE_LEFT: json = new JSONObject(MoveLeft); break; case REQ_MOVE_RIGHT: json = new JSONObject(MoveRight); break; case REQ_SELECT: json = new JSONObject(Select); break; case REQ_SHUTDOWN: json = new JSONObject(Shutdown); break; case REQ_BACK: json = new JSONObject(Back); break; case REQ_GET_VERSION: json = new JSONObject(GetParams); break; default: json = new JSONObject(); break; } } catch (JSONException e) { Log.v("RequestAction", e.getMessage()); } request.setMethod(queryRequest); request.setRequest(json); return request; } public static JSONObject getActivPlayers() throws JSONException { return new JSONObject(ActivePlayers); } public static JSONObject getProperties() throws JSONException { return new JSONObject(GetProperties); } public static JSONObject getItems() throws JSONException { return new JSONObject(GetItems); } public static JSONObject getItem() throws JSONException { return new JSONObject(GetItem); } public static JSONObject getVersion() throws JSONException { return new JSONObject(GetParams); } public static JSONRPCRequest goTo(int id) { JSONRPCRequest request = new JSONRPCRequest(); request.setMethod(REQ_GOTO_PLAYLIST); JSONObject req = null; try { req = new JSONObject(GoToPos); JSONObject params = req.optJSONObject("params"); if (params != null) { params.put("to", id); } } catch (JSONException e) { Log.v(TAG, e.getMessage()); } request.setRequest(req); return request; } public static JSONRPCRequest setVolume(int level) { JSONRPCRequest request = new JSONRPCRequest(); request.setMethod(REQ_VOLUME); JSONObject req = null; try { req = new JSONObject(SetVolume); JSONObject params = req.optJSONObject("params"); if (params != null) { params.put("volume", level); } } catch (JSONException e) { Log.v(TAG, e.getMessage()); } request.setRequest(req); return request; } }