CacheActivity.java
5.5 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
158
159
160
161
162
163
164
package com.cnlive.demo.video.cache;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
import com.cnlive.demo.video.R;
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;
/**
* Created by gujun on 2017/10/20.
*/
public class CacheActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
private static final String TAG = "CacheActivity";
//是否是设置缓存文件大小来限制缓存,不是就设置文件个数限制缓存
public static final String CACHE_LIMIT_SIZE = "cache_limit_size";
private SharedPreferencesHelper sph;
private ProxyService proxyService;
private EditText editPlayUrl;
private RadioButton cacheMaxSize, cacheMaxCount;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cache);
init();
}
private void init() {
editPlayUrl = (EditText) findViewById(R.id.playUrl);
editPlayUrl.setText("https://mvvideo5.meitudata.com/571090934cea5517.mp4");
sph = SharedPreferencesHelper.getInstance(this);
cacheMaxSize = (RadioButton) findViewById(R.id.cacheMaxSize);
cacheMaxCount = (RadioButton) findViewById(R.id.cacheMaxCount);
cacheMaxSize.setOnCheckedChangeListener(this);
cacheMaxCount.setOnCheckedChangeListener(this);
cacheMaxSize.setChecked(true);
verifyStoragePermissions(this);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.playCache:
if (TextUtils.isEmpty(editPlayUrl.getText().toString())) {
Toast.makeText(this, "播放地址为空", Toast.LENGTH_LONG).show();
return;
}
Intent intent = new Intent(this, CacheVideoActivity.class);
intent.putExtra("url", editPlayUrl.getText().toString());
startActivity(intent);
break;
case R.id.clearCache:
clearCache();
break;
}
}
/**
* 清除缓存
*/
private void clearCache() {
proxyService = CacheManager.getProxyService(this);
proxyService.registerLogEventListener(mOnLogEventListener);
proxyService.startServer();
proxyService.cleanCaches();
proxyService.shutDownServer();
Toast.makeText(this, "清除缓存成功", Toast.LENGTH_LONG).show();
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) {
case R.id.cacheMaxCount:
sph.setValue(CACHE_LIMIT_SIZE, false);
break;
case R.id.cacheMaxSize:
sph.setValue(CACHE_LIMIT_SIZE, isChecked);
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (proxyService != null) {
proxyService.unregisterLogEventListener(mOnLogEventListener);
}
}
private ICache.OnLogEventListener mOnLogEventListener = new ICache.OnLogEventListener() {
@Override
public void onLogEvent(String log) {
Log.e(TAG, "onLogEvent: " + log);
}
};
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
"android.permission.READ_EXTERNAL_STORAGE",
"android.permission.WRITE_EXTERNAL_STORAGE"};
public static void verifyStoragePermissions(Activity activity) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
return;
try {
//检测是否有写的权限
int permission = ActivityCompat.checkSelfPermission(activity,
"android.permission.WRITE_EXTERNAL_STORAGE");
if (permission != PackageManager.PERMISSION_GRANTED) {
// 没有写的权限,去申请写的权限,会弹出对话框
ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_EXTERNAL_STORAGE) {
if (allRequestsPermitted(grantResults))
Toast.makeText(this, "Permission Granted!", Toast.LENGTH_SHORT).show();
else
Toast.makeText(this, "Permission Denied!", Toast.LENGTH_SHORT).show();
}
}
private boolean allRequestsPermitted(int[] grantResults) {
for (int result : grantResults)
if (result == PackageManager.PERMISSION_DENIED)
return false;
return true;
}
}