!!!!!!!!Listing 1 ///Überschrift: Definition eines Eingabefelds mit festgelegtem Typ
!!!!!!!!Listing 2 ///Überschrift: Beispielhafte Definition eines Intent Filters
!!!!!!!!Listing 3 ///Überschrift: Exemplarische Methode zur Validierung von Intents
// Namen der erlaubten Extra-Attribute
final static ArrayList extraList =
new ArrayList(Arrays.asList(
"extra1",
"extra2"
));
protected boolean checkIntent(Intent intent) {
// Diese Methode geht davon aus, dass
// Action, Category und Scheme deklarativ
// im Manifest vorgefiltert wurden.
// Check 1: Prüfung auf DEFAULT-Kategorie
Set categories = intent.getCategories();
if (categories == null)
return false;
// Check 3: Alle erwarteten Extras vorhanden?
Bundle extras = intent.getExtras();
if (extras == null)
return false;
for (String e: extraList) {
if (!extras.containsKey(e))
return false;
}
// Check 4: Nicht erwartete Extras?
if (extras.size() > extraList.size())
return false;
// Check 5: Prüfung des URI-Formats
String data = intent.getDataString();
if (data == null)
return false;
// Nachfolgend App-spezifische Prüfung
// von data, z.B. mit regulärem Ausdruck
// ...
// Check 6: Prüfung des Absenders
String caller = this.getCallingPackage();
if (caller == null)
return false;
if (!caller.equals("ix.intentdemo"))
return false;
return true;
}
!!!!!!!!Listing 4 ///Überschrift: Installation eines CA-Zertifikats mit Android 4.0
final static int BUFSIZE = 2048;
Intent installIntent;
// Intent erzeugen
installIntent = KeyChain.createInstallIntent();
// Name setzen
installIntent.putExtra(KeyChain.EXTRA_NAME, "My CA Cert");
// Zertifikat einlesen...
BufferedInputStream is = new BufferedInputStream(
getResources().openRawResource(R.raw.cacert), BUFSIZE);
byte[] buf = new byte[BUFSIZE];
int certSize = is.read(buf);
// ...und dem Intent beifügen
installIntent.putExtra(KeyChain.EXTRA_CERTIFICATE,
Arrays.copyOf(buf, certSize));
startActivityForResult(installIntent, 10);
!!!!!!!!Listing 5 ///Überschrift: Einfacher Root Checker
// Dateien, die auf Rooting deuten
final static String[] blacklist = {
"/system/bin/su",
"/system/xbin/su",
"/system/app/Superuser.apk",
"/data/app/com.noshufou.android.su.apk",
"/data/app/com.z4mod.z4root.apk"
};
final static ArrayList suidRootWhitelist = new ArrayList(Arrays.asList(
"/system/bin/run-as"
// ggf. gerätespezifisch anpassen und erweitern
));
public boolean isRooted() {
// Erste Stufe: Existenz von Dateien der Blacklist prüfen
for (String fileName : blacklist) {
if (new File(fileName).exists()) {
return true;
}
}
// Zweite Stufe: SUID-root-Programme prüfen
boolean rooted = false;
// Regulärer Ausdruck fuer SUID-root in der Ausgabe von ls -l
Pattern pattern = Pattern.compile("^-..s...... root");
try {
Process proc = Runtime.getRuntime().exec("ls -l -R /system");
BufferedReader in =
new BufferedReader(new InputStreamReader(proc.getInputStream()), 8192);
String dirEntry;
String currentDir = "";
while ( ((dirEntry = in.readLine()) != null) && (!rooted)) {
// Falls Eintrag ein Verzeichnisname ist: Pfad merken
if (dirEntry.startsWith("/"))
currentDir = dirEntry.substring(0, dirEntry.length()-1);
Matcher matcher = pattern.matcher(dirEntry);
// Falls SUID-root-Programm
if (matcher.find()) {
rooted = true;
// absoluten Pfad konstruieren
String fileName = currentDir + "/" +
dirEntry.substring(dirEntry.lastIndexOf(" ") +1);
// Prüfen, ob gefundenes Programm auf Whitelist steht
if (suidRootWhitelist.contains(fileName))
rooted = false;
}
}
in.close();
proc.waitFor();
}
catch ...
return rooted;
}
!!!!!!!!Listing 6 ///Überschrift:catch-all Exception Handler
Thread.setDefaultUncaughtExceptionHandler(
new Thread.UncaughtExceptionHandler() {
public void uncaughtException(Thread thread, Throwable ex) {
Runtime.getRuntime().exit(1);
}
});