Skip to content

Commit 09982ab

Browse files
committed
revealing assets
1 parent 3cd98b3 commit 09982ab

10 files changed

Lines changed: 147 additions & 3 deletions

File tree

app/build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ android {
1111

1212
defaultConfig {
1313
applicationId "com.efraespada.stringobfuscator"
14-
minSdkVersion 14
14+
minSdkVersion 15
1515
targetSdkVersion 28
1616
versionCode 1
1717
versionName "1.0"
@@ -57,7 +57,8 @@ dependencies {
5757
testImplementation 'junit:junit:4.12'
5858
implementation project(path: ':library')
5959
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
60-
// implementation "com.stringcare:library:$stringcare_version"
60+
implementation 'commons-io:commons-io:2.5'
61+
// implementation "com.stringcare:library:$stringcare_version"
6162
}
6263

6364

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"user": "user123",
4+
"message": "hello world"
5+
},
6+
{
7+
"user": "user456",
8+
"message": "hello there"
9+
}
10+
]

app/src/main/java/com/efraespada/stringobfuscator/MainActivity.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
import android.os.Bundle;
44
import android.support.v7.app.AppCompatActivity;
5+
import android.util.Log;
56
import android.widget.TextView;
67

78
import com.stringcare.library.SC;
89
import com.stringcare.library.SCTextView;
910
import com.stringcare.library.Version;
1011

12+
import org.json.JSONArray;
13+
import org.json.JSONObject;
14+
1115
public class MainActivity extends AppCompatActivity {
1216

1317
@Override
@@ -42,5 +46,15 @@ protected void onCreate(Bundle savedInstanceState) {
4246
boolean equals = SC.reveal(R.string.hello_world_b).equals(getString(R.string.hello_world_a));
4347
String areEquals = "Same result: " + equals;
4448
((TextView) findViewById(R.id.same_value)).setText(areEquals);
49+
50+
JSONObject jsonObject = SC.jsonObjectAsset("test.json");
51+
SC.jsonObjectAssetAsync("test.json", json -> {
52+
String value = json.toString();
53+
});
54+
55+
JSONArray jsonArray = SC.jsonArrayAsset("raw/test_array.json");
56+
SC.jsonArrayAssetAsync("raw/test_array.json", json -> {
57+
String value = json.toString();
58+
});
4559
}
4660
}

