1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.example.android.actionbarcompat.shareactionprovider;
18
19import android.content.Intent;
20import android.os.Bundle;
21import android.support.v4.view.MenuItemCompat;
22import android.support.v4.view.PagerAdapter;
23import android.support.v4.view.ViewPager;
24import android.support.v7.app.ActionBarActivity;
25import android.support.v7.widget.ShareActionProvider;
26import android.view.LayoutInflater;
27import android.view.Menu;
28import android.view.MenuItem;
29import android.view.View;
30import android.view.ViewGroup;
31import android.widget.ImageView;
32import android.widget.TextView;
33
34import com.example.android.actionbarcompat.shareactionprovider.content.ContentItem;
35
36import java.util.ArrayList;
37
38/**
39 * This sample shows you how a provide a {@link ShareActionProvider} with ActionBarCompat,
40 * backwards compatible to API v7.
41 * <p>
42 * The sample contains a {@link ViewPager} which displays content of differing types: image and
43 * text. When a new item is selected in the ViewPager, the ShareActionProvider is updated with
44 * a share intent specific to that content.
45 * <p>
46 * This Activity extends from {@link ActionBarActivity}, which provides all of the function
47 * necessary to display a compatible Action Bar on devices running Android v2.1+.
48 */
49public class MainActivity extends ActionBarActivity {
50
51    // The items to be displayed in the ViewPager
52    private final ArrayList<ContentItem> mItems = getSampleContent();
53
54    // Keep reference to the ShareActionProvider from the menu
55    private ShareActionProvider mShareActionProvider;
56
57    @Override
58    protected void onCreate(Bundle savedInstanceState) {
59        super.onCreate(savedInstanceState);
60
61        // Set content view (which contains a CheeseListFragment)
62        setContentView(R.layout.sample_main);
63
64        // Retrieve the ViewPager from the content view
65        ViewPager vp = (ViewPager) findViewById(R.id.viewpager);
66
67        // Set an OnPageChangeListener so we are notified when a new item is selected
68        vp.setOnPageChangeListener(mOnPageChangeListener);
69
70        // Finally set the adapter so the ViewPager can display items
71        vp.setAdapter(mPagerAdapter);
72    }
73
74    // BEGIN_INCLUDE(get_sap)
75    @Override
76    public boolean onCreateOptionsMenu(Menu menu) {
77        // Inflate the menu resource
78        getMenuInflater().inflate(R.menu.main_menu, menu);
79
80        // Retrieve the share menu item
81        MenuItem shareItem = menu.findItem(R.id.menu_share);
82
83        // Now get the ShareActionProvider from the item
84        mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);
85
86        // Get the ViewPager's current item position and set its ShareIntent.
87        int currentViewPagerItem = ((ViewPager) findViewById(R.id.viewpager)).getCurrentItem();
88        setShareIntent(currentViewPagerItem);
89
90        return super.onCreateOptionsMenu(menu);
91    }
92    // END_INCLUDE(get_sap)
93
94    /**
95     * A PagerAdapter which instantiates views based on the ContentItem's content type.
96     */
97    private final PagerAdapter mPagerAdapter = new PagerAdapter() {
98        LayoutInflater mInflater;
99
100        @Override
101        public int getCount() {
102            return mItems.size();
103        }
104
105        @Override
106        public boolean isViewFromObject(View view, Object o) {
107            return view == o;
108        }
109
110        @Override
111        public void destroyItem(ViewGroup container, int position, Object object) {
112            // Just remove the view from the ViewPager
113            container.removeView((View) object);
114        }
115
116        @Override
117        public Object instantiateItem(ViewGroup container, int position) {
118            // Ensure that the LayoutInflater is instantiated
119            if (mInflater == null) {
120                mInflater = LayoutInflater.from(MainActivity.this);
121            }
122
123            // Get the item for the requested position
124            final ContentItem item = mItems.get(position);
125
126            // The view we need to inflate changes based on the type of content
127            switch (item.contentType) {
128                case ContentItem.CONTENT_TYPE_TEXT: {
129                    // Inflate item layout for text
130                    TextView tv = (TextView) mInflater
131                            .inflate(R.layout.item_text, container, false);
132
133                    // Set text content using it's resource id
134                    tv.setText(item.contentResourceId);
135
136                    // Add the view to the ViewPager
137                    container.addView(tv);
138                    return tv;
139                }
140                case ContentItem.CONTENT_TYPE_IMAGE: {
141                    // Inflate item layout for images
142                    ImageView iv = (ImageView) mInflater
143                            .inflate(R.layout.item_image, container, false);
144
145                    // Load the image from it's content URI
146                    iv.setImageURI(item.getContentUri());
147
148                    // Add the view to the ViewPager
149                    container.addView(iv);
150                    return iv;
151                }
152            }
153
154            return null;
155        }
156    };
157
158    private void setShareIntent(int position) {
159        // BEGIN_INCLUDE(update_sap)
160        if (mShareActionProvider != null) {
161            // Get the currently selected item, and retrieve it's share intent
162            ContentItem item = mItems.get(position);
163            Intent shareIntent = item.getShareIntent(MainActivity.this);
164
165            // Now update the ShareActionProvider with the new share intent
166            mShareActionProvider.setShareIntent(shareIntent);
167        }
168        // END_INCLUDE(update_sap)
169    }
170
171    /**
172     * A OnPageChangeListener used to update the ShareActionProvider's share intent when a new item
173     * is selected in the ViewPager.
174     */
175    private final ViewPager.OnPageChangeListener mOnPageChangeListener
176            = new ViewPager.OnPageChangeListener() {
177
178        @Override
179        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
180            // NO-OP
181        }
182
183        @Override
184        public void onPageSelected(int position) {
185            setShareIntent(position);
186        }
187
188        @Override
189        public void onPageScrollStateChanged(int state) {
190            // NO-OP
191        }
192    };
193
194    /**
195     * @return An ArrayList of ContentItem's to be displayed in this sample
196     */
197    static ArrayList<ContentItem> getSampleContent() {
198        ArrayList<ContentItem> items = new ArrayList<ContentItem>();
199
200        items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "photo_1.jpg"));
201        items.add(new ContentItem(ContentItem.CONTENT_TYPE_TEXT, R.string.quote_1));
202        items.add(new ContentItem(ContentItem.CONTENT_TYPE_TEXT, R.string.quote_2));
203        items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "photo_2.jpg"));
204        items.add(new ContentItem(ContentItem.CONTENT_TYPE_TEXT, R.string.quote_3));
205        items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "photo_3.jpg"));
206
207        return items;
208    }
209
210}