joshuatee.wx.SPCMesoActivity.java Source code

Java tutorial

Introduction

Here is the source code for joshuatee.wx.SPCMesoActivity.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.HashMap;
import java.util.Map;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.util.DisplayMetrics;
import android.util.SparseArray;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ExpandableListView;
import joshuatee.wx.R;

public class SPCMesoActivity extends Activity {

    // native interface to the mobile SPC meso website

    private DrawerLayout mDrawerLayout;
    private ExpandableListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    DisplayMetrics dm;

    String[] model_params;
    String[] model_params_labels;

    String param = "pmsl";
    String sector = "19";
    String sector_str = "US";
    String img_url = "";

    TouchImageView2 img;

    SparseArray<Group> groups = new SparseArray<Group>();
    Map<String, String> sector_map = new HashMap<String, String>();

    SharedPreferences.Editor editor;
    SharedPreferences preferences;

    Bitmap bitmap;
    Drawable[] layers;
    LayerDrawable layerDrawable;
    ColorDrawable cd;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.spcmeso, menu);
        return true;
    }

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

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

        InitSectorMap();

        layers = new Drawable[2];
        cd = new ColorDrawable(Color.WHITE);

        param = preferences.getString("SPCMESO_LAST_USED", param);
        sector = preferences.getString("SPCMESO_SECTOR_LAST_USED", sector);
        setTitle(param + " - " + sector_map.get(sector));

        img = (TouchImageView2) findViewById(R.id.iv);

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

        createData();

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ExpandableListView) findViewById(R.id.left_drawer);
        MyExpandableListAdapter adapter = new MyExpandableListAdapter(this, groups);
        mDrawerList.setAdapter(adapter);
        mDrawerList.setOnChildClickListener(new DrawerItemClickListener2());

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.drawer, R.string.drawer_open,
                R.string.drawer_close) {
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
            }

            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);

            }
        };

        mDrawerLayout.setDrawerListener(mDrawerToggle);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        img_url = "http://www.spc.noaa.gov/exper/mesoanalysis/new/meso_mob.php?sector=" + sector + "&parm=" + param;
        new GetContent().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

    }

    protected void onRestart() {

        new GetContent().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        super.onRestart();

    }

    String[][] short_codes = new String[12][20];

    public void createData() {

        Group group = new Group("Observations");
        group.children.add("Surface Observations");
        group.children.add("Base Reflectivity");
        group.children.add("Visible Satellite");
        groups.append(0, group);

        short_codes[0][0] = "bigsfc";
        short_codes[0][1] = "rgnlrad";
        short_codes[0][2] = "1kmv";

        group = new Group("Basic Surface");
        group.children.add("MSL Pressure/Wind");
        group.children.add("Temp/Dewpt/Wind");
        group.children.add("Moisture Convergence");
        group.children.add("Theta-E Advection");
        group.children.add("Mixing Ratio and Theta");
        group.children.add("2-hr Pressure Change");
        group.children.add("3-hr Temperature Change");
        group.children.add("3-hr Dewpoint Change");
        group.children.add("3-hr 100 mb Mixing Ratio Change");
        group.children.add("3-hr Theta-E Change");
        groups.append(1, group);

        short_codes[1][0] = "pmsl";
        short_codes[1][1] = "ttd";
        short_codes[1][2] = "mcon";
        short_codes[1][3] = "thea";
        short_codes[1][4] = "mxth";
        short_codes[1][5] = "pchg";
        short_codes[1][6] = "temp_chg";
        short_codes[1][7] = "dwpt_chg";
        short_codes[1][8] = "mixr_chg";
        short_codes[1][9] = "thte_chg";

        group = new Group("Basic UA");
        group.children.add("925 mb Analysis");
        group.children.add("850 mb Analysis");
        group.children.add("700 mb Analysis");
        group.children.add("500 mb Analysis");
        group.children.add("300 mb Analysis");
        group.children.add("Deep Moisture Convergence");
        group.children.add("Surface Frontogenesis");
        group.children.add("850 mb Temperature Advection");
        group.children.add("850 mb Frontogenesis");
        group.children.add("700 mb Frontogenesis");
        group.children.add("850-700 mb Frontogenesis");
        group.children.add("700-500 mb Frontogenesis");
        group.children.add("700-400 mb Diff. Vorticity Advection");
        group.children.add("400-250 mb Pot. Vorticity Advection");
        group.children.add("850-250 mb Diff. Divergence");
        group.children.add("300 mb Jet Circulation");
        group.children.add("12-hr 500 mb Hght Change");
        groups.append(2, group);
        short_codes[2][0] = "925mb";
        short_codes[2][1] = "850mb";
        short_codes[2][2] = "700mb";
        short_codes[2][3] = "500mb";
        short_codes[2][4] = "300mb";
        short_codes[2][5] = "dlcp";
        short_codes[2][6] = "sfnt";
        short_codes[2][7] = "tadv";
        short_codes[2][8] = "8fnt";
        short_codes[2][9] = "7fnt";
        short_codes[2][10] = "857f";
        short_codes[2][11] = "75ft";
        short_codes[2][12] = "vadv";
        short_codes[2][13] = "padv";
        short_codes[2][14] = "ddiv";
        short_codes[2][15] = "ageo";
        short_codes[2][16] = "500mb_chg";

        group = new Group("Kinematic Fields");
        group.children.add("Instantaneous Contraction Rate (sfc)");
        group.children.add("Fluid Trapping (sfc)");
        group.children.add("Velocity Tensor Magnitude (sfc)");
        group.children.add("Divergence and Vorticity (sfc)");
        group.children.add("Deformation & Axes of Dilitation (sfc)");
        group.children.add("Fluid Trapping (250 mb)");
        group.children.add("Fluid Trapping (500 mb)");
        groups.append(3, group);
        short_codes[3][0] = "icon";
        short_codes[3][1] = "trap";
        short_codes[3][2] = "vtm";
        short_codes[3][3] = "dvvr";
        short_codes[3][4] = "def";
        short_codes[3][5] = "trap_250";
        short_codes[3][6] = "trap_500";

        group = new Group("Thermodynamic Fields");
        group.children.add("SBCAPE");
        group.children.add("100 mb MLCAPE");
        group.children.add("MUCAPE / LPL Height");
        group.children.add("SB Lifted Index");
        group.children.add("Midlevel Lapse Rates");
        group.children.add("Low-Level Lapse Rates");
        group.children.add("Normalized CAPE");
        group.children.add("Downdraft CAPE");
        group.children.add("LFC Height");
        group.children.add("LCL Height");
        group.children.add("LCL-LFC RH");
        group.children.add("3-hr SBCAPE Change");
        group.children.add("3-hr 100 mb MLCAPE Change");
        group.children.add("3-hr MUCAPE Change");
        group.children.add("3-hr Low-Level LR Change");
        group.children.add("6-hr MidLevel LR Change");
        groups.append(4, group);
        short_codes[4][0] = "sbcp";
        short_codes[4][1] = "mlcp";
        short_codes[4][2] = "mucp";
        short_codes[4][3] = "muli";
        short_codes[4][4] = "laps";
        short_codes[4][5] = "lllr";
        short_codes[4][6] = "ncap";
        short_codes[4][7] = "dcape";
        short_codes[4][8] = "lfch";
        short_codes[4][9] = "lclh";
        short_codes[4][10] = "lfrh";
        short_codes[4][11] = "sbcp_chg";
        short_codes[4][12] = "mlcp_chg";
        short_codes[4][13] = "mucp_chg";
        short_codes[4][14] = "lllr_chg";
        short_codes[4][15] = "laps_chg";

        group = new Group("Wind Shear");
        group.children.add("850-300mb Mean Wind");
        group.children.add("0-6km Shear Vector");
        group.children.add("0-8km Shear Vector");
        group.children.add("Effective Shear");
        group.children.add("BRN Shear");
        group.children.add("0-1km SR Helicity");
        group.children.add("0-3km SR Helecity");
        group.children.add("Effective SR Helicity");
        group.children.add("0-1km Shear Vector");
        group.children.add("0-2km SR Winds");
        group.children.add("4-6km SR Winds");
        group.children.add("9-11km SR Winds");
        group.children.add("Anvil Level SR Winds");
        group.children.add("850 & 500 mb Winds");
        group.children.add("3-hr 0-3km SR Helicity Change");
        group.children.add("3-hr 0-1km Shear Change");
        group.children.add("3-hr 0-6km Shear Change");
        groups.append(5, group);
        short_codes[5][0] = "mnwd";
        short_codes[5][1] = "shr6";
        short_codes[5][2] = "shr8";
        short_codes[5][3] = "eshr";
        short_codes[5][4] = "brns";
        short_codes[5][5] = "srh1";
        short_codes[5][6] = "srh3";
        short_codes[5][7] = "effh";
        short_codes[5][8] = "shr1";
        short_codes[5][9] = "llsr";
        short_codes[5][10] = "mlsr";
        short_codes[5][11] = "ulsr";
        short_codes[5][12] = "alsr";
        short_codes[5][13] = "xover";
        short_codes[5][14] = "srh3_chg";
        short_codes[5][15] = "shr1_chg";
        short_codes[5][16] = "shr6_chg";

        group = new Group("Composite Indices");
        group.children.add("Supercell Composite");
        group.children.add("Supercell Composite (left-moving)");
        group.children.add("Sig. Tornado (fixed layer)");
        group.children.add("Sig. Tornado (effective layer)");
        group.children.add("Cond. Prob. Sigtor (Eqn 1)");
        group.children.add("Cond. Prob. Sigtor (Eqn 2)");
        group.children.add("Sig. Hail Parameter");
        group.children.add("Derecho Composite");
        group.children.add("Craven/Brooks SigSvr");
        group.children.add("3km EHI");
        group.children.add("1km EHI");
        group.children.add("3km VGP");
        group.children.add("Bulk Richardson Number");
        group.children.add("Non-Supercell Tornado");
        group.children.add("MCS Maintenance");
        groups.append(6, group);
        short_codes[6][0] = "scp";
        short_codes[6][1] = "lscp";
        short_codes[6][2] = "stor";
        short_codes[6][3] = "stpc";
        short_codes[6][4] = "sigt1";
        short_codes[6][5] = "sigt2";
        short_codes[6][6] = "sigh";
        short_codes[6][7] = "dcp";
        short_codes[6][8] = "cbsig";
        short_codes[6][9] = "ehi3";
        short_codes[6][10] = "ehi1";
        short_codes[6][11] = "vgp3";
        short_codes[6][12] = "brn";
        short_codes[6][13] = "nstp";
        short_codes[6][14] = "mcsm";

        group = new Group("Multi-Parameter Fields");
        group.children.add("MUCAPE / Eff. Bulk Shear");
        group.children.add("MULI / 850 and 500mb Winds");
        group.children.add("LCL Hght / 0-1km SRH");
        group.children.add("0-3km LR / 0-3km MLCAPE");
        group.children.add("0-3km CAPE / Sfc Vorticity");
        group.children.add("Sfc Dewpoint / 700-500mb LR");
        group.children.add("Hail Parameters");
        groups.append(7, group);
        short_codes[7][0] = "cpsh";
        short_codes[7][1] = "comp";
        short_codes[7][2] = "lcls";
        short_codes[7][3] = "lr3c";
        short_codes[7][4] = "3cvr";
        short_codes[7][5] = "tdlr";
        short_codes[7][6] = "hail";

        group = new Group("Heavy Rainfall");
        group.children.add("Precipitable Water");
        group.children.add("850mb Moisture Transport");
        group.children.add("Upwind Propagation Vector");
        group.children.add("Precipitation Potential Placement");
        groups.append(8, group);
        short_codes[8][0] = "pwtr";
        short_codes[8][1] = "tran";
        short_codes[8][2] = "prop";
        short_codes[8][3] = "peff";

        group = new Group("Winter Weather");
        group.children.add("Near Freezing Sfc Temp.");
        group.children.add("Sfc Wet Bulb Temp");
        group.children.add("Freezing Level Info.");
        group.children.add("Critical Thickness");
        group.children.add("800-750mb EPVg");
        group.children.add("650-500mb EPVg");
        group.children.add("Lake Effect Snow 1");
        group.children.add("Lake Effect Snow 2");
        groups.append(9, group);
        short_codes[9][0] = "fztp";
        short_codes[9][1] = "swbt";
        short_codes[9][2] = "fzlv";
        short_codes[9][3] = "thck";
        short_codes[9][4] = "epvl";
        short_codes[9][5] = "epvm";
        short_codes[9][6] = "les1";
        short_codes[9][7] = "les2";

        group = new Group("Fire Weather");
        group.children.add("Sfc RH, Temp., Wind");
        group.children.add("Fosberg Index");
        group.children.add("Low Altitude Haines Index");
        group.children.add("Mid Altitude Haines Index");
        group.children.add("High Altitude Haines Index");
        group.children.add("Lower Atmos. Severity Index");
        groups.append(10, group);
        short_codes[10][0] = "sfir";
        short_codes[10][1] = "fosb";
        short_codes[10][2] = "lhan";
        short_codes[10][3] = "mhan";
        short_codes[10][4] = "hhan";
        short_codes[10][5] = "lasi";

    }

    private class DrawerItemClickListener2 implements ExpandableListView.OnChildClickListener {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition,
                long id) {

            mDrawerLayout.closeDrawer(mDrawerList);
            param = short_codes[groupPosition][childPosition];
            editor.putString("SPCMESO_LAST_USED", param);
            editor.commit();

            new GetContent().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            return true;
        }

    }

    private class GetContent extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... params) {

            img_url = "http://www.spc.noaa.gov/exper/mesoanalysis/s" + sector + "/" + param + "/" + param + ".gif";
            bitmap = Utility.getBitmapFromURL(img_url);
            layers[0] = cd;
            layers[1] = new BitmapDrawable(getResources(), bitmap);
            layerDrawable = new LayerDrawable(layers);

            return "Executed";
        }

        @Override
        protected void onPostExecute(String result) {

            UtilityImg.ResizeViewSetLD(dm, layerDrawable, img);
            img.setMaxZoom(4f);

            setProgressBarIndeterminateVisibility(false);

        }

        @Override
        protected void onPreExecute() {
            setTitle(param + " - " + sector_map.get(sector));
            setProgressBarIndeterminateVisibility(true);
        }

        @Override
        protected void onProgressUpdate(String... values) {
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        switch (item.getItemId()) {
        case R.id.action_mslp:
            param = "pmsl";
            editor.putString("SPCMESO_LAST_USED", param);
            editor.commit();
            new GetContent().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            return true;
        case R.id.action_500mb:
            param = "500mb";
            editor.putString("SPCMESO_LAST_USED", param);
            editor.commit();
            new GetContent().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            return true;
        case R.id.action_scp:
            param = "scp";
            editor.putString("SPCMESO_LAST_USED", param);
            editor.commit();
            new GetContent().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            return true;
        case R.id.action_eshr:
            param = "eshr";
            editor.putString("SPCMESO_LAST_USED", param);
            editor.commit();
            new GetContent().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            return true;
        case R.id.action_mucp:
            param = "mucp";
            editor.putString("SPCMESO_LAST_USED", param);
            editor.commit();
            new GetContent().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            return true;
        case R.id.action_US:
            img.resetZoom();
            sector = "19";
            editor.putString("SPCMESO_SECTOR_LAST_USED", sector);
            editor.commit();
            sector_str = "US";
            setTitle(param + " - US");
            new GetContent().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            return true;
        case R.id.action_MW:
            img.resetZoom();
            sector = "20";
            editor.putString("SPCMESO_SECTOR_LAST_USED", sector);
            editor.commit();
            sector_str = "MW";
            setTitle(param + " - MW");
            new GetContent().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            return true;
        case R.id.action_NC:
            img.resetZoom();
            sector = "13";
            editor.putString("SPCMESO_SECTOR_LAST_USED", sector);
            editor.commit();
            sector_str = "NC";
            setTitle(param + " - NC");
            new GetContent().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            return true;
        case R.id.action_C:
            img.resetZoom();
            sector = "14";
            editor.putString("SPCMESO_SECTOR_LAST_USED", sector);
            editor.commit();
            sector_str = "C";
            setTitle(param + " - C");
            new GetContent().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            return true;
        case R.id.action_SC:
            img.resetZoom();
            sector = "15";
            editor.putString("SPCMESO_SECTOR_LAST_USED", sector);
            editor.commit();
            sector_str = "SC";
            setTitle(param + " - SC");
            new GetContent().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            return true;
        case R.id.action_NE:
            img.resetZoom();
            sector = "16";
            editor.putString("SPCMESO_SECTOR_LAST_USED", sector);
            editor.commit();
            sector_str = "NE";
            setTitle(param + " - NE");
            new GetContent().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            return true;
        case R.id.action_CE:
            img.resetZoom();
            sector = "17";
            editor.putString("SPCMESO_SECTOR_LAST_USED", sector);
            editor.commit();
            sector_str = "CE";
            setTitle(param + " - CE");
            new GetContent().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            return true;
        case R.id.action_SE:
            img.resetZoom();
            sector = "18";
            editor.putString("SPCMESO_SECTOR_LAST_USED", sector);
            editor.commit();
            sector_str = "SE";
            setTitle(param + " - SE");
            new GetContent().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            return true;
        case R.id.action_SW:
            img.resetZoom();
            sector = "12";
            editor.putString("SPCMESO_SECTOR_LAST_USED", sector);
            editor.commit();
            sector_str = "SW";
            setTitle(param + " - SW");
            new GetContent().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            return true;
        case R.id.action_NW:
            img.resetZoom();
            sector = "11";
            editor.putString("SPCMESO_SECTOR_LAST_USED", sector);
            editor.commit();
            sector_str = "NW";
            setTitle(param + " - NW");
            new GetContent().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            return true;

        default:
            return super.onOptionsItemSelected(item);
        }
    } // end onOptionsItemSelected

    public void InitSectorMap() {

        sector_map.put("19", "US");
        sector_map.put("20", "MW");
        sector_map.put("13", "NC");
        sector_map.put("14", "C");
        sector_map.put("15", "SC");
        sector_map.put("16", "NE");
        sector_map.put("17", "CE");
        sector_map.put("18", "SE");
        sector_map.put("12", "SW");
        sector_map.put("11", "NW");

    }

    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    @Override

    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

}