Skip to content

Commit 50aaeeb

Browse files
committed
添加离线服务下载
1 parent 4f0315d commit 50aaeeb

File tree

11 files changed

+310
-79
lines changed

11 files changed

+310
-79
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.rae.cnblogs;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
11+
/**
12+
* app data cache test
13+
* Created by ChenRui on 2017/7/28 0028 22:12.
14+
*/
15+
@RunWith(AndroidJUnit4.class)
16+
public class CacheTest {
17+
18+
private Context mContext;
19+
20+
@Before
21+
public void setup() {
22+
mContext = InstrumentationRegistry.getContext();
23+
24+
}
25+
26+
@Test
27+
public void testCacheSize() {
28+
// new AppDataManager(mContext).getDatabaseTotalSize();
29+
}
30+
}

app/src/main/AndroidManifest.xml

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,36 @@
2626
android:name=".activity.LauncherActivity"
2727
android:screenOrientation="portrait"
2828
android:theme="@style/AppTheme.NoActionBar.FullScreen">
29-
<intent-filter>
30-
<action android:name="android.intent.action.MAIN"/>
29+
<!-- <intent-filter>
30+
<action android:name="android.intent.action.MAIN"/>
3131
32-
<category android:name="android.intent.category.LAUNCHER"/>
33-
</intent-filter>
32+
<category android:name="android.intent.category.LAUNCHER"/>
33+
</intent-filter>-->
3434
</activity>
3535

3636
<!--主界面-->
3737
<activity
3838
android:name=".activity.MainActivity"
3939
android:screenOrientation="portrait"
4040
android:theme="@style/AppTheme.Dark">
41-
<!-- <intent-filter>
41+
<intent-filter>
4242
<action android:name="android.intent.action.MAIN"/>
4343

4444
<category android:name="android.intent.category.LAUNCHER"/>
45-
</intent-filter>-->
45+
</intent-filter>
4646
</activity>
47-
<!-- <activity
48-
android:name=".activity.TestActivity"
49-
android:label="测试入口"
50-
android:screenOrientation="portrait"
51-
android:theme="@style/AppTheme.Dark">
52-
<intent-filter>
53-
<action android:name="android.intent.action.MAIN"/>
5447

55-
<category android:name="android.intent.category.LAUNCHER"/>
56-
</intent-filter>
57-
</activity>-->
48+
<!-- <activity
49+
android:name=".activity.TestActivity"
50+
android:label="测试入口"
51+
android:screenOrientation="portrait"
52+
android:theme="@style/AppTheme.Dark">
53+
<intent-filter>
54+
<action android:name="android.intent.action.MAIN"/>
55+
56+
<category android:name="android.intent.category.LAUNCHER"/>
57+
</intent-filter>
58+
</activity>-->
5859

5960
<!--博文-->
6061
<activity
@@ -99,6 +100,12 @@
99100
android:name=".activity.SettingActivity"
100101
android:label="@string/setting"
101102
android:launchMode="singleTop"/>
103+
104+
105+
<service
106+
android:name=".service.CnblogsService"
107+
android:label="@string/service_name"/>
108+
102109
<!--应用宝 -->
103110
<activity
104111
android:name="com.umeng.qq.tencent.AssistActivity"
@@ -165,9 +172,6 @@
165172
android:name="UMENG_CHANNEL"
166173
android:value="${UMENG_CHANNEL}"/>
167174

168-
<service
169-
android:name=".service.CnblogsService"
170-
android:label="@string/service_name"/>
171175

