android: Add measurement collector for Installed Packages

This commit is contained in:
Tobias Brunner
2013-07-08 18:49:27 +02:00
parent 2d61172314
commit 7cb8f570ed
3 changed files with 126 additions and 0 deletions
@@ -18,6 +18,7 @@ package org.strongswan.android.logic.imc;
import org.strongswan.android.logic.imc.attributes.Attribute;
import org.strongswan.android.logic.imc.attributes.AttributeType;
import org.strongswan.android.logic.imc.collectors.Collector;
import org.strongswan.android.logic.imc.collectors.InstalledPackagesCollector;
import org.strongswan.android.logic.imc.collectors.ProductInformationCollector;
import org.strongswan.android.logic.imc.collectors.StringVersionCollector;
@@ -53,6 +54,9 @@ public class AndroidImc
case IETF_STRING_VERSION:
collector = new StringVersionCollector();
break;
case IETF_INSTALLED_PACKAGES:
collector = new InstalledPackagesCollector(mContext);
break;
default:
break;
}
@@ -0,0 +1,67 @@
/*
* Copyright (C) 2013 Tobias Brunner
* Copyright (C) 2012 Christoph Buehler
* Copyright (C) 2012 Patrick Loetscher
* Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program 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 General Public License
* for more details.
*/
package org.strongswan.android.logic.imc.attributes;
import java.util.LinkedList;
import org.strongswan.android.utils.BufferedByteWriter;
import android.util.Pair;
/**
* PA-TNC Installed Packages attribute (see section 4.2.7 of RFC 5792)
*
* 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Reserved | Package Count |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Pkg Name Len | Package Name (Variable Length) |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Version Len | Package Version Number (Variable Length) |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
public class InstalledPackagesAttribute implements Attribute
{
private final short RESERVED = 0;
private final LinkedList<Pair<String, String>> mPackages = new LinkedList<Pair<String, String>>();
/**
* Add an installed package to this attribute.
* @param name name of the package
* @param version version number of the package
*/
public void addPackage(String name, String version)
{
mPackages.add(new Pair<String, String>(name, version));
}
@Override
public byte[] getEncoding()
{
BufferedByteWriter writer = new BufferedByteWriter();
writer.put16(RESERVED);
writer.put16((short)mPackages.size());
for (Pair<String, String> pair : mPackages)
{
writer.putLen8(pair.first.getBytes());
writer.putLen8(pair.second.getBytes());
}
return writer.toByteArray();
}
}
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2013 Tobias Brunner
* Copyright (C) 2012 Christoph Buehler
* Copyright (C) 2012 Patrick Loetscher
* Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program 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 General Public License
* for more details.
*/
package org.strongswan.android.logic.imc.collectors;
import java.util.List;
import org.strongswan.android.logic.imc.attributes.Attribute;
import org.strongswan.android.logic.imc.attributes.InstalledPackagesAttribute;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
public class InstalledPackagesCollector implements Collector
{
private final PackageManager mPackageManager;
public InstalledPackagesCollector(Context context)
{
mPackageManager = context.getPackageManager();
}
@Override
public Attribute getMeasurement()
{
InstalledPackagesAttribute attribute = new InstalledPackagesAttribute();
List<PackageInfo> packages = mPackageManager.getInstalledPackages(0);
for (PackageInfo info : packages)
{
if ((info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0 ||
info.packageName == null || info.versionName == null)
{ /* ignore packages installed in the system image */
continue;
}
attribute.addPackage(info.packageName, info.versionName);
}
return attribute;
}
}