SharingReceiverSupport.java revision ac5fe7c617c66850fff75a9fce9979c6e5674b0f
1/*
2 * Copyright (C) 2011 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
17package com.example.android.supportv4.app;
18
19import com.example.android.supportv4.R;
20
21import android.app.Activity;
22import android.graphics.drawable.Drawable;
23import android.net.Uri;
24import android.os.Bundle;
25import androidx.core.app.ShareCompat;
26import android.util.Log;
27import android.widget.TextView;
28
29import java.io.BufferedReader;
30import java.io.FileNotFoundException;
31import java.io.IOException;
32import java.io.InputStreamReader;
33
34/**
35 * This example shows a simple way to handle data shared with your app through the
36 * use of the support library's ShareCompat features. It will display shared text
37 * content as well as the application label and icon of the app that shared the content.
38 */
39public class SharingReceiverSupport extends Activity {
40    private static final String TAG = "SharingReceiverSupport";
41    private static final int ICON_SIZE = 32; // dip
42
43    @Override
44    protected void onCreate(Bundle b) {
45        super.onCreate(b);
46        setContentView(R.layout.sharing_receiver_support);
47
48        final float density = getResources().getDisplayMetrics().density;
49        final int iconSize = (int) (ICON_SIZE * density + 0.5f);
50
51        ShareCompat.IntentReader intentReader = ShareCompat.IntentReader.from(this);
52
53        // The following provides attribution for the app that shared the data with us.
54        TextView info = findViewById(R.id.app_info);
55        Drawable d = intentReader.getCallingActivityIcon();
56        d.setBounds(0, 0, iconSize, iconSize);
57        info.setCompoundDrawables(d, null, null, null);
58        info.setText(intentReader.getCallingApplicationLabel());
59
60        TextView tv = findViewById(R.id.text);
61        StringBuilder txt = new StringBuilder("Received share!\nText was: ");
62
63        txt.append(intentReader.getText());
64        txt.append("\n");
65
66        txt.append("Streams included:\n");
67        final int N = intentReader.getStreamCount();
68        for (int i = 0; i < N; i++) {
69            Uri uri = intentReader.getStream(i);
70            txt.append("Share included stream " + i + ": " + uri + "\n");
71            try {
72                BufferedReader reader = new BufferedReader(new InputStreamReader(
73                        getContentResolver().openInputStream(uri)));
74                try {
75                    txt.append(reader.readLine() + "\n");
76                } catch (IOException e) {
77                    Log.e(TAG, "Reading stream threw exception", e);
78                } finally {
79                    reader.close();
80                }
81            } catch (FileNotFoundException e) {
82                Log.e(TAG, "File not found from share.", e);
83            } catch (IOException e) {
84                Log.d(TAG, "I/O Error", e);
85            }
86        }
87
88        tv.setText(txt.toString());
89    }
90}
91