package com.hh.arome.broadcastreceiver;

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

import com.common.lib_base.common.local.AppLocalConfigHandler;
import com.hh.arome.common.IAppLocalConfig;
import com.hh.arome.ui.MainActivity;
import com.hh.arome.utlis.log.AppLogUtils;

public class AutoStartBroadReceiver extends BroadcastReceiver {

    IAppLocalConfig localConfig;

    private static final String ACTION = "android.intent.action.BOOT_COMPLETED";

    @Override
    public void onReceive(Context context, Intent intent) {
        AppLogUtils.e("接收广播", "onReceive: ");
        AppLogUtils.e("接收广播", "onReceive: " + intent.getAction());

        localConfig = AppLocalConfigHandler.create(context.getApplicationContext(), IAppLocalConfig.class);


        boolean firstStart = localConfig.isFirstStart();
        AppLogUtils.e("firstStart--" + firstStart);
        if (!firstStart) {
            localConfig.setFirstStart(true);

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    Intent mainIntent = new Intent(context, MainActivity.class);
                    /**
                     * Intent.FLAG_ACTIVITY_NEW_TASK
                     * Intent.FLAG_ACTIVITY_CLEAR_TOP
                     */
                    mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(mainIntent);
                }
            }, 5 * 1000); //延时10s执行run内代码

        } else {
            startApp(context,intent);
        }



    }

    private void startApp(Context context, Intent intent) {
        //开机启动
        if (ACTION.equals(intent.getAction())) {
            AppLogUtils.e("接收广播", "onReceive: 启动了。。。");

            boolean autoApp = localConfig.isAutoApp();
            AppLogUtils.e("autoApp--" + autoApp);
            if (autoApp) {
                AppLogUtils.e("autoApp--自启动");
                //第二种方式：指定class类，跳转到相应的Acitivity

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Intent mainIntent = new Intent(context, MainActivity.class);
                        /**
                         * Intent.FLAG_ACTIVITY_NEW_TASK
                         * Intent.FLAG_ACTIVITY_CLEAR_TOP
                         */
                        mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(mainIntent);
                    }
                }, 5 * 1000); //延时10s执行run内代码


            } else {
                AppLogUtils.e("autoApp--非启动");
            }

        }
    }

}