X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fuk%2Fme%2Fnjae%2Fsunshine%2FDetailActivity.java;h=0614eb1b635d7ba570189175621778c3b2583b1e;hb=HEAD;hp=aad043ef8abf9c21d0bf006542b53f937ba872d9;hpb=8751e0563d9f5863957e37c502c8b749afe8db61;p=Sunshine.git diff --git a/app/src/main/java/uk/me/njae/sunshine/DetailActivity.java b/app/src/main/java/uk/me/njae/sunshine/DetailActivity.java index aad043e..0614eb1 100644 --- a/app/src/main/java/uk/me/njae/sunshine/DetailActivity.java +++ b/app/src/main/java/uk/me/njae/sunshine/DetailActivity.java @@ -1,29 +1,45 @@ package uk.me.njae.sunshine; import android.content.Intent; -import android.support.v7.app.ActionBarActivity; -import android.support.v7.app.ActionBar; -import android.support.v4.app.Fragment; +import android.database.Cursor; +import android.net.Uri; import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.support.v4.app.LoaderManager; +import android.support.v4.content.CursorLoader; +import android.support.v4.content.Loader; +import android.support.v4.view.MenuItemCompat; +import android.support.v7.app.ActionBarActivity; +import android.support.v7.widget.ShareActionProvider; +import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; +import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; -import android.os.Build; import android.widget.TextView; -import uk.me.njae.sunshine.R; +import uk.me.njae.sunshine.data.WeatherContract; +import uk.me.njae.sunshine.data.WeatherContract.WeatherEntry; + +// import android.widget.ShareActionProvider; public class DetailActivity extends ActionBarActivity { + private final String LOG_TAG = DetailActivity.class.getSimpleName(); + private ShareActionProvider mShareActionProvider; + + public static final String DATE_KEY = "forecast_date"; + private static final String LOCATION_KEY = "location"; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_detail); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() - .add(R.id.container, new PlaceholderFragment()) + .add(R.id.container, new DetailFragment()) .commit(); } } @@ -43,30 +59,202 @@ public class DetailActivity extends ActionBarActivity { // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { - return true; + startActivity(new Intent(this, SettingsActivity.class)); } return super.onOptionsItemSelected(item); } + // Call to update the share intent + private void setShareIntent(Intent shareIntent) { + if (mShareActionProvider != null) { + mShareActionProvider.setShareIntent(shareIntent); + } + } + /** * A placeholder fragment containing a simple view. */ - public static class PlaceholderFragment extends Fragment { + public static class DetailFragment extends Fragment implements LoaderManager.LoaderCallbacks { + + private static final String LOG_TAG = DetailFragment.class.getSimpleName(); + + private static final String FORECAST_SHARE_HASHTAG = " #SunshineApp"; + + public static final String DATE_KEY = "forecast_date"; + + private ShareActionProvider mShareActionProvider; + private String mLocation; + private String mForecast; + + private static final int DETAIL_LOADER = 0; + + private static final String[] FORECAST_COLUMNS = { + WeatherEntry.TABLE_NAME + "." + WeatherEntry._ID, + WeatherEntry.COLUMN_DATETEXT, + WeatherEntry.COLUMN_SHORT_DESC, + WeatherEntry.COLUMN_MAX_TEMP, + WeatherEntry.COLUMN_MIN_TEMP, + }; - public PlaceholderFragment() { + public DetailFragment() { + setHasOptionsMenu(true); } + @Override + public void onSaveInstanceState(Bundle outState) { + outState.putString(LOCATION_KEY, mLocation); + super.onSaveInstanceState(outState); + } + + @Override + public void onResume() { + super.onResume(); + if (mLocation != null && + !mLocation.equals(Utility.getPreferredLocation(getActivity()))) { + getLoaderManager().restartLoader(DETAIL_LOADER, null, this); + } + } + + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + return inflater.inflate(R.layout.fragment_detail, container, false); + } + + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + // Inflate the menu; this adds items to the action bar if it is present. + inflater.inflate(R.menu.detailfragment, menu); + + // Retrieve the share menu item + MenuItem menuItem = menu.findItem(R.id.action_share); + + // Get the provider and hold onto it to set/change the share intent. + ShareActionProvider mShareActionProvider = + (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem); + + // Attach an intent to this ShareActionProvider. You can update this at any time, + // like when the user selects a new piece of data they might like to share. + if (mShareActionProvider != null ) { + mShareActionProvider.setShareIntent(createShareForecastIntent()); + } else { + Log.d(LOG_TAG, "Share Action Provider is null?"); + } + } + + private Intent createShareForecastIntent() { + Intent shareIntent = new Intent(Intent.ACTION_SEND); + shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); + shareIntent.setType("text/plain"); + shareIntent.putExtra(Intent.EXTRA_TEXT, mForecast + FORECAST_SHARE_HASHTAG); + return shareIntent; + } + + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + getLoaderManager().initLoader(DETAIL_LOADER, null, this); + if (savedInstanceState != null) { + mLocation = savedInstanceState.getString(LOCATION_KEY); + } + super.onActivityCreated(savedInstanceState); + } + + @Override + public Loader onCreateLoader(int id, Bundle args) { + Log.v(LOG_TAG, "In onCreateLoader"); Intent intent = getActivity().getIntent(); - View rootView = inflater.inflate(R.layout.fragment_detail, container, false); - if (intent != null && intent.hasExtra(Intent.EXTRA_TEXT)) { - String forecastStr = intent.getStringExtra(Intent.EXTRA_TEXT); - ((TextView) rootView.findViewById(R.id.detail_text)) - .setText(forecastStr); + if (intent == null || !intent.hasExtra(DATE_KEY)) { + return null; + } + String forecastDate = intent.getStringExtra(DATE_KEY); + + // Sort order: Ascending, by date. + String sortOrder = WeatherContract.WeatherEntry.COLUMN_DATETEXT + " ASC"; + + mLocation = Utility.getPreferredLocation(getActivity()); + Uri weatherForLocationUri = WeatherContract.WeatherEntry.buildWeatherLocationWithDate( + mLocation, forecastDate); + Log.v(LOG_TAG, weatherForLocationUri.toString()); + + // Now create and return a CursorLoader that will take care of + // creating a Cursor for the data being displayed. + return new CursorLoader( + getActivity(), + weatherForLocationUri, + FORECAST_COLUMNS, + null, + null, + sortOrder + ); + } + + @Override + public void onLoadFinished(Loader loader, Cursor data) { + Log.v(LOG_TAG, "In onLoadFinished"); + if (!data.moveToFirst()) { return; } + + String dateString = Utility.formatDate( + data.getString(data.getColumnIndex(WeatherEntry.COLUMN_DATETEXT))); + ((TextView) getView().findViewById(R.id.detail_date_textview)) + .setText(dateString); + + String weatherDescription = + data.getString(data.getColumnIndex(WeatherEntry.COLUMN_SHORT_DESC)); + ((TextView) getView().findViewById(R.id.detail_forecast_textview)) + .setText(weatherDescription); + + boolean isMetric = Utility.isMetric(getActivity()); + + String high = Utility.formatTemperature( + data.getDouble(data.getColumnIndex(WeatherEntry.COLUMN_MAX_TEMP)), isMetric); + ((TextView) getView().findViewById(R.id.detail_high_textview)).setText(high); + + String low = Utility.formatTemperature( + data.getDouble(data.getColumnIndex(WeatherEntry.COLUMN_MIN_TEMP)), isMetric); + ((TextView) getView().findViewById(R.id.detail_low_textview)).setText(low); + + // We still need this for the share intent + mForecast = String.format("%s - %s - %s/%s", dateString, weatherDescription, high, low); + + Log.v(LOG_TAG, "Forecast String: " + mForecast); + + // If onCreateOptionsMenu has already happened, we need to update the share intent now. + if (mShareActionProvider != null) { + mShareActionProvider.setShareIntent(createShareForecastIntent()); } - return rootView; } + + @Override + public void onLoaderReset(Loader loader) { } + + + +// @Override +// public boolean onOptionsItemSelected(MenuItem item) { +// // Handle action bar item clicks here. The action bar will +// // automatically handle clicks on the Home/Up button, so long +// // as you specify a parent activity in AndroidManifest.xml. +// int id = item.getItemId(); +// if (id == R.id.action_show_location) { +// SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); +// String location = preferences.getString(getString(R.string.pref_location_key), +// getString(R.string.pref_location_default)); +// Intent intent = new Intent(Intent.ACTION_VIEW); +// Uri geoLocation; +// try { +// geoLocation = Uri.parse("geo:0,0?q=" + URLEncoder.encode(location, "UTF-8")); +// intent.setData(geoLocation); +// } catch (UnsupportedEncodingException e) { +// Log.e(LOG_TAG, "Error ", e); +// } +// if (intent.resolveActivity(getActivity().getPackageManager()) != null) { +// startActivity(intent); +// } +// +// } +// return super.onOptionsItemSelected(item); +// } } }