Example usage for android.content.pm PackageManager GET_PROVIDERS

List of usage examples for android.content.pm PackageManager GET_PROVIDERS

Introduction

In this page you can find the example usage for android.content.pm PackageManager GET_PROVIDERS.

Prototype

int GET_PROVIDERS

To view the source code for android.content.pm PackageManager GET_PROVIDERS.

Click Source Link

Document

PackageInfo flag: return information about content providers in the package in PackageInfo#providers .

Usage

From source file:Main.java

public static String getAuthorityFromPermission(Context context, String permission) {
    if (permission == null)
        return null;

    List<PackageInfo> packs = context.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS);
    if (null == packs) {
        return null;
    }//from   w w w . j a  v a  2 s .c o  m

    for (PackageInfo pack : packs) {
        ProviderInfo[] providers = pack.providers;
        if (null == providers)
            continue;

        for (ProviderInfo provider : providers) {
            if (provider.readPermission != null && (provider.readPermission).contains(permission)) {
                return provider.authority;
            }
        }
    }

    return null;
}

From source file:Main.java

public static String getAuthorityFromPermission(Context context, String permission) {
    if (TextUtils.isEmpty(permission)) {
        return null;
    }//  ww  w  . java  2 s.  c o m
    List<PackageInfo> packs = context.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS);
    if (packs == null) {
        return null;
    }
    for (PackageInfo pack : packs) {
        ProviderInfo[] providers = pack.providers;
        if (providers != null) {
            for (ProviderInfo provider : providers) {
                if (permission.equals(provider.readPermission) || permission.equals(provider.writePermission)) {
                    return provider.authority;
                }
            }
        }
    }
    return null;

}

From source file:Main.java

public static void getAuthorityFromPermission(Activity context) {
    String read_settings = "com.android.launcher.permission.READ_SETTINGS";
    String write_settings = "com.android.launcher.permission.WRITE_SETTINGS";
    List<PackageInfo> packs = context.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS);
    if (packs != null) {
        for (PackageInfo pack : packs) {
            ProviderInfo[] providers = pack.providers;
            if (providers != null) {
                for (ProviderInfo provider : providers) {
                    if ((read_settings.equals(provider.readPermission)
                            || (write_settings.equals(provider.writePermission)))) {
                        Toast.makeText(context, pack.applicationInfo.packageName, Toast.LENGTH_LONG).show();
                        if (isExistShortcut(context, provider.authority)) {
                            continue;
                        } else {
                            /*
                             * ShotcutUtil.createShortcut(context, SplashActivity.class,
                             * R.string.app_name, R.drawable.icon, false);
                             *///from w  w w . jav  a 2 s  . com
                            continue;

                        }

                    }
                }
            }
        }
    }
    return;
}

