2 * Copyright 2014 Gustavo Martin Morcuende
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package name.gumartinm.weather.information.activity;
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;
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;
40 import java.text.MessageFormat;
41 import java.util.Locale;
44 public class MainTabsActivity extends FragmentActivity {
45 private static final int NUM_ITEMS = 2;
46 private ViewPager mPager;
49 protected void onCreate(final Bundle savedInstanceState) {
50 super.onCreate(savedInstanceState);
51 this.setContentView(R.layout.fragment_pager);
53 this.mPager = (ViewPager)this.findViewById(R.id.pager);
54 this.mPager.setAdapter(new TabsAdapter(this.getSupportFragmentManager()));
57 this.mPager.setOnPageChangeListener(
58 new ViewPager.SimpleOnPageChangeListener() {
60 public void onPageSelected(final int position) {
61 MainTabsActivity.this.getActionBar().setSelectedNavigationItem(position);
66 final ActionBar actionBar = this.getActionBar();
68 PreferenceManager.setDefaultValues(this, R.xml.weather_preferences, false);
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);
75 // Create a tab listener that is called when the user changes tabs.
76 final ActionBar.TabListener tabListener = new ActionBar.TabListener() {
79 public void onTabSelected(final Tab tab, final FragmentTransaction ft) {
80 MainTabsActivity.this.mPager.setCurrentItem(tab.getPosition());
85 public void onTabUnselected(final Tab tab, final FragmentTransaction ft) {
90 public void onTabReselected(final Tab tab, final FragmentTransaction ft) {
96 actionBar.addTab(actionBar.newTab().setText("CURRENTLY").setTabListener(tabListener));
97 actionBar.addTab(actionBar.newTab().setText("FORECAST").setTabListener(tabListener));
101 public boolean onCreateOptionsMenu(final Menu menu) {
103 this.getMenuInflater().inflate(R.menu.weather_main_menu, menu);
105 return super.onCreateOptionsMenu(menu);
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);
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);
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);
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);
138 // TODO: calling again super method?
139 return super.onOptionsItemSelected(item);
143 protected void onRestoreInstanceState(final Bundle savedInstanceState) {
144 super.onRestoreInstanceState(savedInstanceState);
149 public void onResume() {
152 final ActionBar actionBar = this.getActionBar();
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);
165 actionBar.setTitle(this.getString(R.string.text_field_no_chosen_location));
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);
181 actionBar.getTabAt(1).setText(humanValue);
185 public void onSaveInstanceState(final Bundle savedInstanceState) {
186 super.onSaveInstanceState(savedInstanceState);
189 private class TabsAdapter extends FragmentPagerAdapter {
190 public TabsAdapter(final FragmentManager fm) {
195 public int getCount() {
200 public Fragment getItem(final int position) {
202 return new CurrentFragment();
204 return new OverviewFragment();