From f62f771ba0dc882e67cd81feec211fc37a29cf94 Mon Sep 17 00:00:00 2001 From: Dianne Hackborn Date: Fri, 17 Apr 2015 12:58:04 -0700 Subject: [PATCH] New sample code for receiving a share and processing in a service. Change-Id: I892ee1bfca9e0a209b427265b825658e326b0adc --- samples/ReceiveShareDemo/Android.mk | 16 +++ samples/ReceiveShareDemo/AndroidManifest.xml | 31 +++++ .../res/layout/receive_share.xml | 41 +++++++ .../ReceiveShareDemo/res/values/strings.xml | 23 ++++ .../android/receiveshare/ReceiveShare.java | 114 ++++++++++++++++++ .../receiveshare/ReceiveShareService.java | 61 ++++++++++ 6 files changed, 286 insertions(+) create mode 100644 samples/ReceiveShareDemo/Android.mk create mode 100644 samples/ReceiveShareDemo/AndroidManifest.xml create mode 100644 samples/ReceiveShareDemo/res/layout/receive_share.xml create mode 100644 samples/ReceiveShareDemo/res/values/strings.xml create mode 100644 samples/ReceiveShareDemo/src/com/example/android/receiveshare/ReceiveShare.java create mode 100644 samples/ReceiveShareDemo/src/com/example/android/receiveshare/ReceiveShareService.java diff --git a/samples/ReceiveShareDemo/Android.mk b/samples/ReceiveShareDemo/Android.mk new file mode 100644 index 000000000..ad62f6367 --- /dev/null +++ b/samples/ReceiveShareDemo/Android.mk @@ -0,0 +1,16 @@ +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_MODULE_TAGS := samples + +# Only compile source java files in this apk. +LOCAL_SRC_FILES := $(call all-java-files-under, src) + +LOCAL_PACKAGE_NAME := ReceiveShareDemo + +LOCAL_SDK_VERSION := current + +include $(BUILD_PACKAGE) + +# Use the following include to make our test apk. +include $(call all-makefiles-under,$(LOCAL_PATH)) diff --git a/samples/ReceiveShareDemo/AndroidManifest.xml b/samples/ReceiveShareDemo/AndroidManifest.xml new file mode 100644 index 000000000..1e07091f9 --- /dev/null +++ b/samples/ReceiveShareDemo/AndroidManifest.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + diff --git a/samples/ReceiveShareDemo/res/layout/receive_share.xml b/samples/ReceiveShareDemo/res/layout/receive_share.xml new file mode 100644 index 000000000..6d33a1be4 --- /dev/null +++ b/samples/ReceiveShareDemo/res/layout/receive_share.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + diff --git a/samples/ReceiveShareDemo/res/values/strings.xml b/samples/ReceiveShareDemo/res/values/strings.xml new file mode 100644 index 000000000..662ce3bbc --- /dev/null +++ b/samples/ReceiveShareDemo/res/values/strings.xml @@ -0,0 +1,23 @@ + + + + + Receive Share Demo + Received a share from another application. This is + the data received: + Send to service + Preparing to process share... + diff --git a/samples/ReceiveShareDemo/src/com/example/android/receiveshare/ReceiveShare.java b/samples/ReceiveShareDemo/src/com/example/android/receiveshare/ReceiveShare.java new file mode 100644 index 000000000..a0abf6d77 --- /dev/null +++ b/samples/ReceiveShareDemo/src/com/example/android/receiveshare/ReceiveShare.java @@ -0,0 +1,114 @@ +/** + * Copyright (c) 2015, 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.receiveshare; + +import android.app.Activity; +import android.content.ClipData; +import android.content.ContentResolver; +import android.content.Intent; +import android.content.res.AssetFileDescriptor; +import android.net.Uri; +import android.os.Bundle; +import android.text.SpannableStringBuilder; +import android.view.View; +import android.widget.Button; +import android.widget.TextView; + +import java.io.FileNotFoundException; +import java.io.IOException; + +public class ReceiveShare extends Activity { + static Uri getShareUri(Intent intent) { + Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM); + if (uri == null) { + ClipData clip = intent.getClipData(); + if (clip != null && clip.getItemCount() > 0) { + uri = clip.getItemAt(0).getUri(); + } + } + return uri; + } + + static CharSequence buildShareInfo(ContentResolver resolver, Intent intent) { + SpannableStringBuilder sb = new SpannableStringBuilder(); + if (intent.getType() != null) { + sb.append("Type: "); sb.append(intent.getType()); sb.append("\n"); + } + CharSequence text = intent.getCharSequenceExtra(Intent.EXTRA_TEXT); + if (text != null) { + sb.append("Text: "); sb.append(text); + String html = intent.getStringExtra(Intent.EXTRA_HTML_TEXT); + if (html != null) { + sb.append("\n\n"); sb.append("HTML: "); sb.append(html); + } + } else { + Uri uri = getShareUri(intent); + if (uri != null) { + sb.append("Uri: "); sb.append(uri.toString()); sb.append("\n"); + try { + AssetFileDescriptor afd = resolver.openAssetFileDescriptor( + uri, "r"); + sb.append("Start offset: "); + sb.append(Long.toString(afd.getStartOffset())); + sb.append("\n"); + sb.append("Length: "); + sb.append(Long.toString(afd.getLength())); + sb.append("\n"); + afd.close(); + } catch (FileNotFoundException e) { + sb.append(e.toString()); + } catch (SecurityException e) { + sb.append(e.toString()); + } catch (IOException e) { + sb.append(e.toString()); + } + } + } + return sb; + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setContentView(R.layout.receive_share); + + Button sendButton = (Button)findViewById(R.id.send_to_service); + final Uri uri = getShareUri(getIntent()); + if (uri != null) { + sendButton.setEnabled(true); + } else { + sendButton.setEnabled(false); + } + + TextView content = (TextView)findViewById(R.id.receive_share_data); + content.append(buildShareInfo(getContentResolver(), getIntent())); + + // Watch for button clicks. + sendButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent intent = new Intent(ReceiveShare.this, ReceiveShareService.class); + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + ClipData clip = ClipData.newUri(getContentResolver(), "Something", uri); + intent.setClipData(clip); + startService(intent); + finish(); + } + }); + } +} diff --git a/samples/ReceiveShareDemo/src/com/example/android/receiveshare/ReceiveShareService.java b/samples/ReceiveShareDemo/src/com/example/android/receiveshare/ReceiveShareService.java new file mode 100644 index 000000000..548650f8f --- /dev/null +++ b/samples/ReceiveShareDemo/src/com/example/android/receiveshare/ReceiveShareService.java @@ -0,0 +1,61 @@ +/** + * Copyright (c) 2015, 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.receiveshare; + +import android.app.IntentService; +import android.content.Intent; +import android.os.Handler; +import android.util.Log; +import android.widget.Toast; + +public class ReceiveShareService extends IntentService { + Handler mHandler; + + public ReceiveShareService() { + super("ReceiveShareService"); + } + + public void onCreate() { + super.onCreate(); + mHandler = new Handler(getMainLooper()); + } + + @Override + protected void onHandleIntent(Intent intent) { + mHandler.post(new Runnable() { + @Override public void run() { + Toast.makeText(ReceiveShareService.this, R.string.preparing_to_process_share, + Toast.LENGTH_LONG).show(); + } + }); + try { + // Give the activity a chance to finish. + Thread.sleep(5*1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + final CharSequence text = ReceiveShare.buildShareInfo(getContentResolver(), intent); + mHandler.post(new Runnable() { + @Override + public void run() { + Toast.makeText(ReceiveShareService.this, text, Toast.LENGTH_LONG).show(); + } + }); + Log.i("ReceiveShare", text.toString()); + } +}