package com.mn.keeplive.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import java.lang.ref.WeakReference;

public class KeepManager {


    private static final KeepManager mInstance = new KeepManager();

    private keepReceiver mKeepReceiver;

    private WeakReference<Activity> mKeepActivity;

    private KeepManager() {

    }

    public static KeepManager getInstance() {
        return mInstance;
    }

    public void registerKeep(Context context) {
        //动态和静态注册广播
        IntentFilter filter = new IntentFilter();

        filter.addAction(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);

        mKeepReceiver = new keepReceiver();
        context.registerReceiver(mKeepReceiver, filter);
    }

    public void unregisterKeep(Context context) {
        if (mKeepReceiver != null) {
            context.unregisterReceiver(mKeepReceiver);
        }
    }
    //开启1像素activity
    public void startKeep(Context context) {
        Intent intent = new Intent(context, KeepActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
    //关闭1像素activity
    public void finishKeep() {
        if (mKeepActivity != null) {
            Activity activity = mKeepActivity.get();
            if (activity != null) {
                activity.finish();
            }
            mKeepActivity = null;
        }
    }

    public void setKeep(KeepActivity keep) {
        mKeepActivity = new WeakReference<Activity>(keep);
    }


}
