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.
Java Source Code
package com.kniezrec.remoterecorder;
/*fromwww.java2s.com*/import android.util.Log;
import com.samsung.android.sdk.accessory.SASocket;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
/**
* Author Kamil Niezrecki
*/class MainServiceConnection extends SASocket {
privatestaticfinalint REMOTE_REC_CHANNEL_ID = 925;
privatefinal String TAG = "MAIN_SERVICE_CONNECTION";
private MainService mainService;
public MainServiceConnection() {
super(MainServiceConnection.class.getName());
}
publicvoid setMainService(MainService mainService) {
this.mainService = mainService;
}
@Override
publicvoid onError(int channelId, String errorString, int error) {
Log.e(TAG, "Connection is not alive ERROR: " + errorString + " "
+ error);
}
private String[] getAsArray(String json) {
String[] filesArray = null;
try {
JSONObject jsonObject = new JSONObject(json);
JSONArray jsonArray = jsonObject.optJSONArray("files");
if (jsonArray != null) {
finalint len = jsonArray.length();
filesArray = new String[len];
for (int i = 0; i < len; i++) {
filesArray[i] = jsonArray.get(i).toString();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return filesArray;
}
publicvoid onReceive(int channelId, byte[] data) {
// Log.d(TAG, "onReceive:" + new String(data));
Communication req = Communication.getRequestCode(data);
switch (req) {
case CANCEL:
mainService.cancelFile();
break;
case START_REC:
mainService.startRec();
break;
case DOWNLOAD_FILE:
String[] file = getAsArray(new String(data));
if (file != null && file.length > 0) {
mainService.sendFile(file[0]);
}
break;
case STOP_REC:
mainService.stopRec();
break;
case DELETE_FILE:
String arr[] = getAsArray(new String(data));
try {
String deleted = mainService.deleteRecordFile(arr);
sendToGear(deleted);
} catch (JSONException e) {
sendToGear(Request.ERROR);
}
break;
case GET_FILENAME:
try {
sendToGear(mainService.prepareJsonRequest(RequestType.Single));
} catch (JSONException e) {
sendToGear(Request.ERROR);
}
break;
case GET_FILES:
try {
sendToGear(mainService.prepareJsonRequest(RequestType.Array));
if (mainService.isRecording()) {
JSONObject json = new JSONObject();
json.put("type", Request.REC_IN_PROG);
json.put("time", mainService.gerCurrentTime());
sendToGear(json.toString());
}
} catch (JSONException e) {
sendToGear(Request.ERROR);
}
break;
case CHANGE_TIME:
try {
JSONObject json = new JSONObject(new String(data));
String time = json.optString("value");
time = time.replace("min", "").trim();
//convert min to seconds:
int min = Integer.valueOf(time);
mainService.setMaxDuration(min * 60);
} catch (JSONException e) {
Log.v(TAG, e.getMessage());
} catch (NumberFormatException e) {
Log.v(TAG, e.getMessage());
}
break;
default:
break;
}
}
publicvoid sendToGear(String data) {
final String resp = data;
new Thread(new Runnable() {
publicvoid run() {
try {
send(REMOTE_REC_CHANNEL_ID,
resp.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
@Override
protectedvoid onServiceConnectionLost(int errorCode) {
Log.e(TAG, "onServiceConectionLost for peer with error code ="
+ errorCode);
if (errorCode == CONNECTION_LOST_PEER_DISCONNECTED) {
mainService.stopRec();
}
}
}