Search in sources :

Example 6 with ProcessInfo

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

the class SampleReader method writeSample.

/**
     * For simplicity, this method relies on the fact that
     * only the piList of Sample has any List type elements.
     * This will fail to record those from substructs (NetworkDetails, BatteryDetails, CpuStatus),
     * and will need to be changed if those are added.
     * 
     * Does not record CallInfo, CellInfo, or CallMonth types.
     */
public static final HashMap<String, String> writeSample(Sample s) {
    HashMap<String, String> m = new HashMap<String, String>();
    for (_Fields sf : Sample.metaDataMap.keySet()) {
        FieldMetaData md = Sample.metaDataMap.get(sf);
        switch(md.valueMetaData.type) {
            case org.apache.thrift.protocol.TType.STRING:
                m.put(sf.getFieldName(), cleanStr(s.getFieldValue(sf).toString()));
                break;
            case org.apache.thrift.protocol.TType.I32:
            case org.apache.thrift.protocol.TType.DOUBLE:
                m.put(sf.getFieldName(), s.getFieldValue(sf).toString());
                break;
            case org.apache.thrift.protocol.TType.STRUCT:
                if (md.fieldName.equals(Sample._Fields.NETWORK_DETAILS.getFieldName()) && s.networkDetails != null) {
                    int len = NetworkDetails._Fields.values().length;
                    StringBuilder b = new StringBuilder();
                    for (int i = 1; i <= len; i++) {
                        b.append(cleanStr("" + s.networkDetails.getFieldValue(NetworkDetails._Fields.findByThriftId(i))));
                        if (i < len)
                            b.append("\n");
                    }
                    m.put(sf.getFieldName(), b.toString());
                } else if (md.fieldName.equals(Sample._Fields.BATTERY_DETAILS.getFieldName()) && s.batteryDetails != null) {
                    int len = BatteryDetails._Fields.values().length;
                    StringBuilder b = new StringBuilder();
                    for (int i = 1; i <= len; i++) {
                        b.append(cleanStr("" + s.batteryDetails.getFieldValue(BatteryDetails._Fields.findByThriftId(i))));
                        if (i < len)
                            b.append("\n");
                    }
                    m.put(sf.getFieldName(), b.toString());
                } else if (md.fieldName.equals(Sample._Fields.CPU_STATUS.getFieldName()) && s.cpuStatus != null) {
                    int len = CpuStatus._Fields.values().length;
                    StringBuilder b = new StringBuilder();
                    for (int i = 1; i <= len; i++) {
                        b.append(cleanStr("" + s.cpuStatus.getFieldValue(CpuStatus._Fields.findByThriftId(i))));
                        if (i < len)
                            b.append("\n");
                    }
                    m.put(sf.getFieldName(), b.toString());
                }
                /*
                  * else if (md.fieldName.equals("CallInfo")){ }
                  */
                break;
            case org.apache.thrift.protocol.TType.LIST:
                if (md.fieldName.equals(Sample._Fields.EXTRA.getFieldName()) && s.extra != null) {
                    StringBuilder b = new StringBuilder();
                    for (Feature f : s.extra) {
                        b.append(cleanStr(f.key) + ";" + cleanStr(f.value) + "\n");
                    }
                    if (b.length() > 1)
                        b.deleteCharAt(b.lastIndexOf("\n"));
                    m.put(sf.getFieldName(), b.toString());
                } else if (md.fieldName.equals(Sample._Fields.LOCATION_PROVIDERS.getFieldName()) && s.locationProviders != null) {
                    StringBuilder b = new StringBuilder();
                    for (String lp : s.locationProviders) b.append(lp + "\n");
                    if (b.length() > 1)
                        b.deleteCharAt(b.lastIndexOf("\n"));
                    m.put(sf.getFieldName(), b.toString());
                } else if (md.fieldName.equals(Sample._Fields.PI_LIST.getFieldName()) && s.piList != null) {
                    StringBuilder b = new StringBuilder();
                    for (ProcessInfo pi : s.piList) {
                        int len = ProcessInfo._Fields.values().length;
                        for (int i = 1; i <= len; i++) {
                            ProcessInfo._Fields pif = ProcessInfo._Fields.findByThriftId(i);
                            FieldMetaData pmd = ProcessInfo.metaDataMap.get(pif);
                            if (pmd.valueMetaData.type == org.apache.thrift.protocol.TType.LIST) {
                                if (pi.appSignatures != null) {
                                    for (int j = 0; j < pi.appSignatures.size(); j++) {
                                        String sig = pi.appSignatures.get(j);
                                        b.append(sig);
                                        if (j + 1 < len)
                                            b.append("#");
                                    }
                                }
                            } else {
                                b.append(cleanStr("" + pi.getFieldValue(pif)));
                            }
                            if (i < len)
                                b.append(";");
                        }
                        b.append("\n");
                    }
                    if (b.length() > 1)
                        b.deleteCharAt(b.lastIndexOf("\n"));
                    m.put(sf.getFieldName(), b.toString());
                }
                break;
            default:
        }
    }
    return m;
}
Also used : Sample._Fields(edu.berkeley.cs.amplab.carat.thrift.Sample._Fields) FieldMetaData(org.apache.thrift.meta_data.FieldMetaData) HashMap(java.util.HashMap) ProcessInfo(edu.berkeley.cs.amplab.carat.thrift.ProcessInfo) Feature(edu.berkeley.cs.amplab.carat.thrift.Feature)

