get Authority From Permission - Android android.app

Android examples for android.app:Permission

Description

get Authority From Permission

Demo Code

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ProviderInfo;
import java.util.List;

public class Main{

    public static String getAuthorityFromPermission(Context context,
            String permission) {/*from   ww w. j ava 2s  . c  o m*/
        if (context == null || permission == null) {
            return null;
        }

        List<PackageInfo> packageInfoList = context.getPackageManager()
                .getInstalledPackages(PackageManager.GET_PROVIDERS);

        if (packageInfoList != null) {
            for (PackageInfo packageInfo : packageInfoList) {
                ProviderInfo[] providerInfos = packageInfo.providers;
                if (providerInfos != null) {
                    for (ProviderInfo providerInfo : providerInfos) {
                        if (permission.equals(providerInfo.readPermission)
                                || permission
                                        .equals(providerInfo.writePermission)) {
                            return providerInfo.authority;
                        }
                    }
                }
            }
        }

        return null;
    }

}

Related Tutorials