9dafe147f712cb1f6b5e9af4fca26e5ee6070486
[Sunshine.git] / app / src / main / java / uk / me / njae / sunshine / ForecastFragment.java
1 package uk.me.njae.sunshine;
2
3 import android.content.Intent;
4 import android.content.SharedPreferences;
5 import android.net.Uri;
6 import android.os.Bundle;
7 import android.preference.PreferenceManager;
8 import android.support.v4.app.Fragment;
9 import android.util.Log;
10 import android.view.LayoutInflater;
11 import android.view.Menu;
12 import android.view.MenuInflater;
13 import android.view.MenuItem;
14 import android.view.View;
15 import android.view.ViewGroup;
16 import android.widget.AdapterView;
17 import android.widget.ArrayAdapter;
18 import android.widget.ListView;
19
20 import java.io.UnsupportedEncodingException;
21 import java.net.URLEncoder;
22 import java.util.ArrayList;
23
24 /**
25 * A placeholder fragment containing a simple view.
26 */
27 public class ForecastFragment extends Fragment {
28
29 private final String LOG_TAG = ForecastFragment.class.getSimpleName();
30
31 private ArrayAdapter<String> mForecastAdapter;
32
33 public ForecastFragment() {
34 }
35
36 @Override
37 public void onCreate(Bundle savedInstanceState) {
38 super.onCreate(savedInstanceState);
39 setHasOptionsMenu(true);
40 }
41
42 @Override
43 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
44 // Inflate the menu; this adds items to the action bar if it is present.
45 inflater.inflate(R.menu.forecastfragment, menu);
46 }
47
48 @Override
49 public boolean onOptionsItemSelected(MenuItem item) {
50 // Handle action bar item clicks here. The action bar will
51 // automatically handle clicks on the Home/Up button, so long
52 // as you specify a parent activity in AndroidManifest.xml.
53 int id = item.getItemId();
54 if (id == R.id.action_refresh) {
55 updateWeather();
56 return true;
57 }
58 if (id == R.id.action_show_location) {
59 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
60 String location = preferences.getString(getString(R.string.pref_location_key),
61 getString(R.string.pref_location_default));
62 Intent intent = new Intent(Intent.ACTION_VIEW);
63 Uri geoLocation;
64 try {
65 geoLocation = Uri.parse("geo:0,0?q=" + URLEncoder.encode(location, "UTF-8"));
66 intent.setData(geoLocation);
67 } catch (UnsupportedEncodingException e) {
68 Log.e(LOG_TAG, "Error ", e);
69 }
70 if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
71 startActivity(intent);
72 }
73
74 }
75 return super.onOptionsItemSelected(item);
76 }
77
78 private void updateWeather() {
79 String location = Utility.getPreferredLocation(getActivity());
80 new FetchWeatherTask(getActivity(), mForecastAdapter).execute(location);
81 }
82
83 @Override
84 public View onCreateView(LayoutInflater inflater, ViewGroup container,
85 Bundle savedInstanceState) {
86
87 mForecastAdapter = new ArrayAdapter<String>(
88 getActivity(),
89 R.layout.list_item_forecast,
90 R.id.list_item_forecast_textview,
91 new ArrayList<String>() // weekForecast
92 );
93
94 View rootView = inflater.inflate(R.layout.fragment_main, container, false);
95
96 ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
97 listView.setAdapter(mForecastAdapter);
98 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
99
100 @Override
101 public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
102 String forecast = mForecastAdapter.getItem(position);
103 Intent intent = new Intent(getActivity(), DetailActivity.class)
104 .putExtra(Intent.EXTRA_TEXT, forecast);
105 startActivity(intent);
106 }
107 });
108
109 return rootView;
110 }
111
112 public void onStart() {
113 super.onStart();
114 updateWeather();
115 }
116
117
118 }