com.dictionary.activity.ArticleCollectionActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.dictionary.activity.ArticleCollectionActivity.java

Source

/*
 * Copyright 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.dictionary.activity;

import android.app.ActionBar;
import android.app.SearchManager;
import android.content.Intent;
import android.database.DataSetObserver;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.app.TaskStackBuilder;
import android.support.v4.view.PagerTitleStrip;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.util.TypedValue;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.dictionary.aard.pro.Application;
import com.dictionary.aard.pro.BlobDescriptor;
import com.dictionary.aard.pro.R;
import com.dictionary.aard.pro.R.id;
import com.dictionary.aard.pro.R.layout;
import com.dictionary.aard.pro.R.string;
import com.dictionary.adapter.BlobDescriptorListAdapter;
import com.dictionary.adapter.BlobListAdapter;
import com.dictionary.fragment.ArticleFragment;

import itkach.slob.Slob;
import itkach.slob.Slob.Blob;

public class ArticleCollectionActivity extends BaseActivity {

    ArticleCollectionPagerAdapter articleCollectionPagerAdapter;
    ViewPager viewPager;

    ToBlob blobToBlob = new ToBlob() {

        @Override
        public Blob convert(Object item) {
            return (Blob) item;
        }

    };

    private boolean onDestroyCalled = false;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Application app = (Application) getApplication();
        app.installTheme(this);
        requestWindowFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.activity_article_collection_loading);
        app.push(this);
        final ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setSubtitle("...");
        final Intent intent = getIntent();
        final int position = intent.getIntExtra("position", 0);

        AsyncTask<Void, Void, ArticleCollectionPagerAdapter> createAdapterTask = new AsyncTask<Void, Void, ArticleCollectionPagerAdapter>() {

            @Override
            protected ArticleCollectionPagerAdapter doInBackground(Void... params) {
                ArticleCollectionPagerAdapter result;
                Uri articleUrl = intent.getData();
                if (articleUrl != null) {
                    result = createFromUri(app, articleUrl);
                } else {
                    String action = intent.getAction();
                    if (action == null) {
                        result = createFromLastResult(app);
                    } else if (action.equals("showRandom")) {
                        result = createFromRandom(app);
                    } else if (action.equals("showBookmarks")) {
                        result = createFromBookmarks(app);
                    } else if (action.equals("showHistory")) {
                        result = createFromHistory(app);
                    } else {
                        result = createFromIntent(app, intent);
                    }
                }
                return result;
            }

            @Override
            protected void onPostExecute(ArticleCollectionPagerAdapter adapter) {
                if (isFinishing() || onDestroyCalled) {
                    return;
                }
                articleCollectionPagerAdapter = adapter;
                if (articleCollectionPagerAdapter == null || articleCollectionPagerAdapter.getCount() == 0) {
                    int messageId;
                    if (articleCollectionPagerAdapter == null) {
                        messageId = R.string.article_collection_invalid_link;
                    } else {
                        messageId = R.string.article_collection_nothing_found;
                    }
                    Toast.makeText(ArticleCollectionActivity.this, messageId, Toast.LENGTH_SHORT).show();
                    finish();
                    return;
                }

                if (position > articleCollectionPagerAdapter.getCount() - 1) {
                    Toast.makeText(ArticleCollectionActivity.this,
                            R.string.article_collection_selected_not_available, Toast.LENGTH_SHORT).show();
                    finish();
                    return;
                }

                setContentView(R.layout.activity_article_collection);

                findViewById(R.id.pager_title_strip).setVisibility(
                        articleCollectionPagerAdapter.getCount() == 1 ? ViewGroup.GONE : ViewGroup.VISIBLE);

                viewPager = (ViewPager) findViewById(R.id.pager);
                viewPager.setAdapter(articleCollectionPagerAdapter);
                viewPager.setOnPageChangeListener(new OnPageChangeListener() {

                    @Override
                    public void onPageScrollStateChanged(int arg0) {
                    }

                    @Override
                    public void onPageScrolled(int arg0, float arg1, int arg2) {
                    }

                    @Override
                    public void onPageSelected(final int position) {
                        updateTitle(position);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                ArticleFragment fragment = (ArticleFragment) articleCollectionPagerAdapter
                                        .getItem(position);
                                fragment.applyTextZoomPref();
                            }
                        });

                    }
                });
                viewPager.setCurrentItem(position);

                PagerTitleStrip titleStrip = (PagerTitleStrip) findViewById(R.id.pager_title_strip);
                titleStrip.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10);
                updateTitle(position);
                articleCollectionPagerAdapter.registerDataSetObserver(new DataSetObserver() {
                    @Override
                    public void onChanged() {
                        if (articleCollectionPagerAdapter.getCount() == 0) {
                            finish();
                        }
                    }
                });
            }
        };

        createAdapterTask.execute();

    }

    private ArticleCollectionPagerAdapter createFromUri(Application app, Uri articleUrl) {
        BlobDescriptor bd = BlobDescriptor.fromUri(articleUrl);
        if (bd == null) {
            return null;
        }
        Iterator<Slob.Blob> result = app.find(bd.key, bd.slobId);
        BlobListAdapter data = new BlobListAdapter(this, 3, 1);
        data.setData(result);
        return new ArticleCollectionPagerAdapter(app, data, blobToBlob, getSupportFragmentManager());
    };

    private ArticleCollectionPagerAdapter createFromLastResult(Application app) {
        return new ArticleCollectionPagerAdapter(app, app.lastResult, blobToBlob, getSupportFragmentManager());
    }

    private ArticleCollectionPagerAdapter createFromBookmarks(final Application app) {
        return new ArticleCollectionPagerAdapter(app, new BlobDescriptorListAdapter(app.bookmarks), new ToBlob() {
            @Override
            public Blob convert(Object item) {
                return app.bookmarks.resolve((BlobDescriptor) item);
            }
        }, getSupportFragmentManager());
    }

    private ArticleCollectionPagerAdapter createFromHistory(final Application app) {
        return new ArticleCollectionPagerAdapter(app, new BlobDescriptorListAdapter(app.history), new ToBlob() {
            @Override
            public Blob convert(Object item) {
                return app.history.resolve((BlobDescriptor) item);
            }
        }, getSupportFragmentManager());
    }

    private ArticleCollectionPagerAdapter createFromRandom(Application app) {
        BlobListAdapter data = new BlobListAdapter(this);
        List<Blob> result = new ArrayList<Blob>();
        Blob blob = app.random();
        if (blob != null) {
            result.add(blob);
        }
        data.setData(result);
        return new ArticleCollectionPagerAdapter(app, data, blobToBlob, getSupportFragmentManager());
    }

    private ArticleCollectionPagerAdapter createFromIntent(Application app, Intent intent) {
        String lookupKey = intent.getStringExtra(Intent.EXTRA_TEXT);
        if (lookupKey == null) {
            lookupKey = intent.getStringExtra(SearchManager.QUERY);
        }
        BlobListAdapter data = new BlobListAdapter(this, 3, 1);
        if (lookupKey == null || lookupKey.length() == 0) {
            Toast.makeText(this, R.string.article_collection_nothing_to_lookup, Toast.LENGTH_SHORT).show();
        } else {
            Iterator<Slob.Blob> result;
            do {
                result = app.find(lookupKey, null, true);
                if (result.hasNext()) {
                    break;
                }
                lookupKey = lookupKey.substring(0, lookupKey.length() - 1);
            } while (lookupKey.length() > 0);
            data.setData(result);
        }
        return new ArticleCollectionPagerAdapter(app, data, blobToBlob, getSupportFragmentManager());
    }

    private void updateTitle(int position) {
        Log.d("updateTitle", "" + position + " count: " + articleCollectionPagerAdapter.getCount());
        Slob.Blob blob = articleCollectionPagerAdapter.get(position);
        CharSequence pageTitle = articleCollectionPagerAdapter.getPageTitle(position);
        Log.d("updateTitle", "" + blob);
        ActionBar actionBar = getActionBar();
        if (blob != null) {
            String dictLabel = blob.owner.getTags().get("label");
            actionBar.setTitle(dictLabel);
            Application app = (Application) getApplication();
            app.history.add(app.getUrl(blob));
        } else {
            actionBar.setTitle("???");
        }
        actionBar.setSubtitle(pageTitle);
    }

    @Override
    protected void onDestroy() {
        onDestroyCalled = true;
        if (viewPager != null) {
            viewPager.setAdapter(null);
        }
        if (articleCollectionPagerAdapter != null) {
            articleCollectionPagerAdapter.destroy();
        }
        Application app = (Application) getApplication();
        app.pop(this);
        super.onDestroy();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            Intent upIntent = new Intent(this, MainActivity.class);
            if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
                TaskStackBuilder.create(this).addNextIntent(upIntent).startActivities();
                finish();
            } else {
                // This activity is part of the application's task, so simply
                // navigate up to the hierarchical parent activity.
                NavUtils.navigateUpTo(this, upIntent);
            }
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    static interface ToBlob {
        Slob.Blob convert(Object item);
    }

    public static class ArticleCollectionPagerAdapter extends FragmentStatePagerAdapter {

        private Application app;
        private DataSetObserver observer;
        private BaseAdapter data;
        private ToBlob toBlob;
        private int count;

        public ArticleCollectionPagerAdapter(Application app, BaseAdapter data, ToBlob toBlob, FragmentManager fm) {
            super(fm);
            this.app = app;
            this.data = data;
            this.count = data.getCount();
            this.observer = new DataSetObserver() {
                @Override
                public void onChanged() {
                    count = ArticleCollectionPagerAdapter.this.data.getCount();
                    notifyDataSetChanged();
                }
            };
            data.registerDataSetObserver(observer);
            this.toBlob = toBlob;
        }

        void destroy() {
            data.unregisterDataSetObserver(observer);
            data = null;
            app = null;
        }

        @Override
        public Fragment getItem(int i) {
            Fragment fragment = new ArticleFragment();
            Slob.Blob blob = get(i);
            if (blob != null) {
                String articleUrl = app.getUrl(blob);
                Bundle args = new Bundle();
                Log.i("Setting article fragment url", String.format("%s (key %s slob %s)", articleUrl, blob.key,
                        blob.owner.getTags().get("label")));
                args.putString(ArticleFragment.ARG_URL, articleUrl);
                fragment.setArguments(args);
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return count;
        }

        Slob.Blob get(int position) {
            return toBlob.convert(data.getItem(position));
        }

        @Override
        public CharSequence getPageTitle(int position) {
            if (position < data.getCount()) {
                Object item = data.getItem(position);
                if (item instanceof BlobDescriptor) {
                    return ((BlobDescriptor) item).key;
                }
                if (item instanceof Slob.Blob) {
                    return ((Blob) item).key;
                }
            }
            return "???";
        }

        // this is needed so that fragment is properly updated
        // if underlying data changes (such as on unbookmark)
        // https://code.google.com/p/android/issues/detail?id=19001
        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }
    }
}