1 package uk
.me
.njae
.sunshine
;
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
;
23 import uk
.me
.njae
.sunshine
.data
.WeatherContract
;
24 import uk
.me
.njae
.sunshine
.data
.WeatherContract
.WeatherEntry
;
26 // import android.widget.ShareActionProvider;
28 public class DetailActivity
extends ActionBarActivity
{
30 private final String LOG_TAG
= DetailActivity
.class.getSimpleName();
31 private ShareActionProvider mShareActionProvider
;
33 public static final String DATE_KEY
= "forecast_date";
34 private static final String LOCATION_KEY
= "location";
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())
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
);
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));
64 return super.onOptionsItemSelected(item
);
67 // Call to update the share intent
68 private void setShareIntent(Intent shareIntent
) {
69 if (mShareActionProvider
!= null) {
70 mShareActionProvider
.setShareIntent(shareIntent
);
75 * A placeholder fragment containing a simple view.
77 public static class DetailFragment
extends Fragment
implements LoaderManager
.LoaderCallbacks
<Cursor
> {
79 private static final String LOG_TAG
= DetailFragment
.class.getSimpleName();
81 private static final String FORECAST_SHARE_HASHTAG
= " #SunshineApp";
83 public static final String DATE_KEY
= "forecast_date";
85 private ShareActionProvider mShareActionProvider
;
86 private String mLocation
;
87 private String mForecast
;
89 private static final int DETAIL_LOADER
= 0;
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
,
99 public DetailFragment() {
100 setHasOptionsMenu(true);
104 public void onSaveInstanceState(Bundle outState
) {
105 outState
.putString(LOCATION_KEY
, mLocation
);
106 super.onSaveInstanceState(outState
);
110 public void onResume() {
112 if (mLocation
!= null &&
113 !mLocation
.equals(Utility
.getPreferredLocation(getActivity()))) {
114 getLoaderManager().restartLoader(DETAIL_LOADER
, null, this);
120 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
121 Bundle savedInstanceState
) {
122 return inflater
.inflate(R
.layout
.fragment_detail
, container
, false);
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
);
130 // Retrieve the share menu item
131 MenuItem menuItem
= menu
.findItem(R
.id
.action_share
);
133 // Get the provider and hold onto it to set/change the share intent.
134 ShareActionProvider mShareActionProvider
=
135 (ShareActionProvider
) MenuItemCompat
.getActionProvider(menuItem
);
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());
142 Log
.d(LOG_TAG
, "Share Action Provider is null?");
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
);
156 public void onActivityCreated(Bundle savedInstanceState
) {
157 getLoaderManager().initLoader(DETAIL_LOADER
, null, this);
158 if (savedInstanceState
!= null) {
159 mLocation
= savedInstanceState
.getString(LOCATION_KEY
);
161 super.onActivityCreated(savedInstanceState
);
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
)) {
171 String forecastDate
= intent
.getStringExtra(DATE_KEY
);
173 // Sort order: Ascending, by date.
174 String sortOrder
= WeatherContract
.WeatherEntry
.COLUMN_DATETEXT
+ " ASC";
176 mLocation
= Utility
.getPreferredLocation(getActivity());
177 Uri weatherForLocationUri
= WeatherContract
.WeatherEntry
.buildWeatherLocationWithDate(
178 mLocation
, forecastDate
);
179 Log
.v(LOG_TAG
, weatherForLocationUri
.toString());
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(
185 weatherForLocationUri
,
194 public void onLoadFinished(Loader
<Cursor
> loader
, Cursor data
) {
195 Log
.v(LOG_TAG
, "In onLoadFinished");
196 if (!data
.moveToFirst()) { return; }
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
);
203 String weatherDescription
=
204 data
.getString(data
.getColumnIndex(WeatherEntry
.COLUMN_SHORT_DESC
));
205 ((TextView
) getView().findViewById(R
.id
.detail_forecast_textview
))
206 .setText(weatherDescription
);
208 boolean isMetric
= Utility
.isMetric(getActivity());
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
);
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
);
218 // We still need this for the share intent
219 mForecast
= String
.format("%s - %s - %s/%s", dateString
, weatherDescription
, high
, low
);
221 Log
.v(LOG_TAG
, "Forecast String: " + mForecast
);
223 // If onCreateOptionsMenu has already happened, we need to update the share intent now.
224 if (mShareActionProvider
!= null) {
225 mShareActionProvider
.setShareIntent(createShareForecastIntent());
230 public void onLoaderReset(Loader
<Cursor
> loader
) { }
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);
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);
252 // if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
253 // startActivity(intent);
257 // return super.onOptionsItemSelected(item);