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 java.util.Locale; import java.util.regex.Pattern; import joshuatee.wx.R; import android.support.v4.app.FragmentActivity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; public class CommonActionBarFragment extends FragmentActivity { // All activities that need the common action bad extend this activity // Provides access to vis, nexrad, AFD, hourly, rad mosiac, map of statewide alerts, observations // settings, and about String nws1_current; String nws1_state_current; String rid1; String loc1_x_current; String loc1_y_current; String current_loc_fragment; SharedPreferences preferences; String theme_blue_current; Pattern comma; Intent intent; @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.cab, menu); if (theme_blue_current.contains("white")) { MenuItem cloud = menu.findItem(R.id.action_cloud); cloud.setIcon(R.drawable.cloud_dark); MenuItem radar = menu.findItem(R.id.action_radar); radar.setIcon(R.drawable.radar_dark); MenuItem forecast = menu.findItem(R.id.action_forecast); forecast.setIcon(R.drawable.forecast_dark); MenuItem afd = menu.findItem(R.id.action_afd); afd.setIcon(R.drawable.afd_dark); //MenuItem alerts = menu.findItem(R.id.action_alert); //alerts.setIcon(R.drawable.alert_dark); MenuItem mosaic = menu.findItem(R.id.action_radar_mosaic); mosaic.setIcon(R.drawable.map_dark); } comma = Pattern.compile(","); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { current_loc_fragment = preferences.getString("CURRENT_LOC_FRAGMENT", "1"); nws1_current = preferences.getString("NWS" + current_loc_fragment, ""); nws1_state_current = comma.split(preferences.getString("NWS_LOCATION_" + nws1_current, ""))[0]; rid1 = Utility.GetRid(preferences, current_loc_fragment); loc1_x_current = preferences.getString("LOC" + current_loc_fragment + "_X", ""); loc1_y_current = preferences.getString("LOC" + current_loc_fragment + "_Y", ""); switch (item.getItemId()) { case R.id.action_alert: //String onek = preferences.getString("COD_1KM_"+rid1,""); if (Utility.US(loc1_x_current)) { /*Intent i_1km = new Intent(this, CODMosaicKMActivity.class); i_1km.putExtra(CODMosaicKMActivity.URL, new String[] {"1km","rad",onek,nws1_state_current}); startActivity(i_1km);*/ intent = new Intent(this, USWarningsWithRadarActivity.class); intent.putExtra(USWarningsWithRadarActivity.URL, new String[] { ".*?Tornado Warning.*?|.*?Severe Thunderstorm Warning.*?|.*?Flash Flood Warning.*?", "us" }); startActivity(intent); } else if (loc1_x_current.contains("CANADA")) { intent = new Intent(this, CanadaAlertsActivity.class); startActivity(intent); } else if (loc1_x_current.contains("AUSTRALIA")) { String[] provs = loc1_x_current.split(":"); intent = new Intent(this, AustraliaAlertsActivity.class); intent.putExtra(AustraliaAlertsActivity.PROV, new String[] { provs[1], "" }); startActivity(intent); } else if (loc1_x_current.contains("NZ")) { intent = new Intent(this, NZAlertsActivity.class); startActivity(intent); } return true; case R.id.action_observations: if (Utility.US(loc1_x_current)) { intent = new Intent(this, ObservationsActivity.class); intent.putExtra(ObservationsActivity.LOC, nws1_state_current.toLowerCase(Locale.US)); startActivity(intent); } else { intent = new Intent(this, ImageShowActivity.class); intent.putExtra(ImageShowActivity.URL, new String[] { "http://weather.gc.ca/data/wxoimages/wocanmap0_e.jpg", "Observations" }); startActivity(intent); } return true; case R.id.action_cloud: if (Utility.US(loc1_x_current)) { /*Intent i_1km_vis = new Intent(this, CODMosaicKMActivity.class); i_1km_vis.putExtra(CODMosaicKMActivity.URL, new String[] {"1km","vis",onek,nws1_state_current}); startActivity(i_1km_vis);*/ intent = new Intent(this, USNWSGOESActivity.class); intent.putExtra(USNWSGOESActivity.RID, new String[] { "nws", nws1_current.toLowerCase(Locale.US) }); startActivity(intent); } else if (loc1_x_current.contains("CANADA")) { intent = new Intent(this, CanadaRadarActivity.class); intent.putExtra(CanadaRadarActivity.RID, new String[] { rid1, "vis" }); startActivity(intent); } else if (loc1_x_current.contains("AUSTRALIA")) { intent = new Intent(this, AustraliaRadarActivity.class); intent.putExtra(AustraliaRadarActivity.RID, new String[] { rid1, "vis" }); startActivity(intent); } else if (loc1_x_current.contains("NZ")) { intent = new Intent(this, NZRadarActivity.class); intent.putExtra(NZRadarActivity.RID, new String[] { "VIS", "vis" }); startActivity(intent); } return true; case R.id.action_radar: if (Utility.US(loc1_x_current)) { intent = new Intent(this, UtilityUSImg.getRadarClass(preferences)); intent.putExtra(UtilityUSImg.getRadarClassExtra(preferences), new String[] { rid1, nws1_state_current }); startActivity(intent); } else if (loc1_x_current.contains("CANADA")) { intent = new Intent(this, CanadaRadarActivity.class); intent.putExtra(CanadaRadarActivity.RID, new String[] { rid1, "rad" }); startActivity(intent); } else if (loc1_x_current.contains("AUSTRALIA")) { intent = new Intent(this, AustraliaRadarActivity.class); intent.putExtra(AustraliaRadarActivity.RID, new String[] { rid1, "rad" }); startActivity(intent); } else if (loc1_x_current.contains("NZ")) { intent = new Intent(this, NZRadarActivity.class); intent.putExtra(NZRadarActivity.RID, new String[] { rid1, "rad" }); startActivity(intent); } return true; case R.id.action_forecast: if (Utility.US(loc1_x_current)) { intent = new Intent(this, HourlyActivity.class); intent.putExtra(HourlyActivity.LOC_NUM, current_loc_fragment); startActivity(intent); } else if (loc1_x_current.contains("CANADA")) { intent = new Intent(this, CanadaHourlyActivity.class); intent.putExtra(CanadaHourlyActivity.LOC_NUM, current_loc_fragment); startActivity(intent); } else if (loc1_x_current.contains("NZ")) { intent = new Intent(this, NZHourlyActivity.class); startActivity(intent); } return true; case R.id.action_afd: if (Utility.US(loc1_x_current)) { intent = new Intent(this, AFDActivity.class); intent.putExtra(AFDActivity.URL, new String[] { nws1_current, "AFD" }); startActivity(intent); } else if (loc1_x_current.contains("CANADA")) { intent = new Intent(this, CanadaTextActivity.class); startActivity(intent); } else if (loc1_x_current.contains("AUSTRALIA")) { intent = new Intent(this, AustraliaForecastActivity.class); startActivity(intent); } else if (loc1_x_current.contains("NZ")) { intent = new Intent(this, NZForecastActivity.class); startActivity(intent); } return true; case R.id.action_dashboard: intent = new Intent(this, SevereDashboardActivity.class); startActivity(intent); return true; case R.id.action_settings: intent = new Intent(this, SettingsMainActivity.class); startActivity(intent); return true; case R.id.action_radar_mosaic: String prov = ""; if (loc1_x_current.contains("CANADA")) { prov = preferences.getString("NWS" + current_loc_fragment + "_STATE", ""); } if (Utility.US(loc1_x_current)) { intent = new Intent(this, USNWSGOESActivity.class); intent.putExtra(USNWSGOESActivity.RID, new String[] { "nws", nws1_current.toLowerCase(Locale.US), "mosaic" }); startActivity(intent); } else if (loc1_x_current.contains("CANADA")) { intent = new Intent(this, CanadaRadarActivity.class); intent.putExtra(CanadaRadarActivity.RID, new String[] { UtilityCanada.GetECSectorFromProv(prov), "vis" }); startActivity(intent); } else if (loc1_x_current.contains("AUSTRALIA")) { intent = new Intent(this, AustraliaRadarActivity.class); intent.putExtra(AustraliaRadarActivity.RID, new String[] { rid1, "vis" }); startActivity(intent); } else if (loc1_x_current.contains("NZ")) { intent = new Intent(this, NZRadarActivity.class); intent.putExtra(NZRadarActivity.RID, new String[] { "NZ", "rad" }); startActivity(intent); } return true; case R.id.action_about: String vers = ""; try { vers = getPackageManager().getPackageInfo(getPackageName(), 0).versionName; } catch (Exception e) { } String tmp_str = getResources().getString(R.string.about_wx); AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); alertDialogBuilder.setTitle("About wX " + vers); alertDialogBuilder.setMessage(tmp_str).setIcon(R.drawable.wx).setCancelable(false) /*.setPositiveButton("Yes",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { // if this button is clicked, close // current activity MainActivity.this.finish(); } })*/ .setNegativeButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // if this button is clicked, just close // the dialog box and do nothing dialog.cancel(); } }); AlertDialog alertDialog = alertDialogBuilder.create(); alertDialog.show(); return true; default: return super.onOptionsItemSelected(item); } } // end onOptionsItemSelected }