Merge "Add code sample for ShareCompat"

This commit is contained in:
Adam Powell
2012-01-10 13:31:09 -08:00
committed by Android (Google) Code Review
7 changed files with 384 additions and 0 deletions

View File

@@ -242,5 +242,26 @@
</intent-filter>
</activity>
<activity android:name=".app.SharingSupport"
android:label="@string/sharing_support_title">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
</intent-filter>
</activity>
<activity android:name=".app.SharingReceiverSupport"
android:label="@string/sharing_receiver_title">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SEND_MULTIPLE" />
<data android:mimeType="text/plain" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<provider android:authorities="com.example.supportv4.content.sharingsupportprovider"
android:name=".content.SharingSupportProvider" />
</application>
</manifest>

View File

@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 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.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/app_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"/>
<ScrollView android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
</LinearLayout>

View File

@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 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.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:id="@+id/share_text"
android:text="@string/share_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onShareTextClick" />
<Button android:id="@+id/share_file"
android:text="@string/share_file"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onShareFileClick" />
<Button android:id="@+id/share_multiple_file"
android:text="@string/share_multiple_file"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onShareMultipleFileClick" />
</LinearLayout>

View File

@@ -122,4 +122,10 @@
<string name="accessibility_delegate_button">Button</string>
<string name="accessibility_delegate_custom_text_added">Custom text added via an accessibility delegate.</string>
<string name="share_text">Share some text</string>
<string name="share_file">Share a file</string>
<string name="share_multiple_file">Share multiple files</string>
<string name="sharing_support_title">ShareCompat Demo</string>
<string name="sharing_receiver_title">ShareCompat Receiver</string>
</resources>

View File

@@ -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());
}
}

View File

@@ -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();
}
}
}

View File

@@ -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;
}
}