172176
</application>
173177

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package com.rae.cnblogs;
2+
3+
import android.content.Context;
4+
import android.os.Build;
5+
import android.os.Environment;
6+
import android.os.StatFs;
7+
8+
import java.io.File;
9+
10+
/**
11+
* 应用程序数据管理器
12+
* Created by ChenRui on 2017/7/28 0028 21:57.
13+
*/
14+
public final class AppDataManager {
15+
16+
private Context mContext;
17+
18+
public AppDataManager(Context context) {
19+
mContext = context;
20+
}
21+
22+
/**
23+
* 清除应用缓存
24+
*/
25+
public void clearCache() {
26+
// 清除文件缓存
27+
deleteDir(mContext.getExternalCacheDir());
28+
deleteDir(mContext.getCacheDir());
29+
}
30+
31+
/**
32+
* 获取SDCard总大小
33+
*
34+
* @return 单位MB
35+
*/
36+
public long getSDCardTotalSize() {
37+
File file = Environment.getExternalStorageDirectory();
38+
if (!file.exists() || !file.canRead()) return -1;
39+
StatFs stat = new StatFs(file.getPath());
40+
if (Build.VERSION.SDK_INT > 18) {
41+
return (stat.getBlockSizeLong() * stat.getBlockCountLong()) / 1048576;
42+
} else {
43+
return (stat.getBlockSize() * stat.getBlockCount()) / 1048576;
44+
}
45+
}
46+
47+
/**
48+
* 获取SDCard可用空间
49+
*
50+
* @return 单位MB
51+
*/
52+
public long getSDCardFreeSpace() {
53+
File file = Environment.getExternalStorageDirectory();
54+
if (!file.exists() || !file.canRead()) return -1;
55+
StatFs stat = new StatFs(file.getPath());
56+
if (Build.VERSION.SDK_INT > 18) {
57+
return (stat.getAvailableBlocksLong() * stat.getBlockSizeLong()) / 1048576;
58+
} else {
59+
return (stat.getAvailableBlocks() * stat.getBlockSize()) / 1048576;
60+
}
61+
}
62+
63+
/**
64+
* 获取缓存大小
65+
*
66+
* @return 返回单位MB
67+
*/
68+
public long getCacheSize() {
69+
File cacheDir = mContext.getCacheDir();
70+
File extCacheDir = mContext.getExternalCacheDir();
71+
return (getDirectorySize(cacheDir) + getDirectorySize(extCacheDir)) / 1048576;
72+
}
73+
74+
/**
75+
* 是否空间不足,条件:可用空间小于1GB
76+
*/
77+
public boolean isInsufficient() {
78+
long size = getSDCardFreeSpace();
79+
return size < 1024 && size != -1;
80+
}
81+
82+
/**
83+
* 数据库总大小
84+
*
85+
* @return 返回单位MB
86+
*/
87+
public double getDatabaseTotalSize() {
88+
File dbFile = mContext.getDatabasePath("cnblogs").getParentFile();
89+
return getDirectorySize(dbFile) / 1048576.0f; // MB
90+
}
91+
92+
93+
/**
94+
* 获取文件夹大小
95+
*
96+
* @param file 文件
97+
*/
98+
public long getDirectorySize(File file) {
99+
if (!file.exists() || !file.canRead()) {
100+
return 0;
101+
}
102+
103+
if (!file.isDirectory()) {
104+
return file.length();
105+
}
106+
107+
long size = 0;
108+
File[] files = file.listFiles();
109+
if (files == null) return 0;
110+
for (File item : files) {
111+
if (item.isDirectory()) {
112+
size += getDirectorySize(item);
113+
} else {
114+
size += item.length();
115+
}
116+
}
117+
return size;
118+
}
119+
120+
/**
121+
* 递归删除文件夹
122+
*
123+
* @param file
124+
* @return
125+
*/
126+
public void deleteDir(File file) {
127+
if (file != null && file.exists()) {
128+
try {
129+
if (file.isDirectory()) {
130+
// 删除文件夹
131+
File[] files = file.listFiles();
132+
if (files.length <= 0) {
133+
file.delete(); // 文件夹类型
134+
return;
135+
}
136+
for (File item : files) {
137+
deleteDir(item); // 递归删除子文件夹
138+
}
139+
} else {
140+
file.delete(); // 文件类型
141+
}
142+
} catch (Exception e) {
143+
e.printStackTrace();
144+
}
145+
}
146+
}
147+
148+
}

app/src/main/java/com/rae/cnblogs/CnblogsApplication.java

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
import com.tencent.tinker.loader.app.TinkerApplication;
1212
import com.tencent.tinker.loader.shareutil.ShareConstants;
1313

