Search in sources :

Example 1 with ProcessInfo

use of edu.berkeley.cs.amplab.carat.thrift.ProcessInfo in project carat by amplab.

the class SamplingLibrary method getRunningAppInfo.

public static List<ProcessInfo> getRunningAppInfo(Context c) {
    List<RunningAppProcessInfo> runningProcs = getRunningProcessInfo(c);
    List<RunningServiceInfo> runningServices = getRunningServiceInfo(c);
    List<ProcessInfo> l = new ArrayList<ProcessInfo>();
    if (runningProcs != null) {
        for (RunningAppProcessInfo pi : runningProcs) {
            if (pi == null)
                continue;
            ProcessInfo item = new ProcessInfo();
            item.setImportance(CaratApplication.importanceString(pi.importance));
            item.setPId(pi.pid);
            item.setPName(pi.processName);
            l.add(item);
        }
    }
    if (runningServices != null) {
        for (RunningServiceInfo pi : runningServices) {
            if (pi == null)
                continue;
            ProcessInfo item = new ProcessInfo();
            item.setImportance(pi.foreground ? "Foreground app" : "Service");
            item.setPId(pi.pid);
            item.setPName(pi.clientPackage);
            l.add(item);
        }
    }
    return l;
}
Also used : RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) RunningServiceInfo(android.app.ActivityManager.RunningServiceInfo) ArrayList(java.util.ArrayList) RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) ProcessInfo(edu.berkeley.cs.amplab.carat.thrift.ProcessInfo)

Example 2 with ProcessInfo

use of edu.berkeley.cs.amplab.carat.thrift.ProcessInfo in project carat by amplab.

the class SamplingLibrary method getSample.

