package com.jld.vod.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.jld.vod.model.event.UsbStatusChangeEvent;
import com.jld.vod.utils.FileFuUtils;
import com.jld.vod.utils.LogUtils;
import com.jld.vod.view.PostFileActivity;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.io.FileFilter;


/**
 *Created by zhaofuxin 2019.9.24
 * Descriotion usb插拔广播接收
 */
public class UsbBroadcastReceiver extends BroadcastReceiver {

    private static final String TAG = UsbBroadcastReceiver.class.getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {//ACTION_MEDIA_REMOVED
            String mountPath = intent.getData().getPath();
            Log.d(TAG, "mountPath = " + mountPath);
            if (!TextUtils.isEmpty(mountPath)&&!mountPath.equals("/storage/emulated/0")) {
                //读取到U盘路径再做其他业务逻辑
                 LogUtils.INSTANCE.logD(TAG,mountPath);

                Intent mIntent = new Intent();
                mIntent.setClass(context, PostFileActivity.class);
                mIntent.putExtra("path",mountPath);
                mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
                context.startActivity(mIntent);

                EventBus.getDefault().post(new UsbStatusChangeEvent(true,mountPath));
            }
        } else if (action.equals(Intent.ACTION_MEDIA_UNMOUNTED) || action.equals(Intent.ACTION_MEDIA_EJECT)) {
            LogUtils.INSTANCE.logD("拔出U盘");
           // Toast.makeText(context, "No services information detected !", Toast.LENGTH_SHORT).show();
            EventBus.getDefault().post(new UsbStatusChangeEvent(true));

        } else if (action.equals("android.intent.action.BOOT_COMPLETED")) {
            //如果是开机完成，则需要调用另外的方法获取U盘的路径
        }
    }

}
