Changed save and read behaviour to json instead of Java serializer.

This commit is contained in:
Ziver Koc 2014-08-13 14:16:16 +02:00
parent 1c985b8c2d
commit 8e582b7995
6 changed files with 69 additions and 7 deletions

9
.idea/libraries/gson_2_3.xml generated Executable file
View file

@ -0,0 +1,9 @@
<component name="libraryTable">
<library name="gson-2.3">
<CLASSES>
<root url="jar://$PROJECT_DIR$/app/libs/gson-2.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

View file

@ -59,7 +59,7 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" /> <excludeFolder url="file://$MODULE_DIR$/build/outputs" />
</content> </content>
<orderEntry type="jdk" jdkName="Android API 19 Platform" jdkType="Android SDK" /> <orderEntry type="jdk" jdkName="Android API 15 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="dom4j-1.6.1" level="project" /> <orderEntry type="library" exported="" name="dom4j-1.6.1" level="project" />
<orderEntry type="library" exported="" name="GraphView-3.1.2" level="project" /> <orderEntry type="library" exported="" name="GraphView-3.1.2" level="project" />

View file

@ -1,7 +1,7 @@
apply plugin: 'com.android.application' apply plugin: 'com.android.application'
android { android {
compileSdkVersion 19 compileSdkVersion 15
buildToolsVersion '19.1.0' buildToolsVersion '19.1.0'
defaultConfig { defaultConfig {

View file

@ -2,8 +2,12 @@ package com.ericsson.uecontrol.core;
import android.net.TrafficStats; import android.net.TrafficStats;
import com.ericsson.uecontrol.core.util.AbstractElementAdapter;
import com.ericsson.uecontrol.core.util.ThroughputCalculator; import com.ericsson.uecontrol.core.util.ThroughputCalculator;
import com.ericsson.uecontrol.gui.MainActivity; import com.ericsson.uecontrol.gui.MainActivity;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
@ -36,12 +40,17 @@ public class UeControlExecutor implements Runnable, UeBehaviour.DataHandledListe
private ThroughputCalculator downloadSpeed; private ThroughputCalculator downloadSpeed;
private ThroughputCalculator uploadSpeed; private ThroughputCalculator uploadSpeed;
private ThroughputListener throughputListener; private ThroughputListener throughputListener;
private Gson gson;
public UeControlExecutor(){ public UeControlExecutor(){
behaviours = new ArrayList<UeBehaviour>(); behaviours = new ArrayList<UeBehaviour>();
downloadSpeed = new ThroughputCalculator(); downloadSpeed = new ThroughputCalculator();
uploadSpeed = new ThroughputCalculator(); uploadSpeed = new ThroughputCalculator();
gson = new GsonBuilder()
.registerTypeAdapter(UeBehaviour.class, new AbstractElementAdapter<UeBehaviour>())
.create();
} }
public void addBehaviour(UeBehaviour b){ public void addBehaviour(UeBehaviour b){
@ -50,20 +59,32 @@ public class UeControlExecutor implements Runnable, UeBehaviour.DataHandledListe
} }
public void read(String file) throws Exception { public void read(String file) throws Exception {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(file)); FileReader in = new FileReader(file);
Type type = new TypeToken<ArrayList<UeBehaviour>>(){}.getType();
ArrayList<UeBehaviour> newBehaviours = gson.fromJson(in, type);
behaviours.addAll(newBehaviours);
in.close();
/*ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
int size = in.readInt(); int size = in.readInt();
for(int i=0; i<size; i++){ for(int i=0; i<size; i++){
addBehaviour((UeBehaviour) in.readObject()); addBehaviour((UeBehaviour) in.readObject());
} }
in.close(); in.close();*/
} }
public void save(String file) throws IOException { public void save(String file) throws IOException {
ObjectOutput out = new ObjectOutputStream(new FileOutputStream(file)); FileWriter out = new FileWriter(file);
Type type = new TypeToken<ArrayList<UeBehaviour>>(){}.getType();
log.debug("Saving behaviours: "+gson.toJson(behaviours, type));
gson.toJson(behaviours, type, out);
out.close();
/*ObjectOutput out = new ObjectOutputStream(new FileOutputStream(file));
out.writeInt(behaviours.size()); out.writeInt(behaviours.size());
for(UeBehaviour behaviour : behaviours){ for(UeBehaviour behaviour : behaviours){
out.writeObject(behaviour); out.writeObject(behaviour);
} }
out.close(); out.close();*/
} }

View file

@ -0,0 +1,32 @@
package com.ericsson.uecontrol.core.util;
import com.google.gson.*;
import java.lang.reflect.Type;
public class AbstractElementAdapter<T> implements JsonSerializer<T>, JsonDeserializer<T> {
private static final String CLASS_FIELD = "@class";
private static final String PROPERTIES_FIELD = "@properties";
@Override
public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject result = new JsonObject();
result.add(CLASS_FIELD, new JsonPrimitive(src.getClass().getName()));
result.add(PROPERTIES_FIELD, context.serialize(src, src.getClass()));
return result;
}
@Override
public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
String type = jsonObject.get(CLASS_FIELD).getAsString();
JsonElement element = jsonObject.get(PROPERTIES_FIELD);
try {
return context.deserialize(element, Class.forName(type));
} catch (ClassNotFoundException e) {
throw new JsonParseException("Unknown class: " + type, e);
}
}
}

View file

@ -33,7 +33,7 @@ import de.mindpipe.android.logging.log4j.LogConfigurator;
public class MainActivity extends FragmentActivity implements OnSharedPreferenceChangeListener{ public class MainActivity extends FragmentActivity implements OnSharedPreferenceChangeListener{
private static final Logger log = Logger.getLogger(MainActivity.class); private static final Logger log = Logger.getLogger(MainActivity.class);
public static final String DEFAULT_LOG_PATH = "/sdcard/uecontrol/"; public static final String DEFAULT_LOG_PATH = "/sdcard/uecontrol/";
public static final String BEHAVIOUR_SAVE_FILE = "behaviour_list.dat"; public static final String BEHAVIOUR_SAVE_FILE = "behaviour_list.json";
/** Fragments **/ /** Fragments **/