/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.sunshine.app;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
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.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;

public class DetailActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container_detail, new DetailFragment())
                    .commit();
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.detail, menu);
        return true;
    }

    @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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            Intent settingsIntent = new Intent(this, SettingsActivity.class);
            startActivity(settingsIntent);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class DetailFragment extends Fragment {

        private ShareActionProvider shareActionProvider;
        private String forecastDetails;

        public DetailFragment() {
            setHasOptionsMenu(true);
        }

        /**
         * Initialize the contents of the Activity's standard options menu.  You
         * should place your menu items in to <var>menu</var>.  For this method
         * to be called, you must have first called {@link #setHasOptionsMenu}.  See
         * {@link Activity#onCreateOptionsMenu(Menu) Activity.onCreateOptionsMenu}
         * for more information.
         */
        @Override
        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            super.onCreateOptionsMenu(menu, inflater);
            inflater.inflate(R.menu.detailfragment, menu);
            // Find the menu item with ShareActionProvider
            MenuItem shareMenuItem = menu.findItem(R.id.action_share);

            // Assign ShareActionProvider object to member variable when the options menu is created.
            shareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareMenuItem);

            // Update the shareActionProvider with the intent.
            setShareIntent(createShareIntent());
        }

        /**
         * Updates the share intent to the shareActionProvider.
         *
         * @param shareIntent is the intent, the shareActionProvider is to be updated with.
         */
        @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
        private void setShareIntent(Intent shareIntent) {
            if (shareActionProvider != null) {
                shareActionProvider.setShareIntent(shareIntent);
            }
        }

        /**
         * Creates intent of weather details to be shared.
         *
         * @return returns an Intent object with weather data.
         */
        private Intent createShareIntent() {
            Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            // If you leave out the flag, when returning to your app (from home screen, from
            // recents etc.), you would see the Activity of the share target
            // (messaging/mailing/IM app) instead of yours.
            shareIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
            shareIntent.setType("text/plain");
            shareIntent.putExtra(
                    Intent.EXTRA_TEXT, forecastDetails + getString(R.string.hashSunshineApp));
            return shareIntent;
        }


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_detail, container, false);
            Intent detailIntent = getActivity().getIntent();
            if (detailIntent != null && detailIntent.hasExtra(Intent.EXTRA_TEXT)) {
                forecastDetails = detailIntent.getStringExtra(Intent.EXTRA_TEXT);
                TextView detailTextView = (TextView) rootView.findViewById(R.id.detail_text_view);
                detailTextView.setText(forecastDetails);
            }

            return rootView;
        }
    }
}
@@ -2755,32 +2755,104 @@
 ailFragment() %7B%0A
+            // Without this, system won't call the onCreateOptionsMenu.%0A
             setH
@@ -2868,24 +2868,24 @@
 Menu(true);%0A
-
         %7D%0A%0A 
@@ -3417,16 +3417,157 @@
 flater);
+%0A%0A            // Inflates the menus defined in detailfragment menu xml.%0A            // without this, Share button in this case won't show up.
 %0A       
@@ -3610,32 +3610,33 @@
 ragment, menu);%0A
+%0A
             // F
@@ -4085,32 +4085,32 @@
  %7D%0A%0A        /**%0A
-
          * Updat
@@ -4157,16 +4157,79 @@
 ovider.%0A
+         * Use this whenever the data you are sharing changes.%0A
         

Update comment in DetailFragment