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