Example 7 with ProcessInfo

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

the class SampleReader method readSample.

/**
     * Read a Sample from a HashMap stored in the Carat Sample db.
     * @param data
     * @return
     */
public static final Sample readSample(Object data) {
    Sample s = null;
    if (data != null && data instanceof HashMap<?, ?>) {
        HashMap<String, String> m = (HashMap<String, String>) data;
        s = new Sample();
        NetworkDetails n = new NetworkDetails();
        BatteryDetails bd = new BatteryDetails();
        // CellInfo ci = new CellInfo();
        // CallInfo calli = new CallInfo();
        // CallMonth cm = new CallMonth();
        CpuStatus cs = new CpuStatus();
        // Set single fields automatically:
        for (String k : m.keySet()) {
            _Fields sf = Sample._Fields.findByName(k);
            if (sf != null) {
                // Top level Sample field.
                FieldMetaData md = Sample.metaDataMap.get(sf);
                String cleaned = origStr(m.get(k));
                switch(md.valueMetaData.type) {
                    case org.apache.thrift.protocol.TType.STRING:
                        s.setFieldValue(sf, cleaned);
                        break;
                    case org.apache.thrift.protocol.TType.I32:
                        try {
                            s.setFieldValue(sf, Integer.parseInt(cleaned));
                        } catch (NumberFormatException e) {
                            Log.e(TAG, "Could not read " + md.fieldName + ": \"" + cleaned + "\" as an int");
                        }
                        break;
                    case org.apache.thrift.protocol.TType.DOUBLE:
                        try {
                            s.setFieldValue(sf, Double.parseDouble(cleaned));
                        } catch (NumberFormatException e) {
                            Log.e(TAG, "Could not read " + md.fieldName + ": \"" + cleaned + "\" as a double");
                        }
                        break;
                    case org.apache.thrift.protocol.TType.STRUCT:
                        if (md.fieldName.equals(Sample._Fields.NETWORK_DETAILS.getFieldName())) {
                            fillNetworkDetails(m.get(k), n);
                            s.setNetworkDetails(n);
                        } else if (md.fieldName.equals(Sample._Fields.BATTERY_DETAILS.getFieldName())) {
                            fillBatteryDetails(m.get(k), bd);
                            s.setBatteryDetails(bd);
                        } else if (md.fieldName.equals(Sample._Fields.CPU_STATUS.getFieldName())) {
                            fillCpuStatusDetails(m.get(k), cs);
                            s.setCpuStatus(cs);
                        }
                        /*
                          * else if (md.fieldName.equals("CallInfo")){ }
                          */
                        break;
                    case org.apache.thrift.protocol.TType.LIST:
                        if (md.fieldName.equals(Sample._Fields.EXTRA.getFieldName())) {
                            List<Feature> list = new LinkedList<Feature>();
                            String[] extras = m.get(k).split("\n");
                            for (String e : extras) {
                                Feature f = new Feature();
                                String[] feat = e.split(";");
                                if (feat.length > 1) {
                                    f.setKey(origStr(feat[0]));
                                    f.setValue(origStr(feat[1]));
                                }
                                list.add(f);
                            }
                            s.setExtra(list);
                        } else if (md.fieldName.equals(Sample._Fields.LOCATION_PROVIDERS.getFieldName())) {
                            List<String> list = new LinkedList<String>();
                            String[] arr = m.get(k).split("\n");
                            for (String lp : arr) list.add(lp);
                            s.setLocationProviders(list);
                        } else if (md.fieldName.equals(Sample._Fields.PI_LIST.getFieldName())) {
                            // Set piList fields automatically:
                            LinkedList<ProcessInfo> piList = new LinkedList<ProcessInfo>();
                            String[] processes = m.get(md.fieldName).split("\n");
                            for (String process : processes) {
                                String[] items = process.split(";");
                                ProcessInfo pi = new ProcessInfo();
                                /*
                                 * Items are in the same order as they appear in ProcessInfo
                                 * protocol class, so I can use Thrift ID for setting the fields
                                 * automatically.
                                 */
                                for (int i = 1; i <= items.length; i++) {
                                    if (items[i - 1] == null)
                                        continue;
                                    ProcessInfo._Fields pif = ProcessInfo._Fields.findByThriftId(i);
                                    FieldMetaData pmd = ProcessInfo.metaDataMap.get(pif);
                                    cleaned = origStr(items[i - 1]);
                                    switch(pmd.valueMetaData.type) {
                                        case org.apache.thrift.protocol.TType.STRING:
                                            pi.setFieldValue(pif, cleaned);
                                            break;
                                        case org.apache.thrift.protocol.TType.I32:
                                            try {
                                                pi.setFieldValue(pif, Integer.parseInt(cleaned));
                                            } catch (NumberFormatException e) {
                                                Log.e(TAG, "Could not read " + md.fieldName + ": \"" + cleaned + "\" as an int");
                                            }
                                            break;
                                        case org.apache.thrift.protocol.TType.DOUBLE:
                                            try {
                                                pi.setFieldValue(pif, Double.parseDouble(cleaned));
                                            } catch (NumberFormatException e) {
                                                Log.e(TAG, "Could not read " + md.fieldName + ": \"" + cleaned + "\" as a double");
                                            }
                                            break;
                                        case org.apache.thrift.protocol.TType.BOOL:
                                            try {
                                                pi.setFieldValue(pif, Boolean.parseBoolean(cleaned));
                                            } catch (NumberFormatException e) {
                                                Log.e(TAG, "Could not read " + md.fieldName + ": \"" + cleaned + "\" as a bool");
                                            }
                                            break;
                                        case org.apache.thrift.protocol.TType.LIST:
                                            List<String> list = new LinkedList<String>();
                                            String[] arr = cleaned.split("#");
                                            for (String sig : arr) list.add(sig);
                                            pi.setFieldValue(pif, list);
                                            break;
                                        default:
                                    }
                                }
                                piList.add(pi);
                            }
                            s.setPiList(piList);
                        }
                        break;
                    default:
                }
            }
        }
    }
    return s;
}
Also used : CpuStatus(edu.berkeley.cs.amplab.carat.thrift.CpuStatus) Sample._Fields(edu.berkeley.cs.amplab.carat.thrift.Sample._Fields) HashMap(java.util.HashMap) Sample(edu.berkeley.cs.amplab.carat.thrift.Sample) NetworkDetails(edu.berkeley.cs.amplab.carat.thrift.NetworkDetails) ProcessInfo(edu.berkeley.cs.amplab.carat.thrift.ProcessInfo) Feature(edu.berkeley.cs.amplab.carat.thrift.Feature) LinkedList(java.util.LinkedList) FieldMetaData(org.apache.thrift.meta_data.FieldMetaData) BatteryDetails(edu.berkeley.cs.amplab.carat.thrift.BatteryDetails) List(java.util.List) LinkedList(java.util.LinkedList)