From source file:com.github.michalbednarski.intentslab.AppComponentsFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mPackageName = ((AppInfoHost) getActivity()).getViewedPackageName();

    final PackageManager packageManager = getActivity().getPackageManager();

    ArrayList<Integer> presentSections = new ArrayList<Integer>();

    try {/*from  ww  w .j  a  v  a 2s .  co  m*/
        final PackageInfo packageInfo = packageManager.getPackageInfo(mPackageName,
                PackageManager.GET_ACTIVITIES | PackageManager.GET_RECEIVERS | PackageManager.GET_SERVICES
                        | PackageManager.GET_PROVIDERS | PackageManager.GET_PERMISSIONS);

        // Scan activities and group them as exported and not exported
        {
            ArrayList<ActivityInfo> exportedActivities = new ArrayList<ActivityInfo>();
            ArrayList<ActivityInfo> notExportedActivities = new ArrayList<ActivityInfo>();

            if (packageInfo.activities != null && packageInfo.activities.length != 0) {
                for (ActivityInfo activity : packageInfo.activities) {
                    (activity.exported ? exportedActivities : notExportedActivities).add(activity);
                }
            }

            if (exportedActivities.size() != 0) {
                mActivities = exportedActivities.toArray(new ComponentInfo[exportedActivities.size()]);
                presentSections.add(SECTION_ACTIVITIES);
            }
            if (notExportedActivities.size() != 0) {
                mActivitiesNotExported = notExportedActivities
                        .toArray(new ComponentInfo[notExportedActivities.size()]);
                presentSections.add(SECTION_ACTIVITIES_NOT_EXPORTED);
            }
        }

        // Check receivers, services and providers
        if (packageInfo.receivers != null) {
            mReceivers = packageInfo.receivers;
            presentSections.add(SECTION_RECEIVERS);
        }
        if (packageInfo.services != null) {
            mServices = packageInfo.services;
            presentSections.add(SECTION_SERVICES);
        }
        if (packageInfo.providers != null) {
            mProviders = packageInfo.providers;
            presentSections.add(SECTION_PROVIDERS);
        }

        // Scan defined permissions
        if (packageInfo.permissions != null) {
            mDefinedPermissions = new String[packageInfo.permissions.length];
            for (int i = 0; i < packageInfo.permissions.length; i++) {
                mDefinedPermissions[i] = packageInfo.permissions[i].name;
            }
            presentSections.add(SECTION_DEFINED_PERMISSIONS);
        }

        // Scan requested permissions (granted and denied)
        HashSet<String> testedPermissions = new HashSet<String>();
        if (packageInfo.requestedPermissions != null) {
            ArrayList<String> grantedPermissions = new ArrayList<String>();
            ArrayList<String> deniedPermissions = new ArrayList<String>();
            for (String permission : packageInfo.requestedPermissions) {
                if (packageManager.checkPermission(permission,
                        mPackageName) == PackageManager.PERMISSION_GRANTED) {
                    grantedPermissions.add(permission);
                } else {
                    deniedPermissions.add(permission);
                }
                testedPermissions.add(permission);
            }
            if (grantedPermissions.size() != 0) {
                mGrantedPermissions = grantedPermissions.toArray(new String[grantedPermissions.size()]);
                presentSections.add(SECTION_GRANTED_PERMISSIONS);
            }
            if (deniedPermissions.size() != 0) {
                mDeniedPermissions = deniedPermissions.toArray(new String[deniedPermissions.size()]);
                presentSections.add(SECTION_DENIED_PERMISSIONS);
            }

        }

        // Scan shared user packages for permissions (implicitly granted)
        {
            ArrayList<String> implicitlyGrantedPermissions = new ArrayList<String>();
            for (String sharedUidPackageName : packageManager
                    .getPackagesForUid(packageInfo.applicationInfo.uid)) {
                if (mPackageName.equals(sharedUidPackageName)) {
                    continue;
                }
                final PackageInfo sharedUidPackageInfo = packageManager.getPackageInfo(sharedUidPackageName,
                        PackageManager.GET_PERMISSIONS);
                if (sharedUidPackageInfo.requestedPermissions == null) {
                    continue;
                }
                for (String permission : sharedUidPackageInfo.requestedPermissions) {
                    if (!testedPermissions.contains(permission)) {
                        testedPermissions.add(permission);
                        if (packageManager.checkPermission(permission,
                                mPackageName) == PackageManager.PERMISSION_GRANTED) {
                            implicitlyGrantedPermissions.add(permission);
                        }
                    }
                }
            }
            if (implicitlyGrantedPermissions.size() != 0) {
                mImplicitlyGrantedPermissions = implicitlyGrantedPermissions
                        .toArray(new String[implicitlyGrantedPermissions.size()]);
                presentSections.add(SECTION_IMPLICITLY_GRANTED_PERMISSIONS);
            }
        }

        // Save present sections list as array
        mPresentSections = new int[presentSections.size()];
        for (int i = 0; i < presentSections.size(); i++) {
            mPresentSections[i] = presentSections.get(i);
        }
    } catch (PackageManager.NameNotFoundException e) {
        throw new RuntimeException(e);
    }

}

From source file:com.github.michalbednarski.intentslab.browser.ComponentFetcher.java

