joshuatee.wx.WX.java Source code

Java tutorial

Introduction

Here is the source code for joshuatee.wx.WX.java

Source

/*
    
Copyright 2013, 2014 joshua.tee@gmail.com
    
This file is part of wX.
    
wX 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.
    
wX 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 wX.  If not, see <http://www.gnu.org/licenses/>.
    
 */

package joshuatee.wx;

import android.os.Bundle;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
//import android.support.v4.app.FragmentStatePagerAdapter;
//import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.Window;
import android.preference.PreferenceManager;
import joshuatee.wx.ImagesFragment;
import joshuatee.wx.LocationFragment;
import joshuatee.wx.MiscFragment;
import joshuatee.wx.R;
import joshuatee.wx.SPCFragment;
import android.support.v4.app.Fragment;
//import android.support.v4.app.FragmentActivity;

public class WX extends CommonActionBarFragment implements ActionBar.TabListener {

    // This activity is the main activity and is call from Startup.
    // It manages the main tabs and also checks ( configurable ) for SPC MCD/Watches
    // and severe weather alerts ( tornado, tstorm, flash flood )
    // back button has been disabled to prevent accidental closure

    String notif_current = "";

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    final private String[] tabs = { "LOCAL", "SPC", "MISC", "IMAGES" };
    final private String[] tabs_simple = { "Local Forecast" };

    String simple_mode_current = "";

    @Override
    public void onBackPressed() {

        Utility.MakeToast(this,
                "The back button has been disabled from the main screen to prevent accidental closure of wX. Please use the task manager to close wX if needed.");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        if (savedInstanceState != null) {
            savedInstanceState.remove("android:support:fragments");
        }

        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        theme_blue_current = preferences.getString("THEME_BLUE", "");
        setTheme(Utility.Theme(theme_blue_current));
        setContentView(R.layout.main);

        simple_mode_current = preferences.getString("SIMPLE_MODE", "");

        if (!DataStore.loaded)
            DataStore.Init(this);

        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(mAdapter);
        viewPager.setOffscreenPageLimit(4);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        if (simple_mode_current.startsWith("f")) {
            for (String tab_name : tabs) {
                actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
            }
        } else {
            for (String tab_name : tabs_simple) {
                actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
            }
        }

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }

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

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });

        refresh_dynamic_content();

    }

    public void refresh_dynamic_content() {

        notif_current = preferences.getString("NOTIF", "true");

        if (simple_mode_current.startsWith("f") && notif_current.startsWith("t")) {

            String[] tab_str = Utility.CheckSPC(preferences);

            actionBar.getTabAt(1).setText(tab_str[0]);
            actionBar.getTabAt(2).setText(tab_str[1]);

        }

        if (simple_mode_current.startsWith("f") && notif_current.startsWith("f")) {
            actionBar.getTabAt(1).setText("SPC");
            actionBar.getTabAt(2).setText("MISC");
        }

    }

    protected void onResume() {

        registerReceiver(onBroadcast, new IntentFilter("notifran"));
        super.onResume();

    }

    protected void onPause() {

        unregisterReceiver(onBroadcast);
        super.onPause();

    }

    // http://stackoverflow.com/questions/3977650/how-can-my-service-change-values-of-variables-and-ui-textfields-of-my-activities
    // thanks Andrew
    private BroadcastReceiver onBroadcast = new BroadcastReceiver() {
        @Override
        public void onReceive(Context ctxt, Intent i) {
            // do stuff to the UI
            refresh_dynamic_content();
        }
    };

    protected void onRestart() {

        refresh_dynamic_content();
        super.onRestart();

    }

    public class TabsPagerAdapter extends FragmentPagerAdapter {

        public TabsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int index) {

            if (simple_mode_current.startsWith("f")) {

                switch (index) {
                case 0:
                    return new LocationFragment();
                case 1:
                    return new SPCFragment();
                case 2:
                    return new MiscFragment();
                case 3:
                    return new ImagesFragment();
                }
            } else {
                switch (index) {
                case 0:
                    return new LocationFragment();

                }
            }

            return null;
        }

        @Override
        public int getCount() {

            if (simple_mode_current.startsWith("f")) {
                return 4;
            } else {
                return 1;
            }

        }

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }

}