Skip to content

Commit 668a5c9

Browse files
committed
Initial commit
0 parents  commit 668a5c9

29 files changed

+1138
-0
lines changed

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/*
5+
.DS_Store
6+
/build
7+
/captures
8+
.externalNativeBuild

aar/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

aar/build.gradle

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
apply plugin: 'com.android.library'
2+
android {
3+
compileSdkVersion 25
4+
buildToolsVersion "25.0.2"
5+
6+
defaultConfig {
7+
minSdkVersion 19
8+
targetSdkVersion 25
9+
versionCode 1
10+
versionName "1.0"
11+
12+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13+
14+
}
15+
buildTypes {
16+
release {
17+
minifyEnabled false
18+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19+
}
20+
}
21+
}
22+
23+
dependencies {
24+
compile fileTree(dir: 'libs', include: ['*.jar'])
25+
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
26+
exclude group: 'com.android.support', module: 'support-annotations'
27+
})
28+
compile 'com.android.support:appcompat-v7:25.1.0'
29+
testCompile 'junit:junit:4.12'
30+
}

aar/proguard-rules.pro

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /Users/hongyew/Development/workspace-android/adt-bundle-mac-x86_64-20130729/sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
18+
19+
# Uncomment this to preserve the line number information for
20+
# debugging stack traces.
21+
#-keepattributes SourceFile,LineNumberTable
22+
23+
# If you keep the line number information, uncomment this to
24+
# hide the original source file name.
25+
#-renamesourcefileattribute SourceFile

aar/src/main/AndroidManifest.xml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
3+
package="org.android1liner">
4+
5+
<application android:allowBackup="true" android:label="@string/app_name"
6+
android:supportsRtl="true">
7+
8+
</application>
9+
10+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.android1liner.data;
2+
3+
/**
4+
* Created by hongyew on 26/12/2016.
5+
*/
6+
7+
public class ArrayUtils {
8+
private ArrayUtils() {}
9+
10+
public static int arraySearch(Object[] array, Object item) {
11+
if (array == null || item == null) return -1;
12+
for (int i=0;i<array.length;i++) {
13+
if (item.equals(array[i])) return i;
14+
}
15+
return -1;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package org.android1liner.data;
2+
3+
4+
import java.text.SimpleDateFormat;
5+
import java.util.Calendar;
6+
import java.util.Date;
7+
import java.util.GregorianCalendar;
8+
9+
public class DateUtils {
10+
private DateUtils() {}
11+
12+
public static boolean isToday(long timeSinceEpoc) {
13+
return isToday(new Date(timeSinceEpoc));
14+
}
15+
16+
public static boolean isToday(Date date) {
17+
Date today = dateOnly(new Date());
18+
Date compare = dateOnly(date);
19+
return (today.equals(compare));
20+
}
21+
22+
public static int year(Date date) {
23+
Calendar c = GregorianCalendar.getInstance();
24+
c.setTime(date);
25+
return c.get(Calendar.YEAR);
26+
}
27+
28+
public static int month(Date date) {
29+
Calendar c = GregorianCalendar.getInstance();
30+
c.setTime(date);
31+
return c.get(Calendar.MONTH);
32+
}
33+
34+
public static Date dateOnly(Date date) {
35+
Calendar c = GregorianCalendar.getInstance();
36+
c.setTime(date);
37+
c.set(Calendar.HOUR, 0);
38+
c.set(Calendar.MINUTE, 0);
39+
c.set(Calendar.SECOND, 0);
40+
c.set(Calendar.MILLISECOND, 0);
41+
return c.getTime();
42+
}
43+
44+
public static Date endOfDay(Date date) {
45+
Calendar c = GregorianCalendar.getInstance();
46+
c.setTime(date);
47+
c.set(Calendar.HOUR, 23);
48+
c.set(Calendar.MINUTE, 59);
49+
c.set(Calendar.SECOND, 59);
50+
c.set(Calendar.MILLISECOND, 9999);
51+
return c.getTime();
52+
}
53+
54+
public static SimpleDateFormat DDMMYYYY() {
55+
return new SimpleDateFormat("dd MMM yyyy");
56+
}
57+
58+
public static SimpleDateFormat DDMMYY_HHMM() {
59+
return new SimpleDateFormat("dd/MM/yy hh:mm a");
60+
}
61+
62+
/**
63+
* Parse a date using SimpleDateFormat. Returns null instead of exception.
64+
* @param format
65+
* @param date
66+
* @return
67+
*/
68+
public static Date safeParse(SimpleDateFormat format, String date) {
69+
try {
70+
return format.parse(date);
71+
}
72+
catch(Exception e) {
73+
return null;
74+
}
75+
}
76+
77+
public static Date smartParse(String date) {
78+
if (date == null) return null;
79+
Date d = null;
80+
d = safeParse(DDMMYY_HHMM(), date); if (d != null) return d;
81+
d = safeParse(DDMMYYYY(), date); if (d != null) return d;
82+
// TOOD: more parsing options
83+
return null;
84+
}
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.android1liner.data;
2+
3+
import java.math.BigDecimal;
4+
5+
/**
6+
* Created by hongyew on 26/12/2016.
7+
*/
8+
9+
public class FormatUtils {
10+
private FormatUtils() {}
11+
12+
public static String formatCurrency(Double value) {
13+
if (value != null) {
14+
return "$" + BigDecimal.valueOf(value).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
15+
}
16+
else {
17+
return null;
18+
}
19+
}
20+
21+
public static String formatDecimal(Double value, int decimal) {
22+
if (value != null) {
23+
return BigDecimal.valueOf(value).setScale(decimal, BigDecimal.ROUND_HALF_UP).toString();
24+
}
25+
else {
26+
return null;
27+
}
28+
}
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.android1liner.data;
2+
3+
import android.graphics.Bitmap;
4+
import android.util.Log;
5+
6+
import java.io.ByteArrayOutputStream;
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
10+
/**
11+
* Created by hongyew on 26/12/2016.
12+
*/
13+
14+
public class ImageUtils {
15+
private ImageUtils() {}
16+
private static final int BUFFER_SIZE = 16384;
17+
18+
/**
19+
* Converts bitmap to byte array.
20+
*
21+
* @param bitmap
22+
* bitmap to convert
23+
*
24+
* @return byte array of the bitmap
25+
*/
26+
public static byte[] bitmapToByteArray(Bitmap bitmap) {
27+
ByteArrayOutputStream stream = new ByteArrayOutputStream();
28+
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
29+
return stream.toByteArray();
30+
}
31+
32+
/**
33+
* Get Bytes from input stream.
34+
*
35+
* @param is
36+
* iput stream
37+
* @return input steram as bytes.
38+
*/
39+
public static byte[] getBytesFromIputStream(InputStream is) {
40+
41+
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
42+
int nRead;
43+
byte[] data = new byte[BUFFER_SIZE];
44+
45+
try {
46+
while ((nRead = is.read(data, 0, data.length)) != -1) {
47+
buffer.write(data, 0, nRead);
48+
}
49+
buffer.flush();
50+
} catch (IOException ioe) {
51+
Log.e(ImageUtils.class.getSimpleName(), "getBytesFromIputStream(): Error converting iput steram to byte array", ioe);
52+
}
53+
return buffer.toByteArray();
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.android1liner.data;
2+
3+
import android.widget.EditText;
4+
5+
import static android.text.TextUtils.isEmpty;
6+
7+
/**
8+
* Created by hongyew on 26/12/2016.
9+
*/
10+
11+
public class NullUtils {
12+
private NullUtils() {}
13+
14+
/**
15+
* If the EditText is empty or null, return null. Otherwise the text.
16+
* @param view
17+
* @return
18+
*/
19+
public static String stringOrNull(EditText view) {
20+
if (view == null) return null;
21+
String text = view.getText().toString();
22+
if (isEmpty(text)) {
23+
return null;
24+
}
25+
else {
26+
return text;
27+
}
28+
}
29+
30+
/**
31+
* If the EditText is empty or null, returns default value. Otherwise the text.
32+
* @param view
33+
* @return
34+
*/
35+
public static String stringOrDefault(EditText view, String defaultValue) {
36+
String value = stringOrNull(view);
37+
if (value == null) return defaultValue;
38+
return value;
39+
}
40+
41+
public static Double doubleOrNull(EditText view) {
42+
return isEmpty(view.getText())? null: Double.valueOf(view.getText().toString());
43+
}
44+
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.android1liner.data;
2+
3+
import android.content.Context;
4+
import android.content.res.AssetManager;
5+
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.io.InputStreamReader;
9+
import java.io.Reader;
10+
11+
/**
12+
* Created by hongyew on 26/12/2016.
13+
*/
14+
15+
public class ResourceUtils {
16+
private ResourceUtils() {}
17+
18+
public static String readAssetAsString (String filename, Context context) throws IOException {
19+
AssetManager manager = context.getAssets();
20+
InputStream file = manager.open(filename);
21+
byte[] data = new byte[file.available()];
22+
file.read(data);
23+
file.close();
24+
return new String(data);
25+
}
26+
27+
public static Reader readAssetAsReader (String filename, Context context) throws IOException {
28+
AssetManager manager = context.getAssets();
29+
return new InputStreamReader(manager.open(filename));
30+
}
31+
}

0 commit comments

Comments
 (0)