14-
import java.io.File;
15-
1614
/**
1715
* 集成热更新的应用程序
1816
* Created by ChenRui on 2017/7/25 0025 19:15.
@@ -21,6 +19,13 @@ public class CnblogsApplication extends TinkerApplication {
2119

2220
private static RefWatcher refWatcher;
2321

22+
23+
public static void watch(Object obj) {
24+
if (refWatcher != null) {
25+
refWatcher.watch(obj);
26+
}
27+
}
28+
2429
public CnblogsApplication() {
2530
super(ShareConstants.TINKER_ENABLE_ALL, "com.rae.cnblogs.CnblogsApplicationProxy");
2631
}
@@ -33,10 +38,6 @@ public void onCreate() {
3338
}
3439
}
3540

36-
public static RefWatcher getRefWatcher() {
37-
return refWatcher;
38-
}
39-
4041
@Override
4142
protected void attachBaseContext(Context base) {
4243
super.attachBaseContext(base);
@@ -63,37 +64,10 @@ public void clearCache() {
6364
// 清除图片缓存
6465
ImageLoader.getInstance().clearDiskCache();
6566
ImageLoader.getInstance().clearMemoryCache();
66-
67-
// 清除文件缓存
68-
deleteDir(getExternalCacheDir());
69-
deleteDir(getCacheDir());
70-
7167
// 清除数据库
7268
DbFactory.getInstance().clearCache();
73-
69+
new AppDataManager(this).clearCache();
7470
}
7571

7672

77-
private boolean deleteDir(File file) {
78-
if (file != null && file.exists()) {
79-
try {
80-
if (file.isDirectory()) {
81-
// 删除文件夹
82-
File[] files = file.listFiles();
83-
if (files.length <= 0) {
84-
return file.delete();
85-
}
86-
for (File item : files) {
87-
deleteDir(item); // 递归删除子文件
88-
}
89-
} else {
90-
return file.delete();
91-
}
92-
} catch (Exception e) {
93-
e.printStackTrace();
94-
}
95-
}
96-
97-
return false;
98-
}
9973
}

app/src/main/java/com/rae/cnblogs/CnblogsApplicationProxy.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import com.rae.cnblogs.sdk.bean.UserInfoBean;
88
import com.rae.cnblogs.sdk.db.DbCnblogs;
99
import com.rae.swift.session.SessionManager;
10-
import com.squareup.leakcanary.LeakCanary;
1110
import com.tencent.bugly.Bugly;
1211
import com.tencent.bugly.beta.tinker.TinkerApplicationLike;
1312
import com.umeng.socialize.PlatformConfig;
@@ -33,11 +32,10 @@ public void onCreate() {
3332
initUmengShareConfig();
3433
UserProvider.init(getApplication());
3534
SessionManager.initWithConfig(new SessionManager.ConfigBuilder().context(getApplication()).userClass(UserInfoBean.class).build());
36-
// if (!BuildConfig.BUILD_TYPE.equals("debug")) {
37-
// 日志上报
38-
Bugly.init(getApplication(), BuildConfig.BUGLY_APP_ID, BuildConfig.DEBUG);
39-
// }
40-
35+
if (!BuildConfig.BUILD_TYPE.equals("debug")) {
36+
// 日志上报
37+
Bugly.init(getApplication(), BuildConfig.BUGLY_APP_ID, BuildConfig.DEBUG);
38+
}
4139
}
4240

4341
/**

app/src/main/java/com/rae/cnblogs/activity/LauncherActivity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.nostra13.universalimageloader.core.ImageLoader;
1111
import com.rae.cnblogs.AppRoute;
1212
import com.rae.cnblogs.R;
13+
import com.rae.cnblogs.RaeImageLoader;
1314
import com.rae.cnblogs.presenter.CnblogsPresenterFactory;
1415
import com.rae.cnblogs.presenter.ILauncherPresenter;
1516
import com.rae.cnblogs.sdk.bean.BlogType;
@@ -53,7 +54,7 @@ protected void onStop() {
5354
}
5455

5556
private void showImage(String url) {
56-
ImageLoader.getInstance().displayImage(url, mDisplayView);
57+
ImageLoader.getInstance().displayImage(url, mDisplayView, RaeImageLoader.defaultOptions().showImageOnLoading(0).showImageForEmptyUri(0).showImageOnFail(0).build());
5758
}
5859

5960
@Override

0 commit comments

Comments
 (0)