joshuatee.wx.MiscFragment.java Source code

Java tutorial

Introduction

Here is the source code for joshuatee.wx.MiscFragment.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 java.util.Locale;
import joshuatee.wx.R;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.ActionBar.LayoutParams;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.DisplayMetrics;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.support.v4.app.Fragment;

public class MiscFragment extends Fragment implements OnItemSelectedListener, OnClickListener {

    // Provides fragment for Tab 3 showing misc resources ( NWS mosaics, model, warnings, WPC text prod, twitter )

    String state_code_current = "";
    String twitter_state_id = "";

    View view;

    private Spinner spinner1;
    private Spinner spinner2;

    final String[] fmap_label = new String[] { "Forecast maps", "current", "12hr", "24hr", "36hr", "48hr", "3day",
            "4day", "5day", "6day" };
    final String[] fmap_codes = new String[] { "", "", "92fwbg", "94fwbg", "96fwbg", "98fwbg", "9jhwbg_conus",
            "9khwbg_conus", "9lhwbg_conus", "9mhwbg_conus" };

    final String[] qpf_urls = new String[] { "http://www.hpc.ncep.noaa.gov/qpf/fill_94qwbg.gif",
            "http://www.hpc.ncep.noaa.gov/qpf/fill_98qwbg.gif", "http://www.hpc.ncep.noaa.gov/qpf/fill_99qwbg.gif",
            "http://www.hpc.ncep.noaa.gov/qpf/95ep48iwbg_fill.gif",
            "http://www.hpc.ncep.noaa.gov/qpf/97ep48iwbg_fill.gif", };

    final String[] qpf_label = new String[] { "Quantitative Precipitation Forecasts", "Day 1", "Day 2", "Day 3",
            "Day 4-5", "Day 6-7" };

    SharedPreferences preferences;

    String nws1_current = "";

