afe06c5dfce54787827103cb0e54a172accc007b
[JavaForFun] /
1 /**
2  * Copyright 2014 Gustavo Martin Morcuende
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package name.gumartinm.weather.information.activity;
17
18 import android.app.ActionBar;
19 import android.app.ActionBar.Tab;
20 import android.app.FragmentTransaction;
21 import android.content.ComponentName;
22 import android.content.Intent;
23 import android.content.SharedPreferences;
24 import android.os.Bundle;
25 import android.preference.PreferenceManager;
26 import android.support.v4.app.Fragment;
27 import android.support.v4.app.FragmentActivity;
28 import android.support.v4.app.FragmentManager;
29 import android.support.v4.app.FragmentPagerAdapter;
30 import android.support.v4.view.ViewPager;
31 import android.view.Menu;
32 import android.view.MenuItem;
33
34 import name.gumartinm.weather.information.R;
35 import name.gumartinm.weather.information.fragment.current.CurrentFragment;
36 import name.gumartinm.weather.information.fragment.overview.OverviewFragment;
37 import name.gumartinm.weather.information.model.DatabaseQueries;
38 import name.gumartinm.weather.information.model.WeatherLocation;
39
40 import java.text.MessageFormat;
41 import java.util.Locale;
42
43
44 public class MainTabsActivity extends FragmentActivity {
45     private static final int NUM_ITEMS = 2;
46     private ViewPager mPager;
47     
48     @Override
49     protected void onCreate(final Bundle savedInstanceState) {
50         super.onCreate(savedInstanceState);
51         this.setContentView(R.layout.fragment_pager);
52
53         this.mPager = (ViewPager)this.findViewById(R.id.pager);
54         this.mPager.setAdapter(new TabsAdapter(this.getSupportFragmentManager()));
55
56
57         this.mPager.setOnPageChangeListener(
58                 new ViewPager.SimpleOnPageChangeListener() {
59                     @Override
60                     public void onPageSelected(final int position) {
61                         MainTabsActivity.this.getActionBar().setSelectedNavigationItem(position);
62                     }
63                 });
64
65
66         final ActionBar actionBar = this.getActionBar();
67
68         PreferenceManager.setDefaultValues(this, R.xml.weather_preferences, false);
69
70         // Specify that tabs should be displayed in the action bar.
71         actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
72         actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_SHOW_TITLE);
73         actionBar.setDisplayHomeAsUpEnabled(true);
74
75         // Create a tab listener that is called when the user changes tabs.
76         final ActionBar.TabListener tabListener = new ActionBar.TabListener() {
77
78             @Override
79             public void onTabSelected(final Tab tab, final FragmentTransaction ft) {
80                 MainTabsActivity.this.mPager.setCurrentItem(tab.getPosition());
81
82             }
83
84             @Override
85             public void onTabUnselected(final Tab tab, final FragmentTransaction ft) {
86
87             }
88
89             @Override
90             public void onTabReselected(final Tab tab, final FragmentTransaction ft) {
91
92             }
93
94         };
95
96         actionBar.addTab(actionBar.newTab().setText("CURRENTLY").setTabListener(tabListener));
97         actionBar.addTab(actionBar.newTab().setText("FORECAST").setTabListener(tabListener));
98     }
99
100     @Override
101     public boolean onCreateOptionsMenu(final Menu menu) {
102
103         this.getMenuInflater().inflate(R.menu.weather_main_menu, menu);
104
105         return super.onCreateOptionsMenu(menu);
106     }
107
108     @Override
109     public boolean onOptionsItemSelected(final MenuItem item) {
110         // Handle action bar item clicks here. The action bar will
111         // automatically handle clicks on the Home/Up button, so long
112         // as you specify a parent activity in AndroidManifest.xml.
113         super.onOptionsItemSelected(item);
114
115         Intent intent;
116         final int itemId = item.getItemId();
117         if (itemId == R.id.weather_menu_settings) {
118             intent = new Intent("name.gumartinm.weather.information.WEATHERINFO")
119             .setComponent(new ComponentName("name.gumartinm.weather.information",
120                     "name.gumartinm.weather.information.activity.PreferencesActivity"));
121             this.startActivity(intent);
122             return true;
123         } else if (itemId == R.id.weather_menu_map) {
124             intent = new Intent("name.gumartinm.weather.information.WEATHERINFO").
125                         setComponent(new ComponentName("name.gumartinm.weather.information",
126                                         "name.gumartinm.weather.information.activity.MapActivity"));
127             this.startActivity(intent);
128             return true;
129         } else if (itemId == R.id.weather_menu_about) {
130             intent = new Intent("name.gumartinm.weather.information.WEATHERINFO").
131                     setComponent(new ComponentName("name.gumartinm.weather.information",
132                             "name.gumartinm.weather.information.activity.AboutActivity"));
133             this.startActivity(intent);
134             return true;
135         } else {
136         }
137
138         // TODO: calling again super method?
139         return super.onOptionsItemSelected(item);
140     }
141
142     @Override
143     protected void onRestoreInstanceState(final Bundle savedInstanceState) {
144         super.onRestoreInstanceState(savedInstanceState);
145     }
146
147
148     @Override
149     public void onResume() {
150         super.onResume();
151
152         final ActionBar actionBar = this.getActionBar();
153         
154         // 1. Update title.
155         final DatabaseQueries query = new DatabaseQueries(this.getApplicationContext());
156         final WeatherLocation weatherLocation = query.queryDataBase();
157         if (weatherLocation != null) {
158             final String[] array = new String[2];
159             array[0] = weatherLocation.getCity();
160             array[1] = weatherLocation.getCountry();
161             final MessageFormat message = new MessageFormat("{0},{1}", Locale.US);
162             final String cityCountry = message.format(array);
163             actionBar.setTitle(cityCountry);
164         } else {
165                 actionBar.setTitle(this.getString(R.string.text_field_no_chosen_location));
166         }
167
168         // 2. Update forecast tab text.
169         final SharedPreferences sharedPreferences = PreferenceManager
170                 .getDefaultSharedPreferences(this.getApplicationContext());
171         final String keyPreference = this.getString(R.string.weather_preferences_day_forecast_key);
172         final String value = sharedPreferences.getString(keyPreference, "");
173         String humanValue = "";
174         if (value.equals("5")) {
175             humanValue = this.getString(R.string.text_tab_five_days_forecast);
176         } else if (value.equals("10")) {
177             humanValue = this.getString(R.string.text_tab_ten_days_forecast);
178         } else if (value.equals("14")) {
179             humanValue = this.getString(R.string.text_tab_fourteen_days_forecast);
180         }
181         actionBar.getTabAt(1).setText(humanValue);
182     }
183
184     @Override
185     public void onSaveInstanceState(final Bundle savedInstanceState) {
186         super.onSaveInstanceState(savedInstanceState);
187     }
188
189     private class TabsAdapter extends FragmentPagerAdapter {
190         public TabsAdapter(final FragmentManager fm) {
191             super(fm);
192         }
193
194         @Override
195         public int getCount() {
196             return NUM_ITEMS;
197         }
198
199         @Override
200         public Fragment getItem(final int position) {
201             if (position == 0) {
202                 return new CurrentFragment();
203             } else {
204                 return new OverviewFragment();
205             }
206
207         }
208     }
209 }