joshuatee.wx.SPCCompmapActivity.java Source code

Java tutorial

Introduction

Here is the source code for joshuatee.wx.SPCCompmapActivity.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.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
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.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SPCCompmapActivity extends CommonActionBarFragment {

    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    String layer_str = "";

    private ActionBarDrawerToggle mDrawerToggle;

    final String[] model_params_labels = new String[] { "IR SATL", "SLP (mb)", "TEMP (F)", "DEWPT (F)", "10m BKNT",
            "CAPE/CINH", "HLCY/SHEAR", "PRES TEND", "MCONV", "KIND/PWAI", "12H PCPN", "SFC OBS MAP", "LAPSE RATES",
            "850 WAA/WIND", "700 INFO", "700-500 MRH", "500 HGHT/ABSV", "700-500 UVV", "300 WINDS",
            "DAY 1 Outlook (94O)", "HPC Fronts (90F)", "HPC 6-hr QPF (92E)", };

    final String[] url_index = new String[] { "16", "7", "1", "0", "8", "2", "21", "3", "4", "5", "6", "9", "10",
            "11", "12", "13", "14", "15", "17", "18", "19", "20", };

    DisplayMetrics dm;
    String img_url = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        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.activity_spccompmap);

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

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);
        mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, model_params_labels));
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        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/compmap/basemap.gif";
        new GetContent().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }

    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }
    }

    private void selectItem(int position) {

        mDrawerList.setItemChecked(position, false);
        mDrawerLayout.closeDrawer(mDrawerList);

        String position_str = url_index[position];

        if (model_params_labels[position].contains("(on)")) {
            model_params_labels[position] = model_params_labels[position].replaceAll("\\(on\\) ", "");
            layer_str = layer_str.replaceAll("a" + position_str + ":", "");
            new GetContent().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        } else {
            model_params_labels[position] = "(on) " + model_params_labels[position];
            layer_str = layer_str + "a" + position_str + ":";
            new GetContent().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        }

    }

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

        Bitmap bitmap;
        Drawable[] layers;
        LayerDrawable layerDrawable;
        ColorDrawable cd = new ColorDrawable(Color.WHITE);

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

            bitmap = Utility.getBitmapFromURL(img_url);

            String[] layer_str_arr = layer_str.split(":");

            layers = new Drawable[layer_str_arr.length + 2];
            layers[0] = cd;
            layers[1] = new BitmapDrawable(getResources(), bitmap);

            int j = 0;
            for (j = 0; j < layer_str_arr.length; j++) {
                layer_str_arr[j] = layer_str_arr[j].replaceAll("a", "");
                bitmap = Utility
                        .getBitmapFromURL("http://www.spc.noaa.gov/exper/compmap/" + layer_str_arr[j] + ".gif");
                bitmap = UtilityImg.eraseBG(bitmap, -16777216);
                layers[j + 2] = new BitmapDrawable(getResources(), bitmap);
            }

            layerDrawable = new LayerDrawable(layers);

            return "Executed";
        }

        @Override
        protected void onPostExecute(String result) {

            TouchImageView2 img = (TouchImageView2) findViewById(R.id.iv);
            int width = dm.widthPixels;
            int height = width * layerDrawable.getIntrinsicHeight() / layerDrawable.getIntrinsicWidth();
            LayoutParams params_iv = (LayoutParams) img.getLayoutParams();
            params_iv.height = height;
            params_iv.width = width;
            img.setLayoutParams(params_iv);
            setProgressBarIndeterminateVisibility(false);
            img.setImageDrawable(layerDrawable);
            img.setMaxZoom(4f);

        }

        @Override
        protected void onPreExecute() {
            setProgressBarIndeterminateVisibility(true);
        }

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

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

    @Override

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

    public boolean onOptionsItemSelected(MenuItem item) {

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

        return super.onOptionsItemSelected(item);

    }

}