reworked a whole bunch of stuff

moved from assistant to one single activity model with dialogs and navigation drawer as main content holder
completed separate threaded queue based service to communicate with daemon(s)
added first few elements to the nav drawer
This commit is contained in:
Giovanni Harting
2015-09-01 05:56:48 +02:00
parent ab8b864bee
commit aaa9c5da02
51 changed files with 677 additions and 2037 deletions

View File

@@ -0,0 +1,155 @@
package com.idlegandalf.ledd.helper;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import com.idlegandalf.ledd.callbacks.AddControllerCallback;
import com.idlegandalf.ledd.callbacks.RecieveColorCallback;
import com.idlegandalf.ledd.components.AnswerTask;
import com.idlegandalf.ledd.components.HSV;
import com.idlegandalf.ledd.components.Host;
import com.idlegandalf.ledd.services.ColorService;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import hugo.weaving.DebugLog;
public class LedDHelper {
final String ACTION_SETCOLOR = "set_color";
final String ACTION_GETCOLOR = "get_color";
final String ACTION_ADDCONTROLLER = "add_controller";
final String ACTION_GETCONTROLLER = "get_controller";
final String ACTION_ADDSTRIPES = "add_stripes";
private Host host;
private Context context;
private ColorService.ColorBinder binderService;
private boolean mBound = false;
ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
System.out.println("service bound!");
binderService = (ColorService.ColorBinder) service;
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
public LedDHelper(Host host, Context appl) throws IOException {
this.host = host;
this.context = appl;
Intent intent = new Intent(appl, ColorService.class);
appl.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
/**
* Add controller to ledd daemon
*
* @param channels channel amount that this controller holds
* @param i2c_dev number of i2c device, e.g. /dev/i2c-1 would be 1
* @throws JSONException no valid json
* @throws IOException socket error
*/
public void addController(int channels, int i2c_dev, String address, final AddControllerCallback callback) throws JSONException, IOException {
JSONObject jnson = new JSONObject();
jnson.put("action", ACTION_ADDCONTROLLER);
jnson.put("channels", channels);
jnson.put("i2c_dev", i2c_dev);
jnson.put("address", address);
sendJSON(jnson, new AnswerTask() {
@Override
public void run() {
try {
if (response.getBoolean("success")) {
callback.onControllerAdded(response.getInt("cid"));
} else {
callback.onAddFailed(response.getString("message"), response.getString("message_detail"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Set color using the stripeid
*
* @param sid Stripeid
* @param color Color in HSV format
* @throws JSONException no valid json
* @throws IOException socket error
*/
public void setColor(int sid, HSV color) throws JSONException, IOException {
JSONObject jnson = new JSONObject();
JSONObject hsv = new JSONObject();
hsv.put("h", color.getHue());
hsv.put("s", color.getSaturation());
hsv.put("v", color.getValue());
jnson.put("action", ACTION_SETCOLOR);
jnson.put("sid", sid);
jnson.put("color", hsv);
sendJSON(jnson, null);
}
/**
* Get color using the stripeid
*
* @param sid Stripeid
* @throws JSONException no valid json
* @throws IOException socket error
*/
public void getColor(int sid, final RecieveColorCallback callback) throws JSONException, IOException {
JSONObject jnson = new JSONObject();
jnson.put("action", ACTION_GETCOLOR);
jnson.put("sid", sid);
sendJSON(jnson, new AnswerTask() {
@Override
public void run() {
try {
if (response.getBoolean("success")) {
JSONObject hsv = null;
hsv = response.getJSONObject("hsv");
callback.onColorRecieved(new HSV(hsv.getDouble("h"), hsv.getDouble("s"), hsv.getDouble("v")));
} else {
callback.onRecievFailed(response.getString("message"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private void sendJSON(JSONObject json, AnswerTask task) throws IOException, JSONException {
if (mBound) binderService.queueSend(host, json, task);
}
@DebugLog
public void teardown() {
if (mBound) {
context.unbindService(mConnection);
mBound = false;
}
}
}