Android Open Source - rss Async Navigation Adapter






From Project

Back to project page rss.

License

The source code is released under:

GNU General Public License

If you think the Android project rss listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.//from  w  w w  .j a  v  a 2  s .co  m
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package com.poloure.simplerss;

import android.app.Activity;
import android.os.AsyncTask;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.WrapperListAdapter;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;

public
class AsyncNavigationAdapter extends AsyncTask<String, Void, String[][]>
{
    private static final NumberFormat NUMBER_FORMAT = NumberFormat.getNumberInstance(Locale.getDefault());
    private final FeedsActivity m_activity;

    private
    AsyncNavigationAdapter(Activity activity)
    {
        m_activity = (FeedsActivity) activity;
    }

    public static
    void run(Activity activity)
    {
        AsyncTask<String, Void, String[][]> task = new AsyncNavigationAdapter(activity);
        task.executeOnExecutor(THREAD_POOL_EXECUTOR);
    }

    /* Get the unread counts for the tags. */
    @Override
    protected
    String[][] doInBackground(String... applicationFolder)
    {
        // Get the total number of tags and feeds that exist.
        int tagTotal = PagerAdapterTags.s_tagList.size();

        // Make a NavItem for each tag we will display in the navigation drawer.
        String[][] navItems = new String[tagTotal][2];

        // Number of feeds. */
        int feedCount = m_activity.m_index.size();

        // This is a list of Sets each containing all of the feed's items.
        List<Collection<Long>> feedItems = new ArrayList<Collection<Long>>(feedCount);

        for(IndexItem indexItem : m_activity.m_index)
        {
            ObjectIO reader = new ObjectIO(m_activity, indexItem.m_uid + ServiceUpdate.ITEM_LIST);
            Collection<Long> set = (Collection<Long>) reader.read();
            feedItems.add(null == set ? new HashSet<Long>(0) : set);
        }

        // Create a temporary collection we will .clear() each iteration of the next for loop.
        Collection<Long> itemsInTag = Collections.synchronizedCollection(new HashSet<Long>(0));
        Collection<Long> readItemTimes = m_activity.getReadItemTimes();

        // For each tag excluding the all tag.
        for(int i = 0; tagTotal > i; i++)
        {
            String tag = PagerAdapterTags.s_tagList.get(i);

            // For each feed, if the feed ? this tag, add the feed items to the tag collection.
            for(int j = 0; j < feedCount; j++)
            {
                // If the feed's index entry (tag1, tag2, etc) contains this tag or is the all tag.
                if(0 == i || Arrays.asList(m_activity.m_index.get(j).m_tags).contains(tag))
                {
                    itemsInTag.addAll(feedItems.get(j));
                }
            }
            itemsInTag.removeAll(readItemTimes);

            int size = itemsInTag.size();
            navItems[i][0] = tag;
            navItems[i][1] = 0 == size ? "" : NUMBER_FORMAT.format(size);
            itemsInTag.clear();
        }

        return navItems;
    }

    @Override
    protected
    void onPostExecute(String[][] result)
    {
        // Set the titles & counts arrays in this file and notify the adapter.
        ListView navigationList = (ListView) m_activity.findViewById(R.id.fragment_navigation_drawer);
        WrapperListAdapter wrapperAdapter = (WrapperListAdapter) navigationList.getAdapter();
        ArrayAdapter<String[]> adapter = (ArrayAdapter<String[]>) wrapperAdapter.getWrappedAdapter();

        // Update the data in the adapter.
        adapter.clear();
        adapter.addAll(result);

        // Update the subtitle.
        if(Constants.s_fragmentFeeds.isVisible())
        {
            Utilities.setTitlesAndDrawerAndPage(null, -10);
        }
    }
}




Java Source Code List

com.poloure.simplerss.AsyncCheckFeed.java
com.poloure.simplerss.AsyncLoadImage.java
com.poloure.simplerss.AsyncManageAdapter.java
com.poloure.simplerss.AsyncNavigationAdapter.java
com.poloure.simplerss.AsyncNewTagAdapters.java
com.poloure.simplerss.Constants.java
com.poloure.simplerss.DialogConfirm.java
com.poloure.simplerss.DialogEditFeed.java
com.poloure.simplerss.FeedItem.java
com.poloure.simplerss.FeedsActivity.java
com.poloure.simplerss.FragmentFeeds.java
com.poloure.simplerss.FragmentNavigationDrawer.java
com.poloure.simplerss.FragmentSettings.java
com.poloure.simplerss.IndexItem.java
com.poloure.simplerss.ListFragmentFavourites.java
com.poloure.simplerss.ListFragmentManage.java
com.poloure.simplerss.ListFragmentTag.java
com.poloure.simplerss.ObjectIO.java
com.poloure.simplerss.PagerAdapterTags.java
com.poloure.simplerss.RssLogger.java
com.poloure.simplerss.ServiceUpdate.java
com.poloure.simplerss.Utilities.java
com.poloure.simplerss.ViewFeedItem.java
com.poloure.simplerss.ViewNavItem.java
com.poloure.simplerss.ViewPagerDelegate.java
com.poloure.simplerss.adapters.AdapterFeedItems.java
com.poloure.simplerss.adapters.AdapterManageItems.java
com.poloure.simplerss.adapters.AdapterNavigationDrawer.java
com.poloure.simplerss.adapters.LinkedMapAdapter.java
com.poloure.simplerss.asynctasks.AsyncTaskSaveImage.java
com.poloure.simplerss.fragments.FragmentWebView.java
com.poloure.simplerss.listeners.MultiModeListenerFavourites.java
com.poloure.simplerss.listeners.MultiModeListenerManage.java
com.poloure.simplerss.listeners.MultiModeListener.java
com.poloure.simplerss.ui.ListViewFeeds.java