private void createShareDialog(List<SharingHelper.SHARE_WITH> preferredOptions) {
        final PackageManager packageManager = context_.getPackageManager();
        final List<ResolveInfo> preferredApps = new ArrayList<>();
        final List<ResolveInfo> matchingApps = packageManager.queryIntentActivities(shareLinkIntent_, PackageManager.MATCH_DEFAULT_ONLY);
        List<ResolveInfo> cleanedMatchingApps = new ArrayList<>();
        final List<ResolveInfo> cleanedMatchingAppsFinal = new ArrayList<>();
        ArrayList<SharingHelper.SHARE_WITH> packagesFilterList = new ArrayList<>(preferredOptions);

        /* Get all apps available for sharing and the available preferred apps. */
        for (ResolveInfo resolveInfo : matchingApps) {
            SharingHelper.SHARE_WITH foundMatching = null;
            String packageName = resolveInfo.activityInfo.packageName;
            for (SharingHelper.SHARE_WITH PackageFilter : packagesFilterList) {
                if (resolveInfo.activityInfo != null && packageName.toLowerCase().contains(PackageFilter.toString().toLowerCase())) {
                    foundMatching = PackageFilter;
                    break;
                }
            }
            if (foundMatching != null) {
                preferredApps.add(resolveInfo);
                preferredOptions.remove(foundMatching);
            }
        }
        /* Create all app list with copy link item. */
        matchingApps.removeAll(preferredApps);
        matchingApps.addAll(0, preferredApps);
        
        //if apps are explicitly being included, add only those, otherwise at the else statement add them all
        if (includeInShareSheet.size() > 0) {
            for (ResolveInfo r : matchingApps) {
                if (includeInShareSheet.contains(r.activityInfo.packageName)) {
                    cleanedMatchingApps.add(r);
                }
            }
        } else {
            cleanedMatchingApps = matchingApps;
        }
        
        //does our list contain explicitly excluded items? do not carry them into the next list
        for (ResolveInfo r : cleanedMatchingApps) {
            if (!excludeFromShareSheet.contains(r.activityInfo.packageName)) {
                cleanedMatchingAppsFinal.add(r);
            }
        }
        
        //make sure our "show more" option includes preferred apps
        for (ResolveInfo r : matchingApps) {
            for (SharingHelper.SHARE_WITH shareWith : packagesFilterList)
                if (shareWith.toString().equalsIgnoreCase(r.activityInfo.packageName)) {
                    cleanedMatchingAppsFinal.add(r);
                }
        }
        
        cleanedMatchingAppsFinal.add(new CopyLinkItem());
        matchingApps.add(new CopyLinkItem());
        preferredApps.add(new CopyLinkItem());
        
        if (preferredApps.size() > 1) {
            /* Add more and copy link option to preferred app.*/
            if (matchingApps.size() > preferredApps.size()) {
                preferredApps.add(new MoreShareItem());
            }
            appList_ = preferredApps;
        } else {
            appList_ = cleanedMatchingAppsFinal;
        }

        /* Copy link option will be always there for sharing. */
        final ChooserArrayAdapter adapter = new ChooserArrayAdapter();
        final ListView shareOptionListView;
        if (shareDialogThemeID_ > 1 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            shareOptionListView = new ListView(context_, null, 0, shareDialogThemeID_);
        } else {
            shareOptionListView = new ListView(context_);
        }
        shareOptionListView.setHorizontalFadingEdgeEnabled(false);
        shareOptionListView.setBackgroundColor(Color.WHITE);
        shareOptionListView.setSelector(new ColorDrawable(Color.TRANSPARENT));
        
        if (builder_.getSharingTitleView() != null) {
            shareOptionListView.addHeaderView(builder_.getSharingTitleView(), null, false);
        } else if (!TextUtils.isEmpty(builder_.getSharingTitle())) {
            TextView textView = new TextView(context_);
            textView.setText(builder_.getSharingTitle());
            textView.setBackgroundColor(BG_COLOR_DISABLED);
            textView.setTextColor(BG_COLOR_DISABLED);
            textView.setTextAppearance(context_, android.R.style.TextAppearance_Medium);
            textView.setTextColor(context_.getResources().getColor(android.R.color.darker_gray));
            textView.setPadding(leftMargin, padding, padding, padding);
            shareOptionListView.addHeaderView(textView, null, false);
        }
        
        shareOptionListView.setAdapter(adapter);
        
        if (builder_.getDividerHeight() >= 0) { //User set height
            shareOptionListView.setDividerHeight(builder_.getDividerHeight());
        } else if (builder_.getIsFullWidthStyle()) {
            shareOptionListView.setDividerHeight(0); // Default no divider for full width dialog
        }
        
        shareOptionListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
                if (view.getTag() instanceof MoreShareItem) {
                    appList_ = cleanedMatchingAppsFinal;
                    adapter.notifyDataSetChanged();
                } else {
                    if (callback_ != null) {
                        String selectedChannelName = "";
                        if (view.getTag() != null && context_ != null && ((ResolveInfo) view.getTag()).loadLabel(context_.getPackageManager()) != null) {
                            selectedChannelName = ((ResolveInfo) view.getTag()).loadLabel(context_.getPackageManager()).toString();
                        }
                        builder_.getShortLinkBuilder().setChannel(((ResolveInfo) view.getTag()).loadLabel(context_.getPackageManager()).toString());
                        callback_.onChannelSelected(selectedChannelName);
                    }
                    adapter.selectedPos = pos - shareOptionListView.getHeaderViewsCount();
                    adapter.notifyDataSetChanged();
                    invokeSharingClient((ResolveInfo) view.getTag());
                    if (shareDlg_ != null) {
                        shareDlg_.cancel();
                    }
                }
            }
        });
        if (builder_.getDialogThemeResourceID() > 0) {
            shareDlg_ = new AnimatedDialog(context_, builder_.getDialogThemeResourceID());
        } else {
            shareDlg_ = new AnimatedDialog(context_, builder_.getIsFullWidthStyle());
        }
        shareDlg_.setContentView(shareOptionListView);
        shareDlg_.show();
        if (callback_ != null) {
            callback_.onShareLinkDialogLaunched();
        }
        shareDlg_.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                if (callback_ != null) {
                    callback_.onShareLinkDialogDismissed();
                    callback_ = null;
                }
                // Release  context to prevent leaks
                if (!isShareInProgress_) {
                    context_ = null;
                    builder_ = null;
                }
                shareDlg_ = null;
            }
        });
    }
