package com.avg.zaap.crashlibrary.core.helper.process;

import android.app.ActivityManager;
import android.content.Context;
import android.support.annotation.Nullable;

import com.avg.zaap.crashlibrary.core.logger.CrashLogger;

/**
 * This class serves as the default process helper.
 * <p/>
 * Created by marek.osvald on 13.7.2016.
 */
public class ProcessHelper implements IProcessHelper {

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

    @Override
    public boolean isReportingServiceProcess(Context context) {
        return SERVICE_NAME.equals(getCurrentProcessName(context));
    }

    @Override
    @Nullable
    public String getCurrentProcessName(Context context) {
        final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo process : activityManager.getRunningAppProcesses()) {
            if (process.pid == android.os.Process.myPid()) {
                return process.processName;
            }
        }

        CrashLogger.w(TAG, "Current process name not found.");
        return null;
    }

}