    Intent intent;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_misc, container, false);

        state_code_current = preferences.getString("STATE_CODE", "");
        twitter_state_id = preferences.getString("STATE_TW_ID_" + state_code_current, "");

        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);

        ImageView iv_1 = (ImageView) view.findViewById(R.id.iv_1);
        iv_1.setOnClickListener(this);
        ImageView iv_2 = (ImageView) view.findViewById(R.id.iv_2);
        iv_2.setOnClickListener(this);
        ImageView iv_3 = (ImageView) view.findViewById(R.id.iv_3);
        iv_3.setOnClickListener(this);
        ImageView iv_4 = (ImageView) view.findViewById(R.id.iv_4);
        iv_4.setOnClickListener(this);
        ImageView iv_5 = (ImageView) view.findViewById(R.id.iv_5);
        iv_5.setOnClickListener(this);
        ImageView iv_6 = (ImageView) view.findViewById(R.id.iv_6);
        iv_6.setOnClickListener(this);
        ImageView iv_7 = (ImageView) view.findViewById(R.id.iv_7);
        iv_7.setOnClickListener(this);
        ImageView iv_8 = (ImageView) view.findViewById(R.id.iv_8);
        iv_8.setOnClickListener(this);
        ImageView iv_9 = (ImageView) view.findViewById(R.id.iv_9);
        iv_9.setOnClickListener(this);
        ImageView iv_10 = (ImageView) view.findViewById(R.id.iv_10);
        iv_10.setOnClickListener(this);
        ImageView iv_11 = (ImageView) view.findViewById(R.id.iv_11);
        iv_11.setOnClickListener(this);
        ImageView iv_12 = (ImageView) view.findViewById(R.id.iv_12);
        iv_12.setOnClickListener(this);

        LayoutParams params_iv = null;
        UtilityImg.ResizeViewSetImgTile(dm, UtilityImg.LoadBM(getActivity(), R.drawable.models), iv_1, 3,
                params_iv);
        UtilityImg.ResizeViewSetImgTile(dm, UtilityImg.LoadBM(getActivity(), R.drawable.srfd), iv_2, 3, params_iv);
        UtilityImg.ResizeViewSetImgTile(dm, UtilityImg.LoadBM(getActivity(), R.drawable.nexrad_attr), iv_3, 3,
                params_iv);
        UtilityImg.ResizeViewSetImgTile(dm, UtilityImg.LoadBM(getActivity(), R.drawable.warn), iv_4, 3, params_iv);
        UtilityImg.ResizeViewSetImgTile(dm, UtilityImg.LoadBM(getActivity(), R.drawable.warn_cod), iv_5, 3,
                params_iv);
        UtilityImg.ResizeViewSetImgTile(dm, UtilityImg.LoadBM(getActivity(), R.drawable.nhc), iv_6, 3, params_iv);
        UtilityImg.ResizeViewSetImgTile(dm, UtilityImg.LoadBM(getActivity(), R.drawable.nws_sector), iv_7, 3,
                params_iv);
        UtilityImg.ResizeViewSetImgTile(dm, UtilityImg.LoadBM(getActivity(), R.drawable.goes), iv_8, 3, params_iv);
        UtilityImg.ResizeViewSetImgTile(dm, UtilityImg.LoadBM(getActivity(), R.drawable.lightning), iv_9, 3,
                params_iv);
        UtilityImg.ResizeViewSetImgTile(dm, UtilityImg.LoadBM(getActivity(), R.drawable.twstate), iv_10, 3,
                params_iv);
        UtilityImg.ResizeViewSetImgTile(dm, UtilityImg.LoadBM(getActivity(), R.drawable.twwu), iv_11, 3, params_iv);
        UtilityImg.ResizeViewSetImgTile(dm, UtilityImg.LoadBM(getActivity(), R.drawable.twtornado), iv_12, 3,
                params_iv);

        registerForContextMenu(iv_1);
        registerForContextMenu(iv_7);
        registerForContextMenu(iv_8);
        registerForContextMenu(iv_9);
        registerForContextMenu(iv_10);

        spinner1 = (Spinner) view.findViewById(R.id.spinner1);

        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_spinner_item, fmap_label);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(dataAdapter);
        spinner1.setOnItemSelectedListener(this);

        spinner2 = (Spinner) view.findViewById(R.id.spinner2);

        ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_spinner_item, qpf_label);
        dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(dataAdapter2);
        spinner2.setOnItemSelectedListener(this);

        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
        nws1_current = preferences.getString("NWS" + preferences.getString("CURRENT_LOC_FRAGMENT", "1"), "");

    }

    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

        switch (parent.getId()) {
        case R.id.spinner2:
            if (spinner2.getSelectedItemPosition() != 0) {
                intent = new Intent(getActivity(), ImageShowActivity.class);
                intent.putExtra(ImageShowActivity.URL,
                        new String[] { qpf_urls[spinner2.getSelectedItemPosition() - 1],
                                qpf_label[spinner2.getSelectedItemPosition()] + " QPF" });
                startActivity(intent);
            }
            spinner2.setSelection(0);
            break;
        case R.id.spinner1:
            int spinner1_pos = spinner1.getSelectedItemPosition();
            String type = "basicwx";
            if (spinner1_pos > 5) {
                type = "medr";
            }

            //medr
            if (spinner1_pos == 1) {
                intent = new Intent(getActivity(), ImageShowActivity.class);
                intent.putExtra(ImageShowActivity.URL, new String[] { "http://www.hpc.ncep.noaa.gov/noaa/noaa.gif",
                        fmap_label[spinner1_pos] + " Forecast" });
                startActivity(intent);
            } else if (spinner1_pos != 0) {
                intent = new Intent(getActivity(), ImageShowActivity.class);
                intent.putExtra(ImageShowActivity.URL,
                        new String[] {
                                "http://www.hpc.ncep.noaa.gov/" + type + "/"
                                        + fmap_codes[spinner1.getSelectedItemPosition()] + ".gif",
                                fmap_label[spinner1_pos] + " Forecast" });
                startActivity(intent);
            }
            spinner1.setSelection(0);
            break;

        }

    }

    public void onNothingSelected(AdapterView<?> parent) {
    }

    public void onResume() {

        state_code_current = preferences.getString("STATE_CODE", "");
        twitter_state_id = preferences.getString("STATE_TW_ID_" + state_code_current, "");
        super.onResume();

    }

    @Override
    public void onClick(View v2) {

        String nws_radar_mosaic_sector_current = preferences.getString("NWS_RADAR_MOSAIC_SECTOR_CURRENT",
                "centgrtlakes");

        switch (v2.getId()) {

        case R.id.iv_4:
            intent = new Intent(getActivity(), USWarningsWithRadarActivity.class);
            intent.putExtra(USWarningsWithRadarActivity.URL, new String[] {
                    ".*?Tornado Warning.*?|.*?Severe Thunderstorm Warning.*?|.*?Flash Flood Warning.*?", "us" });
            startActivity(intent);
            break;

        case R.id.iv_7:
            intent = new Intent(getActivity(), USNWSMosaicActivity.class);
            intent.putExtra(USNWSMosaicActivity.URL, new String[] { "", nws_radar_mosaic_sector_current });
            startActivity(intent);
            break;

        case R.id.iv_8:
            intent = new Intent(getActivity(), USNWSGOESActivity.class);
            intent.putExtra(USNWSGOESActivity.RID,
                    new String[] { "nws", nws1_current.toLowerCase(Locale.US), "wv" }); //  was { "nws",""}
            startActivity(intent);

            break;

        case R.id.iv_9:
            String web_url = "http://radarimg.weatherbug.com/images/lightning/LxEnlarge.jpg";
            intent = new Intent(getActivity(), ImageShowActivity.class);
            intent.putExtra(ImageShowActivity.URL, new String[] { web_url, "US Lightning strikes" });
            startActivity(intent);
            break;

        case R.id.iv_1:
            intent = new Intent(getActivity(), ModelInterfaceActivity.class);
            startActivity(intent);
            break;
        case R.id.iv_2:
            intent = new Intent(getActivity(), WPCTextProductsActivity.class);
            intent.putExtra(WPCTextProductsActivity.URL,
                    new String[] { "pmdspd", "Short Range Forecast Discussion" });
            startActivity(intent);
            break;
        case R.id.iv_3:
            intent = new Intent(getActivity(), NEXRADAttributesActivity.class);
            startActivity(intent);
            break;

        case R.id.iv_5:
            intent = new Intent(getActivity(), TextScreenActivity.class);
            intent.putExtra(TextScreenActivity.URL,
                    new String[] { "http://kamala.cod.edu/svr/", "Recent Severe Weather Warnings" });
            startActivity(intent);
            break;

        case R.id.iv_6:
            intent = new Intent(getActivity(), NHCActivity.class);
            startActivity(intent);
            break;

        case R.id.iv_10:
            intent = new Intent(getActivity(), WebscreenAB.class);
            intent.putExtra(WebscreenAB.URL, new String[] {
                    "<a class=\"twitter-timeline\" data-dnt=\"true\" href=\"https://twitter.com/search?q=%23"
                            + state_code_current.toLowerCase(Locale.US) + "wx\" data-widget-id=\""
                            + twitter_state_id
                            + "\" data-chrome=\"noscrollbar noheader nofooter noborders  \" data-tweet-limit=20>Tweets about \"#"
                            + state_code_current.toLowerCase(Locale.US)
                            + "wx\"</a><script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+\"://platform.twitter.com/widgets.js\";fjs.parentNode.insertBefore(js,fjs);}}(document,\"script\",\"twitter-wjs\");</script>",
                    "twitter: " + state_code_current.toLowerCase(Locale.US) });
            startActivity(intent);
            break;
        case R.id.iv_11:
            intent = new Intent(getActivity(), WebscreenAB.class);
            intent.putExtra(WebscreenAB.URL, new String[] {
                    "<a class=\"twitter-timeline\" data-dnt=\"true\" href=\"https://twitter.com/wunderground\" data-widget-id=\"406096371456802816\" data-chrome=\"noscrollbar noheader nofooter noborders \" data-tweet-limit=20>Tweets by @wunderground</a><script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+\"://platform.twitter.com/widgets.js\";fjs.parentNode.insertBefore(js,fjs);}}(document,\"script\",\"twitter-wjs\");</script>",
                    "@wunderground" });
            startActivity(intent);
            break;
        case R.id.iv_12:
            intent = new Intent(getActivity(), WebscreenAB.class);
            intent.putExtra(WebscreenAB.URL, new String[] {
                    "<html><meta name=\"viewport\" content=\"width=device-width, user-scalable=no\" /> <body width=\"100%\"><div><script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+\"://platform.twitter.com/widgets.js\";fjs.parentNode.insertBefore(js,fjs);}}(document,\"script\",\"twitter-wjs\");</script><html><a class=\"twitter-timeline\" data-dnt=\"true\" href=\"https://twitter.com/search?q=%23tornado\" data-widget-id=\"406096257220763648\" data-chrome=\"noscrollbar noheader nofooter noborders \" data-tweet-limit=20>Tweets about \"#tornado\"</a></div></body></html>",
                    "#tornado" });
            startActivity(intent);
            break;

        }
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {

        switch (v.getId()) {
        case R.id.iv_10: // STATE
            String state_cur = preferences.getString("STATE_CODE", "");
            menu.add(0, v.getId(), 0, "Edit State (" + state_cur + ")");
            break;
        case R.id.iv_9: // conus mosaic
            menu.setHeaderTitle("Beta area");
            menu.add(0, v.getId(), 0, "Iowa Mesonet hires conus radar");
            menu.add(0, v.getId(), 0, "SPC Compmap");

            break;
        case R.id.iv_8: // conus mosaic
            menu.setHeaderTitle("Set default sector");
            menu.add(0, v.getId(), 0, "alaska");
            menu.add(0, v.getId(), 0, "hawaii");
            menu.add(0, v.getId(), 0, "pacsouthwest");
            menu.add(0, v.getId(), 0, "pacnorthwest");
            menu.add(0, v.getId(), 0, "southrockies");
            menu.add(0, v.getId(), 0, "northrockies");
            menu.add(0, v.getId(), 0, "uppermissvly");
            menu.add(0, v.getId(), 0, "southplains");
            menu.add(0, v.getId(), 0, "centgrtlakes");
            menu.add(0, v.getId(), 0, "southmissvly");
            menu.add(0, v.getId(), 0, "southeast");
            menu.add(0, v.getId(), 0, "northeast");
            menu.add(0, v.getId(), 0, "conus");
            break;
        case R.id.iv_7: // conus mosaic
            menu.setHeaderTitle("Set default sector");
            menu.add(0, v.getId(), 0, "alaska");
            menu.add(0, v.getId(), 0, "hawaii");
            menu.add(0, v.getId(), 0, "pacsouthwest");
            menu.add(0, v.getId(), 0, "pacnorthwest");
            menu.add(0, v.getId(), 0, "southrockies");
            menu.add(0, v.getId(), 0, "northrockies");
            menu.add(0, v.getId(), 0, "uppermissvly");
            menu.add(0, v.getId(), 0, "southplains");
            menu.add(0, v.getId(), 0, "centgrtlakes");
            menu.add(0, v.getId(), 0, "southmissvly");
            menu.add(0, v.getId(), 0, "southeast");
            menu.add(0, v.getId(), 0, "northeast");
            menu.add(0, v.getId(), 0, "conus");
            break;
        case R.id.iv_1:
            menu.add(0, v.getId(), 0, "COD Model Web");
            break;

        }

    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {

        if (getUserVisibleHint()) {
            String web_url = "";
            if (((String) item.getTitle()).contains("Edit State")) {
                StateEdit(item.getItemId());
            } else if (((String) item.getTitle()).contains("Iowa Mesonet hires conus radar")) {
                web_url = "http://mesonet.agron.iastate.edu/data/gis/images/4326/USCOMP/n0q_0.png";
                intent = new Intent(getActivity(), USIOWAMesoRadarMosaicActivity.class);
                intent.putExtra(USIOWAMesoRadarMosaicActivity.URL, new String[] { web_url, "US Mosaics" });
                startActivity(intent);
            } else if (((String) item.getTitle()).contains("SPC Compmap")) {
                intent = new Intent(getActivity(), SPCCompmapActivity.class);
                startActivity(intent);
            } else if (((String) item.getTitle()).contains("COD Model Web")) {
                intent = new Intent(getActivity(), WebscreenAB.class);
                intent.putExtra(WebscreenAB.URL,
                        new String[] { "http://weather.cod.edu/forecast", "Cod Model Web" });
                startActivity(intent);
            } else {
                SharedPreferences.Editor editor = preferences.edit();
                editor.putString("NWS_RADAR_MOSAIC_SECTOR_CURRENT", (String) item.getTitle());
                editor.commit();
            }
            return true;
        } else {
            return false;
        }

    }

    public void StateEdit(int id) {

        if (id == R.id.iv_10) {
            intent = new Intent(getActivity(), SettingsStateListViewActivity.class);
            startActivity(intent);

        }

    }

}