Skip to content

Commit

Permalink
rx support
Browse files Browse the repository at this point in the history
  • Loading branch information
orhanobut committed Jul 15, 2015
1 parent 28db5d9 commit b5da999
Show file tree
Hide file tree
Showing 9 changed files with 348 additions and 138 deletions.
19 changes: 14 additions & 5 deletions app/src/main/java/com/orhanobut/hawksample/Foo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@
* @author Orhan Obut
*/
public class Foo {
int a = 1;
int b = 3;
String c = "asdfasdf";
int a = 1;
int b = 3;
String c = "asdfasdf";

void test() {
void test() {

}
}

@Override
public String toString() {
return "Foo{" +
"a=" + a +
", b=" + b +
", c='" + c + '\'' +
'}';
}
}
92 changes: 61 additions & 31 deletions app/src/main/java/com/orhanobut/hawksample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.orhanobut.hawk.Hawk;
import com.orhanobut.hawk.LogLevel;

import java.lang.reflect.Type;
import java.util.ArrayList;
Expand All @@ -18,6 +17,10 @@
import java.util.Map;
import java.util.Set;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


public class MainActivity extends Activity {

Expand All @@ -31,38 +34,65 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

long start = System.nanoTime();
Hawk.initWithoutEncryption(this, LogLevel.FULL);
long end = System.nanoTime();
// long start = System.nanoTime();
// Hawk.initWithoutEncryption(this, LogLevel.FULL);
// long end = System.nanoTime();
//
// double resultHawk = getTime(start, end);
// log("Hawk init with password : " + resultHawk);
//
// start = System.nanoTime();
// prefs = getSharedPreferences("BENCHARMK", MODE_PRIVATE);
// editor = prefs.edit();
// end = System.nanoTime();
// double resultPref = getTime(start, end);
// log("init: Prefs: " + resultPref + " Hawk : " + resultHawk);
//
//
// benchmarkPrimitivePut();
// benchmarkStringPut();
// benchmarkListObjectPut();
// benchmarkListStringPut();
// benchmarkObjectPut();
// benchmarkMapPut();
// benchmarkSetPut();
//
// benchmarkPrimitiveGet();
// benchmarkStringGet();
// benchmarkListObjectGet();
// benchmarkListStringGet();
// benchmarkObjectGet();
// benchmarkMapGet();
// benchmarkSetGet();
//
// benchmarkDelete();

double resultHawk = getTime(start, end);
log("Hawk init with password : " + resultHawk);
Hawk.init(this);
testRx();
}

start = System.nanoTime();
prefs = getSharedPreferences("BENCHARMK", MODE_PRIVATE);
editor = prefs.edit();
end = System.nanoTime();
double resultPref = getTime(start, end);
log("init: Prefs: " + resultPref + " Hawk : " + resultHawk);


benchmarkPrimitivePut();
benchmarkStringPut();
benchmarkListObjectPut();
benchmarkListStringPut();
benchmarkObjectPut();
benchmarkMapPut();
benchmarkSetPut();

benchmarkPrimitiveGet();
benchmarkStringGet();
benchmarkListObjectGet();
benchmarkListStringGet();
benchmarkObjectGet();
benchmarkMapGet();
benchmarkSetGet();

benchmarkDelete();
private void testRx() {
Hawk.put(KEY, new Foo());

Hawk.<Foo>getObservable(KEY)
.observeOn(Schedulers.io())
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<Foo>() {
@Override
public void onCompleted() {
Log.d("rxtest", "completed");
}

@Override
public void onError(Throwable e) {
Log.d("rxtest", "error");
}

@Override
public void onNext(Foo s) {
Log.d("rxtest", s.toString());
}
});
}

private void testHawkInitWithoutPassword() {
Expand Down
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.1'
classpath 'com.android.tools.build:gradle:1.2.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files

}
}

Expand Down
5 changes: 2 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
# org.gradle.parallel=true

# VERSION_NAME=1.10-SNAPSHOT
# VERSION_CODE=11
VERSION_NAME=1.15
VERSION_CODE=16
VERSION_NAME=1.16-SNAPSHOT
VERSION_CODE=17
GROUP=com.orhanobut

POM_DESCRIPTION=Secure, Advanced Storage for android
Expand Down
29 changes: 17 additions & 12 deletions hawk/build.gradle
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
compileSdkVersion 21
buildToolsVersion "21.1.2"

defaultConfig {
minSdkVersion 8
targetSdkVersion 21
}

defaultConfig {
minSdkVersion 8
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
lintOptions {
abortOnError false
}
}

lintOptions {
abortOnError false
}
configurations {
optional
compile.extendsFrom optional
}

