aboutsummaryrefslogtreecommitdiff
path: root/app/src/main
diff options
context:
space:
mode:
authorSize432015-05-21 22:22:51 +0200
committerSize432015-05-21 22:22:51 +0200
commit3a7831ed8397c65852c9ed9eee33f1d592378943 (patch)
tree6e72d56cb73251417c53db621e5a79bd04a1792b /app/src/main
parentVolley; org.rssin.http; org.rssin.listener; fetching XML with listeners; Feed... (diff)
Added writeObject & readObject methods
Diffstat (limited to 'app/src/main')
-rwxr-xr-x[-rw-r--r--]app/src/main/AndroidManifest.xml2
-rwxr-xr-x[-rw-r--r--]app/src/main/java/org/rssin/android/FiltersActivity.java11
-rwxr-xr-x[-rw-r--r--]app/src/main/java/org/rssin/android/FiltersList.java14
-rwxr-xr-xapp/src/main/java/org/rssin/neurons/FeedSorter.java51
-rwxr-xr-xapp/src/main/java/org/rssin/neurons/MultiNeuralNetwork.java15
-rwxr-xr-xapp/src/main/java/org/rssin/neurons/NeuralNetwork.java16
-rwxr-xr-xapp/src/main/java/org/rssin/neurons/Neuron.java14
-rwxr-xr-xapp/src/main/java/org/rssin/neurons/SerializationTools.java69
8 files changed, 174 insertions, 18 deletions
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index e4f63c3..4e125b5 100644..100755
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -3,6 +3,8 @@
package="org.rssin.rssin" >
<uses-permission android:name="android.permission.INTERNET" />
+ <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
+ <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
diff --git a/app/src/main/java/org/rssin/android/FiltersActivity.java b/app/src/main/java/org/rssin/android/FiltersActivity.java
index b5f8760..63c667d 100644..100755
--- a/app/src/main/java/org/rssin/android/FiltersActivity.java
+++ b/app/src/main/java/org/rssin/android/FiltersActivity.java
@@ -28,6 +28,7 @@ import org.rssin.rssin.Keyword;
import org.rssin.rssin.R;
import java.io.IOException;
+import java.io.InvalidClassException;
import java.net.MalformedURLException;
import java.util.List;
@@ -67,11 +68,11 @@ public class FiltersActivity extends ActionBarActivity {
final FilterAdapter adapter = new FilterAdapter(this, R.layout.item_filter, filtersList.getFilters());
filtersView.setAdapter(adapter);
- try {
- filtersList.save();
- } catch (IOException ex) {
- Toast.makeText(this, getResources().getString(R.string.error_save_filters), Toast.LENGTH_SHORT).show();
- }
+// try {
+// filtersList.save();
+// } catch (IOException ex) {
+// Toast.makeText(this, getResources().getString(R.string.error_save_filters), Toast.LENGTH_SHORT).show();
+// }
setupListeners();
}
diff --git a/app/src/main/java/org/rssin/android/FiltersList.java b/app/src/main/java/org/rssin/android/FiltersList.java
index 608fbc7..129cc27 100644..100755
--- a/app/src/main/java/org/rssin/android/FiltersList.java
+++ b/app/src/main/java/org/rssin/android/FiltersList.java
@@ -1,8 +1,11 @@
package org.rssin.android;
import android.content.Context;
+import android.os.Debug;
import android.preference.PreferenceManager;
import android.util.Base64;
+import android.util.Log;
+import android.util.TimingLogger;
import org.rssin.rssin.Filter;
@@ -68,12 +71,23 @@ public class FiltersList {
* @throws IOException if serializing failed
*/
public synchronized void save() throws IOException {
+ long startTime = System.currentTimeMillis();
+ Log.d("FiltersList", "Start saving...");
+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(filters);
oos.close();
+
+ Log.d("FiltersList", "Serializing took " + ( System.currentTimeMillis() - startTime) + "ms");
+
String string = Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
+
+ Log.d("FiltersList", "Converting to base64 took " + ( System.currentTimeMillis() - startTime) + "ms");
+
PreferenceManager.getDefaultSharedPreferences(context).edit().putString(PREF_KEY, string).commit();
+
+ Log.d("FiltersList", "Saving took " + ( System.currentTimeMillis() - startTime) + "ms");
}
public Filter getFilterFromHashCode(int hashcode) {
diff --git a/app/src/main/java/org/rssin/neurons/FeedSorter.java b/app/src/main/java/org/rssin/neurons/FeedSorter.java
index 8549fcf..d567e63 100755
--- a/app/src/main/java/org/rssin/neurons/FeedSorter.java
+++ b/app/src/main/java/org/rssin/neurons/FeedSorter.java
@@ -1,7 +1,10 @@
package org.rssin.neurons;
+import android.util.Log;
+
import org.rssin.rss.FeedItem;
+import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
@@ -19,15 +22,45 @@ public class FeedSorter implements Serializable {
private static final long serialVersionUID = 0;
private final SentenceSplitter splitter = new SentenceSplitter();
- private final MultiNeuralNetwork nn = new MultiNeuralNetwork(25, 50);
- private final List<TrainingCase> trainingCases = new ArrayList<>();
-
- private final int[] isNthMonthInput = new int[12];
- private final int[] isNthWeekDayInput = new int[7];
- private final int isMorning, isAfternoon, isEvening, isNight, biasInput;
- private final Hashtable<String, Integer> categoryInputs = new Hashtable<>();
- private final Hashtable<String, Integer> wordInputs = new Hashtable<>();
- private final Hashtable<String, Integer> authorInputs = new Hashtable<>();
+ private MultiNeuralNetwork nn = new MultiNeuralNetwork(25, 50);
+ private List<TrainingCase> trainingCases = new ArrayList<>();
+
+ private int[] isNthMonthInput = new int[12];
+ private int[] isNthWeekDayInput = new int[7];
+ private int isMorning, isAfternoon, isEvening, isNight, biasInput;
+ private Hashtable<String, Integer> categoryInputs = new Hashtable<>();
+ private Hashtable<String, Integer> wordInputs = new Hashtable<>();
+ private Hashtable<String, Integer> authorInputs = new Hashtable<>();
+
+ private void writeObject(java.io.ObjectOutputStream stream) throws IOException {
+ stream.writeObject(nn);
+ stream.writeObject(trainingCases);
+ SerializationTools.writeArray(isNthMonthInput, stream);
+ SerializationTools.writeArray(isNthWeekDayInput, stream);
+ stream.writeInt(isMorning);
+ stream.writeInt(isAfternoon);
+ stream.writeInt(isEvening);
+ stream.writeInt(isNight);
+ stream.writeInt(biasInput);
+ stream.writeObject(categoryInputs);
+ stream.writeObject(wordInputs);
+ stream.writeObject(authorInputs);
+ }
+
+ private void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException {
+ nn = (MultiNeuralNetwork) stream.readObject();
+ trainingCases = (List<TrainingCase>) stream.readObject();
+ isNthMonthInput = SerializationTools.readArrayInt(stream);
+ isNthWeekDayInput = SerializationTools.readArrayInt(stream);
+ isMorning = stream.readInt();
+ isAfternoon = stream.readInt();
+ isEvening = stream.readInt();
+ isNight = stream.readInt();
+ biasInput = stream.readInt();
+ categoryInputs = (Hashtable<String, Integer>) stream.readObject();
+ wordInputs = (Hashtable<String, Integer>) stream.readObject();
+ authorInputs = (Hashtable<String, Integer>) stream.readObject();
+ }
public FeedSorter() {
biasInput = nn.addInput();
diff --git a/app/src/main/java/org/rssin/neurons/MultiNeuralNetwork.java b/app/src/main/java/org/rssin/neurons/MultiNeuralNetwork.java
index 03ad2d1..68ddddd 100755
--- a/app/src/main/java/org/rssin/neurons/MultiNeuralNetwork.java
+++ b/app/src/main/java/org/rssin/neurons/MultiNeuralNetwork.java
@@ -1,6 +1,11 @@
package org.rssin.neurons;
+import android.util.Log;
+
+import java.io.IOException;
import java.io.Serializable;
+import java.util.Hashtable;
+import java.util.List;
/**
* @author Jos
@@ -8,7 +13,15 @@ import java.io.Serializable;
*/
class MultiNeuralNetwork implements Serializable {
private static final long serialVersionUID = 0;
- private final NeuralNetwork[] networks;
+ private NeuralNetwork[] networks;
+
+ private void writeObject(java.io.ObjectOutputStream stream) throws IOException {
+ SerializationTools.writeArray(networks, stream);
+ }
+
+ private void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException {
+ networks = SerializationTools.readArray(stream, NeuralNetwork.class);
+ }
public MultiNeuralNetwork(int numNetworks, int numHiddenNodes) {
networks = new NeuralNetwork[numNetworks];
diff --git a/app/src/main/java/org/rssin/neurons/NeuralNetwork.java b/app/src/main/java/org/rssin/neurons/NeuralNetwork.java
index 0acfda7..0540652 100755
--- a/app/src/main/java/org/rssin/neurons/NeuralNetwork.java
+++ b/app/src/main/java/org/rssin/neurons/NeuralNetwork.java
@@ -1,7 +1,9 @@
package org.rssin.neurons;
import android.annotation.SuppressLint;
+import android.util.Log;
+import java.io.IOException;
import java.io.Serializable;
/**
@@ -9,8 +11,18 @@ import java.io.Serializable;
*/
class NeuralNetwork implements Serializable {
private static final long serialVersionUID = 0;
- private final Neuron[] hiddenNodes;
- private final Neuron outputNode;
+ private Neuron[] hiddenNodes;
+ private Neuron outputNode;
+
+ private void writeObject(java.io.ObjectOutputStream stream) throws IOException {
+ SerializationTools.writeArray(hiddenNodes, stream);
+ stream.writeObject(outputNode);
+ }
+
+ private void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException {
+ hiddenNodes = SerializationTools.readArray(stream, Neuron.class);
+ outputNode = (Neuron) stream.readObject();
+ }
NeuralNetwork(int numHiddenNodes) {
if (numHiddenNodes < 1) {
diff --git a/app/src/main/java/org/rssin/neurons/Neuron.java b/app/src/main/java/org/rssin/neurons/Neuron.java
index 23f69e1..ec4bf2c 100755
--- a/app/src/main/java/org/rssin/neurons/Neuron.java
+++ b/app/src/main/java/org/rssin/neurons/Neuron.java
@@ -1,5 +1,8 @@
package org.rssin.neurons;
+import android.util.Log;
+
+import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@@ -12,7 +15,16 @@ class Neuron implements Serializable {
private static final long serialVersionUID = 0;
private static final Random r = new Random();
- private final List<Double> weights = new ArrayList<>();
+ private List<Double> weights = new ArrayList<>();
+
+ private void writeObject(java.io.ObjectOutputStream stream) throws IOException {
+ SerializationTools.writeList(weights, stream);
+ }
+
+ private void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException {
+ weights = SerializationTools.readList(stream);
+ }
+
public Neuron() {
}
diff --git a/app/src/main/java/org/rssin/neurons/SerializationTools.java b/app/src/main/java/org/rssin/neurons/SerializationTools.java
new file mode 100755
index 0000000..41e79f2
--- /dev/null
+++ b/app/src/main/java/org/rssin/neurons/SerializationTools.java
@@ -0,0 +1,69 @@
+package org.rssin.neurons;
+
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.lang.reflect.Array;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author Jos.
+ */
+public class SerializationTools {
+ public static void writeArray(int[] array, ObjectOutputStream stream) throws IOException {
+ stream.write(array.length);
+ for(int i : array)
+ {
+ stream.write(i);
+ }
+ }
+
+ public static int[] readArrayInt(ObjectInputStream stream) throws IOException {
+ int[] array = new int[stream.readInt()];
+ for(int i = 0; i < array.length; i++)
+ {
+ array[i] = stream.readInt();
+ }
+
+ return array;
+ }
+
+ public static <T> void writeArray(T[] array, ObjectOutputStream stream) throws IOException {
+ stream.write(array.length);
+ for(T i : array)
+ {
+ stream.writeObject(i);
+ }
+ }
+
+ public static <T> T[] readArray(ObjectInputStream stream, Class<T> c) throws IOException, ClassNotFoundException {
+ T[] array = (T[]) Array.newInstance(c, stream.readInt());
+ for(int i = 0; i < array.length; i++)
+ {
+ array[i] = (T) stream.readObject();
+ }
+
+ return array;
+ }
+
+ public static <T> void writeList(List<T> list, ObjectOutputStream stream) throws IOException {
+ stream.write(list.size());
+ for(T i : list)
+ {
+ stream.writeObject(i);
+ }
+ }
+
+ public static <T> List<T> readList(ObjectInputStream stream) throws IOException, ClassNotFoundException {
+ List<T> array = new ArrayList<>();
+ int count = stream.readInt();
+
+ for(int i = 0; i < count; i++)
+ {
+ array.add((T) stream.readObject());
+ }
+
+ return array;
+ }
+}