private void createShareDialog ( List < SharingHelper . SHARE_WITH > preferredOptions ) { final PackageManager packageManager = context_ . getPackageManager ( ) ; final List < ResolveInfo > preferredApps = new ArrayList < > ( ) ; final List < ResolveInfo > matchingApps = packageManager . queryIntentActivities ( shareLinkIntent_ , PackageManager . MATCH_DEFAULT_ONLY ) ; List < ResolveInfo > cleanedMatchingApps = new ArrayList < > ( ) ; final List < ResolveInfo > cleanedMatchingAppsFinal = new ArrayList < > ( ) ; ArrayList < SharingHelper . SHARE_WITH > packagesFilterList = new ArrayList < > ( preferredOptions ) ; for ( ResolveInfo resolveInfo : matchingApps ) { SharingHelper . SHARE_WITH foundMatching = null ; String packageName = resolveInfo . activityInfo . packageName ; for ( SharingHelper . SHARE_WITH PackageFilter : packagesFilterList ) { if ( resolveInfo . activityInfo != null && packageName . toLowerCase ( ) . contains ( PackageFilter . toString ( ) . toLowerCase ( ) ) ) { foundMatching = PackageFilter ; break ; } } if ( foundMatching != null ) { preferredApps . add ( resolveInfo ) ; preferredOptions . remove ( foundMatching ) ; } } matchingApps . removeAll ( preferredApps ) ; matchingApps . addAll ( 0 , preferredApps ) ; if ( includeInShareSheet . size ( ) > 0 ) { for ( ResolveInfo r : matchingApps ) { if ( includeInShareSheet . contains ( r . activityInfo . packageName ) ) { cleanedMatchingApps . add ( r ) ; } } } else { cleanedMatchingApps = matchingApps ; } for ( ResolveInfo r : cleanedMatchingApps ) { if ( ! excludeFromShareSheet . contains ( r . activityInfo . packageName ) ) { cleanedMatchingAppsFinal . add ( r ) ; } } for ( ResolveInfo r : matchingApps ) { for ( SharingHelper . SHARE_WITH shareWith : packagesFilterList ) if ( shareWith . toString ( ) . equalsIgnoreCase ( r . activityInfo . packageName ) ) { cleanedMatchingAppsFinal . add ( r ) ; } } cleanedMatchingAppsFinal . add ( new CopyLinkItem ( ) ) ; matchingApps . add ( new CopyLinkItem ( ) ) ; preferredApps . add ( new CopyLinkItem ( ) ) ; if ( preferredApps . size ( ) > 1 ) { if ( matchingApps . size ( ) > preferredApps . size ( ) ) { preferredApps . add ( new MoreShareItem ( ) ) ; } appList_ = preferredApps ; } else { appList_ = cleanedMatchingAppsFinal ; } final ChooserArrayAdapter adapter = new ChooserArrayAdapter ( ) ; final ListView shareOptionListView ; if ( shareDialogThemeID_ > 1 && Build . VERSION . SDK_INT >= Build . VERSION_CODES . LOLLIPOP ) { shareOptionListView = new ListView ( context_ , null , 0 , shareDialogThemeID_ ) ; } else { shareOptionListView = new ListView ( context_ ) ; } shareOptionListView . setHorizontalFadingEdgeEnabled ( false ) ; shareOptionListView . setBackgroundColor ( Color . WHITE ) ; shareOptionListView . setSelector ( new ColorDrawable ( Color . TRANSPARENT ) ) ; if ( builder_ . getSharingTitleView ( ) != null ) { shareOptionListView . addHeaderView ( builder_ . getSharingTitleView ( ) , null , false ) ; } else if ( ! TextUtils . isEmpty ( builder_ . getSharingTitle ( ) ) ) { TextView textView = new TextView ( context_ ) ; textView . setText ( builder_ . getSharingTitle ( ) ) ; textView . setBackgroundColor ( BG_COLOR_DISABLED ) ; textView . setTextColor ( BG_COLOR_DISABLED ) ; textView . setTextAppearance ( context_ , android . R . style . TextAppearance_Medium ) ; textView . setTextColor ( context_ . getResources ( ) . getColor ( android . R . color . darker_gray ) ) ; textView . setPadding ( leftMargin , padding , padding , padding ) ; shareOptionListView . addHeaderView ( textView , null , false ) ; } shareOptionListView . setAdapter ( adapter ) ; if ( builder_ . getDividerHeight ( ) >= 0 ) { shareOptionListView . setDividerHeight ( builder_ . getDividerHeight ( ) ) ; } else if ( builder_ . getIsFullWidthStyle ( ) ) { shareOptionListView . setDividerHeight ( 0 ) ; } shareOptionListView . setOnItemClickListener ( new AdapterView . OnItemClickListener ( ) { @ Override public void onItemClick ( AdapterView < ? > adapterView , View view , int pos , long l ) { if ( view . getTag ( ) instanceof MoreShareItem ) { appList_ = cleanedMatchingAppsFinal ; adapter . notifyDataSetChanged ( ) ; } else { if ( callback_ != null ) { String selectedChannelName = "" ; if ( view . getTag ( ) != null && context_ != null && ( ( ResolveInfo ) view . getTag ( ) ) . loadLabel ( context_ . getPackageManager ( ) ) != null ) { selectedChannelName = ( ( ResolveInfo ) view . getTag ( ) ) . loadLabel ( context_ . getPackageManager ( ) ) . toString ( ) ; } builder_ . getShortLinkBuilder ( ) . setChannel ( ( ( ResolveInfo ) view . getTag ( ) ) . loadLabel ( context_ . getPackageManager ( ) ) . toString ( ) ) ; callback_ . onChannelSelected ( selectedChannelName ) ; } adapter . selectedPos = pos - shareOptionListView . getHeaderViewsCount ( ) ; adapter . notifyDataSetChanged ( ) ; invokeSharingClient ( ( ResolveInfo ) view . getTag ( ) ) ; if ( shareDlg_ != null ) { shareDlg_ . cancel ( ) ; } } } } ) ; if ( builder_ . getDialogThemeResourceID ( ) > 0 ) { shareDlg_ = new AnimatedDialog ( context_ , builder_ . getDialogThemeResourceID ( ) ) ; } else { shareDlg_ = new AnimatedDialog ( context_ , builder_ . getIsFullWidthStyle ( ) ) ; } shareDlg_ . setContentView ( shareOptionListView ) ; shareDlg_ . show ( ) ; if ( callback_ != null ) { callback_ . onShareLinkDialogLaunched ( ) ; } shareDlg_ . setOnDismissListener ( new DialogInterface . OnDismissListener ( ) { @ Override public void onDismiss ( DialogInterface dialogInterface ) { if ( callback_ != null ) { callback_ . onShareLinkDialogDismissed ( ) ; callback_ = null ; } if ( ! isShareInProgress_ ) { context_ = null ; builder_ = null ; } shareDlg_ = null ; } } ) ; }
Create a custom chooser dialog with available share options.

@param preferredOptions List of {@link io.branch.referral.SharingHelper.SHARE_WITH} options.