Finished lesson 4
[Sunshine.git] / app / src / main / java / uk / me / njae / sunshine / DetailActivity.java
1 package uk.me.njae.sunshine;
2
3 import android.content.Intent;
4 import android.database.Cursor;
5 import android.net.Uri;
6 import android.os.Bundle;
7 import android.support.v4.app.Fragment;
8 import android.support.v4.app.LoaderManager;
9 import android.support.v4.content.CursorLoader;
10 import android.support.v4.content.Loader;
11 import android.support.v4.view.MenuItemCompat;
12 import android.support.v7.app.ActionBarActivity;
13 import android.support.v7.widget.ShareActionProvider;
14 import android.util.Log;
15 import android.view.LayoutInflater;
16 import android.view.Menu;
17 import android.view.MenuInflater;
18 import android.view.MenuItem;
19 import android.view.View;
20 import android.view.ViewGroup;
21 import android.widget.TextView;
22
23 import uk.me.njae.sunshine.data.WeatherContract;
24 import uk.me.njae.sunshine.data.WeatherContract.WeatherEntry;
25
26 // import android.widget.ShareActionProvider;
27
28 public class DetailActivity extends ActionBarActivity {
29
30 private final String LOG_TAG = DetailActivity.class.getSimpleName();
31 private ShareActionProvider mShareActionProvider;
32
33 public static final String DATE_KEY = "forecast_date";
34 private static final String LOCATION_KEY = "location";
35
36 @Override
37 protected void onCreate(Bundle savedInstanceState) {
38 super.onCreate(savedInstanceState);
39 setContentView(R.layout.activity_detail);
40 if (savedInstanceState == null) {
41 getSupportFragmentManager().beginTransaction()
42 .add(R.id.container, new DetailFragment())
43 .commit();
44 }
45 }
46
47
48 @Override
49 public boolean onCreateOptionsMenu(Menu menu) {
50 // Inflate the menu; this adds items to the action bar if it is present.
51 getMenuInflater().inflate(R.menu.detail, menu);
52 return true;
53 }
54
55 @Override
56 public boolean onOptionsItemSelected(MenuItem item) {
57 // Handle action bar item clicks here. The action bar will
58 // automatically handle clicks on the Home/Up button, so long
59 // as you specify a parent activity in AndroidManifest.xml.
60 int id = item.getItemId();
61 if (id == R.id.action_settings) {
62 startActivity(new Intent(this, SettingsActivity.class));
63 }
64 return super.onOptionsItemSelected(item);
65 }
66
67 // Call to update the share intent
68 private void setShareIntent(Intent shareIntent) {
69 if (mShareActionProvider != null) {
70 mShareActionProvider.setShareIntent(shareIntent);
71 }
72 }
73
74 /**
75 * A placeholder fragment containing a simple view.
76 */
77 public static class DetailFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
78
79 private static final String LOG_TAG = DetailFragment.class.getSimpleName();
80
81 private static final String FORECAST_SHARE_HASHTAG = " #SunshineApp";
82
83 public static final String DATE_KEY = "forecast_date";
84
85 private ShareActionProvider mShareActionProvider;
86 private String mLocation;
87 private String mForecast;
88
89 private static final int DETAIL_LOADER = 0;
90
91 private static final String[] FORECAST_COLUMNS = {
92 WeatherEntry.TABLE_NAME + "." + WeatherEntry._ID,
93 WeatherEntry.COLUMN_DATETEXT,
94 WeatherEntry.COLUMN_SHORT_DESC,
95 WeatherEntry.COLUMN_MAX_TEMP,
96 WeatherEntry.COLUMN_MIN_TEMP,
97 };
98
99 public DetailFragment() {
100 setHasOptionsMenu(true);
101 }
102
103 @Override
104 public void onSaveInstanceState(Bundle outState) {
105 outState.putString(LOCATION_KEY, mLocation);
106 super.onSaveInstanceState(outState);
107 }
108
109 @Override
110 public void onResume() {
111 super.onResume();
112 if (mLocation != null &&
113 !mLocation.equals(Utility.getPreferredLocation(getActivity()))) {
114 getLoaderManager().restartLoader(DETAIL_LOADER, null, this);
115 }
116 }
117
118
119 @Override
120 public View onCreateView(LayoutInflater inflater, ViewGroup container,
121 Bundle savedInstanceState) {
122 return inflater.inflate(R.layout.fragment_detail, container, false);
123 }
124
125 @Override
126 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
127 // Inflate the menu; this adds items to the action bar if it is present.
128 inflater.inflate(R.menu.detailfragment, menu);
129
130 // Retrieve the share menu item
131 MenuItem menuItem = menu.findItem(R.id.action_share);
132
133 // Get the provider and hold onto it to set/change the share intent.
134 ShareActionProvider mShareActionProvider =
135 (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
136
137 // Attach an intent to this ShareActionProvider. You can update this at any time,
138 // like when the user selects a new piece of data they might like to share.
139 if (mShareActionProvider != null ) {
140 mShareActionProvider.setShareIntent(createShareForecastIntent());
141 } else {
142 Log.d(LOG_TAG, "Share Action Provider is null?");
143 }
144 }
145
146 private Intent createShareForecastIntent() {
147 Intent shareIntent = new Intent(Intent.ACTION_SEND);
148 shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
149 shareIntent.setType("text/plain");
150 shareIntent.putExtra(Intent.EXTRA_TEXT, mForecast + FORECAST_SHARE_HASHTAG);
151 return shareIntent;
152 }
153
154
155 @Override
156 public void onActivityCreated(Bundle savedInstanceState) {
157 getLoaderManager().initLoader(DETAIL_LOADER, null, this);
158 if (savedInstanceState != null) {
159 mLocation = savedInstanceState.getString(LOCATION_KEY);
160 }
161 super.onActivityCreated(savedInstanceState);
162 }
163
164 @Override
165 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
166 Log.v(LOG_TAG, "In onCreateLoader");
167 Intent intent = getActivity().getIntent();
168 if (intent == null || !intent.hasExtra(DATE_KEY)) {
169 return null;
170 }
171 String forecastDate = intent.getStringExtra(DATE_KEY);
172
173 // Sort order: Ascending, by date.
174 String sortOrder = WeatherContract.WeatherEntry.COLUMN_DATETEXT + " ASC";
175
176 mLocation = Utility.getPreferredLocation(getActivity());
177 Uri weatherForLocationUri = WeatherContract.WeatherEntry.buildWeatherLocationWithDate(
178 mLocation, forecastDate);
179 Log.v(LOG_TAG, weatherForLocationUri.toString());
180
181 // Now create and return a CursorLoader that will take care of
182 // creating a Cursor for the data being displayed.
183 return new CursorLoader(
184 getActivity(),
185 weatherForLocationUri,
186 FORECAST_COLUMNS,
187 null,
188 null,
189 sortOrder
190 );
191 }
192
193 @Override
194 public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
195 Log.v(LOG_TAG, "In onLoadFinished");
196 if (!data.moveToFirst()) { return; }
197
198 String dateString = Utility.formatDate(
199 data.getString(data.getColumnIndex(WeatherEntry.COLUMN_DATETEXT)));
200 ((TextView) getView().findViewById(R.id.detail_date_textview))
201 .setText(dateString);
202
203 String weatherDescription =
204 data.getString(data.getColumnIndex(WeatherEntry.COLUMN_SHORT_DESC));
205 ((TextView) getView().findViewById(R.id.detail_forecast_textview))
206 .setText(weatherDescription);
207
208 boolean isMetric = Utility.isMetric(getActivity());
209
210 String high = Utility.formatTemperature(
211 data.getDouble(data.getColumnIndex(WeatherEntry.COLUMN_MAX_TEMP)), isMetric);
212 ((TextView) getView().findViewById(R.id.detail_high_textview)).setText(high);
213
214 String low = Utility.formatTemperature(
215 data.getDouble(data.getColumnIndex(WeatherEntry.COLUMN_MIN_TEMP)), isMetric);
216 ((TextView) getView().findViewById(R.id.detail_low_textview)).setText(low);
217
218 // We still need this for the share intent
219 mForecast = String.format("%s - %s - %s/%s", dateString, weatherDescription, high, low);
220
221 Log.v(LOG_TAG, "Forecast String: " + mForecast);
222
223 // If onCreateOptionsMenu has already happened, we need to update the share intent now.
224 if (mShareActionProvider != null) {
225 mShareActionProvider.setShareIntent(createShareForecastIntent());
226 }
227 }
228
229 @Override
230 public void onLoaderReset(Loader<Cursor> loader) { }
231
232
233
234 // @Override
235 // public boolean onOptionsItemSelected(MenuItem item) {
236 // // Handle action bar item clicks here. The action bar will
237 // // automatically handle clicks on the Home/Up button, so long
238 // // as you specify a parent activity in AndroidManifest.xml.
239 // int id = item.getItemId();
240 // if (id == R.id.action_show_location) {
241 // SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
242 // String location = preferences.getString(getString(R.string.pref_location_key),
243 // getString(R.string.pref_location_default));
244 // Intent intent = new Intent(Intent.ACTION_VIEW);
245 // Uri geoLocation;
246 // try {
247 // geoLocation = Uri.parse("geo:0,0?q=" + URLEncoder.encode(location, "UTF-8"));
248 // intent.setData(geoLocation);
249 // } catch (UnsupportedEncodingException e) {
250 // Log.e(LOG_TAG, "Error ", e);
251 // }
252 // if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
253 // startActivity(intent);
254 // }
255 //
256 // }
257 // return super.onOptionsItemSelected(item);
258 // }
259 }
260 }