package com.huawei.hwscreenrecorddemo.notification;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import com.huawei.hwscreenrecorddemo.MainActivity;
import com.huawei.hwscreenrecorddemo.NotificationService;
import com.huawei.hwscreenrecorddemo.util.LogUtils;

import java.lang.reflect.Method;

public class NotificationBroadcast extends BroadcastReceiver {

    private final String RECORDER_STOP = "stop";
    private final String RECORDER_PAUSE = "pause";
    private final String RECORDER_RESUME = "resume";
    private final String RECORDER_HOME = "come_home";

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        LogUtils.d("LDS action: "+action);
        collapseStatusBar(context);
        Intent i = new Intent(context, MainActivity.class);
//        Intent i = new Intent();
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        if (action.equals("stop")) {
//            i.putExtra("flag", "stop");
//            context.startActivity(i);
//        }
        switch (action){
            case RECORDER_STOP:
                i.putExtra("flag", "stop");
                //停止消息栏通知服务
                Intent intentNotification = new Intent(context, NotificationService.class);
                context.stopService(intentNotification);
                android.os.Process.killProcess(android.os.Process.myPid());

                System.exit(0);
//                context.startActivity(i);
                break;
            case RECORDER_PAUSE:
                i.putExtra("flag", "pause");
                context.startActivity(i);
                break;
            case RECORDER_RESUME:
                i.putExtra("flag", "resume");
                context.startActivity(i);
                break;
            case RECORDER_HOME:
                i.putExtra("flag", "come_home");
                context.startActivity(i);
                break;
        }
    }

    public void collapseStatusBar(Context context) {
        try {
            Object statusBarManager = context.getSystemService("statusbar");
            Method collapse;
            if (Build.VERSION.SDK_INT <= 16) {
                collapse = statusBarManager.getClass().getMethod("collapse");
            } else {
                collapse = statusBarManager.getClass().getMethod("collapsePanels");
            }
            collapse.invoke(statusBarManager);
        } catch (Exception localException) {
            localException.printStackTrace();
        }
    }

}