Example usage for android.os TransactionTooLargeException getMessage

List of usage examples for android.os TransactionTooLargeException getMessage

Introduction

In this page you can find the example usage for android.os TransactionTooLargeException getMessage.

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:com.fastbootmobile.encore.app.fragments.ListenNowFragment.java

private void setupItems() {
    mItemsSetupThread = new Thread() {
        public void run() {
            final Context context = getActivity();

            if (context == null) {
                Log.e(TAG, "Invalid context when generating Listen Now items!");
                return;
            }/*from   w w w.j a  va  2 s . c o  m*/

            final List<ListenNowAdapter.ListenNowItem> items = new ArrayList<>();
            final ProviderAggregator aggregator = ProviderAggregator.getDefault();
            final PluginsLookup plugins = PluginsLookup.getDefault();
            final List<Playlist> playlists = aggregator.getAllPlaylists();
            final List<Song> songs = new ArrayList<>();

            // Put a card to notify of sound effects
            final SharedPreferences prefs = context.getSharedPreferences(PREFS, 0);
            if (!prefs.getBoolean(LANDCARD_SOUND_EFFECTS, false)) {
                // Show the "You have no custom providers" card
                final ListenNowAdapter.CardItem item = new ListenNowAdapter.CardItem(
                        getString(R.string.ln_landcard_sfx_title), getString(R.string.ln_landcard_sfx_body),
                        getString(R.string.browse), new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                prefs.edit().putBoolean(LANDCARD_SOUND_EFFECTS, true).apply();
                                v.getContext()
                                        .startActivity(new Intent(v.getContext(), SettingsActivity.class));
                            }
                        }, getString(R.string.ln_landcard_dismiss), new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                prefs.edit().putBoolean(LANDCARD_SOUND_EFFECTS, true).apply();
                                // This item must always be the first of the list
                                mAdapter.removeItem((ListenNowAdapter.ListenNowItem) v.getTag());
                                mAdapter.notifyDataSetChanged();
                            }
                        });
                mAdapter.addItem(item);
            }

            // Put cards for new providers
            Set<ProviderConnection> newPlugins = PluginsLookup.getDefault().getNewPlugins();
            if (newPlugins != null) {
                for (final ProviderConnection plugin : newPlugins) {
                    final ListenNowAdapter.CardItem item;
                    if (plugin.getConfigurationActivity() == null) {
                        item = new ListenNowAdapter.CardItem(
                                String.format(getString(R.string.ln_landcard_plugin_installed_title),
                                        plugin.getProviderName()),
                                getString(R.string.ln_landcard_plugin_installed_body),
                                getString(R.string.ln_landcard_dismiss), new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        prefs.edit().putBoolean(LANDCARD_SOUND_EFFECTS, true).apply();
                                        // This item must always be the first of the list
                                        mAdapter.removeItem((ListenNowAdapter.ListenNowItem) v.getTag());
                                        mAdapter.notifyDataSetChanged();
                                    }
                                });
                    } else {
                        item = new ListenNowAdapter.CardItem(
                                String.format(getString(R.string.ln_landcard_plugin_installed_title),
                                        plugin.getProviderName()),
                                getString(R.string.ln_landcard_plugin_installed_body_configure),
                                getString(R.string.ln_landcard_dismiss), new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        prefs.edit().putBoolean(LANDCARD_SOUND_EFFECTS, true).apply();
                                        // This item must always be the first of the list
                                        mAdapter.removeItem((ListenNowAdapter.ListenNowItem) v.getTag());
                                        mAdapter.notifyDataSetChanged();
                                    }
                                }, getString(R.string.configure), new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        mAdapter.removeItem((ListenNowAdapter.ListenNowItem) v.getTag());
                                        mAdapter.notifyDataSetChanged();

                                        Intent intent = new Intent();
                                        intent.setClassName(plugin.getPackage(),
                                                plugin.getConfigurationActivity());
                                        startActivity(intent);
                                    }
                                });
                    }
                    mAdapter.addItem(item);
                }
                PluginsLookup.getDefault().resetNewPlugins();
            }

            // Get the list of songs first
            final List<ProviderConnection> providers = plugins.getAvailableProviders();
            for (ProviderConnection provider : providers) {
                int limit = 50;
                int offset = 0;

                while (!isInterrupted()) {
                    try {
                        List<Song> providerSongs = provider.getBinder().getSongs(offset, limit);

                        if (providerSongs != null) {
                            songs.addAll(providerSongs);
                            offset += providerSongs.size();

                            if (providerSongs.size() < limit) {
                                if (DEBUG)
                                    Log.d(TAG, "Got " + providerSongs.size() + " instead of " + limit
                                            + ", assuming end of list");
                                break;
                            }
                        } else {
                            break;
                        }
                    } catch (TransactionTooLargeException e) {
                        limit -= 5;
                        if (limit <= 0) {
                            Log.e(TAG, "Error getting songs from " + provider.getProviderName()
                                    + ": transaction too large even with limit = 5");
                            break;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "Error getting songs from " + provider.getProviderName() + ": "
                                + e.getMessage());
                        break;
                    }
                }
            }

            if (isInterrupted() || isDetached())
                return;

            // Add a card if we have local music, but no cloud providers
            if (providers.size() <= PluginsLookup.BUNDLED_PROVIDERS_COUNT && songs.size() > 0) {
                if (!prefs.getBoolean(LANDCARD_NO_CUSTOM_PROVIDERS, false)) {
                    // Show the "You have no custom providers" card
                    final ListenNowAdapter.CardItem item = new ListenNowAdapter.CardItem(
                            getString(R.string.ln_landcard_nocustomprovider_title),
                            getString(R.string.ln_landcard_nocustomprovider_body), getString(R.string.browse),
                            new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    ProviderDownloadDialog.newInstance(false).show(getFragmentManager(),
                                            "DOWN");
                                }
                            }, getString(R.string.ln_landcard_dismiss), new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    prefs.edit().putBoolean(LANDCARD_NO_CUSTOM_PROVIDERS, true).apply();
                                    // This item must always be the first of the list
                                    mAdapter.removeItem((ListenNowAdapter.ListenNowItem) v.getTag());
                                    mAdapter.notifyDataSetChanged();
                                }
                            });

                    items.add(item);
                }
            }

            if (isInterrupted() || isDetached())
                return;

            // Add a card if there's no music at all (no songs and no playlists)
            if (providers.size() <= PluginsLookup.BUNDLED_PROVIDERS_COUNT && songs.size() == 0
                    && playlists.size() == 0) {
                items.add(new ListenNowAdapter.CardItem(getString(R.string.ln_card_nothing_title),
                        getString(R.string.ln_card_nothing_body), getString(R.string.browse),
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                ProviderDownloadDialog.newInstance(false).show(getFragmentManager(), "DOWN");
                            }
                        }, getString(R.string.configure), new View.OnClickListener() {
                            public void onClick(View v) {
                                ((MainActivity) context).openSection(MainActivity.SECTION_SETTINGS);
                            }
                        }));

                items.add(new ListenNowAdapter.CardItem(getString(R.string.ln_card_nothinghint_title),
                        getString(R.string.ln_card_nothinghint_body), null, null));
            }

            if (isInterrupted() || isDetached())
                return;

            // Add the "Recently played" section if we have recent tracks
            final ListenLogger logger = new ListenLogger(context);
            List<ListenLogger.LogEntry> logEntries = logger.getEntries(50);

            if (logEntries.size() > 0 && !isDetached()) {
                items.add(new ListenNowAdapter.SectionHeaderItem(getString(R.string.ln_section_recents),
                        R.drawable.ic_nav_history_active, getString(R.string.more), new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                ((MainActivity) context).openSection(MainActivity.SECTION_HISTORY);
                            }
                        }));

                int i = 0;
                List<ListenNowAdapter.ItemCardItem> itemsCouple = new ArrayList<>();

                for (ListenLogger.LogEntry entry : logEntries) {
                    if (i == 4) {
                        // Stop here, add remaining item
                        if (itemsCouple.size() > 0) {
                            for (ListenNowAdapter.ItemCardItem item : itemsCouple) {
                                items.add(item);
                            }
                        }
                        break;
                    }

                    Song song = aggregator.retrieveSong(entry.getReference(), entry.getIdentifier());
                    if (song != null) {
                        int type = Utils.getRandom(2);
                        if (song.getAlbum() != null && (type == 0 || type == 1 && song.getArtist() == null)) {
                            Album album = aggregator.retrieveAlbum(song.getAlbum(), song.getProvider());

                            if (album != null) {
                                itemsCouple.add(new ListenNowAdapter.ItemCardItem(album));
                                ++i;
                            }
                        } else if (song.getArtist() != null) {
                            Artist artist = aggregator.retrieveArtist(song.getArtist(), song.getProvider());

                            if (artist != null) {
                                itemsCouple.add(new ListenNowAdapter.ItemCardItem(artist));
                                ++i;
                            }
                        }
                    }

                    if (itemsCouple.size() == 2) {
                        ListenNowAdapter.CardRowItem row = new ListenNowAdapter.CardRowItem(itemsCouple.get(0),
                                itemsCouple.get(1));
                        items.add(row);
                        itemsCouple.clear();
                    }
                }
            }

            if (isInterrupted() || isDetached())
                return;

            // Add playlists section
            items.add(new ListenNowAdapter.SectionHeaderItem(getString(R.string.ln_section_playlists),
                    R.drawable.ic_nav_playlist_active, getString(R.string.browse), new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ((MainActivity) context).openSection(MainActivity.SECTION_PLAYLISTS);
                        }
                    }));

            if (playlists != null && playlists.size() > 0) {
                int i = 0;
                List<ListenNowAdapter.ItemCardItem> itemsCouple = new ArrayList<>();

                for (Playlist playlist : playlists) {
                    if (i == 4) {
                        // Stop here, add remaining item
                        if (itemsCouple.size() > 0) {
                            for (ListenNowAdapter.ItemCardItem item : itemsCouple) {
                                items.add(item);
                            }
                        }
                        break;
                    }

                    if (playlist != null) {
                        ListenNowAdapter.ItemCardItem item = new ListenNowAdapter.ItemCardItem(playlist);
                        itemsCouple.add(item);
                        ++i;
                    }

                    if (itemsCouple.size() == 2) {
                        ListenNowAdapter.CardRowItem row = new ListenNowAdapter.CardRowItem(itemsCouple.get(0),
                                itemsCouple.get(1));
                        items.add(row);
                        itemsCouple.clear();
                    }
                }
            }

            if (isInterrupted() || isDetached())
                return;

            // Add automix section
            items.add(new ListenNowAdapter.SectionHeaderItem(getString(R.string.lb_section_automixes),
                    R.drawable.ic_nav_automix_active, getString(R.string.create), new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ((MainActivity) context).openSection(MainActivity.SECTION_AUTOMIX);
                        }
                    }));

            List<AutoMixBucket> buckets = AutoMixManager.getDefault().getBuckets();
            if (buckets == null || buckets.size() == 0) {
                items.add(new ListenNowAdapter.GetStartedItem(getString(R.string.ln_automix_getstarted_body),
                        getString(R.string.ln_action_getstarted), new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                ((MainActivity) context)
                                        .onNavigationDrawerItemSelected(MainActivity.SECTION_AUTOMIX);
                            }
                        }));
            } else {
                for (final AutoMixBucket bucket : buckets) {
                    items.add(new ListenNowAdapter.SimpleItem(bucket.getName(), new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            new Thread() {
                                public void run() {
                                    AutoMixManager.getDefault().startPlay(bucket);
                                }
                            }.start();
                        }
                    }));
                }
            }

            if (isInterrupted() || isDetached())
                return;

            mHandler.post(new Runnable() {
                public void run() {
                    for (ListenNowAdapter.ListenNowItem item : items) {
                        mAdapter.addItem(item);
                    }
                    mAdapter.notifyDataSetChanged();
                }
            });
        }
    };

    mItemsSetupThread.start();
}