package com.sheep.gamegroup.absBase;

import android.content.Intent;

import com.sheep.gamegroup.event.BigEvent;
import com.sheep.gamegroup.greendao.download.DownLoadInfo;


import static android.content.Intent.ACTION_PACKAGE_ADDED;
import static android.content.Intent.ACTION_PACKAGE_REMOVED;

/**
 * Created by realicing on 2018/10/31.
 * realicing@sina.com
 * 下载和应用安装的回调，使用时请在Activity的onCreate中注册并在onDestroy中取消注册
 * 并在onEventMainThread(Intent intent)和onEventMainThread(BigEvent event)中添加org.greenrobot.eventbus.Subscribe注解
 * 如：
 EventBus.getDefault().register(this);
 private AbsDownloadListener absDownloadListener = new AbsDownloadListener() {
@Override public void removedApk(String packageName) {

}

@Override public void addedApk(String packageName) {

}

@Override public void running(DownLoadInfo task) {

}

@Override public void taskStop(DownLoadInfo task) {

}

@Override public void taskComplete(DownLoadInfo task) {

}

@Override public void taskCancel(DownLoadInfo task) {

}

@Override public void taskFail(DownLoadInfo task) {

}
};
 @Subscribe public void onEventMainThread(Intent intent) {
 absDownloadListener.onEventMainThread(intent);
 }
 @Subscribe public void onEventMainThread(BigEvent event) {
 absDownloadListener.onEventMainThread(event);
 }
 @Override protected void onDestroy() {
 super.onDestroy();
 EventBus.getDefault().unregister(this);
 }

 */
public abstract class AbsDownloadListener {

    public void onEventMainThread(Intent intent) {
        if (intent != null && intent.getAction() != null && intent.getDataString() != null && intent.getDataString().contains("package:")) {
            String packageName = intent.getDataString().replace("package:", "");
            switch (intent.getAction()) {
                case ACTION_PACKAGE_ADDED:
                    addedApk(packageName);
                    break;
                case ACTION_PACKAGE_REMOVED:
                    removedApk(packageName);
                    break;
            }
        }
    }

    //卸载应用的回调
    public abstract void removedApk(String packageName);

    //安装应用的回调
    public abstract void addedApk(String packageName);

    public void onEventMainThread(BigEvent event) {
        if (event.getData() instanceof DownLoadInfo) {
            DownLoadInfo task = (DownLoadInfo) event.getData();
            switch (event.getEventTypes()) {
                case DOWNLOAD_RUNNING:
                    running(task);
                    break;
                case DOWNLOAD_STOP:
                    taskStop(task);
                    break;
                case DOWNLOAD_COMPLETE:
                    taskComplete(task);
                    break;
                case DOWNLOAD_CANCEL:
                    taskCancel(task);
                    break;
                case DOWNLOAD_FAIL:
                    taskFail(task);
                    break;
            }
        }
    }

    //下载中的的回调
    public abstract void running(DownLoadInfo task);

    //下载暂停的回调
    public abstract void taskStop(DownLoadInfo task);

    //下载完成的回调
    public abstract void taskComplete(DownLoadInfo task);

    //取消下载的回调
    public abstract void taskCancel(DownLoadInfo task);

    //下载失败的回调
    public abstract void taskFail(DownLoadInfo task);
}
