private static String computePackageSignatureHash(PackageInfo packageInfo) { String signatureHash = ""; final Signature[] sigs = packageInfo.signatures; try { final MessageDigest messageDigest = MessageDigest.getInstance("SHA-1"); for (Signature s : sigs) { messageDigest.update(s.toByteArray()); } signatureHash = StringBuilderUtils.bytesToHexString(messageDigest.digest()); } catch (NoSuchAlgorithmException e) { LogUtils.log(CustomLabelManager.class, Log.WARN, "Unable to create SHA-1 MessageDigest"); } return signatureHash; }
/** * Creates a {@link Label} and persists it to the label database, and refreshes the label cache. * * @param resourceName The fully-qualified resource identifier, such as * "com.android.deskclock:id/analog_appwidget", as provided by {@link * AccessibilityNodeInfo#getViewIdResourceName()} * @param userLabel The label provided for the node by the user */ public void addLabel(String resourceName, String userLabel) { if (!isInitialized()) { return; } final String finalLabel; if (userLabel == null) { throw new IllegalArgumentException("Attempted to add a label with a null userLabel value"); } else { finalLabel = userLabel.trim(); if (TextUtils.isEmpty(finalLabel)) { throw new IllegalArgumentException( "Attempted to add a label with an empty userLabel value"); } } Pair<String, String> parsedId = splitResourceName(resourceName); if (parsedId == null) { LogUtils.log( this, Log.WARN, "Attempted to add a label with an invalid or poorly formed view ID."); return; } final PackageInfo packageInfo; try { packageInfo = mPackageManager.getPackageInfo(parsedId.first, PackageManager.GET_SIGNATURES); } catch (NameNotFoundException e) { LogUtils.log(this, Log.WARN, "Attempted to add a label for an unknown package."); return; } final String locale = Locale.getDefault().toString(); final int version = packageInfo.versionCode; final long timestamp = System.currentTimeMillis(); String signatureHash = ""; final Signature[] sigs = packageInfo.signatures; try { final MessageDigest messageDigest = MessageDigest.getInstance("SHA-1"); for (Signature s : sigs) { messageDigest.update(s.toByteArray()); } signatureHash = StringBuilderUtils.bytesToHexString(messageDigest.digest()); } catch (NoSuchAlgorithmException e) { LogUtils.log(this, Log.WARN, "Unable to create SHA-1 MessageDigest"); } // For the current implementation, screenshots are disabled final String screenshotPath = ""; final Label label = new Label( parsedId.first, signatureHash, parsedId.second, finalLabel, locale, version, screenshotPath, timestamp); final LabelAddRequest request = new LabelAddRequest(label, null); final LabelAddTask task = new LabelAddTask(); task.execute(request); }