ActionBarCompat-ShareActionProvider / src / com.example.android.actionbarcompat.shareactionprovider /

MainActivity.java

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.actionbarcompat.shareactionprovider;
18
 
19
import android.content.Intent;
20
import android.os.Bundle;
21
import android.support.v4.view.MenuItemCompat;
22
import android.support.v4.view.PagerAdapter;
23
import android.support.v4.view.ViewPager;
24
import android.support.v7.app.ActionBarActivity;
25
import android.support.v7.widget.ShareActionProvider;
26
import android.view.LayoutInflater;
27
import android.view.Menu;
28
import android.view.MenuItem;
29
import android.view.View;
30
import android.view.ViewGroup;
31
import android.widget.ImageView;
32
import android.widget.TextView;
33
 
34
import com.example.android.actionbarcompat.shareactionprovider.content.ContentItem;
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
 * 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
 */
49
public 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
 
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
        return super.onCreateOptionsMenu(menu);
87
    }
89
 
90
    /**
91
     * A PagerAdapter which instantiates views based on the ContentItem's content type.
92
     */
93
    private final PagerAdapter mPagerAdapter = new PagerAdapter() {
94
        LayoutInflater mInflater;
95
 
96
        @Override
97
        public int getCount() {
98
            return mItems.size();
99
        }
100
 
101
        @Override
102
        public boolean isViewFromObject(View view, Object o) {
103
            return view == o;
104
        }
105
 
106
        @Override
107
        public void destroyItem(ViewGroup container, int position, Object object) {
108
            // Just remove the view from the ViewPager
109
            container.removeView((View) object);
110
        }
111
 
112
        @Override
113
        public Object instantiateItem(ViewGroup container, int position) {
114
            // Ensure that the LayoutInflater is instantiated
115
            if (mInflater == null) {
116
                mInflater = LayoutInflater.from(MainActivity.this);
117
            }
118
 
119
            // Get the item for the requested position
120
            final ContentItem item = mItems.get(position);
121
 
122
            // The view we need to inflate changes based on the type of content
123
            switch (item.contentType) {
124
                case ContentItem.CONTENT_TYPE_TEXT: {
125
                    // Inflate item layout for text
126
                    TextView tv = (TextView) mInflater
127
                            .inflate(R.layout.item_text, container, false);
128
 
129
                    // Set text content using it's resource id
130
                    tv.setText(item.contentResourceId);
131
 
132
                    // Add the view to the ViewPager
133
                    container.addView(tv);
134
                    return tv;
135
                }
136
                case ContentItem.CONTENT_TYPE_IMAGE: {
137
                    // Inflate item layout for images
138
                    ImageView iv = (ImageView) mInflater
139
                            .inflate(R.layout.item_image, container, false);
140
 
141
                    // Load the image from it's content URI
142
                    iv.setImageURI(item.getContentUri());
143
 
144
                    // Add the view to the ViewPager
145
                    container.addView(iv);
146
                    return iv;
147
                }
148
            }
149
 
150
            return null;
151
        }
152
    };
153
 
154
    /**
155
     * A OnPageChangeListener used to update the ShareActionProvider's share intent when a new item
156
     * is selected in the ViewPager.
157
     */
158
    private final ViewPager.OnPageChangeListener mOnPageChangeListener
159
            = new ViewPager.OnPageChangeListener() {
160
 
161
        @Override
162
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
163
            // NO-OP
164
        }
165
 
166
        @Override
167
        public void onPageSelected(int position) {
169
            if (mShareActionProvider != null) {
170
                // Get the currently selected item, and retrieve it's share intent
171
                ContentItem item = mItems.get(position);
172
                Intent shareIntent = item.getShareIntent(MainActivity.this);
173
 
174
                // Now update the ShareActionProvider with the new share intent
175
                mShareActionProvider.setShareIntent(shareIntent);
176
            }
178
        }
179
 
180
        @Override
181
        public void onPageScrollStateChanged(int state) {
182
            // NO-OP
183
        }
184
    };
185
 
186
    /**
187
     * @return An ArrayList of ContentItem's to be displayed in this sample
188
     */
189
    static ArrayList<ContentItem> getSampleContent() {
190
        ArrayList<ContentItem> items = new ArrayList<ContentItem>();
191
 
192
        items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "photo_1.jpg"));
193
        items.add(new ContentItem(ContentItem.CONTENT_TYPE_TEXT, R.string.quote_1));
194
        items.add(new ContentItem(ContentItem.CONTENT_TYPE_TEXT, R.string.quote_2));
195
        items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "photo_2.jpg"));
196
        items.add(new ContentItem(ContentItem.CONTENT_TYPE_TEXT, R.string.quote_3));
197
        items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "photo_3.jpg"));
198
 
199
        return items;
200
    }
201
 
202
}