public static Sample getSample(Context context, Intent intent, String lastBatteryState) {
    final String TAG = "SamplingLibrary.getSample";
    Log.d(TAG, "getSample() was invoked.");
    String action = intent.getAction();
    Log.d(TAG, "action = " + action);
    // Construct sample and return it in the end
    Sample mySample = new Sample();
    SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(context);
    String uuId = p.getString(CaratApplication.getRegisteredUuid(), null);
    mySample.setUuId(uuId);
    mySample.setTriggeredBy(action);
    // required always
    long now = System.currentTimeMillis();
    mySample.setTimestamp(now / 1000.0);
    // Record first data point for CPU usage
    long[] idleAndCpu1 = readUsagePoint();
    // If the sampler is running because of the SCREEN_ON or SCREEN_OFF
    // event/action,
    // we want to get the info of all installed apps/packages, not only
    // those running.
    // This is because we need the traffic info of all apps, some might not
    // be running when
    // those events (screen on / screen off) occur
    // TODO: let's comment out these lines for debugging purpose
    // if (action.equals(Intent.ACTION_SCREEN_ON) ||
    // action.equals(Intent.ACTION_SCREEN_OFF)) {
    // Log.d(TAG,
    // "the action has been Intent.ACTION_SCREEN_ON or SCREEN_OFF. Taking sample of ALL INSTALLED packages (rather than running processes)");
    // Map<String, ProcessInfo> installedPackages =
    // getInstalledPackages(context, false);
    // List<ProcessInfo> processes = new ArrayList<ProcessInfo>();
    // processes.addAll(installedPackages.values());
    // } else {
    // Log.d(TAG,
    // "the action has NOT been Intent.ACTION_SCREEN_ON or SCREEN_OFF. Taking sample of running processes.");
    List<ProcessInfo> processes = getRunningProcessInfoForSample(context);
    mySample.setPiList(processes);
    // }
    int screenBrightness = SamplingLibrary.getScreenBrightness(context);
    mySample.setScreenBrightness(screenBrightness);
    boolean autoScreenBrightness = SamplingLibrary.isAutoBrightness(context);
    if (autoScreenBrightness)
        // Auto
        mySample.setScreenBrightness(-1);
    // boolean gpsEnabled = SamplingLibrary.getGpsEnabled(context);
    // Location providers
    List<String> enabledLocationProviders = SamplingLibrary.getEnabledLocationProviders(context);
    mySample.setLocationProviders(enabledLocationProviders);
    // TODO: not in Sample yet
    // int maxNumSatellite = SamplingLibrary.getMaxNumSatellite(context);
    String network = SamplingLibrary.getNetworkStatus(context);
    String networkType = SamplingLibrary.getNetworkType(context);
    String mobileNetworkType = SamplingLibrary.getMobileNetworkType(context);
    // Required in new Carat protocol
    if (network.equals(NETWORKSTATUS_CONNECTED)) {
        if (networkType.equals("WIFI"))
            mySample.setNetworkStatus(networkType);
        else
            mySample.setNetworkStatus(mobileNetworkType);
    } else
        mySample.setNetworkStatus(network);
    // String ns = mySample.getNetworkStatus();
    // Log.d(STAG, "Set networkStatus="+ns);
    // Network details
    NetworkDetails nd = new NetworkDetails();
    // Network type
    nd.setNetworkType(networkType);
    nd.setMobileNetworkType(mobileNetworkType);
    boolean roamStatus = SamplingLibrary.getRoamingStatus(context);
    nd.setRoamingEnabled(roamStatus);
    String dataState = SamplingLibrary.getDataState(context);
    nd.setMobileDataStatus(dataState);
    String dataActivity = SamplingLibrary.getDataActivity(context);
    nd.setMobileDataActivity(dataActivity);
    // Wifi stuff
    String wifiState = SamplingLibrary.getWifiState(context);
    nd.setWifiStatus(wifiState);
    int wifiSignalStrength = SamplingLibrary.getWifiSignalStrength(context);
    nd.setWifiSignalStrength(wifiSignalStrength);
    int wifiLinkSpeed = SamplingLibrary.getWifiLinkSpeed(context);
    nd.setWifiLinkSpeed(wifiLinkSpeed);
    // Add NetworkDetails substruct to Sample
    mySample.setNetworkDetails(nd);
    /* Calling Information */
    // List<String> callInfo;
    // callInfo=SamplingLibrary.getCallInfo(context);
    /* Total call time */
    // long totalCallTime=0;
    // totalCallTime=SamplingLibrary.getTotalCallDur(context);
    /*
		 * long[] incomingOutgoingIdle = getCalltimesSinceBoot(context);
		 * Log.d(STAG, "Call time since boot: Incoming=" +
		 * incomingOutgoingIdle[0] + " Outgoing=" + incomingOutgoingIdle[1] +
		 * " idle=" + incomingOutgoingIdle[2]);
		 * 
		 * // Summary Call info CallInfo ci = new CallInfo(); String callState =
		 * SamplingLibrary.getCallState(context); ci.setCallStatus(callState);
		 * ci.setIncomingCallTime(incomingOutgoingIdle[0]);
		 * ci.setOutgoingCallTime(incomingOutgoingIdle[1]);
		 * ci.setNonCallTime(incomingOutgoingIdle[2]);
		 * 
		 * mySample.setCallInfo(ci);
		 */
    // Bundle b = intent.getExtras();
    int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, 0);
    int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, 0);
    // This is really an int.
    // FIXED: Not used yet, Sample needs more fields
    int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
    String batteryTechnology = intent.getExtras().getString(BatteryManager.EXTRA_TECHNOLOGY);
    // FIXED: Not used yet, Sample needs more fields
    String batteryHealth = "Unknown";
    String batteryStatus = "Unknown";
    switch(health) {
        case BatteryManager.BATTERY_HEALTH_DEAD:
            batteryHealth = "Dead";
            break;
        case BatteryManager.BATTERY_HEALTH_GOOD:
            batteryHealth = "Good";
            break;
        case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
            batteryHealth = "Over voltage";
            break;
        case BatteryManager.BATTERY_HEALTH_OVERHEAT:
            batteryHealth = "Overheat";
            break;
        case BatteryManager.BATTERY_HEALTH_UNKNOWN:
            batteryHealth = "Unknown";
            break;
        case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
            batteryHealth = "Unspecified failure";
            break;
    }
    switch(status) {
        case BatteryManager.BATTERY_STATUS_CHARGING:
            batteryStatus = "Charging";
            break;
        case BatteryManager.BATTERY_STATUS_DISCHARGING:
            batteryStatus = "Discharging";
            break;
        case BatteryManager.BATTERY_STATUS_FULL:
            batteryStatus = "Full";
            break;
        case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
            batteryStatus = "Not charging";
            break;
        case BatteryManager.BATTERY_STATUS_UNKNOWN:
            batteryStatus = "Unknown";
            break;
        default:
            batteryStatus = lastBatteryState != null ? lastBatteryState : "Unknown";
    }
    // FIXED: Not used yet, Sample needs more fields
    String batteryCharger = "unplugged";
    switch(plugged) {
        case BatteryManager.BATTERY_PLUGGED_AC:
            batteryCharger = "ac";
            break;
        case BatteryManager.BATTERY_PLUGGED_USB:
            batteryCharger = "usb";
            break;
    }
    BatteryDetails bd = new BatteryDetails();
    // otherInfo.setCPUIdleTime(totalIdleTime);
    // IMPORTANT: All of the battery details fields were never set (=always
    // zero), like the last battery level.
    // Now all must have been fixed.
    // current battery temperature in degrees Centigrade (the unit of the
    // temperature value
    // (returned by BatteryManager) is not Centigrade, it should be divided
    // by 10)
    int temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0) / 10;
    bd.setBatteryTemperature(temperature);
    // otherInfo.setBatteryTemperature(temperature);
    // current battery voltage in VOLTS (the unit of the returned value by
    // BatteryManager is millivolts)
    double voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0) / 1000;
    bd.setBatteryVoltage(voltage);
    // otherInfo.setBatteryVoltage(voltage);
    bd.setBatteryTechnology(batteryTechnology);
    bd.setBatteryCharger(batteryCharger);
    bd.setBatteryHealth(batteryHealth);
    mySample.setBatteryDetails(bd);
    mySample.setBatteryLevel(currentBatteryLevel);
    mySample.setBatteryState(batteryStatus);
    int[] usedFreeActiveInactive = SamplingLibrary.readMeminfo();
    if (usedFreeActiveInactive != null && usedFreeActiveInactive.length == 4) {
        mySample.setMemoryUser(usedFreeActiveInactive[0]);
        mySample.setMemoryFree(usedFreeActiveInactive[1]);
        mySample.setMemoryActive(usedFreeActiveInactive[2]);
        mySample.setMemoryInactive(usedFreeActiveInactive[3]);
    }
    // TODO: Memory Wired should have memory that is "unevictable", that
    // will always be used even when all apps are killed
    // Log.d(STAG, "serial=" + getBuildSerial());
    // Record second data point for cpu/idle time
    now = System.currentTimeMillis();
    long[] idleAndCpu2 = readUsagePoint();
    CpuStatus cs = new CpuStatus();
    cs.setCpuUsage(getUsage(idleAndCpu1, idleAndCpu2));
    cs.setUptime(getUptime());
    mySample.setCpuStatus(cs);
    mySample.setDeveloperMode(isDeveloperModeOn(context));
    mySample.setUnknownSources(allowUnknownSources(context));
    mySample.setScreenOn(isScreenOn(context));
    mySample.setTimeZone(getTimeZone(context));
    // printAverageFeaturePower(context);
    // If there are extra fields, include them into the sample.
    List<Feature> extras = getExtras(context);
    if (extras != null && extras.size() > 0)
        mySample.setExtra(extras);
    return mySample;
}
Also used : CpuStatus(edu.berkeley.cs.amplab.carat.thrift.CpuStatus) SharedPreferences(android.content.SharedPreferences) Sample(edu.berkeley.cs.amplab.carat.thrift.Sample) NetworkDetails(edu.berkeley.cs.amplab.carat.thrift.NetworkDetails) RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) ProcessInfo(edu.berkeley.cs.amplab.carat.thrift.ProcessInfo) Feature(edu.berkeley.cs.amplab.carat.thrift.Feature) BatteryDetails(edu.berkeley.cs.amplab.carat.thrift.BatteryDetails)

