<commit_before>package com.ilis.memoryoptimizer.data;

import android.app.ActivityManager;
import android.support.annotation.Nullable;
import android.text.format.Formatter;

import com.ilis.memoryoptimizer.MemOptApplication;
import com.jaredrummler.android.processes.AndroidProcesses;

import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.PublishSubject;

public class ProcessInfoRepository implements ProcessInfoSource {

    @Nullable
    private static volatile ProcessInfoRepository INSTANCE = null;

    private volatile boolean mRefreshing;
    private PublishSubject<Long> mRefreshStartNotification = PublishSubject.create();
    private PublishSubject<Long> mRefreshEndNotification = PublishSubject.create();

    private List<ProcessInfo> mCachedProcessInfo;
    private ActivityManager.MemoryInfo mCachedMemoryInfo;

    private ProcessInfoRepository() {
        initRefreshListener();
    }

    public static synchronized ProcessInfoSource getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new ProcessInfoRepository();
        }
        return INSTANCE;
    }

    @Override
    public void refresh() {
        mCachedProcessInfo = null;
        mCachedMemoryInfo = null;
    }

    @Override
    public Observable<List<ProcessInfo>> getAllProcess() {
        if (mCachedProcessInfo != null) {
            return Observable.just(mCachedProcessInfo);
        }
        notifyRefreshStart();
        return refreshEnd()
                .map(endTime -> mCachedProcessInfo)
                .observeOn(AndroidSchedulers.mainThread());
    }

    @Override
    public Observable<Integer> getProcessCount() {
        if (mCachedProcessInfo != null) {
            return Observable.just(mCachedProcessInfo.size());
        }
        notifyRefreshStart();
        return Observable.just(AndroidProcesses.getRunningAppProcesses())
                .map(List::size)
                .observeOn(AndroidSchedulers.mainThread());
    }

    private Observable<Long> getTotalMemoryByte() {
        if (mCachedMemoryInfo != null) {
            return Observable.just(mCachedMemoryInfo.totalMem);
        }
        return Observable.just(cacheMemoryInfo())
                .map(info -> info.totalMem)
                .observeOn(AndroidSchedulers.mainThread());
    }

    private Observable<Long> getAvailMemoryByte() {
        if (mCachedMemoryInfo != null) {
            return Observable.just(mCachedMemoryInfo.availMem);
        }
        return Observable.just(cacheMemoryInfo())
                .map(info -> info.totalMem)
                .observeOn(AndroidSchedulers.mainThread());
    }

    @Override
    public Observable<String> getSystemMemStatus() {
        return Observable.combineLatest(<commit_msg>bugs fix<commit_after>package com.ilis.memoryoptimizer.data;

import android.app.ActivityManager;
import android.support.annotation.Nullable;
import android.text.format.Formatter;

import com.ilis.memoryoptimizer.MemOptApplication;
import com.jaredrummler.android.processes.AndroidProcesses;

import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.PublishSubject;

public class ProcessInfoRepository implements ProcessInfoSource {

    @Nullable
    private static volatile ProcessInfoRepository INSTANCE = null;

    private volatile boolean mRefreshing;
    private PublishSubject<Long> mRefreshStartNotification = PublishSubject.create();
    private PublishSubject<Long> mRefreshEndNotification = PublishSubject.create();

    private List<ProcessInfo> mCachedProcessInfo;
    private ActivityManager.MemoryInfo mCachedMemoryInfo;

    private ProcessInfoRepository() {
        initRefreshListener();
    }

    public static synchronized ProcessInfoSource getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new ProcessInfoRepository();
        }
        return INSTANCE;
    }

    @Override
    public void refresh() {
        mCachedProcessInfo = null;
        mCachedMemoryInfo = null;
    }

    @Override
    public Observable<List<ProcessInfo>> getAllProcess() {
        if (mCachedProcessInfo != null) {
            return Observable.just(mCachedProcessInfo);
        }
        notifyRefreshStart();
        return refreshEnd()
                .map(endTime -> mCachedProcessInfo)
                .observeOn(AndroidSchedulers.mainThread());
    }

    @Override
    public Observable<Integer> getProcessCount() {
        if (mCachedProcessInfo != null) {
            return Observable.just(mCachedProcessInfo.size());
        }
        notifyRefreshStart();
        return Observable.just(AndroidProcesses.getRunningAppProcesses())
                .map(List::size)
                .observeOn(AndroidSchedulers.mainThread());
    }

    private Observable<Long> getTotalMemoryByte() {
        if (mCachedMemoryInfo != null) {
            return Observable.just(mCachedMemoryInfo.totalMem);
        }
        return Observable.just(cacheMemoryInfo())
                .map(info -> info.totalMem)
                .observeOn(AndroidSchedulers.mainThread());
    }

    private Observable<Long> getAvailMemoryByte() {
        if (mCachedMemoryInfo != null) {
            return Observable.just(mCachedMemoryInfo.availMem);
        }
        return Observable.just(cacheMemoryInfo())
                .map(info -> info.availMem)
                .observeOn(AndroidSchedulers.mainThread());
    }

    @Override
    public Observable<String> getSystemMemStatus() {
        return Observable.combineLatest(
{'fraction_non_alphanumeric': 0.08973252804141502, 'fraction_numerical': 0.0, 'mean_word_length': 3.0138504155124655, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7061521', 'n_tokens_mistral': 1552, 'n_tokens_neox': 1511, 'n_words': 278}