PreDownloadByUrlActivity.java 5.17 KB
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);
        }
    };
}