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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations