Java Code Examples for android.content.pm.PackageManager#queryBroadcastReceivers()

The following examples show how to use android.content.pm.PackageManager#queryBroadcastReceivers() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: divide   File: GCMRegistrar.java    License: Apache License 2.0 6 votes vote down vote up
private static void checkReceiver(Context context,
        Set<String> allowedReceivers, String action) {
    PackageManager pm = context.getPackageManager();
    String packageName = context.getPackageName();
    Intent intent = new Intent(action);
    intent.setPackage(packageName);
    List<ResolveInfo> receivers = pm.queryBroadcastReceivers(intent,
            PackageManager.GET_INTENT_FILTERS);
    if (receivers.isEmpty()) {
        throw new IllegalStateException("No receivers for action " +
                action);
    }
    if (Log.isLoggable(TAG, Log.VERBOSE)) {
        Log.v(TAG, "Found " + receivers.size() + " receivers for action " +
                action);
    }
    // make sure receivers match
    for (ResolveInfo receiver : receivers) {
        String name = receiver.activityInfo.name;
        if (!allowedReceivers.contains(name)) {
            throw new IllegalStateException("Receiver " + name +
                    " is not set with permission " +
                    GCMConstants.PERMISSION_GCM_INTENTS);
        }
    }
}
 
Example 2
static Pair<String, Resources> findSystemApk(String action, PackageManager pm) {
    final Intent intent = new Intent(action);
    for (ResolveInfo info : pm.queryBroadcastReceivers(intent, 0)) {
        if (info.activityInfo != null &&
                (info.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
            final String packageName = info.activityInfo.packageName;
            try {
                final Resources res = pm.getResourcesForApplication(packageName);
                return Pair.create(packageName, res);
            } catch (NameNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}
 
Example 3
Source Project: media-button-router   File: Utils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Gets the list of available media receivers, optionally filtering out ones
 * the user has indicated should be hidden in preferences.
 * 
 * @param packageManager
 *            The {@code PackageManager} used to retrieve media button
 *            receivers.
 * 
 * @param filterHidden
 *            Whether user-hidden media receivers should be shown.
 * @return The list of {@code ResolveInfo} for different media button
 *         receivers.
 */
public static List<ResolveInfo> getMediaReceivers(PackageManager packageManager, boolean filterHidden,
        Context context) {
    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);

    List<ResolveInfo> mediaReceivers = packageManager.queryBroadcastReceivers(mediaButtonIntent,
            PackageManager.GET_INTENT_FILTERS | PackageManager.GET_RESOLVED_FILTER);
    if (filterHidden) {

        String hiddenReceiverIdsString = PreferenceManager.getDefaultSharedPreferences(context).getString(
                Constants.HIDDEN_APPS_KEY, "");
        List<String> hiddenIds = Arrays.asList(hiddenReceiverIdsString.split(","));

        for (int i = mediaReceivers.size() - 1; i >= 0; i--) {
            ResolveInfo mediaReceiverResolveInfo = mediaReceivers.get(i);
          
            if (hiddenIds.contains(getMediaReceiverUniqueID(mediaReceiverResolveInfo, packageManager))) {
                mediaReceivers.remove(i);
            }
        }
    }

    return mediaReceivers;
}
 
Example 4
/**
 * Retrieve outgoing call handlers available as plugin for csipsimple Also
 * contains stock call handler if available
 * 
 * @param ctxt context of application
 * @return A map of package name => Fancy name of call handler
 */
public static Map<String, String> getAvailableCallHandlers(Context ctxt) {

    if (AVAILABLE_HANDLERS == null) {
        AVAILABLE_HANDLERS = new HashMap<String, String>();

        PackageManager packageManager = ctxt.getPackageManager();
        Intent it = new Intent(SipManager.ACTION_GET_PHONE_HANDLERS);

        List<ResolveInfo> availables = packageManager.queryBroadcastReceivers(it, 0);
        for (ResolveInfo resInfo : availables) {
            ActivityInfo actInfos = resInfo.activityInfo;
            Log.d(THIS_FILE, "Found call handler " + actInfos.packageName + " " + actInfos.name);
            if (packageManager.checkPermission(permission.PROCESS_OUTGOING_CALLS,
                    actInfos.packageName) == PackageManager.PERMISSION_GRANTED) {
                String packagedActivityName = (new ComponentName(actInfos.packageName,
                        actInfos.name)).flattenToString();
                AVAILABLE_HANDLERS.put(packagedActivityName,
                        (String) resInfo.loadLabel(packageManager));
            }
        }
    }

    return AVAILABLE_HANDLERS;
}
 
Example 5
Source Project: CSipSimple   File: Theme.java    License: GNU General Public License v3.0 6 votes vote down vote up
public static HashMap<String, String> getAvailableThemes(Context ctxt){
	HashMap<String, String> result = new HashMap<String, String>();
	result.put("", ctxt.getResources().getString(R.string.app_name));
	
	PackageManager packageManager = ctxt.getPackageManager();
	Intent it = new Intent(SipManager.ACTION_GET_DRAWABLES);
	
	List<ResolveInfo> availables = packageManager.queryBroadcastReceivers(it, 0);
	Log.d(THIS_FILE, "We found " + availables.size() + "themes");
	for(ResolveInfo resInfo : availables) {
		Log.d(THIS_FILE, "We have -- "+resInfo);
		ActivityInfo actInfos = resInfo.activityInfo;
		ComponentName cmp = new ComponentName(actInfos.packageName, actInfos.name);
		String label = (String) actInfos.loadLabel(packageManager);
		if(TextUtils.isEmpty(label)) {
		    label = (String) resInfo.loadLabel(packageManager);
		}
		result.put(cmp.flattenToString(), label);
	}
	
	return result;
}
 
Example 6
Source Project: MockSMS   File: Utils.java    License: Apache License 2.0 6 votes vote down vote up
public static HashMap getSMSAppsContent(Context context) {
    PackageManager packageManager = context.getPackageManager();
    Intent intent = new Intent(Telephony.Sms.Intents.SMS_DELIVER_ACTION);
    List<ResolveInfo> smsReceivers = packageManager.queryBroadcastReceivers(intent, 0);
    HashMap<String, String> receivers = new HashMap<>();
    for (ResolveInfo resolveInfo : smsReceivers) {
        final ActivityInfo activityInfo = resolveInfo.activityInfo;
        if (activityInfo == null) {
            continue;
        }
        if (!Manifest.permission.BROADCAST_SMS.equals(activityInfo.permission)) {
            continue;
        }
        final String packageName = activityInfo.packageName;
        if (!receivers.containsKey(packageName)) {
            final String applicationName = resolveInfo.loadLabel(packageManager).toString();
            receivers.put(packageName, applicationName);
        }
    }
    return receivers;
}
 
Example 7
Source Project: MockSMS   File: Utils.java    License: Apache License 2.0 6 votes vote down vote up
public static HashMap getSMSAppsContent(Context context) {
    PackageManager packageManager = context.getPackageManager();
    Intent intent = new Intent(Telephony.Sms.Intents.SMS_DELIVER_ACTION);
    List<ResolveInfo> smsReceivers = packageManager.queryBroadcastReceivers(intent, 0);
    HashMap<String, String> receivers = new HashMap<>();
    for (ResolveInfo resolveInfo : smsReceivers) {
        final ActivityInfo activityInfo = resolveInfo.activityInfo;
        if (activityInfo == null) {
            continue;
        }
        if (!Manifest.permission.BROADCAST_SMS.equals(activityInfo.permission)) {
            continue;
        }
        final String packageName = activityInfo.packageName;
        if (!receivers.containsKey(packageName)) {
            final String applicationName = resolveInfo.loadLabel(packageManager).toString();
            receivers.put(packageName, applicationName);
        }
    }
    return receivers;
}
 
Example 8
public static boolean checkTorReceier(Context c) {
    Intent startOrbot = getOrbotStartIntent(c);
    PackageManager pm = c.getPackageManager();
    Intent result = null;
    List<ResolveInfo> receivers =
            pm.queryBroadcastReceivers(startOrbot, 0);

    return receivers != null && receivers.size() > 0;
}
 
Example 9
Source Project: Noyze   File: Utils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Gets the list of available media receivers.
 * @return The list of {@code ResolveInfo} for different media button receivers.
 */
public static List<ResolveInfo> getMediaReceivers(PackageManager packageManager) {
    if (null == packageManager) return new ArrayList<ResolveInfo>(0);
    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    return packageManager.queryBroadcastReceivers(mediaButtonIntent,
            PackageManager.GET_INTENT_FILTERS | PackageManager.GET_RESOLVED_FILTER);
}
 
Example 10
/**
 * 获取指定包名的自启接收器以及IntentFilter的Action参数,此方法较为耗时(遍历操作)
 * @deprecated 建议用获取Bundle实例的方法
 * @param package_name 指定包名
 * @return < Receiver的class名,IntentFilter的Actions >
 */
public static @NonNull HashMap<String,List<String>> getStaticRegisteredReceiversForPackageName(@NonNull Context context, @NonNull String package_name){
    HashMap<String,List<String>>map=new HashMap<>();
    PackageManager packageManager=context.getPackageManager();
    String[] static_filters=context.getResources().getStringArray(R.array.static_receiver_filters);
   // if(static_filters==null)return new HashMap<>();
    for(String s:static_filters){
        List<ResolveInfo>list=packageManager.queryBroadcastReceivers(new Intent(s),0);
        if(list==null)continue;
        for(ResolveInfo info:list){
            String pn=info.activityInfo.packageName;
            if(pn==null)continue;
            List<String> filters_class=map.get(info.activityInfo.name);
            if(filters_class==null){
                filters_class=new ArrayList<>();
                filters_class.add(s);
                if(pn.equals(package_name))map.put(info.activityInfo.name,filters_class);
            }
            else{
                if(!filters_class.contains(s)) filters_class.add(s);
            }

        }
    }

    return map;

}
 
Example 11
/**
 * 当SharedPreference中设置了加载启动项的值,则会查询启动Receiver,否则会直接返回一个空Bundle(查询为耗时操作,此方法会阻塞)
 */
public static @NonNull Bundle getStaticRegisteredReceiversOfBundleTypeForPackageName(@NonNull Context context,@NonNull String package_name){
    Bundle bundle=new Bundle();
    if(!SPUtil.getGlobalSharedPreferences(context)
            .getBoolean(Constants.PREFERENCE_LOAD_STATIC_LOADERS,Constants.PREFERENCE_LOAD_STATIC_LOADERS_DEFAULT)){
        return bundle;
    }
    PackageManager packageManager=context.getPackageManager();
    String[] static_filters=context.getResources().getStringArray(R.array.static_receiver_filters);

    for(String s:static_filters){
        List<ResolveInfo>list=packageManager.queryBroadcastReceivers(new Intent(s),0);
        if(list==null)continue;
        for(ResolveInfo info:list){
            String pn=info.activityInfo.packageName;
            if(pn==null)continue;
            ArrayList<String> filters_class=bundle.getStringArrayList(info.activityInfo.name);
            if(filters_class==null){
                filters_class=new ArrayList<>();
                filters_class.add(s);
                if(pn.equals(package_name))bundle.putStringArrayList(info.activityInfo.name,filters_class);
            }
            else{
                if(!filters_class.contains(s)) filters_class.add(s);
            }

        }
    }
    return bundle;
}
 
Example 12
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.transfer_ownership_dialog,
            null);
    final ListView listView = rootView.findViewById(R.id.list);
    final EditText componentName = rootView.findViewById(R.id.component_name);
    final EditText result = rootView.findViewById(R.id.result);
    final Button transferButton = rootView.findViewById(R.id.transfer_btn);

    final Intent intent = new Intent(DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED);
    final List<String> items = new ArrayList<>();
    final PackageManager packageManager = getActivity().getPackageManager();
    final List<ResolveInfo> resolveInfos = packageManager.queryBroadcastReceivers(intent, 0);
    for (ResolveInfo resolveInfo : resolveInfos) {
        ActivityInfo activityInfo = resolveInfo.activityInfo;
        if (activityInfo == null) {
            continue;
        }
        items.add(activityInfo.packageName + "/" + activityInfo.name);
    }

    final ListAdapter adapter = new ArrayAdapter<>(getActivity(),
            android.R.layout.simple_list_item_1, items);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener((adapterView, view1, i, __) ->
            componentName.setText(adapterView.getItemAtPosition(i).toString()));

    transferButton.setOnClickListener(view -> {
        ComponentName target =
            ComponentName.unflattenFromString(componentName.getText().toString());
        if (target != null) {
            result.setText(performTransfer(target));
        } else {
            result.setText(R.string.transfer_ownership_invalid_target_format);
        }
    });

    return rootView;
}
 
Example 13
Source Project: Noyze   File: Utils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Gets the list of available media receivers.
 * @return The list of {@code ResolveInfo} for different media button receivers.
 */
public static List<ResolveInfo> getMediaReceivers(PackageManager packageManager) {
    if (null == packageManager) return new ArrayList<ResolveInfo>(0);
    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    return packageManager.queryBroadcastReceivers(mediaButtonIntent,
            PackageManager.GET_INTENT_FILTERS | PackageManager.GET_RESOLVED_FILTER);
}
 
Example 14
private static void sendImplicitBroadcast(Context ctxt, Intent i) {
    final PackageManager pm = ctxt.getPackageManager();
    final List<ResolveInfo> matches = pm.queryBroadcastReceivers(i, 0);

    for (ResolveInfo resolveInfo : matches) {
        final Intent explicit = new Intent(i);
        ComponentName cn = new ComponentName(resolveInfo.activityInfo.applicationInfo.packageName, resolveInfo.activityInfo.name);
        explicit.setComponent(cn);
        ctxt.sendBroadcast(explicit);
    }
}
 
Example 15
private static boolean canResolveBroadcast(Intent intent) {
    PackageManager packageManager = ApplicationLoader.applicationContext.getPackageManager();
    List<ResolveInfo> receivers = packageManager.queryBroadcastReceivers(intent, 0);
    return receivers != null && receivers.size() > 0;
}
 
Example 16
private static boolean canResolveBroadcast(Intent intent) {
    PackageManager packageManager = ApplicationLoader.applicationContext.getPackageManager();
    List<ResolveInfo> receivers = packageManager.queryBroadcastReceivers(intent, 0);
    return receivers != null && receivers.size() > 0;
}
 
Example 17
Source Project: BadgeForAppIcon   File: Utils.java    License: MIT License 4 votes vote down vote up
public boolean canResolveBroadcast(Context context, Intent intent) {
    PackageManager packageManager = context.getPackageManager();
    List<ResolveInfo> receivers = packageManager.queryBroadcastReceivers(intent, 0);
    return receivers != null && receivers.size() > 0;
}
 
Example 18
Source Project: AndroidBase   File: BroadcastHelper.java    License: Apache License 2.0 4 votes vote down vote up
public static boolean canResolveBroadcast(Context context, Intent intent) {
	PackageManager packageManager = context.getPackageManager();
	List<ResolveInfo> receivers = packageManager.queryBroadcastReceivers(intent, 0);
	return receivers != null && receivers.size() > 0;
}
 
Example 19
private static boolean canResolveBroadcast(Intent intent) {
    PackageManager packageManager = ApplicationLoader.applicationContext.getPackageManager();
    List<ResolveInfo> receivers = packageManager.queryBroadcastReceivers(intent, 0);
    return receivers != null && receivers.size() > 0;
}
 
Example 20
public static boolean canResolveBroadcast(Context context, Intent intent) {
	PackageManager packageManager = context.getPackageManager();
	List<ResolveInfo> receivers = packageManager.queryBroadcastReceivers(intent, 0);
	return receivers != null && receivers.size() > 0;
}