diff options
| author | Adam Powell <adamp@google.com> | 2011-12-13 20:46:54 -0800 |
|---|---|---|
| committer | Adam Powell <adamp@google.com> | 2011-12-14 12:14:40 -0800 |
| commit | 1bc7aaff4888307645c98a510efab64b715cd000 (patch) | |
| tree | 09e044d2dca668c459d55cb72628be546812fe15 /samples/Support4Demos/src | |
| parent | ee4934f63d66ea5fc7d2f89671b4b4ad11d9badb (diff) | |
| download | android_development-1bc7aaff4888307645c98a510efab64b715cd000.tar.gz android_development-1bc7aaff4888307645c98a510efab64b715cd000.tar.bz2 android_development-1bc7aaff4888307645c98a510efab64b715cd000.zip | |
Add code sample for ShareCompat
Add SharingSupport activity - use ShareCompat.IntentBuilder to produce
sharing intents for menu item sharing, text only, single stream, and
multiple stream.
Add SharingReceiverSupport - receive text/plain content as text only,
single stream, and multiple stream.
Add SharingSupportProvider - simple filesystem-based read-only
ContentProvider for providing a data stream.
Change-Id: I7e69a80984c0c5a8d93663f928f644d55c92350d
Diffstat (limited to 'samples/Support4Demos/src')
3 files changed, 290 insertions, 0 deletions
diff --git a/samples/Support4Demos/src/com/example/android/supportv4/app/SharingReceiverSupport.java b/samples/Support4Demos/src/com/example/android/supportv4/app/SharingReceiverSupport.java new file mode 100644 index 000000000..e26af070a --- /dev/null +++ b/samples/Support4Demos/src/com/example/android/supportv4/app/SharingReceiverSupport.java @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2011 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.supportv4.app; + +import com.example.android.supportv4.R; + +import android.app.Activity; +import android.graphics.drawable.Drawable; +import android.net.Uri; +import android.os.Bundle; +import android.support.v4.app.ShareCompat; +import android.util.Log; +import android.widget.TextView; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStreamReader; + +/** + * This example shows a simple way to handle data shared with your app through the + * use of the support library's ShareCompat features. It will display shared text + * content as well as the application label and icon of the app that shared the content. + */ +public class SharingReceiverSupport extends Activity { + private static final String TAG = "SharingReceiverSupport"; + private static final int ICON_SIZE = 32; // dip + + @Override + protected void onCreate(Bundle b) { + super.onCreate(b); + setContentView(R.layout.sharing_receiver_support); + + final float density = getResources().getDisplayMetrics().density; + final int iconSize = (int) (ICON_SIZE * density + 0.5f); + + ShareCompat.IntentReader intentReader = ShareCompat.IntentReader.from(this); + + // The following provides attribution for the app that shared the data with us. + TextView info = (TextView) findViewById(R.id.app_info); + Drawable d = intentReader.getCallingActivityIcon(); + d.setBounds(0, 0, iconSize, iconSize); + info.setCompoundDrawables(d, null, null, null); + info.setText(intentReader.getCallingApplicationLabel()); + + TextView tv = (TextView) findViewById(R.id.text); + StringBuilder txt = new StringBuilder("Received share!\nText was: "); + + txt.append(intentReader.getText()); + txt.append("\n"); + + txt.append("Streams included:\n"); + final int N = intentReader.getStreamCount(); + for (int i = 0; i < N; i++) { + Uri uri = intentReader.getStream(i); + txt.append("Share included stream " + i + ": " + uri + "\n"); + try { + BufferedReader reader = new BufferedReader(new InputStreamReader( + getContentResolver().openInputStream(uri))); + try { + txt.append(reader.readLine() + "\n"); + } catch (IOException e) { + Log.e(TAG, "Reading stream threw exception", e); + } finally { + reader.close(); + } + } catch (FileNotFoundException e) { + Log.e(TAG, "File not found from share: " + e); + } + } + + tv.setText(txt.toString()); + } +} diff --git a/samples/Support4Demos/src/com/example/android/supportv4/app/SharingSupport.java b/samples/Support4Demos/src/com/example/android/supportv4/app/SharingSupport.java new file mode 100644 index 000000000..68eb0f25d --- /dev/null +++ b/samples/Support4Demos/src/com/example/android/supportv4/app/SharingSupport.java @@ -0,0 +1,113 @@ +/* + * Copyright (C) 2011 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.supportv4.app; + +import com.example.android.supportv4.R; +import com.example.android.supportv4.content.SharingSupportProvider; + +import android.app.Activity; +import android.net.Uri; +import android.os.Bundle; +import android.support.v4.app.ShareCompat; +import android.support.v4.view.MenuItemCompat; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; + +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; + +/** + * This example illustrates the use of the ShareCompat feature of the support library. + * ShareCompat offers several pieces of functionality to assist in sharing content between + * apps and is especially suited for sharing content to social apps that the user has installed. + * + * <p>Two other classes are relevant to this code sample: {@link SharingReceiverSupport} is + * an activity that has been configured to receive ACTION_SEND and ACTION_SEND_MULTIPLE + * sharing intents with a type of text/plain. It provides an example of writing a sharing + * target using ShareCompat features. {@link SharingSupportProvider} is a simple + * {@link android.content.ContentProvider} that provides access to two text files + * created by this app to share as content streams.</p> + */ +public class SharingSupport extends Activity { + @Override + protected void onCreate(Bundle b) { + super.onCreate(b); + setContentView(R.layout.sharing_support); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + ShareCompat.IntentBuilder b = ShareCompat.IntentBuilder.from(this); + b.setType("text/plain").setText("Share from menu"); + MenuItem item = menu.add("Share"); + ShareCompat.configureMenuItem(item, b); + MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_IF_ROOM); + return true; + } + + public void onShareTextClick(View v) { + ShareCompat.IntentBuilder.from(this) + .setType("text/plain") + .setText("I'm sharing!") + .startChooserForResult(); + } + + public void onShareFileClick(View v) { + try { + // This file will be accessed by the target of the share through + // the ContentProvider SharingSupportProvider. + FileWriter fw = new FileWriter(getFilesDir() + "/foo.txt"); + fw.write("This is a file share"); + fw.close(); + + ShareCompat.IntentBuilder.from(this) + .setType("text/plain") + .setStream(Uri.parse(SharingSupportProvider.CONTENT_URI + "/foo.txt")) + .startChooserForResult(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void onShareMultipleFileClick(View v) { + try { + // These files will be accessed by the target of the share through + // the ContentProvider SharingSupportProvider. + FileWriter fw = new FileWriter(getFilesDir() + "/foo.txt"); + fw.write("This is a file share"); + fw.close(); + + fw = new FileWriter(getFilesDir() + "/bar.txt"); + fw.write("This is another file share"); + fw.close(); + + ShareCompat.IntentBuilder.from(this) + .setType("text/plain") + .addStream(Uri.parse(SharingSupportProvider.CONTENT_URI + "/foo.txt")) + .addStream(Uri.parse(SharingSupportProvider.CONTENT_URI + "/bar.txt")) + .startChooserForResult(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/samples/Support4Demos/src/com/example/android/supportv4/content/SharingSupportProvider.java b/samples/Support4Demos/src/com/example/android/supportv4/content/SharingSupportProvider.java new file mode 100644 index 000000000..596fd6cc9 --- /dev/null +++ b/samples/Support4Demos/src/com/example/android/supportv4/content/SharingSupportProvider.java @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2011 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.supportv4.content; + +import android.content.ContentProvider; +import android.content.ContentValues; +import android.database.Cursor; +import android.net.Uri; +import android.os.ParcelFileDescriptor; +import android.util.Log; + +import java.io.File; +import java.io.FileNotFoundException; + +/** + * This simple ContentProvider provides access to the two example files shared + * by the ShareCompat example {@link com.example.android.supportv4.app.SharingSupport}. + */ +public class SharingSupportProvider extends ContentProvider { + public static final Uri CONTENT_URI = + Uri.parse("content://com.example.supportv4.content.sharingsupportprovider"); + + private static final String TAG = "SharingSupportProvider"; + + @Override + public boolean onCreate() { + return true; + } + + @Override + public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, + String sortOrder) { + return null; + } + + @Override + public String getType(Uri uri) { + if (uri.equals(Uri.withAppendedPath(CONTENT_URI, "foo.txt")) || + uri.equals(Uri.withAppendedPath(CONTENT_URI, "bar.txt"))) { + return "text/plain"; + } + return null; + } + + @Override + public Uri insert(Uri uri, ContentValues values) { + return null; + } + + @Override + public int delete(Uri uri, String selection, String[] selectionArgs) { + return 0; + } + + @Override + public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { + return 0; + } + + @Override + public ParcelFileDescriptor openFile(Uri uri, String mode) { + String path = uri.getPath(); + if (mode.equals("r") && + (path.equals("/foo.txt") || path.equals("/bar.txt"))) { + try { + return ParcelFileDescriptor.open( + new File(getContext().getFilesDir() + path), + ParcelFileDescriptor.MODE_READ_ONLY); + } catch (FileNotFoundException e) { + Log.e(TAG, "Bad file " + uri); + } + } + return null; + } +} |
