Android Open Source - android_monitor Running Apps Collector






From Project

Back to project page android_monitor.

License

The source code is released under:

GNU General Public License

If you think the Android project android_monitor listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*******************************************************************************
 * Copyright (C) 2014 MUSE team Inria Paris - Rocquencourt
 * /*from  w ww  . j  a  v a 2s  .c  om*/
 * This file is part of UCNDataCollector.
 * 
 * UCNDataCollector is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * UCNDataCollector is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero Public License for more details.
 * 
 * You should have received a copy of the GNU Affero Public License
 * along with UCNDataCollector.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package fr.inria.ucn.collectors;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import fr.inria.ucn.Constants;
import fr.inria.ucn.Helpers;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;

/**
 * Records the list of running apps, foreground app, ..
 * 
 * @author Anna-Kaisa Pietilainen <anna-kaisa.pietilainen@inria.fr>
 *
 */
public class RunningAppsCollector implements Collector {
  
  /* (non-Javadoc)
   * @see fr.inria.ucn.collectors.Collector#run()
   */
  @SuppressLint("NewApi")
  @Override
  public void run(Context c, long ts) {
    try {
      ActivityManager am = (ActivityManager) c.getSystemService(Context.ACTIVITY_SERVICE);
      PackageManager pm = c.getPackageManager();

      JSONObject data = new JSONObject();
            
      // running tasks
      JSONArray runTaskArray = new JSONArray();
      List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(50);
      boolean first = true;
      for (ActivityManager.RunningTaskInfo info : taskInfo) {
        JSONObject jinfo = new JSONObject();
        jinfo.put("task_id", info.id);
        jinfo.put("task_num_activities", info.numActivities);
        jinfo.put("task_num_running", info.numRunning);
        
        // is this the foreground process ?
        jinfo.put("task_foreground", first);    
        if (first) {
          first = false;
        }
        
        if (info.topActivity!=null) {
          jinfo.put("task_top_class_name", info.topActivity.getClassName());
          jinfo.put("task_top_package_name", info.topActivity.getPackageName());
          try {
            // map package name to app label
            CharSequence appLabel = 
                pm.getApplicationLabel(
                    pm.getApplicationInfo(
                          info.topActivity.getPackageName(), 
                          PackageManager.GET_META_DATA));
            jinfo.put("task_app_label", appLabel.toString());
          } catch (NameNotFoundException e) {
          }
        }
        
        runTaskArray.put(jinfo);
      }
      data.put("runningTasks", runTaskArray);
      
      // processes
      JSONArray runProcArray = new JSONArray();
      for (ActivityManager.RunningAppProcessInfo pinfo : am.getRunningAppProcesses()) {
        JSONObject jinfo = new JSONObject();
        jinfo.put("proc_uid", pinfo.uid);
        jinfo.put("proc_name", pinfo.processName);
        jinfo.put("proc_packages", Helpers.getPackagesForUid(c,pinfo.uid));
        runProcArray.put(jinfo);
      }
      data.put("runningAppProcesses", runProcArray);
      
      // done
      Helpers.sendResultObj(c,"running_apps",ts,data);
      
    } catch (JSONException jex) {
      Log.w(Constants.LOGTAG, "failed to create json obj",jex);
    }
  }
}




Java Source Code List

fr.inria.ucn.CollectorException.java
fr.inria.ucn.CollectorService.java
fr.inria.ucn.Constants.java
fr.inria.ucn.DataStore.java
fr.inria.ucn.DataUploader.java
fr.inria.ucn.Helpers.java
fr.inria.ucn.OnBootReceiver.java
fr.inria.ucn.Scheduler.java
fr.inria.ucn.collectors.AppDataUsageCollector.java
fr.inria.ucn.collectors.Collector.java
fr.inria.ucn.collectors.DeviceInfoCollector.java
fr.inria.ucn.collectors.LlamaCollector.java
fr.inria.ucn.collectors.NetworkStateCollector.java
fr.inria.ucn.collectors.RunningAppsCollector.java
fr.inria.ucn.collectors.SocketsCollector.java
fr.inria.ucn.collectors.SysStateCollector.java
fr.inria.ucn.listeners.MyPhoneStateListener.java
fr.inria.ucn.listeners.SystemStateListener.java
fr.inria.ucn.ui.SettingsActivity.java
fr.inria.ucn.ui.SettingsFragment.java
fr.inria.ucn.ui.TimePreference.java