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 
17 package com.example.android.shareactionprovider;
18 
19 import com.example.android.shareactionprovider.content.ContentItem;
20 
21 import android.content.Intent;
22 import android.os.Bundle;
23 import android.support.v4.view.MenuItemCompat;
24 import android.support.v4.view.PagerAdapter;
25 import android.support.v4.view.ViewPager;
26 import android.support.v7.app.AppCompatActivity;
27 import android.support.v7.widget.ShareActionProvider;
28 import android.view.LayoutInflater;
29 import android.view.Menu;
30 import android.view.MenuItem;
31 import android.view.View;
32 import android.view.ViewGroup;
33 import android.widget.ImageView;
34 import android.widget.TextView;
35 
36 import 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  */
47 public class MainActivity extends AppCompatActivity {
48 
49     // The items to be displayed in the ViewPager
50     private final ArrayList<ContentItem> mItems = getSampleContent();
51 
52     // Keep reference to the ShareActionProvider from the menu
53     private ShareActionProvider mShareActionProvider;
54 
55     @Override
onCreate(Bundle savedInstanceState)56     protected void onCreate(Bundle savedInstanceState) {
57         super.onCreate(savedInstanceState);
58 
59         // Set content view (which contains a CheeseListFragment)
60         setContentView(R.layout.sample_main);
61 
62         // Retrieve the ViewPager from the content view
63         ViewPager vp = (ViewPager) findViewById(R.id.viewpager);
64 
65         // Set an OnPageChangeListener so we are notified when a new item is selected
66         vp.addOnPageChangeListener(mOnPageChangeListener);
67 
68         // Finally set the adapter so the ViewPager can display items
69         vp.setAdapter(mPagerAdapter);
70     }
71 
72     // BEGIN_INCLUDE(get_sap)
73     @Override
onCreateOptionsMenu(Menu menu)74     public boolean onCreateOptionsMenu(Menu menu) {
75         // Inflate the menu resource
76         getMenuInflater().inflate(R.menu.main_menu, menu);
77 
78         // Retrieve the share menu item
79         MenuItem shareItem = menu.findItem(R.id.menu_share);
80 
81         // Now get the ShareActionProvider from the item
82         mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);
83 
84         // Get the ViewPager's current item position and set its ShareIntent.
85         int currentViewPagerItem = ((ViewPager) findViewById(R.id.viewpager)).getCurrentItem();
86         setShareIntent(currentViewPagerItem);
87 
88         return super.onCreateOptionsMenu(menu);
89     }
90     // END_INCLUDE(get_sap)
91 
92     /**
93      * A PagerAdapter which instantiates views based on the ContentItem's content type.
94      */
95     private final PagerAdapter mPagerAdapter = new PagerAdapter() {
96         LayoutInflater mInflater;
97 
98         @Override
99         public int getCount() {
100             return mItems.size();
101         }
102 
103         @Override
104         public boolean isViewFromObject(View view, Object o) {
105             return view == o;
106         }
107 
108         @Override
109         public void destroyItem(ViewGroup container, int position, Object object) {
110             // Just remove the view from the ViewPager
111             container.removeView((View) object);
112         }
113 
114         @Override
115         public Object instantiateItem(ViewGroup container, int position) {
116             // Ensure that the LayoutInflater is instantiated
117             if (mInflater == null) {
118                 mInflater = LayoutInflater.from(MainActivity.this);
119             }
120 
121             // Get the item for the requested position
122             final ContentItem item = mItems.get(position);
123 
124             // The view we need to inflate changes based on the type of content
125             switch (item.contentType) {
126                 case ContentItem.CONTENT_TYPE_TEXT: {
127                     // Inflate item layout for text
128                     TextView tv = (TextView) mInflater
129                             .inflate(R.layout.item_text, container, false);
130 
131                     // Set text content using it's resource id
132                     tv.setText(item.contentResourceId);
133 
134                     // Add the view to the ViewPager
135                     container.addView(tv);
136                     return tv;
137                 }
138                 case ContentItem.CONTENT_TYPE_IMAGE: {
139                     // Inflate item layout for images
140                     ImageView iv = (ImageView) mInflater
141                             .inflate(R.layout.item_image, container, false);
142 
143                     // Load the image from it's content URI
144                     iv.setImageURI(item.getContentUri());
145 
146                     // Add the view to the ViewPager
147                     container.addView(iv);
148                     return iv;
149                 }
150             }
151 
152             return null;
153         }
154     };
155 
setShareIntent(int position)156     private void setShareIntent(int position) {
157         // BEGIN_INCLUDE(update_sap)
158         if (mShareActionProvider != null) {
159             // Get the currently selected item, and retrieve it's share intent
160             ContentItem item = mItems.get(position);
161             Intent shareIntent = item.getShareIntent(MainActivity.this);
162 
163             // Now update the ShareActionProvider with the new share intent
164             mShareActionProvider.setShareIntent(shareIntent);
165         }
166         // END_INCLUDE(update_sap)
167     }
168 
169     /**
170      * A OnPageChangeListener used to update the ShareActionProvider's share intent when a new item
171      * is selected in the ViewPager.
172      */
173     private final ViewPager.OnPageChangeListener mOnPageChangeListener
174             = new ViewPager.OnPageChangeListener() {
175 
176         @Override
177         public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
178             // NO-OP
179         }
180 
181         @Override
182         public void onPageSelected(int position) {
183             setShareIntent(position);
184         }
185 
186         @Override
187         public void onPageScrollStateChanged(int state) {
188             // NO-OP
189         }
190     };
191 
192     /**
193      * @return An ArrayList of ContentItem's to be displayed in this sample
194      */
getSampleContent()195     static ArrayList<ContentItem> getSampleContent() {
196         ArrayList<ContentItem> items = new ArrayList<ContentItem>();
197 
198         items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "photo_1.jpg"));
199         items.add(new ContentItem(ContentItem.CONTENT_TYPE_TEXT, R.string.quote_1));
200         items.add(new ContentItem(ContentItem.CONTENT_TYPE_TEXT, R.string.quote_2));
201         items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "photo_2.jpg"));
202         items.add(new ContentItem(ContentItem.CONTENT_TYPE_TEXT, R.string.quote_3));
203         items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "photo_3.jpg"));
204 
205         return items;
206     }
207 
208 }