Android Open Source - rss Async New Tag Adapters






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.//  ww w  .ja  v a2  s .  c  om
 *
 * 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.os.AsyncTask;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.animation.AlphaAnimation;

import com.poloure.simplerss.adapters.AdapterFeedItems;
import com.poloure.simplerss.adapters.LinkedMapAdapter;
import com.poloure.simplerss.ui.ListViewFeeds;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public
class AsyncNewTagAdapters extends AsyncTask<Void, Void, TreeMap<Long, FeedItem>[]>
{
    private final FeedsActivity m_activity;

    private
    AsyncNewTagAdapters(FeedsActivity activity)
    {
        m_activity = activity;
    }

    public static
    void update(FeedsActivity activity)
    {
        new AsyncNewTagAdapters(activity).executeOnExecutor(THREAD_POOL_EXECUTOR);
    }

    @Override
    protected
    TreeMap<Long, FeedItem>[] doInBackground(Void... nothing)
    {
        String[] tags = PagerAdapterTags.s_tagList.toArray(new String[PagerAdapterTags.s_tagList.size()]);

        TreeMap<Long, FeedItem>[] maps = new TreeMap[tags.length];
        for(int i = 0; tags.length > i; i++)
        {
            maps[i] = new TreeMap<Long, FeedItem>(Collections.reverseOrder());
        }

        // For each feed.
        for(IndexItem indexItem : m_activity.m_index)
        {
            // Get a list of tags that this feed belongs to.
            List<String> feedsTags = Arrays.asList(indexItem.m_tags);

            // Load the data from file.
            ObjectIO reader = new ObjectIO(m_activity, Long.toString(indexItem.m_uid));
            Map<Long, FeedItem> tempMap = (Map<Long, FeedItem>) reader.read();

            // In case the user is still using the old -content.txt file name.
            if(new File(m_activity.getFilesDir(), indexItem.m_uid + "-content.txt").exists())
            {
                reader = new ObjectIO(m_activity, indexItem.m_uid + "-content.txt");
                tempMap = (Map<Long, FeedItem>) reader.read();
                m_activity.deleteFile(indexItem.m_uid + "-content.txt");

                ObjectIO writer = new ObjectIO(m_activity, Long.toString(indexItem.m_uid));
                writer.write(tempMap);
            }

            // Put the item in each map that the feed is tagged with.
            if(null != tempMap)
            {
                for(int j = 0; tags.length > j; j++)
                {
                    if(0 == j || feedsTags.contains(tags[j]))
                    {
                        maps[j].putAll(tempMap);
                    }
                }
            }
        }
        return maps;
    }

    @Override
    protected
    void onPostExecute(TreeMap<Long, FeedItem>[] result)
    {
        ViewPager pager = (ViewPager) m_activity.findViewById(R.id.viewpager);
        PagerAdapter pagerAdapter = pager.getAdapter();
        int pageCount = pagerAdapter.getCount();

        for(int i = 0; pageCount > i; i++)
        {
            // Get the tag page and skip ListViews that are null.
            ListFragmentTag fragment = FragmentFeeds.getViewPagerFragment(i);
            LinkedMapAdapter<Long, FeedItem> adapterTag = fragment.getListAdapter();
            ListViewFeeds listView = fragment.getListView();

            if(null == adapterTag)
            {
                fragment.setListAdapter(new AdapterFeedItems(m_activity, result[i]));
                listView.setSelectionOldestUnread(m_activity.getReadItemTimes());
            }
            else
            {
                long topKeyBefore = 0L;
                int top = 0;

                // If there are items in the currently viewed page, save the position.
                boolean firstLoad = null == listView || 0 == listView.getCount();
                if(!firstLoad)
                {
                    // Get the time of the top item.
                    int topVisibleItem = listView.getFirstVisiblePosition();
                    topKeyBefore = adapterTag.getKey(topVisibleItem);

                    View v = listView.getChildAt(0);
                    top = null == v ? 0 : v.getTop();
                }

                // Update the feedItems in the adapter.
                adapterTag.replaceAll(result[i]);

                // Now find the position of the item with the time timeBefore.
                if(firstLoad)
                {
                    listView.setSelectionOldestUnread(m_activity.getReadItemTimes());
                }
                else
                {
                    int newPos = adapterTag.indexOf(topKeyBefore);
                    listView.setSelectionFromTop(newPos, top - listView.getPaddingTop());
                }
            }
        }

        // If the pager is invisible, use a fade in animation.
        if(View.VISIBLE != pager.getVisibility())
        {
            pager.setVisibility(View.VISIBLE);
            AlphaAnimation animation = new AlphaAnimation(0.0F, 1.0F);
            animation.setDuration(300);
            pager.startAnimation(animation);
        }
    }
}




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