@Override
Object getEntries(Context context) {
    PackageManager pm = context.getPackageManager();
    int requestedPackageInfoFlags = type | PackageManager.GET_DISABLED_COMPONENTS
            | (requireMetaDataSubstring != null ? PackageManager.GET_META_DATA : 0);

    boolean workAroundSmallBinderBuffer = false;
    List<PackageInfo> allPackages = null;
    try {//w w  w. j  av a 2 s .  c o  m
        allPackages = pm.getInstalledPackages(requestedPackageInfoFlags);
    } catch (Exception e) {
        Log.w(TAG, "Loading all apps at once failed, retrying separately", e);
    }

    if (allPackages == null || allPackages.isEmpty()) {
        workAroundSmallBinderBuffer = true;
        allPackages = pm.getInstalledPackages(0);
    }

    ArrayList<Category> selectedApps = new ArrayList<Category>();

    for (PackageInfo pack : allPackages) {
        // Filter out non-applications
        if (pack.applicationInfo == null) {
            continue;
        }

        // System app filter
        if ((((pack.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0 ? APP_TYPE_SYSTEM : APP_TYPE_USER)
                & appType) == 0) {
            continue;
        }

        // Load component information separately if they were to big to send them all at once
        if (workAroundSmallBinderBuffer) {
            try {
                pack = pm.getPackageInfo(pack.packageName, requestedPackageInfoFlags);
            } catch (PackageManager.NameNotFoundException e) {
                Log.w(TAG, "getPackageInfo() thrown NameNotFoundException for " + pack.packageName, e);
                continue;
            }
        }

        // Scan components
        ArrayList<Component> selectedComponents = new ArrayList<Component>();

        if ((type & PackageManager.GET_ACTIVITIES) != 0) {
            scanComponents(pm, pack.activities, selectedComponents);
        }
        if ((type & PackageManager.GET_RECEIVERS) != 0) {
            scanComponents(pm, pack.receivers, selectedComponents);
        }
        if ((type & PackageManager.GET_SERVICES) != 0) {
            scanComponents(pm, pack.services, selectedComponents);
        }
        if ((type & PackageManager.GET_PROVIDERS) != 0) {
            scanComponents(pm, pack.providers, selectedComponents);
        }

        // Check if we filtered out all components and skip whole app if so
        if (selectedComponents.isEmpty()) {
            continue;
        }

        // Build and add app descriptor
        Category app = new Category();
        app.title = String.valueOf(pack.applicationInfo.loadLabel(pm));
        app.subtitle = pack.packageName;
        app.components = selectedComponents.toArray(new Component[selectedComponents.size()]);
        selectedApps.add(app);

        // Allow cancelling task
        if (Thread.interrupted()) {
            return null;
        }
    }
    return selectedApps.toArray(new Category[selectedApps.size()]);
}

From source file:com.github.michalbednarski.intentslab.providerlab.ProviderInfoFragment.java

private void fillProviderInfoLegacy() throws PackageManager.NameNotFoundException {
    PackageInfo packageInfo = getActivity().getPackageManager().getPackageInfo(mPackageName,
            PackageManager.GET_PROVIDERS | PackageManager.GET_DISABLED_COMPONENTS | PackageManager.GET_META_DATA
                    | PackageManager.GET_URI_PERMISSION_PATTERNS);
    for (ProviderInfo provider : packageInfo.providers) {
        if (provider.name.equals(mComponentName)) {
            mProviderInfo = provider;/*  w ww .  j  a  v a2 s.  co  m*/
            return;
        }
    }
    throw new PackageManager.NameNotFoundException("No such provider (manual search in PackageInfo)");
}

From source file:com.github.michalbednarski.intentslab.browser.BrowseComponentsFragment.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int itemId = item.getItemId();

    if (mFetcher.onOptionsItemSelected(itemId)) {
        setFetcher(mFetcher);//from w w w  .j  a  va2  s .  co m
        return true;
    }

    switch (itemId) {
    case R.id.custom_filter:
        FetcherOptionsDialog fetcherOptionsDialog = new FetcherOptionsDialog();
        fetcherOptionsDialog.setTargetFragment(this, 0);
        fetcherOptionsDialog.show(getFragmentManager(), "filterOptions");
        return true;
    case R.id.activities:
    case R.id.broadcasts:
    case R.id.services:
    case R.id.content_providers: {
        // Fetcher isn't component fetcher, otherwise it would intercept this above
        ComponentFetcher fetcher = new ComponentFetcher();
        fetcher.type = itemId == R.id.activities ? PackageManager.GET_ACTIVITIES
                : itemId == R.id.broadcasts ? PackageManager.GET_RECEIVERS
                        : itemId == R.id.services ? PackageManager.GET_SERVICES
                                : itemId == R.id.content_providers ? PackageManager.GET_PROVIDERS : 0;
        if (mFetcher instanceof ApplicationFetcher) {
            fetcher.appType = ((ApplicationFetcher) mFetcher).appType;
        }
        setFetcher(fetcher);
        return true;
    }
    case R.id.applications: {
        if (!(mFetcher instanceof ApplicationFetcher)) {
            ApplicationFetcher fetcher = new ApplicationFetcher();
            if (mFetcher instanceof ComponentFetcher) {
                fetcher.appType = ((ComponentFetcher) mFetcher).appType;
            }
            setFetcher(fetcher);
        }
        return true;
    }
    }
    return super.onOptionsItemSelected(item);
}

From source file:com.brq.wallet.activity.export.BackupToPdfActivity.java

private String getFileProviderAuthority() {
    try {// w  w  w .  j  a  va2 s  .  co  m
        PackageManager packageManager = getApplication().getPackageManager();
        PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), PackageManager.GET_PROVIDERS);
        for (ProviderInfo info : packageInfo.providers) {
            if (info.name.equals("android.support.v4.content.FileProvider")) {
                return info.authority;
            }
        }
    } catch (NameNotFoundException e) {
        throw new RuntimeException(e);
    }
    throw new RuntimeException("No file provider authority specified in manifest");
}

From source file:com.github.michalbednarski.intentslab.browser.ComponentFetcher.java

@Override
void initConfigurationForm(final FetcherOptionsDialog dialog) {
    // Disable development permission checkbox if it's not available
    if (!DEVELOPMENT_PERMISSIONS_SUPPORTED) {
        dialog.findView(R.id.permission_filter_development).setEnabled(false);
    }/*from   ww w.  j av a2  s  .  c om*/

    // Prepare protection preset spinner
    {
        // Find current preset
        int currentPresetId = PROTECTION_PRESETS.length; // "Custom" if nothing found
        if (!testWritePermissionForProviders) {
            for (int i = 0; i < PROTECTION_PRESETS.length; i++) {
                if (protection == PROTECTION_PRESETS[i]) {
                    currentPresetId = i;
                    dialog.findView(R.id.permission_filter_details).setVisibility(View.GONE);
                    break;
                }
            }
        }

        // Fill spinner
        Spinner protectionPresetSpinner = (Spinner) dialog.findView(R.id.permission_filter_spinner);
        Activity activity = dialog.getActivity();
        protectionPresetSpinner
                .setAdapter(new ArrayAdapter<String>(activity, android.R.layout.simple_spinner_item,
                        new String[] { activity.getString(R.string.permission_filter_show_all), // 0
                                activity.getString(R.string.permission_filter_show_exported), // 1
                                activity.getString(R.string.permission_filter_show_obtainable), // 2
                                activity.getString(R.string.permission_filter_world_accessible), // 3
                                activity.getString(R.string.filter_custom) // 4
                        }));
        protectionPresetSpinner.setSelection(currentPresetId);

        // Set up spinner event
        protectionPresetSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                boolean isCustom = position == PROTECTION_PRESETS.length;
                if (!isCustom) {
                    int preset = PROTECTION_PRESETS[position];
                    dialog.setBoxChecked(R.id.permission_filter_world_accessible,
                            (preset & PROTECTION_WORLD_ACCESSIBLE) != 0);
                    dialog.setBoxChecked(R.id.permission_filter_normal, (preset & PROTECTION_NORMAL) != 0);
                    dialog.setBoxChecked(R.id.permission_filter_dangerous,
                            (preset & PROTECTION_DANGEROUS) != 0);
                    dialog.setBoxChecked(R.id.permission_filter_signature,
                            (preset & PROTECTION_SIGNATURE) != 0);
                    dialog.setBoxChecked(R.id.permission_filter_system, (preset & PROTECTION_SYSTEM) != 0);
                    dialog.setBoxChecked(R.id.permission_filter_development,
                            (preset & PROTECTION_DEVELOPMENT) != 0);
                    dialog.setBoxChecked(R.id.permission_filter_unexported,
                            (preset & PROTECTION_UNEXPORTED) != 0);
                    dialog.setBoxChecked(R.id.permission_filter_unknown, (preset & PROTECTION_UNKNOWN) != 0);
                    dialog.setBoxChecked(R.id.read_permission, true);
                }
                dialog.findView(R.id.permission_filter_details)
                        .setVisibility(isCustom ? View.VISIBLE : View.GONE);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Spinner cannot have nothing selected
            }
        });
    }

    // Fill form
    dialog.setBoxChecked(R.id.system_apps, (appType & APP_TYPE_SYSTEM) != 0);
    dialog.setBoxChecked(R.id.user_apps, (appType & APP_TYPE_USER) != 0);

    dialog.setBoxChecked(R.id.activities, (type & PackageManager.GET_ACTIVITIES) != 0);
    dialog.setBoxChecked(R.id.receivers, (type & PackageManager.GET_RECEIVERS) != 0);
    dialog.setBoxChecked(R.id.services, (type & PackageManager.GET_SERVICES) != 0);
    dialog.setBoxChecked(R.id.content_providers, (type & PackageManager.GET_PROVIDERS) != 0);

    dialog.setBoxChecked(R.id.permission_filter_world_accessible,
            (protection & PROTECTION_WORLD_ACCESSIBLE) != 0);
    dialog.setBoxChecked(R.id.permission_filter_normal, (protection & PROTECTION_NORMAL) != 0);
    dialog.setBoxChecked(R.id.permission_filter_dangerous, (protection & PROTECTION_DANGEROUS) != 0);
    dialog.setBoxChecked(R.id.permission_filter_signature, (protection & PROTECTION_SIGNATURE) != 0);
    dialog.setBoxChecked(R.id.permission_filter_system, (protection & PROTECTION_SYSTEM) != 0);
    dialog.setBoxChecked(R.id.permission_filter_development, (protection & PROTECTION_DEVELOPMENT) != 0);
    dialog.setBoxChecked(R.id.permission_filter_unexported, (protection & PROTECTION_UNEXPORTED) != 0);
    dialog.setBoxChecked(R.id.permission_filter_unknown, (protection & PROTECTION_UNKNOWN) != 0);

    dialog.setBoxChecked(testWritePermissionForProviders ? R.id.write_permission : R.id.read_permission, true);
    dialog.setBoxChecked(R.id.only_providers_with_grant_uri_permission,
            includeOnlyProvidersAllowingPermissionGranting);

    dialog.setBoxChecked(R.id.metadata, requireMetaDataSubstring != null);
    dialog.setTextInField(R.id.metadata_substring, requireMetaDataSubstring);

    // Set up sections showing when their checkboxes are checked
    dialog.findView(R.id.content_provider_permission_type)
            .setVisibility(testWritePermissionForProviders ? View.VISIBLE : View.GONE);
    dialog.findView(R.id.content_provider_options)
            .setVisibility((type & PackageManager.GET_PROVIDERS) != 0 ? View.VISIBLE : View.GONE);
    ((CheckBox) dialog.findView(R.id.content_providers))
            .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    dialog.findView(R.id.content_provider_permission_type)
                            .setVisibility(isChecked ? View.VISIBLE : View.GONE);
                    dialog.findView(R.id.content_provider_options)
                            .setVisibility(isChecked ? View.VISIBLE : View.GONE);
                    if (!isChecked) {
                        dialog.setBoxChecked(R.id.read_permission, true);
                        dialog.setBoxChecked(R.id.only_providers_with_grant_uri_permission, false);
                    }
                }
            });

    dialog.findView(R.id.metadata_details)
            .setVisibility(requireMetaDataSubstring != null ? View.VISIBLE : View.GONE);
    ((CheckBox) dialog.findView(R.id.metadata))
            .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    dialog.findView(R.id.metadata_details).setVisibility(isChecked ? View.VISIBLE : View.GONE);
                    if (!isChecked) {
                        dialog.setTextInField(R.id.metadata_substring, "");
                    }
                }
            });
}

