Tags

, , , , , , , , ,

As a part of our ongoing “Fennec should have its own theme” approach, we felt the Share activity chooser provided by Android varies between releases. Also, opening a Share Activity Chooser, causes the foreground browser to go into background, calls onPause(), and on and on. With the implementation of SubMenu as a part of the custom menu, we decided to show the share as a list like the menu items list. The final Share items menu would look like:

As Firefox doesn’t use ActionBar, Android’s ShareActionProvider cannot be used to generate a list like in Android’s Gallery. However, Android provides a way to query the list of activities that can accept an Intent. The code logic would look like:

    // The Share Intent that we would ask Android to query for us.
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_TEXT, _some_text_);
    shareIntent.putExtra(Intent.EXTRA_SUBJECT, _some_subject_);
    shareIntent.setType("text/plain");

    // PackageManager allows us to query the activities that can respond an intent.
    PackageManager pm = getPackageManager();
    List<ResolveInfo> activities = pm.queryIntentActivities(shareIntent, 0);

    // Convert the list to an adapter, and show it as a ListView or GridView as needed.
    ListView listView = _some_list_view_;
    listView.setAdapter(new ArrayAdapter(_some_context,
                                         _text_view_reference_,
                                         activities));
    
    // On click a list item, we can pass it to the actual activity directly.
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, int id) {
            // Get the actual activity info based on the item clicked.
            ActivityInfo activityInfo = activities.get(position).activityInfo;

            // Create the component based on the activity's package and the class.
            shareIntent.setComponent(new ComponentName(activityInfo.packageName, activityInfo.name));
            startActivity(shareIntent);
        }
    });

And we that’s how we can have a share menu, that’s unique, yet following our own app’s themes.

Advertisements