CacheActivity.java 5.5 KB
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;
    }
}