Java tutorial
/* 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.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; import android.graphics.Bitmap; import android.graphics.drawable.AnimationDrawable; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import java.sql.Date; import java.util.ArrayList; import java.util.Calendar; import java.util.List; import java.util.Locale; import java.util.TimeZone; import java.util.regex.Matcher; import java.util.regex.Pattern; import joshuatee.wx.R; import android.support.v4.app.ActionBarDrawerToggle; import android.support.v4.widget.DrawerLayout; import android.util.DisplayMetrics; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.Window; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.ListView; import android.widget.Spinner; import android.widget.TextView; public class ModelInterfaceActivity extends Activity implements OnItemSelectedListener, OnClickListener { // This code provides a native android interface to Weather Models @ COD private Spinner spinner1, spinner2, spinner_sector; private String[] mLocations; private DrawerLayout mDrawerLayout; private ActionBarDrawerToggle mDrawerToggle; private ListView mDrawerList; DisplayMetrics dm; Bitmap bitmap; int i = 0; List<String> list_run = new ArrayList<String>(); List<String> list_time = new ArrayList<String>(); List<String> list_sector = new ArrayList<String>(); boolean spinner1_ran = false; boolean spinner2_ran = false; boolean spinner_sector_ran = false; AnimationDrawable animDrawable; String[] model_params; String[] model_params_labels; int good_height = 600; int good_width = 800; String run = "00Z"; String time = "00"; String param = "sfc_prec"; String model = "GFS"; String sector = "US"; String img_url = ""; int time_tmp; int spinnerPosition; String run_str; TouchImageView2 img; SharedPreferences preferences3; SharedPreferences.Editor editor; TextView tv_time; TextView tv_status; ImageView iv; TextView button_for; LayoutParams params_iv; int width, height; String sig_html_tmp = ""; String url = ""; Pattern p; Matcher m; int z = 0; String tmp_str = ""; @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.model_interface, menu); return true; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); preferences3 = PreferenceManager.getDefaultSharedPreferences(this); editor = preferences3.edit(); String theme_blue_current = preferences3.getString("THEME_BLUE", ""); setTheme(Utility.Theme(theme_blue_current)); setContentView(R.layout.activity_model_interface); tv_time = (TextView) findViewById(R.id.tv_time); tv_status = (TextView) findViewById(R.id.tv_status); iv = (ImageView) findViewById(R.id.iv2); button_for = (TextView) findViewById(R.id.tv); img = (TouchImageView2) findViewById(R.id.iv); model = preferences3.getString("MODEL_LAST_USED", "GFS"); setTitle(model); dm = new DisplayMetrics(); this.getWindowManager().getDefaultDisplay().getMetrics(dm); int width = dm.widthPixels; int height = width * good_height / good_width; LayoutParams params_iv = (LayoutParams) img.getLayoutParams(); params_iv.height = height; params_iv.width = width; img.setLayoutParams(params_iv); addItemsOnSpinner1(); addItemsOnSpinner2(); addItemsOnSpinner3(); addListenerOnButton(); addListenerOnSpinnerItemSelection(); ImageButton button_back = (ImageButton) findViewById(R.id.button_back); button_back.setOnClickListener(this); ImageButton button_forward = (ImageButton) findViewById(R.id.button_forward); button_forward.setOnClickListener(this); ImageButton button_animate = (ImageButton) findViewById(R.id.button_animate); button_animate.setOnClickListener(this); if (theme_blue_current.contains("white")) { button_back.setImageResource(R.drawable.previous_dark); button_forward.setImageResource(R.drawable.next_dark); button_animate.setImageResource(R.drawable.refresh_dark); } model_params_labels = UtilityModelInterface.model_gfs_params_labels; model_params = UtilityModelInterface.model_gfs_params; mLocations = model_params_labels; 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, mLocations)); 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); if (model.equals("GFS")) { SetupGFS("00Z"); } else if (model.equals("NAM")) { SetupNAM(); } else if (model.equals("RAP")) { SetupRAP(); } else if (model.equals("NAM4KM")) { SetupNam4km(); } else if (model.equals("HRRR")) { SetupHrrr(); } else if (model.equals("ECMWF")) { SetupEcmwf(); } else if (model.equals("UKMET")) { //SetupUkmet(); } else if (model.equals("SREF")) { SetupSref(); } new GetContent().execute(); new GetRunStatus().execute(); } @Override public void onClick(View v2) { switch (v2.getId()) { case R.id.button_back: time_tmp = spinner2.getSelectedItemPosition(); time_tmp--; if (time_tmp == -1) { time_tmp = list_time.size() - 1; } spinner2.setSelection(time_tmp); new GetContent().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); break; case R.id.button_forward: time_tmp = spinner2.getSelectedItemPosition(); time_tmp++; if (time_tmp == list_time.size()) { time_tmp = 0; } spinner2.setSelection(time_tmp); new GetContent().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); break; case R.id.button_animate: new GetAnimate().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); break; case R.id.tv: Intent i_rid = new Intent(this, CODSoundingRidSelectActivity.class); startActivity(i_rid); break; case R.id.iv: String cod_souding_nws = preferences3.getString("COD_SOUNDING_NWS", "DTX"); String nws_x = preferences3.getString("NWS_" + cod_souding_nws + "_X", ""); String nws_y = preferences3.getString("NWS_" + cod_souding_nws + "_Y", ""); Intent i_iv = new Intent(this, Webscreen.class); i_iv.putExtra(Webscreen.URL, "http://climate.cod.edu/flanis/model/fsound/index.php?type=" + run.replace("Z", "") + "|" + model + "|US|500|spd|" + time + "|" + nws_x + "," + nws_y + "|false"); startActivity(i_iv); break; } } public void addItemsOnSpinner2() { spinner2 = (Spinner) findViewById(R.id.spinner2); list_time = new ArrayList<String>(); for (i = 0; i <= 84; i = i + 3) { list_time.add(String.format("%03d", i)); } for (i = 90; i <= 180; i = i + 6) { list_time.add(String.format("%03d", i)); } for (i = 192; i <= 240; i = i + 12) { list_time.add(String.format("%03d", i)); } ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list_time); dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner2.setAdapter(dataAdapter); } public void addItemsOnSpinner1() { spinner1 = (Spinner) findViewById(R.id.spinner1); list_run = new ArrayList<String>(); list_run.add("00Z"); list_run.add("06Z"); list_run.add("12Z"); list_run.add("18Z"); ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list_run); dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner1.setAdapter(dataAdapter); } @SuppressWarnings("unchecked") public void addItemsOnSpinner3() { spinner_sector = (Spinner) findViewById(R.id.spinner_sector); String[] list_sector_arr = new String[] { "US", "CGP", "SGP", "SE", "NE", "SW", "NW" }; ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list_sector_arr); dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner_sector.setAdapter(dataAdapter); sector = preferences3.getString("MODEL_SECTOR_LAST_USED", "US"); ArrayAdapter<String> myAdap = (ArrayAdapter<String>) spinner_sector.getAdapter(); //cast to an ArrayAdapter spinnerPosition = myAdap.getPosition(sector); spinner_sector.setSelection(spinnerPosition); } @SuppressWarnings("unchecked") public void addItemsOnSpinner3NAM4KM() { spinner_sector = (Spinner) findViewById(R.id.spinner_sector); list_sector = new ArrayList<String>(); list_sector.add("US"); list_sector.add("SGP"); list_sector.add("CGP"); ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list_sector); dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner_sector.setAdapter(dataAdapter); sector = preferences3.getString("MODEL_SECTOR_LAST_USED", "SE"); ArrayAdapter<String> myAdap = (ArrayAdapter<String>) spinner_sector.getAdapter(); //cast to an ArrayAdapter spinnerPosition = myAdap.getPosition(sector); spinner_sector.setSelection(spinnerPosition); } public void addItemsOnSpinner3HRRR() { spinner_sector = (Spinner) findViewById(R.id.spinner_sector); list_sector = new ArrayList<String>(); list_sector.add("MW"); list_sector.add("CGPsf"); list_sector.add("SGP"); list_sector.add("SL"); list_sector.add("GC"); list_sector.add("CP"); list_sector.add("SE"); list_sector.add("NEsf"); list_sector.add("SW"); list_sector.add("NW"); ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list_sector); dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner_sector.setAdapter(dataAdapter); sector = preferences3.getString("MODEL_SECTOR_LAST_USED", "SE"); @SuppressWarnings("unchecked") ArrayAdapter<String> myAdap = (ArrayAdapter<String>) spinner_sector.getAdapter(); //cast to an ArrayAdapter spinnerPosition = myAdap.getPosition(sector); spinner_sector.setSelection(spinnerPosition); } @SuppressWarnings("unchecked") public void addItemsOnSpinner3Sref() { spinner_sector = (Spinner) findViewById(R.id.spinner_sector); list_sector = new ArrayList<String>(); list_sector.add("US"); list_sector.add("CGP"); list_sector.add("SGP"); list_sector.add("SE"); list_sector.add("NE"); list_sector.add("SW"); list_sector.add("CAN"); ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list_sector); dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner_sector.setAdapter(dataAdapter); sector = preferences3.getString("MODEL_SECTOR_LAST_USED", "US"); ArrayAdapter<String> myAdap = (ArrayAdapter<String>) spinner_sector.getAdapter(); //cast to an ArrayAdapter spinnerPosition = myAdap.getPosition(sector); spinner_sector.setSelection(spinnerPosition); } public void addListenerOnSpinnerItemSelection() { spinner1 = (Spinner) findViewById(R.id.spinner1); } public void addListenerOnButton() { spinner1 = (Spinner) findViewById(R.id.spinner1); spinner2 = (Spinner) findViewById(R.id.spinner2); spinner_sector = (Spinner) findViewById(R.id.spinner_sector); spinner1.setOnItemSelectedListener(this); spinner2.setOnItemSelectedListener(this); spinner_sector.setOnItemSelectedListener(this); } public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { if (spinner1_ran && spinner2_ran && spinner_sector_ran) { new GetContent().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } else { switch (parent.getId()) { case R.id.spinner1: if (!spinner1_ran) { spinner1_ran = true; } break; case R.id.spinner2: if (!spinner2_ran) { spinner2_ran = true; } break; case R.id.spinner_sector: if (!spinner_sector_ran) { spinner_sector_ran = true; } break; } } } public void onNothingSelected(AdapterView<?> parent) { } 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); if (model.equals("GFS")) { param = UtilityModelInterface.model_gfs_params[position]; } else if (model.equals("RAP")) { param = UtilityModelInterface.model_rap_params[position]; } else if (model.equals("NAM")) { param = UtilityModelInterface.model_nam_params[position]; } else if (model.equals("ECMWF")) { param = UtilityModelInterface.model_ecmwf_params[position]; } else if (model.equals("UKMET")) { //param = UtilityModelInterface.model_ukmet_params[position]; } else if (model.equals("HRRR")) { param = UtilityModelInterface.model_hrrr_params[position]; } else if (model.equals("NAM4KM")) { param = UtilityModelInterface.model_nam4km_params[position]; } else if (model.equals("SREF")) { param = UtilityModelInterface.model_sref_params[position]; } editor.putString("MODEL_PARAM_LAST_USED", param); editor.commit(); new GetContent().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } private class GetContent extends AsyncTask<String, String, String> { @Override protected String doInBackground(String... params) { run = String.valueOf(spinner1.getSelectedItem()); time = String.valueOf(spinner2.getSelectedItem()); time = UtilityStringExternal.truncate(time, 3); sector = String.valueOf(spinner_sector.getSelectedItem()); editor.putString("MODEL_SECTOR_LAST_USED", sector); editor.commit(); img_url = "http://climate.cod.edu/data/forecast/" + model + "/" + run.replace("Z", "") + "/" + sector + "/" + model.toLowerCase(Locale.US) + "" + sector + "_" + param + "_" + time + ".gif"; bitmap = Utility.getBitmapFromURL(img_url); return "Executed"; } @Override protected void onPostExecute(String result) { run_str = run.replace("Z", ""); tv_time.setText(ConvertTimeRuntoTimeString(run_str, time)); iv.setBackground(null); iv.setVisibility(View.GONE); img.setVisibility(View.VISIBLE); if (bitmap.getHeight() > 200 && bitmap.getWidth() > 200) { good_height = bitmap.getHeight(); good_width = bitmap.getWidth(); tv_status.setText(""); } else { tv_status.setText("Invalid parameter or time."); } width = dm.widthPixels; height = width * good_height / good_width; params_iv = (LayoutParams) img.getLayoutParams(); params_iv.height = height; params_iv.width = width; img.setLayoutParams(params_iv); img.setImageBitmap(bitmap); img.setMaxZoom(4f); setProgressBarIndeterminateVisibility(false); } @Override protected void onPreExecute() { setProgressBarIndeterminateVisibility(true); setTitle(model + " " + param); } @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_gfs: model = "GFS"; editor.putString("MODEL_LAST_USED", model); editor.commit(); SetupGFS("00Z"); new GetContent().execute(); new GetRunStatus().execute(); return true; case R.id.action_nam: model = "NAM"; editor.putString("MODEL_LAST_USED", model); editor.commit(); SetupNAM(); new GetContent().execute(); new GetRunStatus().execute(); return true; case R.id.action_rap: model = "RAP"; editor.putString("MODEL_LAST_USED", model); editor.commit(); SetupRAP(); new GetContent().execute(); new GetRunStatus().execute(); return true; case R.id.action_ecmwf: model = "ECMWF"; editor.putString("MODEL_LAST_USED", model); editor.commit(); SetupEcmwf(); new GetContent().execute(); new GetRunStatus().execute(); return true; case R.id.action_hrrr: model = "HRRR"; editor.putString("MODEL_LAST_USED", model); editor.commit(); SetupHrrr(); new GetContent().execute(); new GetRunStatus().execute(); return true; /*case R.id.action_nam4km: model = "NAM4KM"; editor.putString("MODEL_LAST_USED", model); editor.commit(); SetupNam4km(); new GetContent().execute(); new GetRunStatus().execute(); return true;*/ case R.id.action_sref: model = "SREF"; editor.putString("MODEL_LAST_USED", model); editor.commit(); SetupSref(); new GetContent().execute(); new GetRunStatus().execute(); return true; default: return super.onOptionsItemSelected(item); } } // end onOptionsItemSelected private class GetAnimate extends AsyncTask<String, String, String> { Bitmap[] bm_arr; int frame_cnt = 0; @Override protected String doInBackground(String... params) { int k = 0; frame_cnt = list_time.size() - spinner2.getSelectedItemPosition(); bm_arr = new Bitmap[frame_cnt]; int bm_ix = 0; String timem = ""; String[] time_arr; for (k = spinner2.getSelectedItemPosition(); k < list_time.size(); k++) { timem = String.valueOf(spinner2.getItemAtPosition(k)); time_arr = timem.split(" "); img_url = "http://climate.cod.edu/data/forecast/" + model + "/" + run.replace("Z", "") + "/" + sector + "/" + model.toLowerCase(Locale.US) + "" + sector + "_" + param + "_" + time_arr[0] + ".gif"; bm_arr[bm_ix] = Utility.getBitmapFromURL(img_url); bm_ix++; } return "Executed"; } @Override protected void onPostExecute(String result) { animDrawable = new AnimationDrawable(); int delay = UtilityImg.GetAnimInterval(preferences3) * 2; //default is 250 Drawable[] fr_arr = new Drawable[frame_cnt]; int a = 0; for (a = 0; a < frame_cnt; a++) { fr_arr[a] = new BitmapDrawable(getResources(), bm_arr[a]); animDrawable.addFrame(fr_arr[a], delay); } //TouchImageView2 img = (TouchImageView2) findViewById(R.id.iv); img.setBackground(null); img.setVisibility(View.GONE); //ImageView iv = (ImageView) findViewById(R.id.iv2); iv.setVisibility(View.VISIBLE); width = dm.widthPixels; height = width * animDrawable.getIntrinsicHeight() / animDrawable.getIntrinsicWidth(); params_iv = (LayoutParams) iv.getLayoutParams(); params_iv.height = height; params_iv.width = width; iv.setLayoutParams(params_iv); iv.setBackground(animDrawable); setProgressBarIndeterminateVisibility(false); animDrawable.start(); animDrawable.setOneShot(false); } @Override protected void onPreExecute() { setProgressBarIndeterminateVisibility(true); } @Override protected void onProgressUpdate(String... values) { } } private class GetRunStatus extends AsyncTask<String, String, String> { @Override protected String doInBackground(String... params) { url = "http://weather.cod.edu/forecast/menus/" + model.toLowerCase(Locale.US) + "_menu.php?type=current-US-null-null-0-0%27,%27prods/" + model.toLowerCase(Locale.US) + "_prods.php%27"; if (model.equals("RAP")) { url = url.replace("rap", "ruc"); } try { sig_html_tmp = Utility.GetStringFromURL(url); if (!model.equals("HRRR")) { p = Pattern.compile("([0-9]{2}Z\\s*: Current Run<div id=.percentage.>[0-9]{1,3}%)"); } else { p = Pattern.compile("_hrrr. target=._self.>([0-9]{2}Z)\\*"); } // >06Z : Current Run<div id="percentage">100%</div> m = p.matcher(sig_html_tmp); while (m.find()) { sig_html_tmp = m.group(1); } } catch (Exception e) { } return "Executed"; } @Override protected void onPostExecute(String result) { try { p = Pattern.compile("([0-9]{2}Z)"); m = p.matcher(sig_html_tmp); while (m.find()) { time = m.group(1); } } catch (Exception e) { } @SuppressWarnings("unchecked") ArrayAdapter<String> myAdap = (ArrayAdapter<String>) spinner1.getAdapter(); //cast to an ArrayAdapter spinnerPosition = myAdap.getPosition(time); spinner1.setSelection(spinnerPosition); sig_html_tmp = sig_html_tmp.replaceAll("<div id=.percentage.>", " "); sig_html_tmp = sig_html_tmp.replace("Current Run", " "); button_for.setText(sig_html_tmp); // time , loop over list time and parse # for (z = 0; z < list_time.size(); z++) { tmp_str = list_time.get(z); list_time.set(z, tmp_str + " " + ConvertTimeRuntoTimeString(time.replace("Z", ""), tmp_str)); } } @Override protected void onPreExecute() { TextView button_for = (TextView) findViewById(R.id.tv); button_for.setText(""); } @Override protected void onProgressUpdate(String... values) { } } public void SetupGFS(String init_time) { setTitle(model); param = "sfc_prec"; param = preferences3.getString("MODEL_PARAM_LAST_USED", param); addItemsOnSpinner3(); model_params_labels = UtilityModelInterface.model_gfs_params_labels; model_params = UtilityModelInterface.model_gfs_params; mLocations = model_params_labels; mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mLocations)); spinner1.setSelection(0); spinner2.setSelection(0); list_run.clear(); list_run.add("00Z"); list_run.add("06Z"); list_run.add("12Z"); list_run.add("18Z"); list_time.clear(); for (i = 0; i <= 84; i = i + 3) { list_time.add(String.format("%03d", i)); // + " " + ConvertTimeRuntoTimeString(init_time.replaceAll("Z",""), Integer.toString(i)) } for (i = 90; i <= 180; i = i + 6) { list_time.add(String.format("%03d", i)); } for (i = 192; i <= 240; i = i + 12) { list_time.add(String.format("%03d", i)); } } public void SetupNAM() { setTitle(model); param = "sfc_prec"; param = preferences3.getString("MODEL_PARAM_LAST_USED", param); addItemsOnSpinner3(); model_params_labels = UtilityModelInterface.model_nam_params_labels; model_params = UtilityModelInterface.model_nam_params; mLocations = model_params_labels; mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mLocations)); spinner1.setSelection(0); spinner2.setSelection(0); list_run.clear(); list_run.add("00Z"); list_run.add("06Z"); list_run.add("12Z"); list_run.add("18Z"); list_time.clear(); for (i = 0; i <= 84; i = i + 3) { list_time.add(String.format("%03d", i)); } } public void SetupRAP() { setTitle(model); param = "sfc_prec"; param = preferences3.getString("MODEL_PARAM_LAST_USED", param); addItemsOnSpinner3(); model_params_labels = UtilityModelInterface.model_rap_params_labels; model_params = UtilityModelInterface.model_rap_params; mLocations = model_params_labels; mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mLocations)); spinner1.setSelection(0); spinner2.setSelection(0); list_run.clear(); for (i = 0; i <= 22; i = i + 3) { list_run.add(String.format("%02d", i) + "Z"); } list_time.clear(); for (i = 0; i <= 18; i++) { list_time.add(String.format("%03d", i)); } } public void SetupEcmwf() { setTitle(model); param = "850_temp"; param = preferences3.getString("MODEL_PARAM_LAST_USED", param); addItemsOnSpinner3(); model_params_labels = UtilityModelInterface.model_ecmwf_params_labels; model_params = UtilityModelInterface.model_ecmwf_params_labels; mLocations = model_params_labels; mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mLocations)); spinner1.setSelection(0); spinner2.setSelection(0); list_run.clear(); list_run.add("00Z"); list_run.add("12Z"); list_time.clear(); for (i = 0; i <= 168; i = i + 24) { list_time.add(String.format("%03d", i)); } } /*public void SetupUkmet() { setTitle(model); param = "sfc_prec"; param = preferences3.getString("MODEL_PARAM_LAST_USED",param); addItemsOnSpinner3(); model_params_labels = model_ukmet_params_labels; model_params = model_ukmet_params; mLocations = model_params_labels; mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mLocations)); spinner1.setSelection(0); spinner2.setSelection(0); list_run.clear(); list_run.add("00Z"); list_run.add("12Z"); list_time.clear(); for ( i = 0; i <= 72; i = i + 6) { list_time.add(String.format("%03d", i)); } }*/ public void SetupHrrr() { setTitle(model); param = "sfc_radar"; param = preferences3.getString("MODEL_PARAM_LAST_USED", param); addItemsOnSpinner3HRRR(); model_params_labels = UtilityModelInterface.model_hrrr_params_labels; model_params = UtilityModelInterface.model_hrrr_params; mLocations = model_params_labels; mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mLocations)); spinner1.setSelection(0); spinner2.setSelection(0); list_run.clear(); for (i = 0; i <= 23; i++) { list_run.add(String.format("%02d", i) + "Z"); } list_time.clear(); for (i = 0; i <= 15; i++) { list_time.add(String.format("%03d", i)); } } public void SetupNam4km() { setTitle(model); param = "sfc_radar"; param = preferences3.getString("MODEL_PARAM_LAST_USED", param); addItemsOnSpinner3NAM4KM(); model_params_labels = UtilityModelInterface.model_nam4km_params_labels; model_params = UtilityModelInterface.model_nam4km_params; mLocations = model_params_labels; mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mLocations)); spinner1.setSelection(0); spinner2.setSelection(0); list_run.clear(); list_run.add("00Z"); list_run.add("12Z"); list_time.clear(); for (i = 0; i <= 36; i++) { list_time.add(String.format("%03d", i)); } } public void SetupSref() { setTitle(model); param = "con_cape500"; param = preferences3.getString("MODEL_PARAM_LAST_USED", param); addItemsOnSpinner3Sref(); model_params_labels = UtilityModelInterface.model_sref_params_labels; model_params = UtilityModelInterface.model_sref_params; mLocations = model_params_labels; mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mLocations)); spinner1.setSelection(0); spinner2.setSelection(0); list_run.clear(); list_run.add("03Z"); list_run.add("09Z"); list_run.add("15Z"); list_run.add("21Z"); list_time.clear(); for (i = 0; i <= 87; i = i + 3) { list_time.add(String.format("%03d", i)); } } protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); mDrawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mDrawerToggle.onConfigurationChanged(newConfig); } private String ConvertTimeRuntoTimeString(String run_str, String time_str) { int run_int = Integer.parseInt(run_str); int time_int = Integer.parseInt(time_str); int real_time_gmt = run_int + time_int; TimeZone tz = TimeZone.getDefault(); Date now = new Date(System.currentTimeMillis()); int offsetFromUtc = tz.getOffset(now.getTime()) / 1000; int real_time = real_time_gmt + offsetFromUtc / 60 / 60; int hour_of_day = real_time % 24; String am_pm = ""; if (hour_of_day > 11) { am_pm = "pm"; if (hour_of_day > 12) { hour_of_day = hour_of_day - 12; } } else { am_pm = "am"; } int day = (int) real_time / 24; if (hour_of_day < 0) { hour_of_day = 12 + hour_of_day; am_pm = "pm"; day--; } Calendar calendar = Calendar.getInstance(); int day_of_week = calendar.get(Calendar.DAY_OF_WEEK); int hour_of_day_local = calendar.get(Calendar.HOUR_OF_DAY); if (run_int >= 0 && run_int < -offsetFromUtc / 60 / 60 && (hour_of_day_local - offsetFromUtc / 60 / 60) >= 24) { day++; } String future_day = ""; switch ((day_of_week + day) % 7) { case Calendar.SUNDAY: future_day = "Sun"; break; case Calendar.MONDAY: future_day = "Mon"; break; case Calendar.TUESDAY: future_day = "Tue"; break; case Calendar.WEDNESDAY: future_day = "Wed"; break; case Calendar.THURSDAY: future_day = "Thu"; break; case Calendar.FRIDAY: future_day = "Fri"; break; case 0: future_day = "Sat"; break; } //return future_day + " "+hour_of_day.toString()+ am_pm; return future_day + " " + Integer.toString(hour_of_day) + am_pm; } }