library/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ android {
1111
buildToolsVersion '28.0.3'
1212

1313
defaultConfig {
14-
minSdkVersion 14
14+
minSdkVersion 15
1515
targetSdkVersion 28
1616
versionCode 4
1717
versionName version
@@ -46,6 +46,7 @@ dependencies {
4646
exclude group: 'com.android.support', module: 'support-annotations'
4747
})
4848
implementation 'com.android.support:appcompat-v7:28.0.0'
49+
implementation 'org.jetbrains.anko:anko:0.10.8'
4950
implementation 'org.apache.commons:commons-lang3:3.9'
5051
testImplementation 'junit:junit:4.12'
5152
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.stringcare.library
2+
3+
interface AssetByteArrayListener : AssetListener {
4+
fun assetReady(byteArray: ByteArray)
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.stringcare.library
2+
3+
interface AssetListener

library/src/main/java/com/stringcare/library/CPlusLogic.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,20 @@ class CPlusLogic {
179179
return java.lang.String.format(Resources.getSystem().locale(), revealV3(context, id, androidTreatment), *formatArgs)
180180
}
181181

182+
/**
183+
* Reveals the given ByteArray
184+
* @param value
185+
* @return String
186+
*/
187+
@JvmStatic
188+
fun revealByteArray(context: Context, value: ByteArray): ByteArray {
189+
return try {
190+
SC().jniRevealV3(context, getCertificateSHA1Fingerprint(context), value)
191+
} catch (e: Exception) {
192+
value
193+
}
194+
}
195+
182196
}
183197

184198
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.stringcare.library
2+
3+
import org.json.JSONArray
4+
5+
interface JSONArrayListener : AssetListener {
6+
fun assetReady(json: JSONArray)
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.stringcare.library
2+
3+
import org.json.JSONObject
4+
5+
interface JSONObjectListener : AssetListener {
6+
fun assetReady(json: JSONObject)
7+
}

library/src/main/java/com/stringcare/library/SC.kt

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ package com.stringcare.library
33
import android.content.Context
44
import android.support.annotation.StringRes
55
import android.util.Log
6+
import org.jetbrains.anko.doAsync
7+
import org.json.JSONArray
8+
import org.json.JSONObject
9+
import java.nio.charset.Charset
610

711
/**
812
* Created by efrainespada on 02/10/2016.
@@ -290,6 +294,84 @@ class SC {
290294
}
291295
}
292296

297+
private fun assetByteArray(path: String, predicate: () -> Boolean = { true }): ByteArray {
298+
val inputStream = context.assets.openFd(path)
299+
var bytes = inputStream.createInputStream().readBytes()
300+
if (predicate()) {
301+
bytes = CPlusLogic.revealByteArray(context, bytes)
302+
}
303+
return bytes
304+
}
305+
306+
fun jsonObjectAsset(path: String, predicate: () -> Boolean): JSONObject {
307+
val bytes = assetByteArray(path, predicate)
308+
return JSONObject(String(bytes, Charset.forName("UTF-8")))
309+
}
310+
311+
fun jsonObjectAssetAsync(path: String, json: (json: JSONObject) -> Unit, predicate: () -> Boolean) {
312+
doAsync {
313+
val j = jsonObjectAsset(path, predicate)
314+
json(j)
315+
}
316+
}
317+
318+
fun jsonArrayAsset(path: String, predicate: () -> Boolean): JSONArray {
319+
val bytes = assetByteArray(path, predicate)
320+
return JSONArray(String(bytes, Charset.forName("UTF-8")))
321+
}
322+
323+
fun jsonArrayAssetAsync(path: String, json: (json: JSONArray) -> Unit, predicate: () -> Boolean) {
324+
doAsync {
325+
val j = jsonArrayAsset(path, predicate)
326+
json(j)
327+
}
328+
}
329+
330+
@JvmStatic
331+
fun jsonObjectAsset(path: String): JSONObject {
332+
return jsonObjectAsset(path) { true }
333+
}
334+
335+
@JvmStatic
336+
fun jsonObjectAsset(path: String, predicate: Boolean): JSONObject {
337+
return jsonObjectAsset(path) { predicate }
338+
}
339+
340+
@JvmStatic
341+
fun jsonObjectAssetAsync(path: String, jsonObjectListener: JSONObjectListener) {
342+
jsonObjectAssetAsync(path, jsonObjectListener, true)
343+
}
344+
345+
@JvmStatic
346+
fun jsonObjectAssetAsync(path: String, jsonObjectListener: JSONObjectListener, predicate: Boolean) {
347+
jsonObjectAssetAsync(path, { file ->
348+
jsonObjectListener.assetReady(file)
349+
}) { predicate }
350+
}
351+
352+
353+
@JvmStatic
354+
fun jsonArrayAsset(path: String): JSONArray {
355+
return jsonArrayAsset(path) { true }
356+
}
357+
358+
@JvmStatic
359+
fun jsonArrayAsset(path: String, predicate: Boolean): JSONArray {
360+
return jsonArrayAsset(path) { predicate }
361+
}
362+
363+
@JvmStatic
364+
fun jsonArrayAssetAsync(path: String, jsonArrayListener: JSONArrayListener) {
365+
jsonArrayAssetAsync(path, jsonArrayListener, true)
366+
}
367+
368+
@JvmStatic
369+
fun jsonArrayAssetAsync(path: String, jsonArrayListener: JSONArrayListener, predicate: Boolean) {
370+
jsonArrayAssetAsync(path, { file ->
371+
jsonArrayListener.assetReady(file)
372+
}) { predicate }
373+
}
374+
293375

294376
}
295377

0 commit comments

Comments
 (0)