1 package com.weather.information.activity;
3 import android.app.ActionBar;
4 import android.app.ActionBar.Tab;
5 import android.app.FragmentTransaction;
6 import android.content.ComponentName;
7 import android.content.Intent;
8 import android.content.SharedPreferences;
9 import android.os.Bundle;
10 import android.preference.PreferenceManager;
11 import android.support.v4.app.Fragment;
12 import android.support.v4.app.FragmentActivity;
13 import android.support.v4.app.FragmentManager;
14 import android.support.v4.app.FragmentPagerAdapter;
15 import android.support.v4.view.ViewPager;
16 import android.view.Menu;
17 import android.view.MenuItem;
19 import com.weather.information.R;
20 import com.weather.information.fragment.current.CurrentFragment;
21 import com.weather.information.fragment.overview.OverviewFragment;
22 import com.weather.information.model.DatabaseQueries;
23 import com.weather.information.model.WeatherLocation;
25 import java.text.MessageFormat;
26 import java.util.Locale;
29 public class WeatherTabsActivity extends FragmentActivity {
30 private static final int NUM_ITEMS = 2;
31 private ViewPager mPager;
34 protected void onCreate(final Bundle savedInstanceState) {
35 super.onCreate(savedInstanceState);
36 this.setContentView(R.layout.fragment_pager);
38 this.mPager = (ViewPager)this.findViewById(R.id.pager);
39 this.mPager.setAdapter(new TabsAdapter(this.getSupportFragmentManager()));
42 this.mPager.setOnPageChangeListener(
43 new ViewPager.SimpleOnPageChangeListener() {
45 public void onPageSelected(final int position) {
46 WeatherTabsActivity.this.getActionBar().setSelectedNavigationItem(position);
51 final ActionBar actionBar = this.getActionBar();
53 PreferenceManager.setDefaultValues(this, R.xml.weather_preferences, false);
55 // Specify that tabs should be displayed in the action bar.
56 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
57 actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_SHOW_TITLE);
58 actionBar.setDisplayHomeAsUpEnabled(true);
60 // Create a tab listener that is called when the user changes tabs.
61 final ActionBar.TabListener tabListener = new ActionBar.TabListener() {
64 public void onTabSelected(final Tab tab, final FragmentTransaction ft) {
65 WeatherTabsActivity.this.mPager.setCurrentItem(tab.getPosition());
70 public void onTabUnselected(final Tab tab, final FragmentTransaction ft) {
75 public void onTabReselected(final Tab tab, final FragmentTransaction ft) {
81 actionBar.addTab(actionBar.newTab().setText("CURRENTLY").setTabListener(tabListener));
82 actionBar.addTab(actionBar.newTab().setText("FORECAST").setTabListener(tabListener));
86 public boolean onCreateOptionsMenu(final Menu menu) {
88 this.getMenuInflater().inflate(R.menu.weather_main_menu, menu);
90 return super.onCreateOptionsMenu(menu);
94 public boolean onOptionsItemSelected(final MenuItem item) {
95 // Handle action bar item clicks here. The action bar will
96 // automatically handle clicks on the Home/Up button, so long
97 // as you specify a parent activity in AndroidManifest.xml.
98 super.onOptionsItemSelected(item);
101 final int itemId = item.getItemId();
102 if (itemId == R.id.weather_menu_settings) {
103 intent = new Intent("com.weather.information.WEATHERINFO")
104 .setComponent(new ComponentName("com.weather.information",
105 "com.weather.information.activity.WeatherInformationPreferencesActivity"));
106 this.startActivity(intent);
108 } else if (itemId == R.id.weather_menu_map) {
109 intent = new Intent("com.weather.information.WEATHERINFO").
110 setComponent(new ComponentName("com.weather.information",
111 "com.weather.information.activity.MapActivity"));
112 this.startActivity(intent);
114 } else if (itemId == R.id.weather_menu_about) {
115 intent = new Intent("com.weather.information.WEATHERINFO").
116 setComponent(new ComponentName("com.weather.information",
117 "com.weather.information.activity.AboutActivity"));
118 this.startActivity(intent);
123 // TODO: calling again super method?
124 return super.onOptionsItemSelected(item);
128 protected void onRestoreInstanceState(final Bundle savedInstanceState) {
129 super.onRestoreInstanceState(savedInstanceState);
134 public void onResume() {
137 final ActionBar actionBar = this.getActionBar();
140 final DatabaseQueries query = new DatabaseQueries(this.getApplicationContext());
141 final WeatherLocation weatherLocation = query.queryDataBase();
142 if (weatherLocation != null) {
143 final String[] array = new String[2];
144 array[0] = weatherLocation.getCity();
145 array[1] = weatherLocation.getCountry();
146 final MessageFormat message = new MessageFormat("{0},{1}", Locale.US);
147 final String cityCountry = message.format(array);
148 actionBar.setTitle(cityCountry);
150 actionBar.setTitle(this.getString(R.string.text_field_no_chosen_location));
153 // 2. Update forecast tab text.
154 final SharedPreferences sharedPreferences = PreferenceManager
155 .getDefaultSharedPreferences(this);
156 final String keyPreference = this.getString(R.string.weather_preferences_day_forecast_key);
157 final String value = sharedPreferences.getString(keyPreference, "");
158 String humanValue = "";
159 if (value.equals("5")) {
160 humanValue = "5 DAY FORECAST";
161 } else if (value.equals("10")) {
162 humanValue = "10 DAY FORECAST";
163 } else if (value.equals("14")) {
164 humanValue = "14 DAY FORECAST";
166 actionBar.getTabAt(1).setText(humanValue);
170 public void onSaveInstanceState(final Bundle savedInstanceState) {
171 super.onSaveInstanceState(savedInstanceState);
174 private class TabsAdapter extends FragmentPagerAdapter {
175 public TabsAdapter(final FragmentManager fm) {
180 public int getCount() {
185 public Fragment getItem(final int position) {
187 return new CurrentFragment();
189 return new OverviewFragment();