Example 8 with ProcessInfo

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

the class ProcessListFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.processlist, container, false);
    ListView lv = (ListView) view.findViewById(R.id.processList);
    List<ProcessInfo> searchResults = SamplingLibrary.getRunningAppInfo(getActivity());
    lv.setAdapter(new ProcessInfoAdapter(getActivity(), searchResults));
    Tracker tracker = Tracker.getInstance();
    tracker.trackUser("ProcessList");
    return view;
}
Also used : Tracker(edu.berkeley.cs.amplab.carat.android.utils.Tracker) ProcessInfoAdapter(edu.berkeley.cs.amplab.carat.android.lists.ProcessInfoAdapter) ListView(android.widget.ListView) ProcessInfo(edu.berkeley.cs.amplab.carat.thrift.ProcessInfo) View(android.view.View) ListView(android.widget.ListView)

Example 9 with ProcessInfo

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

the class CommunicationManager method getQuickHogsAndMaybeRegister.

private boolean getQuickHogsAndMaybeRegister(String uuid, String os, String model) {
    if (System.currentTimeMillis() - CaratApplication.storage.getQuickHogsFreshness() < Constants.FRESHNESS_TIMEOUT_QUICKHOGS)
        return false;
    CaratService.Client instance = null;
    try {
        instance = ProtocolClient.open(a.getApplicationContext());
        Registration registration = new Registration(uuid);
        registration.setPlatformId(model);
        registration.setSystemVersion(os);
        registration.setTimestamp(System.currentTimeMillis() / 1000.0);
        List<ProcessInfo> pi = SamplingLibrary.getRunningAppInfo(a.getApplicationContext());
        List<String> processList = new ArrayList<String>();
        for (ProcessInfo p : pi) processList.add(p.pName);
        HogBugReport r = instance.getQuickHogsAndMaybeRegister(registration, processList);
        // ProtocolClient.close();
        if (r != null) {
            CaratApplication.storage.writeHogReport(r);
            CaratApplication.storage.writeQuickHogsFreshness();
        }
        // Assume freshness written by caller.
        // s.writeFreshness();
        safeClose(instance);
        return true;
    } catch (Throwable th) {
        Log.e(TAG, "Error refreshing main reports.", th);
        safeClose(instance);
    }
    return false;
}
Also used : CaratService(edu.berkeley.cs.amplab.carat.thrift.CaratService) HogBugReport(edu.berkeley.cs.amplab.carat.thrift.HogBugReport) Registration(edu.berkeley.cs.amplab.carat.thrift.Registration) ArrayList(java.util.ArrayList) ProcessInfo(edu.berkeley.cs.amplab.carat.thrift.ProcessInfo)