Example 3 with ProcessInfo

use of edu.berkeley.cs.amplab.carat.thrift.ProcessInfo in project carat by amplab.

the class SamplingLibrary method uninstalledItem.

/**
	 * Helper to set application to the uninstalled state in the Carat sample.
	 * @param pname the package that was uninstalled.
	 * @param pref The preference that stored the uninstallation directive. This preference will be deleted to ensure uninstallations are not sent multiple times.
	 * @param e the Editor (passed and not created here for efficiency)
	 * @return a new ProcessInfo entry describing the uninstalled item.
	 */
private static ProcessInfo uninstalledItem(String pname, String pref, SharedPreferences.Editor e) {
    ProcessInfo item = new ProcessInfo();
    item.setPName(pname);
    List<String> sigs = new LinkedList<String>();
    sigs.add("uninstalled");
    item.setAppSignatures(sigs);
    item.setPId(-1);
    item.setImportance(Constants.IMPORTANCE_UNINSTALLED);
    // Remember to remove it so we do not send
    // multiple uninstall events
    e.remove(pref);
    return item;
}
Also used : RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) ProcessInfo(edu.berkeley.cs.amplab.carat.thrift.ProcessInfo) LinkedList(java.util.LinkedList)

Example 4 with ProcessInfo

use of edu.berkeley.cs.amplab.carat.thrift.ProcessInfo in project carat by amplab.

the class SamplingLibrary method getInstalledPackage.

/**
	 * Returns info about an installed package. Will be called when receiving
	 * the PACKAGE_ADDED or PACKAGE_REPLACED intent.
	 * 
	 * @param context
	 * @param filterSystem
	 *            if true, exclude system packages.
	 * @return a list of installed packages on the device.
	 */
