Skip to content

Commit 4b48736

Browse files
committed
- Build bump to 1.0.9
- readConfiguration OutOfMemory catch - Updated gradle dependencies and build tools
1 parent 4b7d9cf commit 4b48736

File tree

4 files changed

+96
-58
lines changed

4 files changed

+96
-58
lines changed

app/build.gradle

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
apply plugin: 'com.android.application'
22

33
android {
4-
compileSdkVersion 25
5-
buildToolsVersion "25.0.3"
4+
compileSdkVersion 26
5+
buildToolsVersion "26.0.1"
66
defaultConfig {
77
applicationId "com.ak93.encryptedjsondatastorageexample"
88
minSdkVersion 16
9-
targetSdkVersion 25
9+
targetSdkVersion 26
1010
versionCode 1
1111
versionName "1.0"
1212
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
@@ -22,9 +22,7 @@ android {
2222
dependencies {
2323
compile fileTree(dir: 'libs', include: ['*.jar'])
2424

25-
compile 'com.android.support:appcompat-v7:25.3.1'
26-
compile 'com.android.support:design:25.3.1'
27-
28-
compile 'com.android.support:appcompat-v7:25.3.1'
25+
compile 'com.android.support:appcompat-v7:26.0.2'
26+
compile 'com.android.support:design:26.0.2'
2927
compile project(path: ':holocron')
3028
}

build.gradle

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ buildscript {
1616
allprojects {
1717
repositories {
1818
jcenter()
19+
maven {
20+
url "https://maven.google.com"
21+
}
1922
}
2023
}
2124

holocron/build.gradle

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ group='com.github.nzkozar'
55

66
android {
77
compileSdkVersion 26
8-
buildToolsVersion "25.0.3"
8+
buildToolsVersion "26.0.1"
99

1010
defaultConfig {
1111
minSdkVersion 16
1212
targetSdkVersion 26
13-
versionCode 9
14-
versionName "1.0.8"
13+
versionCode 10
14+
versionName "1.0.9"
1515

1616
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1717

@@ -30,7 +30,7 @@ ext{
3030

3131
dependencies {
3232
compile fileTree(include: ['*.jar'], dir: 'libs')
33-
compile 'com.android.support:appcompat-v7:25.3.1'
33+
compile 'com.android.support:appcompat-v7:26.0.2'
3434
compile 'com.google.code.gson:gson:2.8.0'
3535
}
3636

holocron/src/main/java/com/ak93/holocron/Holocron.java

+84-47
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ public Holocron(Context context){
4949
mGson = new Gson();
5050

5151
mForce = new Force(mContext);
52-
readConfiguration();
52+
53+
try{
54+
readConfiguration(); //Ensure we have configuration file
55+
}catch (OutOfMemoryError error){
56+
if(debug) error.printStackTrace();
57+
}
5358
}
5459

5560
/**
@@ -66,8 +71,12 @@ public Holocron(final Context context, @Nullable final HolocronInitListener call
6671
@Override
6772
public void run() {
6873
mForce = new Force(context);
69-
readConfiguration();
70-
if(callback!=null) callback.onHolocronInitComplete();
74+
try{
75+
readConfiguration(); //Ensure we have configuration file
76+
if(callback!=null) callback.onHolocronInitComplete();
77+
}catch (OutOfMemoryError error){
78+
if(debug) error.printStackTrace();
79+
}
7180
}
7281
}).start();
7382
}
@@ -85,7 +94,13 @@ public void enableDebug(boolean debug){
8594
* @param o The object to be saved
8695
*/
8796
public boolean put(Object o,long id){
88-
if(mConfiguration==null)readConfiguration(); //Ensure we have configuration file
97+
if(mConfiguration==null){
98+
try{
99+
readConfiguration(); //Ensure we have configuration file
100+
}catch (OutOfMemoryError error){
101+
return false;
102+
}
103+
}
89104
String classHash = mConfiguration.getClassHash(o.getClass());
90105
if(classHash==null){
91106
classHash = mConfiguration.addClassHash(o.getClass());
@@ -103,52 +118,62 @@ public boolean put(Object o,long id){
103118
*/
104119
public <T>List<T> getAll(Class<T> c) throws OutOfMemoryError{
105120
List<T> objects = new ArrayList<>();
106-
if(mConfiguration==null)readConfiguration(); //Ensure we have configuration file
107-
final String classHash = mConfiguration.getClassHash(c);
108-
if(classHash==null)return objects;
109-
File filesDir = mContext.getFilesDir();
110-
File[] objectFiles = filesDir.listFiles(new FilenameFilter() {
111-
@Override
112-
public boolean accept(File dir, String name) {
113-
return name.contains(classHash);
121+
if(mConfiguration==null){
122+
try{
123+
readConfiguration(); //Ensure we have configuration file
124+
}catch (OutOfMemoryError error){
125+
return objects;
114126
}
115-
});
116-
117-
//sort object files by name
118-
//Bubble sort algorithm (sort by name) //TODO upgrade to a faster sort algorithm
119-
//Log.i(TAG,"Sort started");
120-
121-
boolean sorted = false;
122-
while (!sorted) {
123-
boolean switched = false;
124-
for (int i = 0; i < (objectFiles.length - 1); i++) {
125-
File a = objectFiles[i];
126-
File b = objectFiles[i+1];
127-
128-
Long idAlong = Long.parseLong(a.getName().split("_")[1]);
129-
Long idBlong = Long.parseLong(b.getName().split("_")[1]);
130-
131-
if(idBlong<idAlong){
132-
objectFiles[i] = b;
133-
objectFiles[i+1] = a;
134-
if(debug)Log.i(TAG, "BUBLESORT@" + i + " SWITCHED: " + idAlong + " & " + idBlong);
135-
switched = true;
127+
}
128+
final String classHash = mConfiguration.getClassHash(c);
129+
if(classHash==null){
130+
return objects;
131+
}else {
132+
File filesDir = mContext.getFilesDir();
133+
File[] objectFiles = filesDir.listFiles(new FilenameFilter() {
134+
@Override
135+
public boolean accept(File dir, String name) {
136+
return name.contains(classHash);
137+
}
138+
});
139+
140+
//sort object files by name
141+
//Bubble sort algorithm (sort by name) //TODO upgrade to a faster sort algorithm
142+
//Log.i(TAG,"Sort started");
143+
144+
boolean sorted = false;
145+
while (!sorted) {
146+
boolean switched = false;
147+
for (int i = 0; i < (objectFiles.length - 1); i++) {
148+
File a = objectFiles[i];
149+
File b = objectFiles[i + 1];
150+
151+
Long idAlong = Long.parseLong(a.getName().split("_")[1]);
152+
Long idBlong = Long.parseLong(b.getName().split("_")[1]);
153+
154+
if (idBlong < idAlong) {
155+
objectFiles[i] = b;
156+
objectFiles[i + 1] = a;
157+
if (debug)
158+
Log.i(TAG, "BUBLESORT@" + i + " SWITCHED: " + idAlong + " & " + idBlong);
159+
switched = true;
160+
}
161+
}
162+
if (!switched) {
163+
sorted = true;
136164
}
137165
}
138-
if (!switched) {
139-
sorted = true;
140-
}
141-
}
142-
//Log.i(TAG,"Sorted!");
166+
//Log.i(TAG,"Sorted!");
143167

144-
for (File f : objectFiles) {
145-
if (f.isFile()) {
146-
String objectJson = readObject(f.getName());
147-
if (objectJson != null)
148-
objects.add(mGson.fromJson(objectJson, c));
168+
for (File f : objectFiles) {
169+
if (f.isFile()) {
170+
String objectJson = readObject(f.getName());
171+
if (objectJson != null)
172+
objects.add(mGson.fromJson(objectJson, c));
173+
}
149174
}
175+
return objects;
150176
}
151-
return objects;
152177
}
153178

154179
/**
@@ -213,7 +238,13 @@ public boolean remove(final Class c,final long id) {
213238
* @return true if all objects of c were removed
214239
*/
215240
public boolean removeAll(Class c){
216-
if(mConfiguration==null)readConfiguration(); //Ensure we have configuration file
241+
if(mConfiguration==null){
242+
try {
243+
readConfiguration(); //Ensure we have configuration file
244+
}catch (OutOfMemoryError error){
245+
return false;
246+
}
247+
}
217248
final String classHash = mConfiguration.getClassHash(c);
218249
if(classHash==null)return true;
219250
File filesDir = mContext.getFilesDir();
@@ -249,7 +280,13 @@ public void run() {
249280

250281
@Nullable
251282
private String getObjectFileName(Class c, long id){
252-
if(mConfiguration==null)readConfiguration();
283+
if(mConfiguration==null){
284+
try{
285+
readConfiguration(); //Ensure we have configuration file
286+
}catch (OutOfMemoryError error){
287+
return null;
288+
}
289+
}
253290
String classHash = mConfiguration.getClassHash(c);
254291
if(classHash == null)return null;
255292
return classHash + "_" + String.valueOf(id);
@@ -265,7 +302,7 @@ public Configuration getConfiguration(){
265302
/**
266303
* Retrieves a saved Holocron configuration object
267304
*/
268-
private void readConfiguration(){
305+
private void readConfiguration() throws OutOfMemoryError{
269306
File file = new File(mContext.getFilesDir().getAbsolutePath()+"/"+"cfg.j");
270307
String json;
271308
if(file.exists()) {

0 commit comments

Comments
 (0)