/*
 * Copyright (C) 2015. Jared Rummler <jared.rummler@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.jaredrummler.android.processes.sample;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;

import com.jaredrummler.android.processes.ProcessManager;
import com.jaredrummler.android.processes.models.AndroidAppProcess;
import com.jaredrummler.android.processes.models.AndroidProcess;

import java.util.List;

public class MainActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    for (int id : new int[]{
        R.id.btn_all_processes,
        R.id.btn_running_apps,
        R.id.btn_foreground_apps}) {
      findViewById(id).setOnClickListener(new View.OnClickListener() {

        @Override public void onClick(View v) {
          new ListProcessTask().execute(v.getId());
        }
      });
    }
  }

  private class ListProcessTask extends AsyncTask<Integer, Void, String> {

    @Override protected String doInBackground(Integer... params) {
      StringBuilder sb = new StringBuilder();
      switch (params[0]) {
        case R.id.btn_all_processes: {
          List<AndroidProcess> processes = ProcessManager.getRunningProcesses();
          for (AndroidProcess process : processes) {
            sb.append(process.name).append(" ").append(process.pid).append('\n');
          }
          break;
        } case R.id.btn_running_apps: {
          List<AndroidAppProcess> processes = ProcessManager.getRunningAppProcesses();
          for (AndroidAppProcess process : processes) {
            sb.append(process.name).append('\n');
          }
          break;
        } case R.id.btn_foreground_apps: {
          List<AndroidAppProcess> processes = ProcessManager.getRunningAppProcesses();
          for (AndroidAppProcess process : processes) {
            if (process.foreground) {
              sb.append(process.name).append('\n');
            }
          }
          break;
        }
      }
      return sb.toString();
    }

    @Override protected void onPostExecute(String s) {
      new AlertDialog.Builder(MainActivity.this).setMessage(s).show();
    }
  }

}

@@ -739,16 +739,58 @@
 Dialog;%0A
+import android.content.pm.PackageManager;%0A
 import a
@@ -810,16 +810,16 @@
 ncTask;%0A
-
 import a
@@ -1055,16 +1055,130 @@
 ocess;%0A%0A
+import java.io.IOException;%0Aimport java.util.Collections;%0Aimport java.util.Comparator;%0Aimport java.util.Iterator;%0A
 import j
@@ -2519,32 +2519,32 @@
 eground_apps: %7B%0A
-
           List%3CA
@@ -2606,32 +2606,1458 @@
 AppProcesses();%0A
+%0A          PackageManager pm = getPackageManager();%0A          for (Iterator%3CAndroidAppProcess%3E it = processes.iterator(); it.hasNext();) %7B%0A            AndroidAppProcess process = it.next();%0A            int uid = process.uid;%0A            if (uid %3E= 1000 && uid %3C= 9999) %7B%0A              // remove system process'%0A              it.remove();%0A            %7D else if (process.name.contains(%22:%22)) %7B%0A              // remove processes that are not in the app's default process%0A              it.remove();%0A            %7D else if (pm.getLaunchIntentForPackage(process.getPackageName()) == null) %7B%0A              // remove app's that don't have a launcher intent%0A              it.remove();%0A            %7D%0A          %7D%0A%0A          // sort by oom score%0A          Collections.sort(processes, new Comparator%3CAndroidAppProcess%3E() %7B%0A%0A            @Override public int compare(AndroidAppProcess lhs, AndroidAppProcess rhs) %7B%0A              try %7B%0A                int score1 = lhs.oom_score_adj();%0A                int score2 = rhs.oom_score_adj();%0A                if (score1 %3C score2) %7B%0A                  return -1;%0A                %7D else if (score1 %3E score2) %7B%0A                  return 1;%0A                %7D%0A                return lhs.name.compareToIgnoreCase(rhs.name);%0A              %7D catch (IOException e) %7B%0A                e.printStackTrace();%0A                return lhs.name.compareToIgnoreCase(rhs.name);%0A              %7D%0A            %7D%0A          %7D);%0A%0A
           for (A

Remove system or background processes in sample