d6edf7d69a25b1d2242cacd65b6e9fae04a36929
[JavaForFun] /
1 package com.weather.information.activity;
2
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;
18
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;
24
25 import java.text.MessageFormat;
26 import java.util.Locale;
27
28
29 public class WeatherTabsActivity extends FragmentActivity {
30     private static final int NUM_ITEMS = 2;
31     private ViewPager mPager;
32     
33     @Override
34     protected void onCreate(final Bundle savedInstanceState) {
35         super.onCreate(savedInstanceState);
36         this.setContentView(R.layout.fragment_pager);
37
38         this.mPager = (ViewPager)this.findViewById(R.id.pager);
39         this.mPager.setAdapter(new TabsAdapter(this.getSupportFragmentManager()));
40
41
42         this.mPager.setOnPageChangeListener(
43                 new ViewPager.SimpleOnPageChangeListener() {
44                     @Override
45                     public void onPageSelected(final int position) {
46                         WeatherTabsActivity.this.getActionBar().setSelectedNavigationItem(position);
47                     }
48                 });
49
50
51         final ActionBar actionBar = this.getActionBar();
52
53         PreferenceManager.setDefaultValues(this, R.xml.weather_preferences, false);
54
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);
59
60         // Create a tab listener that is called when the user changes tabs.
61         final ActionBar.TabListener tabListener = new ActionBar.TabListener() {
62
63             @Override
64             public void onTabSelected(final Tab tab, final FragmentTransaction ft) {
65                 WeatherTabsActivity.this.mPager.setCurrentItem(tab.getPosition());
66
67             }
68
69             @Override
70             public void onTabUnselected(final Tab tab, final FragmentTransaction ft) {
71
72             }
73
74             @Override
75             public void onTabReselected(final Tab tab, final FragmentTransaction ft) {
76
77             }
78
79         };
80
81         actionBar.addTab(actionBar.newTab().setText("CURRENTLY").setTabListener(tabListener));
82         actionBar.addTab(actionBar.newTab().setText("FORECAST").setTabListener(tabListener));
83     }
84
85     @Override
86     public boolean onCreateOptionsMenu(final Menu menu) {
87
88         this.getMenuInflater().inflate(R.menu.weather_main_menu, menu);
89
90         return super.onCreateOptionsMenu(menu);
91     }
92
93     @Override
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);
99
100         Intent intent;
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);
107             return true;
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);
113             return true;
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);
119             return true;
120         } else {
121         }
122
123         // TODO: calling again super method?
124         return super.onOptionsItemSelected(item);
125     }
126
127     @Override
128     protected void onRestoreInstanceState(final Bundle savedInstanceState) {
129         super.onRestoreInstanceState(savedInstanceState);
130     }
131
132
133     @Override
134     public void onResume() {
135         super.onResume();
136
137         final ActionBar actionBar = this.getActionBar();
138         
139         // 1. Update title.
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);
149         } else {
150                 actionBar.setTitle(this.getString(R.string.text_field_no_chosen_location));
151         }
152
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";
165         }
166         actionBar.getTabAt(1).setText(humanValue);
167     }
168
169     @Override
170     public void onSaveInstanceState(final Bundle savedInstanceState) {
171         super.onSaveInstanceState(savedInstanceState);
172     }
173
174     private class TabsAdapter extends FragmentPagerAdapter {
175         public TabsAdapter(final FragmentManager fm) {
176             super(fm);
177         }
178
179         @Override
180         public int getCount() {
181             return NUM_ITEMS;
182         }
183
184         @Override
185         public Fragment getItem(final int position) {
186             if (position == 0) {
187                 return new CurrentFragment();
188             } else {
189                 return new OverviewFragment();
190             }
191
192         }
193     }
194 }