Got sharing working, finished lesson 3. lesson3
authorNeil Smith <neil.git@njae.me.uk>
Sun, 9 Nov 2014 14:32:23 +0000 (14:32 +0000)
committerNeil Smith <neil.git@njae.me.uk>
Sun, 9 Nov 2014 14:32:23 +0000 (14:32 +0000)
app/src/main/AndroidManifest.xml
app/src/main/java/uk/me/njae/sunshine/DetailActivity.java
app/src/main/res/menu/detailfragment.xml [new file with mode: 0644]
app/src/main/res/menu/forecastfragment.xml
app/src/main/res/menu/main.xml
app/src/main/res/values/strings.xml

index 8a7bee94778ccaf7c0dd55a0a843550e735369b5..3958f30708f0a303dbccb28108f7621f54758699 100644 (file)
@@ -10,7 +10,7 @@
         android:label="@string/app_name"
         android:theme="@style/AppTheme" >
         <activity
-            android:name=".MainActivity"
+            android:name="uk.me.njae.sunshine.MainActivity"
             android:label="@string/app_name" >
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
@@ -19,7 +19,7 @@
             </intent-filter>
         </activity>
         <activity
-            android:name=".DetailActivity"
+            android:name="uk.me.njae.sunshine.DetailActivity"
             android:label="@string/title_activity_detail"
             android:parentActivityName=".MainActivity" >
             <meta-data
index e6e30a18a48211f6ab1539a4c9f3151bcecd7c5f..83031345f846c79fb1ff7b1e388d257d10190187 100644 (file)
@@ -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);
+        }
     }
 }
diff --git a/app/src/main/res/menu/detailfragment.xml b/app/src/main/res/menu/detailfragment.xml
new file mode 100644 (file)
index 0000000..d6c812b
--- /dev/null
@@ -0,0 +1,11 @@
+<menu xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto">
+    <item android:id="@+id/action_show_location"
+        android:title="@string/action_show_location"
+        android:orderInCategory="50"
+        app:showAsAction="never" />
+    <item android:id="@+id/action_share"
+        app:showAsAction="always"
+        android:title="@string/action_share"
+        app:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
+</menu>
index 2b6caaae190e3cd8ff60635b7da8e8650c7e4ec5..48c679508b90ce0eb5fbfab4fa7d4d0a0fd846ac 100644 (file)
@@ -2,10 +2,6 @@
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     tools:context=".MainActivity" >
-    <item android:id="@+id/action_show_location"
-        android:title="@string/action_show_location"
-        android:orderInCategory="50"
-        app:showAsAction="never" />
     <item android:id="@+id/action_refresh"
         android:title="@string/action_refresh"
         android:orderInCategory="50"
index 7fe7424d78336d2e75aaf026937a946fa864cea4..802ec3d82c59da6a45bfbf15f5f3a042deef2860 100644 (file)
@@ -2,6 +2,10 @@
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     tools:context=".MainActivity" >
+    <item android:id="@+id/action_show_location"
+        android:title="@string/action_show_location"
+        android:orderInCategory="50"
+        app:showAsAction="never" />
     <item android:id="@+id/action_settings"
         android:title="@string/action_settings"
         android:orderInCategory="100"
index ecce75ae1436b08f191311646458d9e23102390d..9ff2b0e4be7866cd2230608082dcab4f61679ff4 100644 (file)
@@ -6,8 +6,7 @@
     <string name="action_settings">Settings</string>
     <string name="action_refresh">Refresh</string>
     <string name="action_show_location">Show location</string>
-    <string name="title_activity_detail">DetailActivity</string>
-
-
+    <string name="title_activity_detail">Weather in detail</string>
+    <string name="action_share">Share</string>
 
 </resources>