0

我面临与 WhatsApp 贴纸实施相关的问题。我已经使用了这个库,我想检查 WhatsApp 中是否已经添加了特定的贴纸。

我怎样才能做到这一点?请指导我。

4

1 回答 1

0

我使用了这个库中的以下文件。isPackageInstalled() 函数对我有用(注意:我只希望这个功能只适用于 Android)

        package org.roborox.whatsapp;
    
    import android.content.ContentResolver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.pm.ApplicationInfo;
    import android.content.pm.PackageManager;
    import android.content.pm.ProviderInfo;
    import android.database.Cursor;
    import android.net.Uri;
    
    import androidx.annotation.NonNull;

public class WhitelistCheck {
    private static final String AUTHORITY_QUERY_PARAM = "authority";
    private static final String IDENTIFIER_QUERY_PARAM = "identifier";
    private static String STICKER_APP_AUTHORITY = "sticker_pack_authority";// StickerContentProvider.authority;
    public static String CONSUMER_WHATSAPP_PACKAGE_NAME = "com.whatsapp";
    public static String INSTAGRAM_PACKAGE_NAME = "com.instagram.android";
    public static String SMB_WHATSAPP_PACKAGE_NAME = "com.whatsapp.w4b";
    private static String CONTENT_PROVIDER = ".provider.sticker_whitelist_check";
    private static String QUERY_PATH = "is_whitelisted";
    private static String QUERY_RESULT_COLUMN_NAME = "result";

public static boolean isWhitelisted(@NonNull Context context, @NonNull String identifier) {
    try {
        boolean consumerResult = isWhitelistedFromProvider(context, identifier, CONSUMER_WHATSAPP_PACKAGE_NAME);
        boolean smbResult = isWhitelistedFromProvider(context, identifier, SMB_WHATSAPP_PACKAGE_NAME);
        return consumerResult && smbResult;
    } catch (Exception e) {
        return false;
    }
}

private static boolean isWhitelistedFromProvider(@NonNull Context context, @NonNull String identifier, String whatsappPackageName) {
    final PackageManager packageManager = context.getPackageManager();
    if (isPackageInstalled(whatsappPackageName, packageManager)) {
        final String whatsappProviderAuthority = whatsappPackageName + CONTENT_PROVIDER;
        final ProviderInfo providerInfo = packageManager.resolveContentProvider(whatsappProviderAuthority, PackageManager.GET_META_DATA);
        // provider is not there.
        if (providerInfo == null) {
            return false;
        }
        final Uri queryUri = new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(whatsappProviderAuthority).appendPath(QUERY_PATH).
                appendQueryParameter(AUTHORITY_QUERY_PARAM, BuildConfig.CONTENT_PROVIDER_AUTHORITY).appendQueryParameter(IDENTIFIER_QUERY_PARAM, identifier).build();
        final Cursor cursor = context.getContentResolver().query(queryUri, null, null, null, null);

        if (cursor != null && cursor.moveToFirst()) {
            final int whiteListResult = cursor.getInt(cursor.getColumnIndexOrThrow(QUERY_RESULT_COLUMN_NAME));
            return whiteListResult == 1;
        }

    } else {
        //if app is not installed, then don't need to take into its whitelist info into account.
        return true;
    }
    return false;
}

public static boolean isPackageInstalled(String packageName, PackageManager packageManager) {
    try {
        final ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName, 0);
        //noinspection SimplifiableIfStatement
        if (applicationInfo != null) {
            return applicationInfo.enabled;
        } else {
            return false;
        }
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}


public static boolean redirectToInstagram(Context context) {
    final PackageManager packageManager = context.getPackageManager();
    if (isPackageInstalled(INSTAGRAM_PACKAGE_NAME, packageManager)) {
        Intent i = context.getPackageManager().getLaunchIntentForPackage(INSTAGRAM_PACKAGE_NAME);
        context.startActivity(i);
        return true;
    } else {
        return false;
        }
    }
}
于 2021-01-28T07:19:03.130 回答