dependencies {
compile 'com.google.code.gson:gson:2.3.1'
compile 'com.google.code.gson:gson:2.3.1'
optional 'io.reactivex:rxandroid:0.25.0'
}

apply from: '../maven_push.gradle'
65 changes: 65 additions & 0 deletions hawk/src/androidTest/java/com/orhanobut/hawk/HawkRxTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.orhanobut.hawk;

import android.content.Context;
import android.test.InstrumentationTestCase;
import android.util.Log;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
* @author Orhan Obut
*/
public class HawkRxTest extends InstrumentationTestCase {

Context context;

@Override
protected void setUp() throws Exception {
super.setUp();
System.setProperty(
"dexmaker.dexcache",
getInstrumentation().getTargetContext().getCacheDir().getPath());
context = getInstrumentation().getContext();
Hawk.init(context, "testPassword");
}

@Override
protected void tearDown() throws Exception {
super.tearDown();
context = null;
Hawk.clear();
Hawk.resetCrypto();
}

private static final String KEY = "TAG";

public void testString() throws Exception {
Hawk.put(KEY, "hawk");

Hawk.<String>getObservable(KEY)
.observeOn(Schedulers.io())
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<String>() {
@Override
public void onCompleted() {
Log.d("rxtest", "completed");
assertTrue(true);
}

@Override
public void onError(Throwable e) {
Log.d("rxtest", "error");
assertTrue(false);
}

@Override
public void onNext(String s) {
Log.d("rxtest", s);
assertTrue(true);
}
});
}

}
98 changes: 76 additions & 22 deletions hawk/src/main/java/com/orhanobut/hawk/Hawk.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import rx.Observable;
import rx.Subscriber;

/**
* @author Orhan Obut
*/
Expand Down Expand Up @@ -175,28 +178,39 @@ public static <T> boolean put(String key, T value) {
//if any exception occurs during encoding, encodedText will be null and thus operation is unsuccessful
return encodedText != null && storage.put(key, encodedText);
}
//
// /**
// * Saves the list of objects to the storage
// *
// * @param key is used to save the data
// * @param value is the data that will be saved
// * @return true if put is successful
// */
// @Deprecated
// public static <T> boolean put(String key, List<T> value) {
// if (key == null) {
// throw new NullPointerException("Key cannot be null");
// }
// //if the value is null, simply remove it
// if (value == null) {
// return remove(key);
// }
//
// String encodedText = zip(value);
// //if any exception occurs during encoding, encodedText will be null and thus operation is unsuccessful
// return encodedText != null && storage.put(key, encodedText);
// }

/**
* Creates a stream to put data
*
* @param <T> value type
* @return Observable<Boolean>
*/
public static <T> Observable<Boolean> putObservable(final String key, final T value) {
checkRx();
return Observable.create(new Observable.OnSubscribe<Boolean>() {
@Override
public void call(Subscriber<? super Boolean> subscriber) {
try {
boolean result = put(key, value);
if (!subscriber.isUnsubscribed()) {
subscriber.onNext(result);
subscriber.onCompleted();
}
} catch (Exception e) {
if (!subscriber.isUnsubscribed()) {
subscriber.onError(e);
}
}
}
});
}

private static void checkRx() {
if (!Utils.hasRxJavaOnClasspath()) {
throw new NullPointerException("RxJava is not on classpath, " +
"make sure that you have it in your dependencies");
}
}

/**
* Encodes the given value as full text (cipher + data info)
Expand Down Expand Up @@ -272,6 +286,46 @@ public static <T> T get(String key, T defaultValue) {
return t;
}

/**
* Creates a stream of data
*
* @param key of the data
* @param <T> type of the data
* @return Observable</T>
*/
public static <T> Observable<T> getObservable(String key) {
checkRx();
return getObservable(key, null);
}

/**
* Creates a stream of data
*
* @param key of the data
* @param defaultValue of the default value if the value doesn't exists
* @param <T> type of the data
* @return Observable</T>
*/
public static <T> Observable<T> getObservable(final String key, final T defaultValue) {
checkRx();
return Observable.create(new Observable.OnSubscribe<T>() {
@Override
public void call(Subscriber<? super T> subscriber) {
try {
T t = get(key, defaultValue);
if (!subscriber.isUnsubscribed()) {
subscriber.onNext(t);
subscriber.onCompleted();
}
} catch (Exception e) {
if (!subscriber.isUnsubscribed()) {
subscriber.onError(e);
}
}
}
});
}

/**
* Enables chaining of multiple put invocations.
*
Expand Down
Loading

0 comments on commit b5da999

Please sign in to comment.