public static ProcessInfo getInstalledPackage(Context context, String pkg) {
    PackageManager pm = context.getPackageManager();
    if (pm == null)
        return null;
    PackageInfo pak;
    try {
        pak = pm.getPackageInfo(pkg, PackageManager.GET_SIGNATURES | PackageManager.GET_PERMISSIONS);
    } catch (NameNotFoundException e) {
        return null;
    }
    if (pak == null)
        return null;
    ProcessInfo pi = new ProcessInfo();
    int vc = pak.versionCode;
    ApplicationInfo info = pak.applicationInfo;
    String label = pm.getApplicationLabel(info).toString();
    int flags = pak.applicationInfo.flags;
    // Check if it is a system app
    boolean isSystemApp = (flags & ApplicationInfo.FLAG_SYSTEM) > 0;
    isSystemApp = isSystemApp || (flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) > 0;
    if (pak.signatures.length > 0) {
        List<String> sigList = getSignatures(pak);
        pi.setPName(pkg);
        pi.setApplicationLabel(label);
        pi.setVersionCode(vc);
        pi.setPId(-1);
        pi.setIsSystemApp(isSystemApp);
        pi.setAppSignatures(sigList);
        pi.setImportance(Constants.IMPORTANCE_NOT_RUNNING);
        pi.setInstallationPkg(pm.getInstallerPackageName(pkg));
        pi.setVersionName(pak.versionName);
    }
    return pi;
}
Also used : PackageManager(android.content.pm.PackageManager) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) PackageInfo(android.content.pm.PackageInfo) ApplicationInfo(android.content.pm.ApplicationInfo) RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) ProcessInfo(edu.berkeley.cs.amplab.carat.thrift.ProcessInfo)

Example 5 with ProcessInfo

use of edu.berkeley.cs.amplab.carat.thrift.ProcessInfo in project carat by amplab.

the class ProcessInfoAdapter method getView.

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.process, null);
        holder = new ViewHolder();
        holder.appIcon = (ImageView) convertView.findViewById(R.id.app_icon);
        holder.txtName = (TextView) convertView.findViewById(R.id.processName);
        holder.pkgName = (TextView) convertView.findViewById(R.id.pkgName);
        holder.txtBenefit = (TextView) convertView.findViewById(R.id.processPriority);
        // holder.moreInfo = (ImageView)
        // convertView.findViewById(R.id.moreinfo);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    if (searchArrayList == null || position < 0 || position >= searchArrayList.size())
        return convertView;
    ProcessInfo x = searchArrayList.get(position);
    if (x == null)
        return convertView;
    String p = x.getPName();
    PackageInfo pak = SamplingLibrary.getPackageInfo(c, p);
    String ver = "";
    if (pak != null) {
        ver = pak.versionName;
        if (ver == null)
            ver = pak.versionCode + "";
    }
    holder.appIcon.setImageDrawable(CaratApplication.iconForApp(c, p));
    holder.pkgName.setText(p);
    holder.txtName.setText(CaratApplication.labelForApp(c, p) + " " + ver);
    holder.txtBenefit.setText(CaratApplication.translatedPriority(x.getImportance()));
    return convertView;
}
Also used : PackageInfo(android.content.pm.PackageInfo) ProcessInfo(edu.berkeley.cs.amplab.carat.thrift.ProcessInfo)

Aggregations

ProcessInfo (edu.berkeley.cs.amplab.carat.thrift.ProcessInfo)12 RunningAppProcessInfo (android.app.ActivityManager.RunningAppProcessInfo)7 PackageInfo (android.content.pm.PackageInfo)4 ApplicationInfo (android.content.pm.ApplicationInfo)3 PackageManager (android.content.pm.PackageManager)3 Feature (edu.berkeley.cs.amplab.carat.thrift.Feature)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 SharedPreferences (android.content.SharedPreferences)2 BatteryDetails (edu.berkeley.cs.amplab.carat.thrift.BatteryDetails)2 CpuStatus (edu.berkeley.cs.amplab.carat.thrift.CpuStatus)2 NetworkDetails (edu.berkeley.cs.amplab.carat.thrift.NetworkDetails)2 Sample (edu.berkeley.cs.amplab.carat.thrift.Sample)2 Sample._Fields (edu.berkeley.cs.amplab.carat.thrift.Sample._Fields)2 LinkedList (java.util.LinkedList)2 FieldMetaData (org.apache.thrift.meta_data.FieldMetaData)2 RunningServiceInfo (android.app.ActivityManager.RunningServiceInfo)1 Editor (android.content.SharedPreferences.Editor)1 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)1 View (android.view.View)1