Example 10 with ProcessInfo

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

the class SamplingLibrary method getRunningProcessInfoForSample.

/**
	 * Returns a List of ProcessInfo objects, helper for getSample.
	 * 
	 * @param context the Context.
	 * @return a List of ProcessInfo objects, helper for getSample.
	 */
private static List<ProcessInfo> getRunningProcessInfoForSample(Context context) {
    SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(context);
    // Reset list for each sample
    runningAppInfo = null;
    List<ProcessInfo> list = getRunningAppInfo(context);
    List<ProcessInfo> result = new ArrayList<ProcessInfo>();
    PackageManager pm = context.getPackageManager();
    // Collected in the same loop to save computation.
    int[] procMem = new int[list.size()];
    Set<String> procs = new HashSet<String>();
    boolean inst = p.getBoolean(Constants.PREFERENCE_SEND_INSTALLED_PACKAGES, true);
    Map<String, ProcessInfo> ipkg = null;
    if (inst)
        ipkg = getInstalledPackages(context, false);
    for (ProcessInfo pi : list) {
        String pname = pi.getPName();
        if (ipkg != null && ipkg.containsKey(pname))
            ipkg.remove(pname);
        procs.add(pname);
        ProcessInfo item = new ProcessInfo();
        PackageInfo pak = getPackageInfo(context, pname);
        if (pak != null) {
            String ver = pak.versionName;
            int vc = pak.versionCode;
            item.setVersionName(ver);
            item.setVersionCode(vc);
            ApplicationInfo info = pak.applicationInfo;
            // Human readable label (if any)
            String label = pm.getApplicationLabel(info).toString();
            if (label != null && label.length() > 0)
                item.setApplicationLabel(label);
            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;
            item.setIsSystemApp(isSystemApp);
        /*
				 * boolean sigSent = p.getBoolean(SIG_SENT_256 + pname, false);
				 * if (collectSignatures && !sigSent && pak.signatures != null
				 * && pak.signatures.length > 0) { List<String> sigList =
				 * getSignatures(pak); boolean sigSentOld =
				 * p.getBoolean(SIG_SENT + pname, false); if (sigSentOld)
				 * p.edit().remove(SIG_SENT + pname);
				 * p.edit().putBoolean(SIG_SENT_256 + pname, true).commit();
				 * item.setAppSignatures(sigList); }
				 */
        }
        item.setImportance(pi.getImportance());
        item.setPId(pi.getPId());
        item.setPName(pname);
        String installationSource = null;
        if (!pi.isSystemApp) {
            try {
                // Log.w(STAG, "Calling getInstallerPackageName with: " +
                // pname);
                installationSource = pm.getInstallerPackageName(pname);
            } catch (IllegalArgumentException iae) {
                Log.e(STAG, "Could not get installer for " + pname);
            }
        }
        if (installationSource == null)
            installationSource = "null";
        item.setInstallationPkg(installationSource);
        // procMem[list.indexOf(pi)] = pi.getPId();
        // FIXME: More fields will need to be added here, but ProcessInfo
        // needs to change.
        /*
			 * uid lru
			 */
        // add to result
        result.add(item);
    }
    // Send installed packages if we were to do so.
    if (ipkg != null && ipkg.size() > 0) {
        result.addAll(ipkg.values());
        p.edit().putBoolean(Constants.PREFERENCE_SEND_INSTALLED_PACKAGES, false).commit();
    }
    // Go through the preferences and look for UNINSTALL, INSTALL and
    // REPLACE keys set by InstallReceiver.
    Set<String> ap = p.getAll().keySet();
    SharedPreferences.Editor e = p.edit();
    boolean edited = false;
    for (String pref : ap) {
        if (pref.startsWith(INSTALLED)) {
            String pname = pref.substring(INSTALLED.length());
            boolean installed = p.getBoolean(pref, false);
            if (installed) {
                Log.i(STAG, "Installed:" + pname);
                ProcessInfo i = getInstalledPackage(context, pname);
                if (i != null) {
                    i.setImportance(Constants.IMPORTANCE_INSTALLED);
                    result.add(i);
                    e.remove(pref);
                    edited = true;
                }
            }
        } else if (pref.startsWith(REPLACED)) {
            String pname = pref.substring(REPLACED.length());
            boolean replaced = p.getBoolean(pref, false);
            if (replaced) {
                Log.i(STAG, "Replaced:" + pname);
                ProcessInfo i = getInstalledPackage(context, pname);
                if (i != null) {
                    i.setImportance(Constants.IMPORTANCE_REPLACED);
                    result.add(i);
                    e.remove(pref);
                    edited = true;
                }
            }
        } else if (pref.startsWith(UNINSTALLED)) {
            String pname = pref.substring(UNINSTALLED.length());
            boolean uninstalled = p.getBoolean(pref, false);
            if (uninstalled) {
                Log.i(STAG, "Uninstalled:" + pname);
                result.add(uninstalledItem(pname, pref, e));
                edited = true;
            }
        } else if (pref.startsWith(DISABLED)) {
            String pname = pref.substring(DISABLED.length());
            boolean disabled = p.getBoolean(pref, false);
            if (disabled) {
                Log.i(STAG, "Disabled app:" + pname);
                result.add(disabledItem(pname, pref, e));
                edited = true;
            }
        }
    }
    if (edited)
        e.commit();
    return result;
}
Also used : SharedPreferences(android.content.SharedPreferences) PackageInfo(android.content.pm.PackageInfo) ArrayList(java.util.ArrayList) ApplicationInfo(android.content.pm.ApplicationInfo) RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) ProcessInfo(edu.berkeley.cs.amplab.carat.thrift.ProcessInfo) PackageManager(android.content.pm.PackageManager) Editor(android.content.SharedPreferences.Editor) HashSet(java.util.HashSet)

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