Skip to content

Commit b80d9ec

Browse files
author
DovSnier
authored
Merge pull request #6 from DovSnier/developer
[DONE]v0.0.5
2 parents 8133ab1 + e52f9ed commit b80d9ec

29 files changed

+1489
-498
lines changed

README.md

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#### 使用Gradle构建时添加一下依赖即可:
1010
```
11-
compile 'com.dvsnier:cacheLib:0.0.4'
11+
compile 'com.dvsnier:cacheLib:0.0.5'
1212
```
1313

1414
#### 使用前配置
@@ -24,7 +24,7 @@ compile 'com.dvsnier:cacheLib:0.0.4'
2424
@Override
2525
public void onCreate() {
2626
super.onCreate();
27-
initializedCache();
27+
CacheManager.getInstance().initialize(this);
2828
...
2929
}
3030
@@ -33,18 +33,33 @@ compile 'com.dvsnier:cacheLib:0.0.4'
3333
super.onTerminate();
3434
CacheManager.getInstance().close();
3535
}
36-
37-
private void initializedCache() {
38-
File cache = null;
39-
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
40-
cache = getExternalCacheDir();
41-
} else {
42-
cache = getCacheDir();
43-
}
44-
int cacheMaxSizeOfDisk = 1024 * 1024 * 1024;
45-
CacheManager.getInstance().initialize(new ICacheConfig.Builder(this).setAppVersion(getAppVersionCode(this)).setCacheDirectory(cache).setCacheMaxSizeOfDisk(cacheMaxSizeOfDisk).create());
46-
}
4736
37+
```
38+
#### 扩展
39+
##### 第一种
40+
```
41+
// 在application的onCreate中初始化
42+
@Override
43+
public void onCreate() {
44+
super.onCreate();
45+
// 默认磁盘配置512M 缓存空间
46+
CacheManager.getInstance().initialize(this);
47+
...
48+
}
49+
```
50+
##### 第二种
51+
```
52+
// 在application的onCreate中初始化
53+
@Override
54+
public void onCreate() {
55+
super.onCreate();
56+
// 自定义磁盘1G 缓存空间
57+
int cacheMaxSizeOfDisk = 1024 * 1024 * 1024; // 1G
58+
CacheManager.getInstance().initialize(new ICacheConfig.Builder(this).setCacheMaxSizeOfDisk(cacheMaxSizeOfDisk).create());
59+
// CacheManager.getInstance().initialize(new ICacheConfig.Builder(this).setAppVersion(getAppVersionCode(this)).setCacheMaxSizeOfDisk(cacheMaxSizeOfDisk).create());
60+
...
61+
}
62+
4863
public static int getAppVersionCode(Context context) {
4964
int versionCode = 1;
5065
PackageManager pm = context.getPackageManager();

app/src/main/AndroidManifest.xml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="com.dvsnier.demo">
3+
package="com.dvsnier.demo">
4+
5+
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
6+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
47

58
<application
69
android:name=".DvsnierApplication"
@@ -13,9 +16,9 @@
1316

1417
<activity android:name=".MainActivity">
1518
<intent-filter>
16-
<action android:name="android.intent.action.MAIN"/>
19+
<action android:name="android.intent.action.MAIN" />
1720

18-
<category android:name="android.intent.category.LAUNCHER"/>
21+
<category android:name="android.intent.category.LAUNCHER" />
1922
</intent-filter>
2023
</activity>
2124
</application>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.dvsnier.demo;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* Created by dovsnier on 2018/6/12.
7+
*/
8+
public class Bean implements Serializable {
9+
10+
private static final long serialVersionUID = 8219150308909916068L;
11+
private String name;
12+
private String version;
13+
14+
public Bean() {
15+
}
16+
17+
public Bean(String name, String version) {
18+
this.name = name;
19+
this.version = version;
20+
}
21+
22+
public String getName() {
23+
return name;
24+
}
25+
26+
public void setName(String name) {
27+
this.name = name;
28+
}
29+
30+
public String getVersion() {
31+
return version;
32+
}
33+
34+
public void setVersion(String version) {
35+
this.version = version;
36+
}
37+
38+
@Override
39+
public String toString() {
40+
return "Bean{" +
41+
"name='" + name + '\'' +
42+
", version='" + version + '\'' +
43+
'}';
44+
}
45+
}

app/src/main/java/com/dvsnier/demo/DvsnierApplication.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@
44
import android.content.Context;
55
import android.content.pm.PackageInfo;
66
import android.content.pm.PackageManager;
7-
import android.os.Environment;
87

98
import com.dvsnier.cache.CacheManager;
10-
import com.dvsnier.cache.ICacheConfig;
11-
12-
import java.io.File;
139

1410
/**
1511
* Created by lizw on 2017/6/23.
@@ -20,7 +16,9 @@ public class DvsnierApplication extends Application {
2016
@Override
2117
public void onCreate() {
2218
super.onCreate();
23-
initializedCache();
19+
CacheManager.getInstance().initialize(this);
20+
// int cacheMaxSizeOfDisk = 1024 * 1024 * 1024; // 1G
21+
// CacheManager.getInstance().initialize(new ICacheConfig.Builder(this).setAppVersion(getAppVersionCode(this)).setCacheMaxSizeOfDisk(cacheMaxSizeOfDisk).create());
2422
}
2523

2624
@Override
@@ -29,17 +27,6 @@ public void onTerminate() {
2927
CacheManager.getInstance().close();
3028
}
3129

32-
private void initializedCache() {
33-
File cache = null;
34-
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
35-
cache = getExternalCacheDir();
36-
} else {
37-
cache = getCacheDir();
38-
}
39-
int cacheMaxSizeOfDisk = 1024 * 1024 * 1024;
40-
CacheManager.getInstance().initialize(new ICacheConfig.Builder(this).setAppVersion(getAppVersionCode(this)).setCacheDirectory(cache).setCacheMaxSizeOfDisk(cacheMaxSizeOfDisk).create());
41-
}
42-
4330
public static int getAppVersionCode(Context context) {
4431
int versionCode = 1;
4532
PackageManager pm = context.getPackageManager();

app/src/main/java/com/dvsnier/demo/MainActivity.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,53 @@
1010

1111
public class MainActivity extends Activity {
1212

13+
private final String key0 = "key_0";
14+
private final String key1 = "key_1";
15+
private final String key2 = "key_2";
1316
private TextView test;
17+
private TextView content;
1418

1519
@Override
1620
protected void onCreate(Bundle savedInstanceState) {
1721
super.onCreate(savedInstanceState);
1822
setContentView(R.layout.activity_main);
1923
test = (TextView) findViewById(R.id.test);
24+
content = (TextView) findViewById(R.id.content);
25+
obtainContent();
2026
test.setOnClickListener(new View.OnClickListener() {
2127
@Override
2228
public void onClick(View view) {
23-
String key = String.valueOf(System.currentTimeMillis());
24-
CacheManager.getInstance().putString(key, view.toString()).put(this.toString(), "测试数据: " + System.currentTimeMillis()).commit();
25-
Toast.makeText(MainActivity.this, "测试完成,准备关闭...", Toast.LENGTH_SHORT).show();
29+
CacheManager.getInstance().put(key0, "测试数据: " + System.currentTimeMillis())
30+
.putString(key1, "测试字符串: " + view.toString())
31+
.putObject(key2, new Bean("cache", "0.0.5"))
32+
.commit();
33+
obtainContent();
34+
Toast.makeText(MainActivity.this, "测试完成,60s 后准备关闭...", Toast.LENGTH_SHORT).show();
2635
view.postDelayed(new Runnable() {
2736
@Override
2837
public void run() {
2938
finish();
3039
}
31-
}, 1000);
40+
}, 60 * 1000);
3241
}
3342
});
3443
}
3544

3645
@Override
37-
protected void onPause() {
38-
super.onPause();
39-
// CacheManager.getInstance().onFlush(); // the deprecated
46+
protected void onResume() {
47+
super.onResume();
48+
obtainContent();
49+
}
50+
51+
protected void obtainContent() {
52+
content.postDelayed(new Runnable() {
53+
@Override
54+
public void run() {
55+
Object o0 = CacheManager.getInstance().get(key0);
56+
String o1 = CacheManager.getInstance().getString(key1);
57+
Object o2 = CacheManager.getInstance().getObject(key2);
58+
content.setText(String.format("1. value:\n\t\t\t\t%1$s\n\n2. 字符串:\n\t\t\t\t%2$s\n\n3. 对象:\n\t\t\t\t%3$s\n\n", o0, o1, o2));
59+
}
60+
}, 1000);
4061
}
4162
}
Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<LinearLayout
3-
xmlns:android="http://schemas.android.com/apk/res/android"
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
43
xmlns:tools="http://schemas.android.com/tools"
54
android:layout_width="match_parent"
65
android:layout_height="match_parent"
6+
android:background="@android:color/white"
77
android:gravity="center"
8-
android:orientation="vertical"
98
tools:context="com.dvsnier.demo.MainActivity">
109

10+
<TextView
11+
android:id="@+id/content"
12+
android:layout_width="match_parent"
13+
android:layout_height="match_parent"
14+
android:gravity="left|top"
15+
android:padding="25dp"
16+
android:textColor="@color/colorPrimary"
17+
tools:ignore="RtlHardcoded" />
18+
1119
<TextView
1220
android:id="@+id/test"
1321
android:layout_width="99dp"
1422
android:layout_height="99dp"
23+
android:layout_centerInParent="true"
24+
android:background="@color/colorAccent"
1525
android:gravity="center"
1626
android:text="测试"
27+
android:textColor="@color/colorPrimary"
1728
android:textSize="20sp"
18-
android:textStyle="bold"/>
19-
</LinearLayout>
29+
android:textStyle="bold"
30+
tools:ignore="HardcodedText" />
31+
32+
</RelativeLayout>

bintrayUpload.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
gradlew clean build publishToMavenLocal bintrayUpload -PdryRun=false > bintrayUpload.log

build.gradle

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
2+
apply from: 'dependencies.gradle'
23

34
buildscript {
45
repositories {
56
jcenter()
67
mavenCentral()
8+
google()
79
}
810
dependencies {
9-
classpath 'com.android.tools.build:gradle:2.3.3'
11+
classpath "com.android.tools.build:gradle:${build_gradle_version}"
1012

1113
// NOTE: Do not place your application dependencies here; they belong
1214
// in the individual module build.gradle files
13-
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
14-
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.4'
15+
classpath "com.novoda:bintray-release:${bintray_release_version}"
1516
}
1617
}
1718

1819
allprojects {
1920
repositories {
2021
jcenter()
22+
mavenCentral()
23+
google()
24+
}
25+
26+
tasks.withType(Javadoc) {
27+
options.addStringOption('Xdoclint:none', '-quiet')
28+
options.addStringOption('encoding', 'UTF-8')
2129
}
2230
}
2331

0 commit comments

Comments
 (0)