Got sharing working, finished lesson 3.
[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.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.support.v4.view.MenuItemCompat;
10 import android.support.v7.app.ActionBarActivity;
11 import android.support.v7.widget.ShareActionProvider;
12 import android.util.Log;
13 import android.view.LayoutInflater;
14 import android.view.Menu;
15 import android.view.MenuInflater;
16 import android.view.MenuItem;
17 import android.view.View;
18 import android.view.ViewGroup;
19 import android.widget.TextView;
20
21 import java.io.UnsupportedEncodingException;
22 import java.net.URLEncoder;
23
24 // import android.widget.ShareActionProvider;
25
26 public class DetailActivity extends ActionBarActivity {
27
28 private final String LOG_TAG = DetailActivity.class.getSimpleName();
29 private ShareActionProvider mShareActionProvider;
30
31 @Override
32 protected void onCreate(Bundle savedInstanceState) {
33 super.onCreate(savedInstanceState);
34 setContentView(R.layout.activity_detail);
35 if (savedInstanceState == null) {
36 getSupportFragmentManager().beginTransaction()
37 .add(R.id.container, new DetailFragment())
38 .commit();
39 }
40 }
41
42
43 @Override
44 public boolean onCreateOptionsMenu(Menu menu) {
45 // Inflate the menu; this adds items to the action bar if it is present.
46 getMenuInflater().inflate(R.menu.detail, menu);
47 return true;
48 }
49
50 @Override
51 public boolean onOptionsItemSelected(MenuItem item) {
52 // Handle action bar item clicks here. The action bar will
53 // automatically handle clicks on the Home/Up button, so long
54 // as you specify a parent activity in AndroidManifest.xml.
55 int id = item.getItemId();
56 if (id == R.id.action_settings) {
57 startActivity(new Intent(this, SettingsActivity.class));
58 }
59 return super.onOptionsItemSelected(item);
60 }
61
62 // Call to update the share intent
63 private void setShareIntent(Intent shareIntent) {
64 if (mShareActionProvider != null) {
65 mShareActionProvider.setShareIntent(shareIntent);
66 }
67 }
68
69 /**
70 * A placeholder fragment containing a simple view.
71 */
72 public static class DetailFragment extends Fragment {
73
74 private static final String LOG_TAG = DetailFragment.class.getSimpleName();
75
76 private static final String FORECAST_SHARE_HASHTAG = " #SunshineApp";
77 private String mForecastStr;
78
79 public DetailFragment() {
80 setHasOptionsMenu(true);
81 }
82
83 @Override
84 public View onCreateView(LayoutInflater inflater, ViewGroup container,
85 Bundle savedInstanceState) {
86 Intent intent = getActivity().getIntent();
87 View rootView = inflater.inflate(R.layout.fragment_detail, container, false);
88 if (intent != null && intent.hasExtra(Intent.EXTRA_TEXT)) {
89 mForecastStr = intent.getStringExtra(Intent.EXTRA_TEXT);
90 ((TextView) rootView.findViewById(R.id.detail_text))
91 .setText(mForecastStr);
92 }
93 return rootView;
94 }
95
96 @Override
97 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
98 // Inflate the menu; this adds items to the action bar if it is present.
99 inflater.inflate(R.menu.detailfragment, menu);
100
101 // Retrieve the share menu item
102 MenuItem menuItem = menu.findItem(R.id.action_share);
103
104 // Get the provider and hold onto it to set/change the share intent.
105 ShareActionProvider mShareActionProvider =
106 (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
107
108 // Attach an intent to this ShareActionProvider. You can update this at any time,
109 // like when the user selects a new piece of data they might like to share.
110 if (mShareActionProvider != null ) {
111 mShareActionProvider.setShareIntent(createShareForecastIntent());
112 } else {
113 Log.d(LOG_TAG, "Share Action Provider is null?");
114 }
115 }
116
117 private Intent createShareForecastIntent() {
118 Intent shareIntent = new Intent(Intent.ACTION_SEND);
119 shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
120 shareIntent.setType("text/plain");
121 shareIntent.putExtra(Intent.EXTRA_TEXT,
122 mForecastStr + FORECAST_SHARE_HASHTAG);
123 return shareIntent;
124 }
125
126 @Override
127 public boolean onOptionsItemSelected(MenuItem item) {
128 // Handle action bar item clicks here. The action bar will
129 // automatically handle clicks on the Home/Up button, so long
130 // as you specify a parent activity in AndroidManifest.xml.
131 int id = item.getItemId();
132 if (id == R.id.action_show_location) {
133 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
134 String location = preferences.getString(getString(R.string.pref_location_key),
135 getString(R.string.pref_location_default));
136 Intent intent = new Intent(Intent.ACTION_VIEW);
137 Uri geoLocation;
138 try {
139 geoLocation = Uri.parse("geo:0,0?q=" + URLEncoder.encode(location, "UTF-8"));
140 intent.setData(geoLocation);
141 } catch (UnsupportedEncodingException e) {
142 Log.e(LOG_TAG, "Error ", e);
143 }
144 if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
145 startActivity(intent);
146 }
147
148 }
149 return super.onOptionsItemSelected(item);
150 }
151 }
152 }