package com.fox2code.mmm.utils;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.fox2code.mmm.MainActivity;

import java.util.concurrent.ThreadLocalRandom;

public enum ProcessHelper {
    ;
    private static final int sPendingIntentId = ThreadLocalRandom.current().nextInt(100, 1000000 + 1);

    public static void restartApplicationProcess(Context context) {
        Intent mStartActivity = new Intent(context, MainActivity.class);
        mStartActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent mPendingIntent = PendingIntent.getActivity(context, sPendingIntentId,
                mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE);
        AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
        System.exit(0); // Exit app process
    }
}
