Skip to content
This repository has been archived by the owner on Sep 21, 2021. It is now read-only.

Commit

Permalink
feat(policy): validate methods improve pattern
Browse files Browse the repository at this point in the history
Signed-off-by: Rafa Hernandez <rhernandez@teclib.com>
  • Loading branch information
rafaelje authored and ajsb85 committed May 18, 2018
1 parent d34c029 commit 35fb12d
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 101 deletions.
40 changes: 4 additions & 36 deletions app/src/main/java/org/flyve/mdm/agent/data/PoliciesDataNew.java
Expand Up @@ -42,16 +42,16 @@ public PoliciesDataNew(Context context) {
dataBase = AppDataBase.getAppDatabase(context);
}

public String getStringValue(String policyName) {
public Policies getValue(String policyName) {
List<Policies> arrPolicies = dataBase.PoliciesDao().getPolicyByName(policyName);
if(!arrPolicies.isEmpty()) {
return arrPolicies.get(0).value;
return arrPolicies.get(0);
} else {
return "";
return null;
}
}

public Object setStringValue(String policyName, String value, int priority) {
public Object setValue(String policyName, String value, int priority) {
if(dataBase.PoliciesDao().getPolicyBy(policyName, priority).isEmpty()) {
Policies policies = new Policies();
policies.policyName = policyName;
Expand All @@ -68,36 +68,4 @@ public Object setStringValue(String policyName, String value, int priority) {
Policies policies = dataBase.PoliciesDao().getPolicyByName(policyName).get(0);
return policies.value;
}

public boolean getBooleanValue(String policyName) {
String value = getStringValue(policyName);
if(value.equals("")) {
value = "false";
}
return Boolean.valueOf(value);
}

public Object setBooleanValue(String policyName, Boolean enable, int priority) {
Object data = setStringValue(policyName, String.valueOf(enable), priority);
if(data.equals("")) {
return null;
}
return data;
}

public int getIntValue(String policyName) {
String value = getStringValue(policyName);
if(value.equals("")) {
value = "0";
}
return Integer.parseInt(value);
}

public Object setIntValue(String policyName, int value, int priority) {
Object data = setStringValue(policyName, String.valueOf(value), priority);
if(data.equals("")) {
return null;
}
return data;
}
}
34 changes: 0 additions & 34 deletions app/src/main/java/org/flyve/mdm/agent/policies/BasePolicies.java

This file was deleted.

24 changes: 12 additions & 12 deletions app/src/main/java/org/flyve/mdm/agent/policies/CameraPolicy.java
Expand Up @@ -33,20 +33,20 @@

public class CameraPolicy extends Policies {

public CameraPolicy(Context context) {
super(context, "CameraPolicy");
}
private final static String POLICY_NAME = "CameraPolicy";

@Override
public void execute() {
PoliciesDeviceManager mdm = new PoliciesDeviceManager(this.context);
mdm.disableCamera(Boolean.valueOf(this.policyValue));
public CameraPolicy(Context context) {
super(context, POLICY_NAME);
}

@Override
public void execute(PolicyCallback policyCallback) {
policyCallback.onSuccess();


protected void process(PolicyCallback policyCallback) {
try {
PoliciesDeviceManager mdm = new PoliciesDeviceManager(this.context);
mdm.disableCamera(Boolean.valueOf(this.policyValue.toString()));
policyCallback.onSuccess();
} catch (Exception ex) {
policyCallback.onFail(ex.getMessage());
}
}
}
}
101 changes: 82 additions & 19 deletions app/src/main/java/org/flyve/mdm/agent/policies/Policies.java
Expand Up @@ -30,57 +30,120 @@
import android.content.Context;

import org.eclipse.paho.android.service.MqttAndroidClient;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.flyve.mdm.agent.data.PoliciesDataNew;
import org.flyve.mdm.agent.utils.FlyveLog;
import org.flyve.mdm.agent.utils.Helpers;

public abstract class Policies implements BasePolicies {
public abstract class Policies {

private static final String ERROR = "ERROR";
private static final String MQTT_SEND = "MQTT Send";

protected Context context;
private String policyName;
private PoliciesDataNew data;
private boolean enableLog;
private MqttAndroidClient MQTTclient;
protected String policyValue;
protected Context context;
protected String policyName;
protected PoliciesDataNew data;
private MqttAndroidClient mqttClient;
protected Object policyValue;
protected int policyPriority;
private boolean mqttEnable;

public Policies(Context context, String policyName) {
public Policies(Context context, String name) {
this.context = context;
this.policyName = name;
this.data = new PoliciesDataNew(context);
this.policyName = policyName;
this.policyPriority = 0;
this.enableLog = false;
org.flyve.mdm.agent.room.entity.Policies policies = data.getValue(this.policyName);
this.policyValue = policies.value;
this.policyPriority = policies.priority;
}

onStart();
public void setMqttEnable(boolean enable) {
this.mqttEnable = enable;
}

public void setPolicyName(String name) {
this.policyName = name;
storage();
}

public void setValue(String value) {
this.policyValue = value;
storage();
}

public void setPriority(int priority) {
this.policyPriority = priority;
storage();
}

public void setLog(Boolean enable) {
this.enableLog = enable;
}

public void storage() {
if(!policyName.isEmpty()) {
data.setValue(this.policyName, String.valueOf(this.policyValue), this.policyPriority);
}
}

public void mqttSendTaskStatus(String mqttTopic, String taskId, String status) {
String topic = mqttTopic + "/Status/Task/" + taskId;
byte[] encodedPayload;
try {
String payload = "{ \"status\": \"" + status + "\" }";

encodedPayload = payload.getBytes("UTF-8");
MqttMessage message = new MqttMessage(encodedPayload);
IMqttDeliveryToken token = this.mqttClient.publish(topic, message);

// send broadcast
Log(Helpers.broadCastMessage(MQTT_SEND, "Send Inventory", "ID: " + token.getMessageId()));
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());

// send broadcast
Log(Helpers.broadCastMessage(ERROR, "Error on sendKeepAlive", ex.getMessage()));
}
}

public void mqttResponse(MqttAndroidClient client, String mqttTopic, String taskId, String status) {
if(mqttEnable) {
this.setMQTTclient(client);
this.mqttSendTaskStatus(mqttTopic, taskId, status);
}
}

public void setMQTTclient(MqttAndroidClient client) {
this.MQTTclient = client;
this.mqttClient = client;
}

@Override
public void onStart() {
// store the policy on database
data.setStringValue(policyName, policyValue, policyPriority);
protected void Log(String message){
// write log file
if(enableLog) {
FlyveLog.f(message, FlyveLog.FILE_NAME_LOG);
}
}

public abstract void execute(PolicyCallback policyCallback);
private void validate() throws PoliciesException {
if(this.policyName.isEmpty()){
throw new PoliciesException("provide a name for the policy");
}

@Override
public void onFinish() {
if(this.policyValue.toString().isEmpty()){
throw new PoliciesException("provide a value for the policy");
}
}

public void execute(PolicyCallback policyCallback) throws PoliciesException {
validate();
Log("Start the policy: " + this.policyName + " value: " + this.policyValue + " priority: " + this.policyPriority);
process(policyCallback);
}

protected abstract void process(PolicyCallback policyCallback);

public interface PolicyCallback {
void onSuccess();
void onFail(String error);
Expand Down

0 comments on commit 35fb12d

Please sign in to comment.