ActionBarCompat + ShareActionProvider Sample
Change-Id: I05ba13d79bd0f74825f8ff3641ffa3fcb7e0462e
Signed-off-by: Chris Banes <chrisbanes@google.com>
(cherry picked from commit afb952b43d34f1a91687df9144353fea6eaaf2a0)
diff --git a/ui/actionbarcompat/ShareActionProvider/big_icon.png b/ui/actionbarcompat/ShareActionProvider/big_icon.png
new file mode 100644
index 0000000..7da0b23
--- /dev/null
+++ b/ui/actionbarcompat/ShareActionProvider/big_icon.png
Binary files differ
diff --git a/ui/actionbarcompat/ShareActionProvider/build.gradle b/ui/actionbarcompat/ShareActionProvider/build.gradle
new file mode 100644
index 0000000..b99b102
--- /dev/null
+++ b/ui/actionbarcompat/ShareActionProvider/build.gradle
@@ -0,0 +1,16 @@
+apply plugin: 'android'
+
+dependencies {
+ compile "com.android.support:support-v4:18.0.+"
+ compile "com.android.support:appcompat-v7:18.0.+"
+}
+
+android {
+ compileSdkVersion 17
+ buildToolsVersion "17.0.0"
+
+ defaultConfig {
+ minSdkVersion 7
+ targetSdkVersion 16
+ }
+}
diff --git a/ui/actionbarcompat/ShareActionProvider/src/main/AndroidManifest.xml b/ui/actionbarcompat/ShareActionProvider/src/main/AndroidManifest.xml
new file mode 100644
index 0000000..8a5af4f
--- /dev/null
+++ b/ui/actionbarcompat/ShareActionProvider/src/main/AndroidManifest.xml
@@ -0,0 +1,52 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2010 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.
+-->
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.example.android.actionbarcompat.shareactionprovider"
+ android:versionCode="1"
+ android:versionName="1.0">
+
+ <!--
+ ActionBarCompat provides an Action Bar from API v7 onwards
+ -->
+ <uses-sdk
+ android:minSdkVersion="7"
+ android:targetSdkVersion="17" />
+
+ <application
+ android:icon="@drawable/ic_launcher"
+ android:label="@string/app_name"
+ android:theme="@style/Theme.AppCompat"
+ android:allowBackup="true">
+
+ <activity
+ android:name=".MainActivity">
+ <!-- Launcher Intent filter -->
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+ <category android:name="android.intent.category.LAUNCHER" />
+ </intent-filter>
+ </activity>
+
+ <!-- ContentProvider which serves files from this application's asset folder -->
+ <provider
+ android:name=".content.AssetProvider"
+ android:authorities="com.example.android.actionbarcompat.shareactionprovider"
+ android:grantUriPermissions="true"
+ android:exported="true" />
+
+ </application>
+
+</manifest>
\ No newline at end of file
diff --git a/ui/actionbarcompat/ShareActionProvider/src/main/assets/photo_1.jpg b/ui/actionbarcompat/ShareActionProvider/src/main/assets/photo_1.jpg
new file mode 100644
index 0000000..cd365c7
--- /dev/null
+++ b/ui/actionbarcompat/ShareActionProvider/src/main/assets/photo_1.jpg
Binary files differ
diff --git a/ui/actionbarcompat/ShareActionProvider/src/main/assets/photo_2.jpg b/ui/actionbarcompat/ShareActionProvider/src/main/assets/photo_2.jpg
new file mode 100644
index 0000000..f088c11
--- /dev/null
+++ b/ui/actionbarcompat/ShareActionProvider/src/main/assets/photo_2.jpg
Binary files differ
diff --git a/ui/actionbarcompat/ShareActionProvider/src/main/assets/photo_3.jpg b/ui/actionbarcompat/ShareActionProvider/src/main/assets/photo_3.jpg
new file mode 100644
index 0000000..d4cff6a
--- /dev/null
+++ b/ui/actionbarcompat/ShareActionProvider/src/main/assets/photo_3.jpg
Binary files differ
diff --git a/ui/actionbarcompat/ShareActionProvider/src/main/res/drawable-hdpi/ic_launcher.png b/ui/actionbarcompat/ShareActionProvider/src/main/res/drawable-hdpi/ic_launcher.png
new file mode 100644
index 0000000..48db73f
--- /dev/null
+++ b/ui/actionbarcompat/ShareActionProvider/src/main/res/drawable-hdpi/ic_launcher.png
Binary files differ
diff --git a/ui/actionbarcompat/ShareActionProvider/src/main/res/drawable-mdpi/ic_launcher.png b/ui/actionbarcompat/ShareActionProvider/src/main/res/drawable-mdpi/ic_launcher.png
new file mode 100644
index 0000000..674b1ee
--- /dev/null
+++ b/ui/actionbarcompat/ShareActionProvider/src/main/res/drawable-mdpi/ic_launcher.png
Binary files differ
diff --git a/ui/actionbarcompat/ShareActionProvider/src/main/res/drawable-xhdpi/ic_launcher.png b/ui/actionbarcompat/ShareActionProvider/src/main/res/drawable-xhdpi/ic_launcher.png
new file mode 100644
index 0000000..e76105d
--- /dev/null
+++ b/ui/actionbarcompat/ShareActionProvider/src/main/res/drawable-xhdpi/ic_launcher.png
Binary files differ
diff --git a/ui/actionbarcompat/ShareActionProvider/src/main/res/drawable-xxhdpi/ic_launcher.png b/ui/actionbarcompat/ShareActionProvider/src/main/res/drawable-xxhdpi/ic_launcher.png
new file mode 100644
index 0000000..67605d8
--- /dev/null
+++ b/ui/actionbarcompat/ShareActionProvider/src/main/res/drawable-xxhdpi/ic_launcher.png
Binary files differ
diff --git a/ui/actionbarcompat/ShareActionProvider/src/main/res/layout/activity_main.xml b/ui/actionbarcompat/ShareActionProvider/src/main/res/layout/activity_main.xml
new file mode 100644
index 0000000..902e8ab
--- /dev/null
+++ b/ui/actionbarcompat/ShareActionProvider/src/main/res/layout/activity_main.xml
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ Copyright 2013 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.
+-->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:orientation="vertical">
+
+ <android.support.v4.view.ViewPager
+ android:id="@+id/viewpager"
+ android:layout_width="match_parent"
+ android:layout_height="0dp"
+ android:layout_weight="1" />
+
+ <TextView
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:text="@string/intro_message"
+ android:padding="16dp"
+ android:textAppearance="?android:textAppearanceMedium"
+ android:lineSpacingMultiplier="1.1"
+ android:background="#fb3"/>
+
+</LinearLayout>
\ No newline at end of file
diff --git a/ui/actionbarcompat/ShareActionProvider/src/main/res/layout/item_image.xml b/ui/actionbarcompat/ShareActionProvider/src/main/res/layout/item_image.xml
new file mode 100644
index 0000000..f7940e7
--- /dev/null
+++ b/ui/actionbarcompat/ShareActionProvider/src/main/res/layout/item_image.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ Copyright 2013 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.
+-->
+<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:scaleType="fitCenter" />
diff --git a/ui/actionbarcompat/ShareActionProvider/src/main/res/layout/item_text.xml b/ui/actionbarcompat/ShareActionProvider/src/main/res/layout/item_text.xml
new file mode 100644
index 0000000..00c6a38
--- /dev/null
+++ b/ui/actionbarcompat/ShareActionProvider/src/main/res/layout/item_text.xml
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ Copyright 2013 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.
+-->
+<TextView xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:padding="16dp"
+ android:textAppearance="?android:textAppearanceLarge"
+ android:lineSpacingMultiplier="1.1"
+ android:gravity="center"/>
diff --git a/ui/actionbarcompat/ShareActionProvider/src/main/res/menu/main_menu.xml b/ui/actionbarcompat/ShareActionProvider/src/main/res/menu/main_menu.xml
new file mode 100644
index 0000000..acd2134
--- /dev/null
+++ b/ui/actionbarcompat/ShareActionProvider/src/main/res/menu/main_menu.xml
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ Copyright 2013 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.
+-->
+
+<!--
+ As we're using ActionBarCompat, any action item attributes come from ActionBarCompat's XML
+ namespace instead of the android namespace. Here we've added a new support namespace added to
+ the menu element allowing us to use the 'showAsAction' attribute in a backwards compatible way.
+ Any other action item attributes used should be referenced from this namespace too
+ (actionProviderClass, actionViewClass, actionLayout).
+-->
+<menu xmlns:android="http://schemas.android.com/apk/res/android"
+ xmlns:support="http://schemas.android.com/apk/res-auto">
+
+ <!--
+ To use ShareActionProvider provided by ActionBarCompat, we reference the class by set the
+ support:actionProviderClass attribute with the full class name of ShareActionProvider.
+ -->
+ <item
+ android:id="@+id/menu_share"
+ android:title="@string/menu_share"
+ support:actionProviderClass="android.support.v7.widget.ShareActionProvider"
+ support:showAsAction="always" />
+
+</menu>
\ No newline at end of file
diff --git a/ui/actionbarcompat/ShareActionProvider/src/main/res/values/strings.xml b/ui/actionbarcompat/ShareActionProvider/src/main/res/values/strings.xml
new file mode 100644
index 0000000..d2b7ef8
--- /dev/null
+++ b/ui/actionbarcompat/ShareActionProvider/src/main/res/values/strings.xml
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ Copyright 2013 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.
+-->
+<resources>
+
+ <string name="app_name">ABC ShareActionProvider</string>
+ <string name="intro_message">This sample demonstrates how to show a
+ <i>ShareActionProvider</i>, updated for the selected content.</string>
+ <string name="menu_share">Share</string>
+
+ <string name="quote_1">Expectation is the root of all heartache - William Shakespeare</string>
+ <string name="quote_2">The true sign of intelligence is not knowledge but imagination - Albert
+ Einstein</string>
+ <string name="quote_3">As for me, all I know is that I know nothing - Socrates</string>
+
+</resources>
\ No newline at end of file
diff --git a/ui/actionbarcompat/ShareActionProvider/src/main/src/com/example/android/actionbarcompat/shareactionprovider/MainActivity.java b/ui/actionbarcompat/ShareActionProvider/src/main/src/com/example/android/actionbarcompat/shareactionprovider/MainActivity.java
new file mode 100644
index 0000000..c651b39
--- /dev/null
+++ b/ui/actionbarcompat/ShareActionProvider/src/main/src/com/example/android/actionbarcompat/shareactionprovider/MainActivity.java
@@ -0,0 +1,202 @@
+/*
+ * Copyright (C) 2013 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.actionbarcompat.shareactionprovider;
+
+import com.example.android.actionbarcompat.shareactionprovider.content.ContentItem;
+
+import android.content.Intent;
+import android.os.Bundle;
+import android.support.v4.view.MenuItemCompat;
+import android.support.v4.view.PagerAdapter;
+import android.support.v4.view.ViewPager;
+import android.support.v7.app.ActionBarActivity;
+import android.support.v7.widget.ShareActionProvider;
+import android.view.LayoutInflater;
+import android.view.Menu;
+import android.view.MenuItem;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ImageView;
+import android.widget.TextView;
+
+import java.util.ArrayList;
+
+/**
+ * This sample shows you how a provide a {@link ShareActionProvider} with ActionBarCompat,
+ * backwards compatible to API v7.
+ * <p>
+ * The sample contains a {@link ViewPager} which displays content of differing types: image and
+ * text. When a new item is selected in the ViewPager, the ShareActionProvider is updated with
+ * a share intent specific to that content.
+ * <p>
+ * This Activity extends from {@link ActionBarActivity}, which provides all of the function
+ * necessary to display a compatible Action Bar on devices running Android v2.1+.
+ */
+public class MainActivity extends ActionBarActivity {
+
+ // The items to be displayed in the ViewPager
+ private final ArrayList<ContentItem> mItems = getSampleContent();
+
+ // Keep reference to the ShareActionProvider from the menu
+ private ShareActionProvider mShareActionProvider;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ // Set content view (which contains a CheeseListFragment)
+ setContentView(R.layout.activity_main);
+
+ // Retrieve the ViewPager from the content view
+ ViewPager vp = (ViewPager) findViewById(R.id.viewpager);
+
+ // Set an OnPageChangeListener so we are notified when a new item is selected
+ vp.setOnPageChangeListener(mOnPageChangeListener);
+
+ // Finally set the adapter so the ViewPager can display items
+ vp.setAdapter(mPagerAdapter);
+ }
+
+ // BEGIN_INCLUDE(get_sap)
+ @Override
+ public boolean onCreateOptionsMenu(Menu menu) {
+ // Inflate the menu resource
+ getMenuInflater().inflate(R.menu.main_menu, menu);
+
+ // Retrieve the share menu item
+ MenuItem shareItem = menu.findItem(R.id.menu_share);
+
+ // Now get the ShareActionProvider from the item
+ mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);
+
+ return super.onCreateOptionsMenu(menu);
+ }
+ // END_INCLUDE(get_sap)
+
+ /**
+ * A PagerAdapter which instantiates views based on the ContentItem's content type.
+ */
+ private final PagerAdapter mPagerAdapter = new PagerAdapter() {
+ LayoutInflater mInflater;
+
+ @Override
+ public int getCount() {
+ return mItems.size();
+ }
+
+ @Override
+ public boolean isViewFromObject(View view, Object o) {
+ return view == o;
+ }
+
+ @Override
+ public void destroyItem(ViewGroup container, int position, Object object) {
+ // Just remove the view from the ViewPager
+ container.removeView((View) object);
+ }
+
+ @Override
+ public Object instantiateItem(ViewGroup container, int position) {
+ // Ensure that the LayoutInflater is instantiated
+ if (mInflater == null) {
+ mInflater = LayoutInflater.from(MainActivity.this);
+ }
+
+ // Get the item for the requested position
+ final ContentItem item = mItems.get(position);
+
+ // The view we need to inflate changes based on the type of content
+ switch (item.contentType) {
+ case ContentItem.CONTENT_TYPE_TEXT: {
+ // Inflate item layout for text
+ TextView tv = (TextView) mInflater
+ .inflate(R.layout.item_text, container, false);
+
+ // Set text content using it's resource id
+ tv.setText(item.contentResourceId);
+
+ // Add the view to the ViewPager
+ container.addView(tv);
+ return tv;
+ }
+ case ContentItem.CONTENT_TYPE_IMAGE: {
+ // Inflate item layout for images
+ ImageView iv = (ImageView) mInflater
+ .inflate(R.layout.item_image, container, false);
+
+ // Load the image from it's content URI
+ iv.setImageURI(item.getContentUri());
+
+ // Add the view to the ViewPager
+ container.addView(iv);
+ return iv;
+ }
+ }
+
+ return null;
+ }
+ };
+
+ /**
+ * A OnPageChangeListener used to update the ShareActionProvider's share intent when a new item
+ * is selected in the ViewPager.
+ */
+ private final ViewPager.OnPageChangeListener mOnPageChangeListener
+ = new ViewPager.OnPageChangeListener() {
+
+ @Override
+ public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
+ // NO-OP
+ }
+
+ @Override
+ public void onPageSelected(int position) {
+ // BEGIN_INCLUDE(update_sap)
+ if (mShareActionProvider != null) {
+ // Get the currently selected item, and retrieve it's share intent
+ ContentItem item = mItems.get(position);
+ Intent shareIntent = item.getShareIntent(MainActivity.this);
+
+ // Now update the ShareActionProvider with the new share intent
+ mShareActionProvider.setShareIntent(shareIntent);
+ }
+ // END_INCLUDE(update_sap)
+ }
+
+ @Override
+ public void onPageScrollStateChanged(int state) {
+ // NO-OP
+ }
+ };
+
+ /**
+ * @return An ArrayList of ContentItem's to be displayed in this sample
+ */
+ static ArrayList<ContentItem> getSampleContent() {
+ ArrayList<ContentItem> items = new ArrayList<ContentItem>();
+
+ items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "photo_1.jpg"));
+ items.add(new ContentItem(ContentItem.CONTENT_TYPE_TEXT, R.string.quote_1));
+ items.add(new ContentItem(ContentItem.CONTENT_TYPE_TEXT, R.string.quote_2));
+ items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "photo_2.jpg"));
+ items.add(new ContentItem(ContentItem.CONTENT_TYPE_TEXT, R.string.quote_3));
+ items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "photo_3.jpg"));
+
+ return items;
+ }
+
+}
\ No newline at end of file
diff --git a/ui/actionbarcompat/ShareActionProvider/src/main/src/com/example/android/actionbarcompat/shareactionprovider/content/AssetProvider.java b/ui/actionbarcompat/ShareActionProvider/src/main/src/com/example/android/actionbarcompat/shareactionprovider/content/AssetProvider.java
new file mode 100644
index 0000000..b60f7d7
--- /dev/null
+++ b/ui/actionbarcompat/ShareActionProvider/src/main/src/com/example/android/actionbarcompat/shareactionprovider/content/AssetProvider.java
@@ -0,0 +1,93 @@
+/*
+ * Copyright (C) 2013 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.actionbarcompat.shareactionprovider.content;
+
+import android.content.ContentProvider;
+import android.content.ContentValues;
+import android.content.res.AssetFileDescriptor;
+import android.content.res.AssetManager;
+import android.database.Cursor;
+import android.net.Uri;
+import android.text.TextUtils;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+
+/**
+ * A simple ContentProvider which can serve files from this application's assets. The majority of
+ * functionality is in {@link #openAssetFile(android.net.Uri, String)}.
+ */
+public class AssetProvider extends ContentProvider {
+
+ public static String CONTENT_URI = "com.example.android.actionbarcompat.shareactionprovider";
+
+ @Override
+ public boolean onCreate() {
+ return true;
+ }
+
+ @Override
+ public int delete(Uri uri, String selection, String[] selectionArgs) {
+ // Do not support delete requests.
+ return 0;
+ }
+
+ @Override
+ public String getType(Uri uri) {
+ // Do not support returning the data type
+ return null;
+ }
+
+ @Override
+ public Uri insert(Uri uri, ContentValues values) {
+ // Do not support insert requests.
+ return null;
+ }
+
+ @Override
+ public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
+ String sortOrder) {
+ // Do not support query requests.
+ return null;
+ }
+
+ @Override
+ public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
+ // Do not support update requests.
+ return 0;
+ }
+
+ @Override
+ public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
+ // The asset file name should be the last path segment
+ final String assetName = uri.getLastPathSegment();
+
+ // If the given asset name is empty, throw an exception
+ if (TextUtils.isEmpty(assetName)) {
+ throw new FileNotFoundException();
+ }
+
+ try {
+ // Try and return a file descriptor for the given asset name
+ AssetManager am = getContext().getAssets();
+ return am.openFd(assetName);
+ } catch (IOException e) {
+ e.printStackTrace();
+ return super.openAssetFile(uri, mode);
+ }
+ }
+}
diff --git a/ui/actionbarcompat/ShareActionProvider/src/main/src/com/example/android/actionbarcompat/shareactionprovider/content/ContentItem.java b/ui/actionbarcompat/ShareActionProvider/src/main/src/com/example/android/actionbarcompat/shareactionprovider/content/ContentItem.java
new file mode 100644
index 0000000..09f8003
--- /dev/null
+++ b/ui/actionbarcompat/ShareActionProvider/src/main/src/com/example/android/actionbarcompat/shareactionprovider/content/ContentItem.java
@@ -0,0 +1,101 @@
+/*
+ * Copyright (C) 2013 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.actionbarcompat.shareactionprovider.content;
+
+import android.content.Context;
+import android.content.Intent;
+import android.net.Uri;
+import android.text.TextUtils;
+
+/**
+ * This class encapsulates a content item. Referencing the content's type, and the differing way
+ * to reference the content (asset URI or resource id).
+ */
+public class ContentItem {
+ // Used to signify an image content type
+ public static final int CONTENT_TYPE_IMAGE = 0;
+ // Used to signify a text/string content type
+ public static final int CONTENT_TYPE_TEXT = 1;
+
+ public final int contentType;
+ public final int contentResourceId;
+ public final String contentAssetFilePath;
+
+ /**
+ * Creates a ContentItem with the specified type, referencing a resource id.
+ *
+ * @param type - One of {@link #CONTENT_TYPE_IMAGE} or {@link #CONTENT_TYPE_TEXT}
+ * @param resourceId - Resource ID to use for this item's content
+ */
+ public ContentItem(int type, int resourceId) {
+ contentType = type;
+ contentResourceId = resourceId;
+ contentAssetFilePath = null;
+ }
+
+ /**
+ * Creates a ContentItem with the specified type, referencing an asset file path.
+ *
+ * @param type - One of {@link #CONTENT_TYPE_IMAGE} or {@link #CONTENT_TYPE_TEXT}
+ * @param assetFilePath - File path from the application's asset for this item's content
+ */
+ public ContentItem(int type, String assetFilePath) {
+ contentType = type;
+ contentAssetFilePath = assetFilePath;
+ contentResourceId = 0;
+ }
+
+ /**
+ * @return Uri to the content
+ */
+ public Uri getContentUri() {
+ if (!TextUtils.isEmpty(contentAssetFilePath)) {
+ // If this content has an asset, then return a AssetProvider Uri
+ return Uri.parse("content://" + AssetProvider.CONTENT_URI + "/" + contentAssetFilePath);
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Returns an {@link Intent} which can be used to share this item's content with other
+ * applications.
+ *
+ * @param context - Context to be used for fetching resources if needed
+ * @return Intent to be given to a ShareActionProvider.
+ */
+ public Intent getShareIntent(Context context) {
+ Intent intent = new Intent(Intent.ACTION_SEND);
+
+ switch (contentType) {
+ case CONTENT_TYPE_IMAGE:
+ intent.setType("image/jpg");
+ // Bundle the asset content uri as the EXTRA_STREAM uri
+ intent.putExtra(Intent.EXTRA_STREAM, getContentUri());
+ break;
+
+ case CONTENT_TYPE_TEXT:
+ intent.setType("text/plain");
+ // Get the string resource and bundle it as an intent extra
+ intent.putExtra(Intent.EXTRA_TEXT, context.getString(contentResourceId));
+ break;
+ }
+
+ return intent;
+ }
+
+}