• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2015, 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.receiveshare;
18 
19 import android.app.Activity;
20 import android.content.ClipData;
21 import android.content.ContentResolver;
22 import android.content.Intent;
23 import android.content.res.AssetFileDescriptor;
24 import android.net.Uri;
25 import android.os.Bundle;
26 import android.text.SpannableStringBuilder;
27 import android.view.View;
28 import android.widget.Button;
29 import android.widget.TextView;
30 
31 import java.io.FileNotFoundException;
32 import java.io.IOException;
33 
34 public class ReceiveShare extends Activity {
getShareUri(Intent intent)35     static Uri getShareUri(Intent intent) {
36         Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
37         if (uri == null) {
38             ClipData clip = intent.getClipData();
39             if (clip != null && clip.getItemCount() > 0) {
40                 uri = clip.getItemAt(0).getUri();
41             }
42         }
43         return uri;
44     }
45 
buildShareInfo(ContentResolver resolver, Intent intent)46     static CharSequence buildShareInfo(ContentResolver resolver, Intent intent) {
47         SpannableStringBuilder sb = new SpannableStringBuilder();
48         if (intent.getType() != null) {
49             sb.append("Type: "); sb.append(intent.getType()); sb.append("\n");
50         }
51         CharSequence text = intent.getCharSequenceExtra(Intent.EXTRA_TEXT);
52         if (text != null) {
53             sb.append("Text: "); sb.append(text);
54             String html = intent.getStringExtra(Intent.EXTRA_HTML_TEXT);
55             if (html != null) {
56                 sb.append("\n\n"); sb.append("HTML: "); sb.append(html);
57             }
58         } else {
59             Uri uri = getShareUri(intent);
60             if (uri != null) {
61                 sb.append("Uri: "); sb.append(uri.toString()); sb.append("\n");
62                 try {
63                     AssetFileDescriptor afd = resolver.openAssetFileDescriptor(
64                             uri, "r");
65                     sb.append("Start offset: ");
66                     sb.append(Long.toString(afd.getStartOffset()));
67                     sb.append("\n");
68                     sb.append("Length: ");
69                     sb.append(Long.toString(afd.getLength()));
70                     sb.append("\n");
71                     afd.close();
72                 } catch (FileNotFoundException e) {
73                     sb.append(e.toString());
74                 } catch (SecurityException e) {
75                     sb.append(e.toString());
76                 } catch (IOException e) {
77                     sb.append(e.toString());
78                 }
79             }
80         }
81         return sb;
82     }
83 
84     @Override
onCreate(Bundle savedInstanceState)85     protected void onCreate(Bundle savedInstanceState) {
86         super.onCreate(savedInstanceState);
87 
88         setContentView(R.layout.receive_share);
89 
90         Button sendButton = (Button)findViewById(R.id.send_to_service);
91         final Uri uri = getShareUri(getIntent());
92         if (uri != null) {
93             sendButton.setEnabled(true);
94         } else {
95             sendButton.setEnabled(false);
96         }
97 
98         TextView content = (TextView)findViewById(R.id.receive_share_data);
99         content.append(buildShareInfo(getContentResolver(), getIntent()));
100 
101         // Watch for button clicks.
102         sendButton.setOnClickListener(new View.OnClickListener() {
103             @Override
104             public void onClick(View v) {
105                 Intent intent = new Intent(ReceiveShare.this, ReceiveShareService.class);
106                 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
107                 ClipData clip = ClipData.newUri(getContentResolver(), "Something", uri);
108                 intent.setClipData(clip);
109                 startService(intent);
110                 finish();
111             }
112         });
113     }
114 }
115