com.mongolduu.android.ng.ChartActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.mongolduu.android.ng.ChartActivity.java

Source

/*
 * Copyright (C) 2011 Erdene-Ochir Tuguldur (https://github.com/tugstugi)
 *
 * 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.mongolduu.android.ng;

import greendroid.widget.ActionBarItem;
import greendroid.widget.ActionBarItem.Type;
import greendroid.widget.LoaderActionBarItem;
import net.peterkuterna.android.apps.swipeytabs.SwipeyTabs;
import net.peterkuterna.android.apps.swipeytabs.SwipeyTabsAdapter;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.mongolduu.android.ng.R;
import com.mongolduu.android.ng.db.DatabaseHelper;
import com.mongolduu.android.ng.misc.Utils;

public class ChartActivity extends OrmLiteGDActivity<DatabaseHelper> {
    private static final String[] chartTypes = { IMongolduuConstants.CHART_TYPE_TODAY,
            IMongolduuConstants.CHART_TYPE_WEEK, IMongolduuConstants.CHART_TYPE_ALLTIME,
            IMongolduuConstants.CHART_TYPE_NEW };

    private static final int[] tabNameStringIds = { R.string.chart_type_today, R.string.chart_type_week,
            R.string.chart_type_alltime, R.string.chart_type_new };

    private LoaderActionBarItem refreshItem = null;
    private SparseArray<ChartFragment> fragments;
    private SwipeyTabs tabs;
    private ViewPager viewpager;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setActionBarContentView(R.layout.swipey);
        setTitle(R.string.chart_activity_title);

        refreshItem = (LoaderActionBarItem) addActionBarItem(Type.Refresh, R.id.action_bar_refresh);
        addActionBarItem(Type.Search, R.id.action_bar_search);

        fragments = new SparseArray<ChartFragment>();
        for (int i = 0; i < chartTypes.length; i++) {
            fragments.put(i, ChartFragment.newInstance(i, chartTypes[i]));
        }

        viewpager = (ViewPager) findViewById(R.id.viewpager);
        tabs = (SwipeyTabs) findViewById(R.id.swipeytabs);

        SwipeyTabsPagerAdapter adapter = new SwipeyTabsPagerAdapter(this, getSupportFragmentManager());
        viewpager.setAdapter(adapter);
        tabs.setAdapter(adapter);
        viewpager.setOnPageChangeListener(tabs);
        viewpager.setCurrentItem(1);
    }

    protected class SwipeyTabsPagerAdapter extends FragmentPagerAdapter implements SwipeyTabsAdapter {

        private final Context mContext;

        public SwipeyTabsPagerAdapter(Context context, FragmentManager fm) {
            super(fm);

            this.mContext = context;
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return chartTypes.length;
        }

        public TextView getTab(final int position, SwipeyTabs root) {
            TextView view = (TextView) LayoutInflater.from(mContext).inflate(R.layout.swipey_tab_indicator, root,
                    false);
            view.setText(getString(tabNameStringIds[position]));
            view.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    viewpager.setCurrentItem(position);
                }
            });

            return view;
        }
    }

    public void onResume() {
        checkIfSongSavedOnDeviceAndUpdateListView();
        super.onResume();
    }

    public void checkIfSongSavedOnDeviceAndUpdateListView() {
        for (int i = 0; i < fragments.size(); i++) {
            fragments.get(i).checkIfSongSavedOnDeviceAndUpdateListView();
        }
    }

    private void refreshChartList() {
        for (int i = 0; i < fragments.size(); i++) {
            fragments.get(i).fetchSongs(true);
        }
    }

    public void onTextAndProgressBarClick(View view) {

    }

    @Override
    public boolean onHandleActionBarItemClick(ActionBarItem item, int position) {
        switch (item.getItemId()) {
        case R.id.action_bar_search:
            startActivity(Utils.createSearchIntent(this));
            return true;
        case R.id.action_bar_refresh:
            refreshChartList();
            refreshItem.setLoading(false);
            return true;

        default:
            return super.onHandleActionBarItemClick(item, position);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        boolean retValue = super.onCreateOptionsMenu(menu);
        menu.add(0, R.id.context_menu_home, 0, R.string.context_menu_home);
        menu.add(0, R.id.context_menu_search, 0, R.string.context_menu_search);
        menu.add(0, R.id.context_menu_refresh, 0, R.string.context_menu_refresh);
        return retValue;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.context_menu_home:
            startActivity(Utils.createHomeIntent(this));
            return true;
        case R.id.context_menu_refresh:
            //refreshItem.setLoading(true);
            refreshChartList();
            return true;
        case R.id.context_menu_search:
            startActivity(Utils.createSearchIntent(this));
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
}