/** * Initialises the {@link ProcessManifest} by parsing the manifest provided by the given {@link InputStream}. * * @param manifestIS InputStream for an AppManifest. * @throws IOException if an I/O error occurs. * @throws XmlPullParserException can occur due to a malformed manifest. */ protected void handle(InputStream manifestIS) throws IOException, XmlPullParserException { this.axml = new AXmlHandler(manifestIS); // get manifest node List<AXmlNode> manifests = this.axml.getNodesWithTag("manifest"); if(manifests.isEmpty()) throw new RuntimeException("Manifest contains no manifest node"); else if(manifests.size() > 1) throw new RuntimeException("Manifest contains more than one manifest node"); this.manifest = manifests.get(0); // get application node List<AXmlNode> applications = this.manifest.getChildrenWithTag("application"); if(applications.isEmpty()) throw new RuntimeException("Manifest contains no application node"); else if(applications.size() > 1) throw new RuntimeException("Manifest contains more than one application node"); this.application = applications.get(0); // Get components this.providers = this.axml.getNodesWithTag("provider"); this.services = this.axml.getNodesWithTag("service"); this.activities = this.axml.getNodesWithTag("activity"); this.receivers = this.axml.getNodesWithTag("receiver"); }
/** * Get the package name of the application * @param apkFileLocation * @return */ public static String getApplicationPackageName(String apkFileLocation) { String packageName = null; try { ProcessManifest pm = new ProcessManifest(apkFileLocation); AXmlHandler axmlh = pm.getAXml(); // Find main activity and remove main intent-filter List<AXmlNode> anodes = axmlh.getNodesWithTag("manifest"); for (AXmlNode an: anodes) { boolean hasMain = false; boolean hasLauncher = false; AXmlNode filter = null; AXmlAttribute aname = an.getAttribute("package"); String aval = (String)aname.getValue(); packageName = aval; System.out.println("package: "+ packageName); break; } } catch (IOException | XmlPullParserException ex) { System.err.println("Could not read Android manifest file: " + ex.getMessage()); throw new RuntimeException(ex); } return packageName; }
/** * Adds a new permission to the manifest. * @param complete permission name e.g. "android.permission.INTERNET" */ public void addPermission(String permissionName) { AXmlNode permission = new AXmlNode("uses-permission", null, manifest); AXmlAttribute<String> permissionNameAttr = new AXmlAttribute<String>("name", permissionName, AXmlHandler.ANDROID_NAMESPACE); permission.addAttribute(permissionNameAttr); }
/** * Get the name of the main activity in the AndroidManifest.xml file * @param apkFileLocation * @return */ public static String getMainActivityName(String apkFileLocation) { String mainActivityName = null; try { ProcessManifest pm = new ProcessManifest(apkFileLocation); AXmlHandler axmlh = pm.getAXml(); // Find main activity and remove main intent-filter List<AXmlNode> anodes = axmlh.getNodesWithTag("activity"); for (AXmlNode an: anodes) { boolean hasMain = false; boolean hasLauncher = false; AXmlNode filter = null; AXmlAttribute aname = an.getAttribute("name"); String aval = (String)aname.getValue(); System.out.println("activity: "+ aval); for (AXmlNode ch : an.getChildren()) { System.out.println("children: "+ ch); } List<AXmlNode> fnodes = an.getChildrenWithTag("intent-filter"); for (AXmlNode fn: fnodes) { hasMain = false; hasLauncher = false; // check action List<AXmlNode> acnodes = fn.getChildrenWithTag("action"); for (AXmlNode acn: acnodes) { AXmlAttribute acname = acn.getAttribute("name"); String acval = (String)acname.getValue(); System.out.println("action: "+ acval); if (acval.equals("android.intent.action.MAIN")) { hasMain = true; } } // check category List<AXmlNode> catnodes = fn.getChildrenWithTag("category"); for (AXmlNode catn: catnodes) { AXmlAttribute catname = catn.getAttribute("name"); String catval = (String)catname.getValue(); System.out.println("category: "+ catval); if (catval.equals("android.intent.category.LAUNCHER")) { hasLauncher = true; filter = fn; } } if (hasLauncher && hasMain) { break; } } if (hasLauncher && hasMain) { // replace name with the activity waiting for the connection to the PDP System.out.println("main activity is: "+ aval); System.out.println("excluding filter: "+ filter); filter.exclude(); mainActivityName = aval; break; } } } catch (IOException | XmlPullParserException ex) { System.err.println("Could not read Android manifest file: " + ex.getMessage()); throw new RuntimeException(ex); } return mainActivityName; }
/** * Returns the handler which parsed and holds the manifest's data. * * @return Android XML handler */ public AXmlHandler getAXml() { return this.axml; }