split treatments to smaller chunks before broadcast

This commit is contained in:
Milos Kozak 2017-06-30 10:23:24 +02:00
parent 39e83f0cfe
commit 1e5f96f841

View file

@ -11,6 +11,7 @@ import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import info.nightscout.androidaps.Services.Intents;
@ -36,8 +37,10 @@ public class BroadcastTreatment {
}
public static void handleNewTreatment(JSONArray treatments, Context context, boolean isDelta) {
List<JSONArray> splitted = splitArray(treatments);
for (JSONArray part: splitted) {
Bundle bundle = new Bundle();
bundle.putString("treatments", treatments.toString());
bundle.putString("treatments", part.toString());
bundle.putBoolean("delta", isDelta);
Intent intent = new Intent(Intents.ACTION_NEW_TREATMENT);
intent.putExtras(bundle);
@ -45,7 +48,8 @@ public class BroadcastTreatment {
context.sendBroadcast(intent);
List<ResolveInfo> x = context.getPackageManager().queryBroadcastReceivers(intent, 0);
log.debug("TREAT_ADD " + treatments.length() + " " + x.size() + " receivers");
log.debug("TREAT_ADD " + part.length() + " " + x.size() + " receivers");
}
}
public void handleChangedTreatment(JSONObject treatment, Context context, boolean isDelta) {
@ -60,12 +64,15 @@ public class BroadcastTreatment {
try {
log.debug("TREAT_CHANGE " + treatment.getString("_id") + " " + x.size() + " receivers");
} catch (JSONException e) {}
} catch (JSONException e) {
}
}
public static void handleChangedTreatment(JSONArray treatments, Context context, boolean isDelta) {
List<JSONArray> splitted = splitArray(treatments);
for (JSONArray part: splitted) {
Bundle bundle = new Bundle();
bundle.putString("treatments", treatments.toString());
bundle.putString("treatments", part.toString());
bundle.putBoolean("delta", isDelta);
Intent intent = new Intent(Intents.ACTION_CHANGED_TREATMENT);
intent.putExtras(bundle);
@ -73,7 +80,8 @@ public class BroadcastTreatment {
context.sendBroadcast(intent);
List<ResolveInfo> x = context.getPackageManager().queryBroadcastReceivers(intent, 0);
log.debug("TREAT_CHANGE " + treatments.length() + " " + x.size() + " receivers");
log.debug("TREAT_CHANGE " + part.length() + " " + x.size() + " receivers");
}
}
public static void handleRemovedTreatment(JSONObject treatment, Context context, boolean isDelta) {
@ -88,7 +96,8 @@ public class BroadcastTreatment {
try {
log.debug("TREAT_REMOVE " + treatment.getString("_id") + " " + x.size() + " receivers");
} catch (JSONException e) {}
} catch (JSONException e) {
}
}
public static void handleRemovedTreatment(JSONArray treatments, Context context, boolean isDelta) {
@ -104,4 +113,32 @@ public class BroadcastTreatment {
log.debug("TREAT_REMOVE " + treatments.length() + " treatments " + x.size() + " receivers");
}
private static List<JSONArray> splitArray(JSONArray array) {
List<JSONArray> ret = new ArrayList<>();
try {
int size = array.length();
int count = 0;
JSONArray newarr = null;
for (int i = 0; i < size; i++) {
if (count == 0) {
if (newarr != null) {
ret.add(newarr);
}
newarr = new JSONArray();
count = 200;
}
newarr.put(array.get(i));
--count;
}
if (newarr != null && newarr.length() > 0) {
ret.add(newarr);
}
} catch (JSONException e) {
e.printStackTrace();
ret = new ArrayList<>();
ret.add(array);
}
return ret;
}
}