X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fuk%2Fme%2Fnjae%2Fsunshine%2FDetailActivity.java;fp=app%2Fsrc%2Fmain%2Fjava%2Fuk%2Fme%2Fnjae%2Fsunshine%2FDetailActivity.java;h=83031345f846c79fb1ff7b1e388d257d10190187;hb=7860705c1ca10c91c5a2095ace5de79e36fdecfb;hp=e6e30a18a48211f6ab1539a4c9f3151bcecd7c5f;hpb=63521d0a7ff37d2f0239be1af3c675cfd80e9095;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 e6e30a1..8303134 100644 --- a/app/src/main/java/uk/me/njae/sunshine/DetailActivity.java +++ b/app/src/main/java/uk/me/njae/sunshine/DetailActivity.java @@ -1,25 +1,40 @@ package uk.me.njae.sunshine; import android.content.Intent; +import android.content.SharedPreferences; +import android.net.Uri; import android.os.Bundle; +import android.preference.PreferenceManager; import android.support.v4.app.Fragment; +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.widget.TextView; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + +// import android.widget.ShareActionProvider; + public class DetailActivity extends ActionBarActivity { + private final String LOG_TAG = DetailActivity.class.getSimpleName(); + private ShareActionProvider mShareActionProvider; + @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(); } } @@ -44,12 +59,25 @@ public class DetailActivity extends ActionBarActivity { 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 { - public PlaceholderFragment() { + private static final String LOG_TAG = DetailFragment.class.getSimpleName(); + + private static final String FORECAST_SHARE_HASHTAG = " #SunshineApp"; + private String mForecastStr; + + public DetailFragment() { + setHasOptionsMenu(true); } @Override @@ -58,11 +86,67 @@ public class DetailActivity extends ActionBarActivity { 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); + mForecastStr = intent.getStringExtra(Intent.EXTRA_TEXT); ((TextView) rootView.findViewById(R.id.detail_text)) - .setText(forecastStr); + .setText(mForecastStr); } return rootView; } + + @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, + mForecastStr + FORECAST_SHARE_HASHTAG); + return shareIntent; + } + + @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); + } } }