From source file:com.github.michalbednarski.intentslab.browser.ComponentFetcher.java

@Override
void updateFromConfigurationForm(FetcherOptionsDialog dialog) {
    appType = (dialog.isBoxChecked(R.id.system_apps) ? APP_TYPE_SYSTEM : 0)
            | (dialog.isBoxChecked(R.id.user_apps) ? APP_TYPE_USER : 0);

    type = (dialog.isBoxChecked(R.id.activities) ? PackageManager.GET_ACTIVITIES : 0)
            | (dialog.isBoxChecked(R.id.receivers) ? PackageManager.GET_RECEIVERS : 0)
            | (dialog.isBoxChecked(R.id.services) ? PackageManager.GET_SERVICES : 0)
            | (dialog.isBoxChecked(R.id.content_providers) ? PackageManager.GET_PROVIDERS : 0);

    protection = (dialog.isBoxChecked(R.id.permission_filter_world_accessible) ? PROTECTION_WORLD_ACCESSIBLE
            : 0) | (dialog.isBoxChecked(R.id.permission_filter_normal) ? PROTECTION_NORMAL : 0)
            | (dialog.isBoxChecked(R.id.permission_filter_dangerous) ? PROTECTION_DANGEROUS : 0)
            | (dialog.isBoxChecked(R.id.permission_filter_signature) ? PROTECTION_SIGNATURE : 0)
            | (dialog.isBoxChecked(R.id.permission_filter_system) ? PROTECTION_SYSTEM : 0)
            | (dialog.isBoxChecked(R.id.permission_filter_development) ? PROTECTION_DEVELOPMENT : 0)
            | (dialog.isBoxChecked(R.id.permission_filter_unexported) ? PROTECTION_UNEXPORTED : 0)
            | (dialog.isBoxChecked(R.id.permission_filter_unknown) ? PROTECTION_UNKNOWN : 0);

    includeOnlyProvidersAllowingPermissionGranting = dialog
            .isBoxChecked(R.id.only_providers_with_grant_uri_permission);

    boolean requireMetaData = dialog.isBoxChecked(R.id.metadata);
    requireMetaDataSubstring = requireMetaData ? dialog.getTextFromField(R.id.metadata_substring) : null;

    testWritePermissionForProviders = dialog.isBoxChecked(R.id.write_permission);
}