PreDownloadByUrlActivity.java
5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package com.cnlive.demo.video.cache;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.cnlive.demo.video.R;
import com.cnlive.demo.video.cache.fragment.SettingFragment;
import com.cnlive.libs.util.SharedPreferencesHelper;
import com.cnlive.libs.video.cache.CacheManager;
import com.cnlive.libs.video.cache.ICache;
import com.cnlive.libs.video.cache.ProxyService;
import java.io.File;
/**
* Created by gujun on 2017/10/20.
*/
public class PreDownloadByUrlActivity extends AppCompatActivity {
private static final String TAG = "PreDownloadUrlActivity";
private String url;
private TextView urlContent, cacheState, cacheProgress;
private ProxyService proxyService;
private SharedPreferencesHelper sph;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pre_download_by_url);
init();
}
private void init() {
sph = SharedPreferencesHelper.getInstance(this);
url = getIntent().getStringExtra("url");
urlContent = (TextView) findViewById(R.id.urlContent);
cacheState = (TextView) findViewById(R.id.cacheState);
cacheProgress = (TextView) findViewById(R.id.cacheProgress);
urlContent.setText(url);
initCacheInfo();
}
private void initCacheInfo() {
proxyService = CacheManager.getProxyService(this);
proxyService.registerCacheStatusListener(mCacheStatusListener, url);
proxyService.registerErrorListener(mOnErrorListener);
proxyService.registerLogEventListener(mOnLogEventListener);
if (sph.getBoolean(SettingFragment.CACHE_LIMIT_SIZE, false)) {
proxyService.setMaxCacheSize(1000 * 1024 * 1024);
} else {
proxyService.setMaxFilesCount(500);
}
proxyService.setCacheRoot(setCacheFilePath());
proxyService.startServer();
initCacheData();
}
private void initCacheData() {
if (proxyService.isCached(url)) {
cacheState.setText("缓存完成");
cacheProgress.setText("");
} else {
cacheState.setText("暂停缓存");
cacheProgress.setText(proxyService.getCachingPercent(url) + "%");
}
}
private File setCacheFilePath() {
String url = Environment.getExternalStorageDirectory().toString() + File.separator + getPackageName() + File.separator + "cache";
File file = new File(url);
if (!file.exists())
file.mkdirs();
return file;
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.cacheStart:
if (proxyService != null && !proxyService.isCached(url)) {
proxyService.startPreDownload(url);
cacheState.setText("正在缓存");
}
break;
case R.id.cacheStop:
if (proxyService != null && !proxyService.isCached(url)) {
proxyService.stopPreDownload(url);
cacheState.setText("暂停缓存");
}
break;
case R.id.cachePlay:
Intent intent = new Intent(this, CacheVideoByUrlActivity.class);
intent.putExtra("url", url);
startActivity(intent);
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (proxyService != null) {
proxyService.unregisterCacheStatusListener(mCacheStatusListener, url);
proxyService.unregisterErrorListener(mOnErrorListener);
proxyService.unregisterLogEventListener(mOnLogEventListener);
//退出页面是否关闭下载服务
proxyService.shutDownServer();
}
}
private ICache.OnCacheStatusListener mCacheStatusListener = new ICache.OnCacheStatusListener() {
@Override
public void OnCacheStatus(String url, long sourceLength, int percentsAvailable) {
Log.e(TAG, "OnCacheStatus: url_" + url + "\nsourceLength:" + sourceLength + "\npercentsAvailable" + percentsAvailable);
if (PreDownloadByUrlActivity.this.url.equals(url)) {
if (percentsAvailable == 100) {
cacheState.setText("缓存完成");
cacheProgress.setText("");
} else {
cacheState.setText("正在缓存");
cacheProgress.setText(proxyService.getCachingPercent(url) + "%");
}
}
}
};
private ICache.OnErrorListener mOnErrorListener = new ICache.OnErrorListener() {
@Override
public void OnError(int errCode) {
Log.e(TAG, "OnError: " + errCode);
}
};
private ICache.OnLogEventListener mOnLogEventListener = new ICache.OnLogEventListener() {
@Override
public void onLogEvent(String log) {
Log.e(TAG, "onLogEvent: " + log);
}
};
}