/* * Copyright (C) 2010 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.nfc; import static android.Manifest.permission.BIND_NFC_SERVICE; import static android.Manifest.permission.NFC; import android.annotation.NonNull; import android.annotation.Nullable; import android.app.PendingIntent; import android.app.PendingIntentProto; import android.content.AuthorityEntryProto; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.IntentFilterProto; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.net.Uri; import android.os.PatternMatcher; import android.os.PatternMatcherProto; import android.os.UserHandle; import android.text.TextUtils; import android.util.Log; import android.util.proto.ProtoOutputStream; import androidx.annotation.VisibleForTesting; import java.util.Arrays; import java.util.Objects; public final class Utils { private static final String TAG = "Utils"; private Utils() { } /** * Returns true if the given {@code array} contains the given element. * * @param array {@code array} to check for {@code elem} * @param elem {@code elem} to test for * @return {@code true} if the given element is contained */ public static boolean arrayContains(@Nullable T[] array, T elem) { if (array == null) { return false; } for (int i = 0; i < array.length; i++) { if (Objects.equals(array[i], elem)) { return true; } } return false; } /** Ported (this uses public API's as opposed to private fields used in the original impl from * {@link IntentFilter#dumpDebug(ProtoOutputStream, long)} */ public static void dumpDebugIntentFilter( @NonNull IntentFilter intentFilter, @NonNull ProtoOutputStream proto, long fieldId) { long token = proto.start(fieldId); for (int i = 0; i < intentFilter.countActions(); i++) { proto.write(IntentFilterProto.ACTIONS, intentFilter.getAction(i)); } for (int i = 0; i < intentFilter.countCategories(); i++) { proto.write(IntentFilterProto.CATEGORIES, intentFilter.getCategory(i)); } for (int i = 0; i < intentFilter.countDataSchemes(); i++) { proto.write(IntentFilterProto.DATA_SCHEMES, intentFilter.getDataScheme(i)); } for (int i = 0; i < intentFilter.countDataSchemeSpecificParts(); i++) { dumpDebugPatternMatcher( intentFilter.getDataSchemeSpecificPart(i), proto, IntentFilterProto.DATA_SCHEME_SPECS); } for (int i = 0; i < intentFilter.countDataAuthorities(); i++) { dumpDebugAuthorityEntry( intentFilter.getDataAuthority(i), proto, IntentFilterProto.DATA_AUTHORITIES); } for (int i = 0; i < intentFilter.countDataPaths(); i++) { dumpDebugPatternMatcher( intentFilter.getDataPath(i), proto, IntentFilterProto.DATA_PATHS); } for (int i = 0; i < intentFilter.countDataTypes(); i++) { proto.write(IntentFilterProto.DATA_TYPES, intentFilter.getDataType(i)); } /* for (int i = 0; i < intentFilter.countMimeGroups(); i++) { proto.write(IntentFilterProto.MIME_GROUPS, intentFilter.getMimeGroup(i)); }*/ if (intentFilter.getPriority() != 0 /* || TODO(b/271463752): Get this info - hasPartialTypes() */) { proto.write(IntentFilterProto.PRIORITY, intentFilter.getPriority()); proto.write(IntentFilterProto.HAS_PARTIAL_TYPES, false /* hasPartialTypes() */); } proto.write(IntentFilterProto.GET_AUTO_VERIFY, false /* intentFilter.getAutoVerify() */); proto.end(token); } /** Ported (this uses public API's as opposed to private fields used in the original impl from * {@link PatternMatcher#dumpDebug(ProtoOutputStream, long)} */ private static void dumpDebugPatternMatcher(@NonNull PatternMatcher patternMatcher, @NonNull ProtoOutputStream proto, long fieldId) { long token = proto.start(fieldId); proto.write(PatternMatcherProto.PATTERN, patternMatcher.getPath()); proto.write(PatternMatcherProto.TYPE, patternMatcher.getType()); // PatternMatcherProto.PARSED_PATTERN is too much to dump, but the field is reserved to // match the current data structure. proto.end(token); } /** Ported (this uses public API's as opposed to private fields used in the original impl from * {@link IntentFilter.AuthorityEntry#dumpDebug(ProtoOutputStream, long)} */ private static void dumpDebugAuthorityEntry( @NonNull IntentFilter.AuthorityEntry authorityEntry, ProtoOutputStream proto, long fieldId) { long token = proto.start(fieldId); // The public API's only give the orig host name. The fields in the proto are derived from // this host info. {@see AuthorityEntry{String, String} String origHost = authorityEntry.getHost(); boolean wild = origHost.length() > 0 && origHost.charAt(0) == '*'; String host = wild ? origHost.substring(1).intern() : origHost; proto.write(AuthorityEntryProto.HOST, host); proto.write(AuthorityEntryProto.WILD, wild); proto.write(AuthorityEntryProto.PORT, authorityEntry.getPort()); proto.end(token); } /** Ported (this uses public API's as opposed to private fields used in the original impl from * {@link PendingIntent#dumpDebug(ProtoOutputStream, long)} */ public static void dumpDebugPendingIntent( @NonNull PendingIntent pendingIntent, @NonNull ProtoOutputStream proto, long fieldId) { final long token = proto.start(fieldId); // TODO: This is not exactly the same format as the original impl. But, it still prints // the target info. proto.write(PendingIntentProto.TARGET, pendingIntent.toString()); proto.end(token); } @Nullable @VisibleForTesting public static String getPackageNameFromIntent(Intent intent) { Uri uri = intent.getData(); if (!Objects.equals(uri.getScheme(), "package")) return null; return uri.getSchemeSpecificPart(); } public static boolean hasCeServicesWithValidPermissions( Context context, Intent pkgChangeIntent, int userId) { String pkgName = Utils.getPackageNameFromIntent(pkgChangeIntent); if (pkgName == null) return true; // Cannot figure out pkg name, assume it's valid. PackageInfo packageInfo = null; try { PackageManager pm = context.createPackageContextAsUser("android", 0, UserHandle.of(userId)) .getPackageManager(); if (pm.checkPermission(NFC, pkgName) != PackageManager.PERMISSION_GRANTED) { return false; } packageInfo = pm.getPackageInfo(pkgName, (PackageManager.GET_PERMISSIONS | PackageManager.GET_SERVICES | PackageManager.MATCH_DISABLED_COMPONENTS)); if (packageInfo == null) return false; return Arrays.stream(packageInfo.services).map(serviceInfo->serviceInfo.permission) .filter(permission-> TextUtils.equals(permission, BIND_NFC_SERVICE )) .findFirst().isPresent(); } catch (PackageManager.NameNotFoundException | NullPointerException e) { Log.e(TAG, "Could not create user package context" + packageInfo); return false; } } public static String maskSubstring(String original, int start) { if (original == null) { return ""; } if (original.length() <= start) { return original; } StringBuilder masked = new StringBuilder(original); for (int i = start; i < original.length(); i++) { masked.setCharAt(i, '*'); } return masked.toString(); } }