/*
 * 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.activities;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;

import com.jaredrummler.android.processes.ProcessManager;
import com.jaredrummler.android.processes.models.AndroidProcess;
import com.jaredrummler.android.processes.sample.fragments.ProcessListFragment;

import java.io.IOException;
import java.util.List;

public class MainActivity extends Activity {

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState == null) {
      getFragmentManager().beginTransaction().add(android.R.id.content, new ProcessListFragment()).commit();
    }
    List<AndroidProcess> runningProcesses = ProcessManager.getRunningProcesses();
    for (AndroidProcess runningProcess : runningProcesses) {
      try {
        int uid = runningProcess.status().getUid();
        String uidName = getNameForId(uid);
        if (uidName != null) {
          System.out.println(runningProcess.name);
        }


      } catch (IOException e) {
        e.printStackTrace();
      }

    }
  }

  /**
   * Returns the UID/GID name assigned to a particular id, or {@code null} if there is none.
   *
   * @param id
   *     The UID/GID of a process or file
   * @return the name of the UID/GID or {@code null} if the id is unrecognized.
   */
  public static String getNameForId(int id) {
    String name;
    // https://android.googlesource.com/platform/system/core/+/master/include/private/android_filesystem_config.h
    int appId = id - 10000;
    int userId = 0;
    // loop until we get the correct user id.
    // 100000 is the offset for each user.
    while (appId > 100000) {
      appId -= 100000;
      userId++;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
      // u{user_id}_a{app_id} is used on API 17+ for multiple user account support.
      name = String.format("u%d_a%d", userId, appId);
    } else {
      // app_{app_id} is used below API 17.
      name = String.format("app_%d", appId);
    }
    if (android.os.Process.getUidForName(name) == id) {
      return name;
    }
    return null; // unknown UID
  }

}

@@ -742,162 +742,14 @@
 s.Bu
-ild;%0Aimport android.os.Bundle;%0A%0Aimport com.jaredrummler.android.processes.ProcessManager;%0Aimport com.jaredrummler.android.processes.models.AndroidProcess;
+ndle;%0A
 %0Aimp
@@ -830,60 +830,8 @@
 t;%0A%0A
-import java.io.IOException;%0Aimport java.util.List;%0A%0A
 publ
@@ -1134,1499 +1134,8 @@
   %7D%0A
-    List%3CAndroidProcess%3E runningProcesses = ProcessManager.getRunningProcesses();%0A    for (AndroidProcess runningProcess : runningProcesses) %7B%0A      try %7B%0A        int uid = runningProcess.status().getUid();%0A        String uidName = getNameForId(uid);%0A        if (uidName != null) %7B%0A          System.out.println(runningProcess.name);%0A        %7D%0A%0A%0A      %7D catch (IOException e) %7B%0A        e.printStackTrace();%0A      %7D%0A%0A    %7D%0A  %7D%0A%0A  /**%0A   * Returns the UID/GID name assigned to a particular id, or %7B@code null%7D if there is none.%0A   *%0A   * @param id%0A   *     The UID/GID of a process or file%0A   * @return the name of the UID/GID or %7B@code null%7D if the id is unrecognized.%0A   */%0A  public static String getNameForId(int id) %7B%0A    String name;%0A    // https://android.googlesource.com/platform/system/core/+/master/include/private/android_filesystem_config.h%0A    int appId = id - 10000;%0A    int userId = 0;%0A    // loop until we get the correct user id.%0A    // 100000 is the offset for each user.%0A    while (appId %3E 100000) %7B%0A      appId -= 100000;%0A      userId++;%0A    %7D%0A    if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.JELLY_BEAN_MR1) %7B%0A      // u%7Buser_id%7D_a%7Bapp_id%7D is used on API 17+ for multiple user account support.%0A      name = String.format(%22u%25d_a%25d%22, userId, appId);%0A    %7D else %7B%0A      // app_%7Bapp_id%7D is used below API 17.%0A      name = String.format(%22app_%25d%22, appId);%0A    %7D%0A    if (android.os.Process.getUidForName(name) == id) %7B%0A      return name;%0A    %7D%0A    return null; // unknown UID%0A
   %7D%0A

remove stuff :dizzy_face: