Example usage for android.content.pm PackageManager queryIntentActivities

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

Introduction

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

Prototype

public abstract List<ResolveInfo> queryIntentActivities(Intent intent, @ResolveInfoFlags int flags);

Source Link

Document

Retrieve all activities that can be performed for the given intent.

Usage

From source file:com.onesignal.OneSignal.java

private static void fireIntentFromNotificationOpen(Context inContext, JSONArray data) {
    PackageManager packageManager = inContext.getPackageManager();

    boolean isCustom = false;

    Intent intent = new Intent().setAction("com.onesignal.NotificationOpened.RECEIVE")
            .setPackage(inContext.getPackageName());

    List<ResolveInfo> resolveInfo = packageManager.queryBroadcastReceivers(intent,
            PackageManager.GET_INTENT_FILTERS);
    if (resolveInfo.size() > 0) {
        intent.putExtra("onesignal_data", data.toString());
        inContext.sendBroadcast(intent);
        isCustom = true;/*w w w . j a v a 2 s  .c om*/
    }

    // Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag.
    resolveInfo = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    if (resolveInfo.size() > 0) {
        if (!isCustom)
            intent.putExtra("onesignal_data", data.toString());
        isCustom = true;
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
        inContext.startActivity(intent);
    }

    if (!isCustom) {
        try {
            ApplicationInfo ai = inContext.getPackageManager().getApplicationInfo(inContext.getPackageName(),
                    PackageManager.GET_META_DATA);
            Bundle bundle = ai.metaData;
            String defaultStr = bundle.getString("com.onesignal.NotificationOpened.DEFAULT");
            isCustom = "DISABLE".equals(defaultStr);
        } catch (Throwable t) {
            Log(LOG_LEVEL.ERROR, "", t);
        }
    }

    if (!isCustom) {
        Intent launchIntent = inContext.getPackageManager()
                .getLaunchIntentForPackage(inContext.getPackageName());

        if (launchIntent != null) {
            launchIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
            inContext.startActivity(launchIntent);
        }
    }
}

From source file:org.mozilla.gecko.GeckoAppShell.java

static String[] getHandlersForIntent(Intent intent) {
    PackageManager pm = GeckoApp.mAppContext.getPackageManager();
    List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
    int numAttr = 4;
    String[] ret = new String[list.size() * numAttr];
    for (int i = 0; i < list.size(); i++) {
        ResolveInfo resolveInfo = list.get(i);
        ret[i * numAttr] = resolveInfo.loadLabel(pm).toString();
        if (resolveInfo.isDefault)
            ret[i * numAttr + 1] = "default";
        else//  w  w  w  .  j a v a 2 s. c o m
            ret[i * numAttr + 1] = "";
        ret[i * numAttr + 2] = resolveInfo.activityInfo.applicationInfo.packageName;
        ret[i * numAttr + 3] = resolveInfo.activityInfo.name;
    }
    return ret;
}

From source file:info.guardianproject.pixelknot.PixelKnotActivity.java

@Override
public List<TrustedShareActivity> getTrustedShareActivities() {
    if (trusted_share_activities == null) {
        trusted_share_activities = new Vector<TrustedShareActivity>();

        Intent intent = new Intent(Intent.ACTION_SEND).setType("image/jpeg");

        PackageManager pm = getPackageManager();
        for (ResolveInfo ri : pm.queryIntentActivities(intent, 0)) {
            if (Arrays.asList(Image.TRUSTED_SHARE_ACTIVITIES)
                    .contains(Image.Activities.get(ri.activityInfo.packageName))) {
                try {
                    ApplicationInfo ai = pm.getApplicationInfo(ri.activityInfo.packageName, 0);
                    TrustedShareActivity tsa = new TrustedShareActivity(
                            Image.Activities.get(ri.activityInfo.packageName), ri.activityInfo.packageName);

                    tsa.setIcon(pm.getApplicationIcon(ai));
                    tsa.createView();/*from   ww w  .  ja  v a 2  s  . co m*/
                    tsa.setIntent();

                    trusted_share_activities.add(tsa);

                } catch (PackageManager.NameNotFoundException e) {
                    Log.e(Logger.UI, e.toString());
                    e.printStackTrace();
                    continue;
                }
            }
        }
    }

    return trusted_share_activities;
}

From source file:org.mariotaku.harmony.activity.MusicPlaybackActivity.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    final MenuItem equalizer = menu.findItem(EQUALIZER);
    if (equalizer != null) {
        final PackageManager pm = getPackageManager();
        final Intent intent = new Intent(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL);
        equalizer.setVisible(!pm.queryIntentActivities(intent, 0).isEmpty());
    }/*from w w  w. j a  v a2s.  c o  m*/
    //      final MenuItem item = menu.findItem(ADD_TO_FAVORITES);
    //      try {
    //         if (item != null && mService != null)
    //            item.setIcon(mService.isFavorite(mService.getAudioId()) ? R.drawable.ic_menu_star
    //                  : R.drawable.ic_menu_star_off);
    //      } catch (RemoteException e) {
    //         e.printStackTrace();
    //      }
    if (mService != null) {
        switch (mService.getShuffleMode()) {
        case SHUFFLE_MODE_ALL: {
            menu.findItem(MENU_SHUFFLE_MODE_ALL).setChecked(true);
            break;
        }
        default: {
            menu.findItem(MENU_SHUFFLE_MODE_NONE).setChecked(true);
            break;
        }
        }
        switch (mService.getRepeatMode()) {
        case REPEAT_MODE_ALL: {
            menu.findItem(MENU_REPEAT_MODE_ALL).setChecked(true);
            break;
        }
        case REPEAT_MODE_CURRENT: {
            menu.findItem(MENU_REPEAT_MODE_CURRENT).setChecked(true);
            break;
        }
        default: {
            menu.findItem(MENU_REPEAT_MODE_NONE).setChecked(true);
            break;
        }
        }
    }
    return true;
}

From source file:net.gsantner.opoc.util.ShareUtil.java

/**
 * By default Chrome Custom Tabs only uses Chrome Stable to open links
 * There are also other packages (like Chrome Beta, Chromium, Firefox, ..)
 * which implement the Chrome Custom Tab interface. This method changes
 * the customtab intent to use an available compatible browser, if available.
 *//*  ww w  .j a v  a  2s  .  co  m*/
public void enableChromeCustomTabsForOtherBrowsers(Intent customTabIntent) {
    String[] checkpkgs = new String[] { "com.android.chrome", "com.chrome.beta", "com.chrome.dev",
            "com.google.android.apps.chrome", "org.chromium.chrome", "org.mozilla.fennec_fdroid",
            "org.mozilla.firefox", "org.mozilla.firefox_beta", "org.mozilla.fennec_aurora", "org.mozilla.klar",
            "org.mozilla.focus", };

    // Get all intent handlers for web links
    PackageManager pm = _context.getPackageManager();
    Intent urlIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.example.com"));
    List<String> browsers = new ArrayList<>();
    for (ResolveInfo ri : pm.queryIntentActivities(urlIntent, 0)) {
        Intent i = new Intent("android.support.customtabs.action.CustomTabsService");
        i.setPackage(ri.activityInfo.packageName);
        if (pm.resolveService(i, 0) != null) {
            browsers.add(ri.activityInfo.packageName);
        }
    }

    // Check if the user has a "default browser" selected
    ResolveInfo ri = pm.resolveActivity(urlIntent, 0);
    String userDefaultBrowser = (ri == null) ? null : ri.activityInfo.packageName;

    // Select which browser to use out of all installed customtab supporting browsers
    String pkg = null;
    if (browsers.isEmpty()) {
        pkg = null;
    } else if (browsers.size() == 1) {
        pkg = browsers.get(0);
    } else if (!TextUtils.isEmpty(userDefaultBrowser) && browsers.contains(userDefaultBrowser)) {
        pkg = userDefaultBrowser;
    } else {
        for (String checkpkg : checkpkgs) {
            if (browsers.contains(checkpkg)) {
                pkg = checkpkg;
                break;
            }
        }
        if (pkg == null && !browsers.isEmpty()) {
            pkg = browsers.get(0);
        }
    }
    if (pkg != null && customTabIntent != null) {
        customTabIntent.setPackage(pkg);
    }
}

From source file:org.awesomeapp.messenger.ui.ConversationDetailActivity.java

/**
 * Create a chooser intent to select the source to get image from.<br/>
 * The source can be camera's (ACTION_IMAGE_CAPTURE) or gallery's (ACTION_GET_CONTENT).<br/>
 * All possible sources are added to the intent chooser.
 *///from   w ww.  j  a  v  a2  s .c  o  m
public Intent getPickImageChooserIntent() {

    List<Intent> allIntents = new ArrayList<>();
    PackageManager packageManager = getPackageManager();

    // collect all gallery intents
    Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
    galleryIntent.setType("image/*");
    List<ResolveInfo> listGallery = packageManager.queryIntentActivities(galleryIntent, 0);
    for (ResolveInfo res : listGallery) {
        Intent intent = new Intent(galleryIntent);
        intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
        intent.setPackage(res.activityInfo.packageName);
        allIntents.add(intent);
    }

    // the main intent is the last in the list (fucking android) so pickup the useless one
    Intent mainIntent = allIntents.get(allIntents.size() - 1);
    for (Intent intent : allIntents) {
        if (intent.getComponent().getClassName().equals("com.android.documentsui.DocumentsActivity")) {
            mainIntent = intent;
            break;
        }
    }
    allIntents.remove(mainIntent);

    // Create a chooser from the main intent
    Intent chooserIntent = Intent.createChooser(mainIntent, getString(R.string.choose_photos));

    // Add all other intents
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, allIntents.toArray(new Parcelable[allIntents.size()]));

    return chooserIntent;
}

From source file:cz.muni.fi.japanesedictionary.fragments.DisplayTranslation.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.favorite:
        Log.i(LOG_TAG, "favorite changed");
        mFavorite.setEnabled(false);/*from w w  w.ja va 2 s .  com*/
        FavoriteChanger changeFavorite = new FavoriteChanger(mCallbackTranslation.getDatabase(), mFavorite,
                this);
        changeFavorite.execute(mTranslation);
        return true;
    case R.id.ab_note:
        Log.i(LOG_TAG, "notes opened");
        TextView note = (TextView) getActivity().findViewById(R.id.translation_note_text);
        if (note != null && note.getText() != null) {
            showNoteAlertBox(note.getText().toString());
        }
        return true;
    case R.id.ab_anki:
        Log.i(LOG_TAG, "anki card clicked");
        if (mTranslation == null) {
            Log.w(LOG_TAG, "add anki card - translation is null");
            return true;
        }
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setAction("org.openintents.action.CREATE_FLASHCARD");
        StringBuilder jap = new StringBuilder();
        if (mTranslation.getJapaneseKeb() != null) {
            for (int i = 0; i < mTranslation.getJapaneseKeb().size(); i++) {
                jap.append(mTranslation.getJapaneseKeb().get(i));
                if (i + 1 < mTranslation.getJapaneseKeb().size()) {
                    jap.append(", ");
                }
            }
        }
        if (jap.length() > 0) {
            jap.append("<br>");
        }
        if (mTranslation.getJapaneseReb() != null) {
            for (int i = 0; i < mTranslation.getJapaneseReb().size(); i++) {
                jap.append(mTranslation.getJapaneseReb().get(i));
                if (i + 1 < mTranslation.getJapaneseReb().size()) {
                    jap.append(", ");
                }
            }
        }
        StringBuilder sense = new StringBuilder();
        if (mEnglish || (!mDutch && !mGerman && !mFrench && !mRussian)) {
            //only english
            sense.append(sensesToString(mTranslation.getEnglishSense()));
        }
        if (mFrench) {
            if (sense.length() > 0
                    && (sense.length() < 3 || !"<br>".equals(sense.substring(sense.length() - 4)))) {
                sense.append("<br>");
            }
            sense.append(sensesToString(mTranslation.getFrenchSense()));
        }
        if (mGerman) {
            if (sense.length() > 0
                    && (sense.length() < 3 || !"<br>".equals(sense.substring(sense.length() - 4)))) {
                sense.append("<br>");
            }
            sense.append(sensesToString(mTranslation.getGermanSense()));
        }
        if (mRussian) {
            if (sense.length() > 0
                    && (sense.length() < 3 || !"<br>".equals(sense.substring(sense.length() - 4)))) {
                sense.append("<br>");
            }
            sense.append(sensesToString(mTranslation.getRussianSense()));
        }
        if (mFrench) {
            if (sense.length() > 0
                    && (sense.length() < 3 || !"<br>".equals(sense.substring(sense.length() - 4)))) {
                sense.append("<br>");
            }
            sense.append(sensesToString(mTranslation.getFrenchSense()));
        }
        Log.e(LOG_TAG, "senses: " + sense.toString());
        intent.putExtra("SOURCE_LANGUAGE", "ja");
        intent.putExtra("SOURCE_TEXT", jap.toString());
        intent.putExtra("TARGET_TEXT", sense.toString());
        PackageManager packageManager = getActivity().getPackageManager();
        List<ResolveInfo> activities = null;
        if (packageManager != null) {
            activities = packageManager.queryIntentActivities(intent, 0);
        }
        if (activities != null && activities.size() > 0) {
            startActivity(intent);
        } else {
            Log.w(LOG_TAG, "Anki application is not installed");
            DialogFragment newFragment = AnkiFragmentAlertDialog.newInstance(R.string.anki_required,
                    R.string.anki_not_found, false);
            newFragment.setCancelable(true);
            newFragment.show(getActivity().getSupportFragmentManager(), "dialog");

            //Toast.makeText(getActivity(), getString(R.string.anki_not_found),Toast.LENGTH_LONG).show();
        }
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

From source file:com.example.android.home.Home.java

/**
 * Loads the list of installed applications in mApplications.
 *///  w  ww  . ja va2 s  . c  o  m
private void loadApplications(boolean isLaunching) {
    if (isLaunching && mApplications != null) {
        return;
    }

    PackageManager manager = getPackageManager();

    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

    final List<ResolveInfo> apps = manager.queryIntentActivities(mainIntent, 0);
    Collections.sort(apps, new ResolveInfo.DisplayNameComparator(manager));

    if (apps != null) {
        final int count = apps.size();

        if (mApplications == null) {
            mApplications = new ArrayList<ApplicationInfo>(count);
        }
        mApplications.clear();

        for (int i = 0; i < count; i++) {
            //for (int i = 0; i < 6; i++) {            // show only 6 apps
            ApplicationInfo application = new ApplicationInfo();
            ResolveInfo info = apps.get(i);

            application.title = info.loadLabel(manager);
            application.setActivity(
                    new ComponentName(info.activityInfo.applicationInfo.packageName, info.activityInfo.name),
                    Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

            Log.v("Home APP", "package neme= " + info.activityInfo.applicationInfo.packageName);
            Log.v("Home APP", "neme= " + info.activityInfo.name);
            Log.v("Home APP", "title= " + application.title);

            application.icon = info.activityInfo.loadIcon(manager);

            // TODO
            // Replace Icons
            //if((application.title+"").equals("Calculator")) application.icon = getResources().getDrawable(R.drawable.ic_launcher_home_big);

            // Add Application
            if ((application.title + "").equals("Browser"))
                mApplications.add(application);
            if ((application.title + "").equals("Phone"))
                mApplications.add(application);
            if ((application.title + "").equals("People"))
                mApplications.add(application);
            if ((application.title + "").equals("Messaging"))
                mApplications.add(application);
            if ((application.title + "").equals("Settings"))
                mApplications.add(application);

        }

        // Took from http://hi-android.info/src/index.html
        // ********************* ADD Call Log
        ApplicationInfo application = new ApplicationInfo();
        application.title = "Call Log";
        application.icon = getResources().getDrawable(R.drawable.ic_launcher_home);
        application.setActivity(
                new ComponentName("com.android.contacts", "com.android.contacts.RecentCallsListActivity"),
                Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        mApplications.add(application);
    }
}

From source file:onion.chat.MainActivity.java

private void listen() {
    //inform();/*from w  w  w  . java  2  s.com*/
    PackageManager pm = getPackageManager();
    List<ResolveInfo> activities = pm
            .queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
    if (activities.size() == 0) {
        Toast.makeText(this, "Voice recognizer not present", Toast.LENGTH_SHORT).show();
    } else {
        try {
            Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

            intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());

            intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Its Ur's");

            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
            startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
        } catch (Exception e) {
            String toSpeak = "Oops your device doesn't support Voice recognition";
            Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
            t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
        }
    }
}

From source file:ir.occc.android.irc.activity.ConversationActivity.java

/**
 * On resume//w ww.  ja  v a 2s  .com
 */
@Override
public void onResume() {
    // register the receivers as early as possible, otherwise we may loose a broadcast message
    channelReceiver = new ConversationReceiver(server.getId(), this);
    registerReceiver(channelReceiver, new IntentFilter(Broadcast.CONVERSATION_MESSAGE));
    registerReceiver(channelReceiver, new IntentFilter(Broadcast.CONVERSATION_NEW));
    registerReceiver(channelReceiver, new IntentFilter(Broadcast.CONVERSATION_REMOVE));
    registerReceiver(channelReceiver, new IntentFilter(Broadcast.CONVERSATION_TOPIC));

    serverReceiver = new ServerReceiver(this);
    registerReceiver(serverReceiver, new IntentFilter(Broadcast.SERVER_UPDATE));

    super.onResume();

    // Check if speech recognition is enabled and available
    if (new Settings(this).isVoiceRecognitionEnabled()) {
        PackageManager pm = getPackageManager();
        Button speechButton = (Button) findViewById(R.id.speech);
        List<ResolveInfo> activities = pm
                .queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);

        if (activities.size() != 0) {
            ((Button) findViewById(R.id.speech)).setOnClickListener(new SpeechClickListener(this));
            speechButton.setVisibility(View.VISIBLE);
        }
    }

    // Start service
    Intent intent = new Intent(this, IRCService.class);
    intent.setAction(IRCService.ACTION_FOREGROUND);
    startService(intent);
    bindService(intent, this, 0);

    if (!server.isConnected()) {
        ((EditText) findViewById(R.id.input)).setEnabled(false);
    } else {
        ((EditText) findViewById(R.id.input)).setEnabled(true);
    }

    // Optimization - cache field lookup
    Collection<Conversation> mConversations = server.getConversations();
    MessageListAdapter mAdapter;

    // Fill view with messages that have been buffered while paused
    for (Conversation conversation : mConversations) {
        String name = conversation.getName();
        mAdapter = pagerAdapter.getItemAdapter(name);

        if (mAdapter != null) {
            mAdapter.addBulkMessages(conversation.getBuffer());
            conversation.clearBuffer();
        } else {
            // Was conversation created while we were paused?
            if (pagerAdapter.getPositionByName(name) == -1) {
                onNewConversation(name);
            }
        }

        // Clear new message notifications for the selected conversation
        if (conversation.getStatus() == Conversation.STATUS_SELECTED && conversation.getNewMentions() > 0) {
            Intent ackIntent = new Intent(this, IRCService.class);
            ackIntent.setAction(IRCService.ACTION_ACK_NEW_MENTIONS);
            ackIntent.putExtra(IRCService.EXTRA_ACK_SERVERID, serverId);
            ackIntent.putExtra(IRCService.EXTRA_ACK_CONVTITLE, name);
            startService(ackIntent);
        }
    }

    // Remove views for conversations that ended while we were paused
    int numViews = pagerAdapter.getCount();
    if (numViews > mConversations.size()) {
        for (int i = 0; i < numViews; ++i) {
            if (!mConversations.contains(pagerAdapter.getItem(i))) {
                pagerAdapter.removeConversation(i--);
                --numViews;
            }
        }
    }

    // Join channel that has been selected in JoinActivity (onActivityResult())
    if (joinChannelBuffer != null) {
        new Thread() {
            @Override
            public void run() {
                binder.getService().getConnection(serverId).joinChannel(joinChannelBuffer);
                joinChannelBuffer = null;
            }
        }.start();
    }

    server.setIsForeground(true);
}