1 package uk
.me
.njae
.sunshine
;
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
;
21 import java
.io
.UnsupportedEncodingException
;
22 import java
.net
.URLEncoder
;
24 // import android.widget.ShareActionProvider;
26 public class DetailActivity
extends ActionBarActivity
{
28 private final String LOG_TAG
= DetailActivity
.class.getSimpleName();
29 private ShareActionProvider mShareActionProvider
;
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())
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
);
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));
59 return super.onOptionsItemSelected(item
);
62 // Call to update the share intent
63 private void setShareIntent(Intent shareIntent
) {
64 if (mShareActionProvider
!= null) {
65 mShareActionProvider
.setShareIntent(shareIntent
);
70 * A placeholder fragment containing a simple view.
72 public static class DetailFragment
extends Fragment
{
74 private static final String LOG_TAG
= DetailFragment
.class.getSimpleName();
76 private static final String FORECAST_SHARE_HASHTAG
= " #SunshineApp";
77 private String mForecastStr
;
79 public DetailFragment() {
80 setHasOptionsMenu(true);
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
);
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
);
101 // Retrieve the share menu item
102 MenuItem menuItem
= menu
.findItem(R
.id
.action_share
);
104 // Get the provider and hold onto it to set/change the share intent.
105 ShareActionProvider mShareActionProvider
=
106 (ShareActionProvider
) MenuItemCompat
.getActionProvider(menuItem
);
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());
113 Log
.d(LOG_TAG
, "Share Action Provider is null?");
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
);
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
);
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
);
144 if (intent
.resolveActivity(getActivity().getPackageManager()) != null) {
145 startActivity(intent
);
149 return super.onOptionsItemSelected(item
);