128a5834185ace02fed8b61989532a6516f30f04eAdam Powell/*
228a5834185ace02fed8b61989532a6516f30f04eAdam Powell * Copyright (C) 2011 The Android Open Source Project
328a5834185ace02fed8b61989532a6516f30f04eAdam Powell *
428a5834185ace02fed8b61989532a6516f30f04eAdam Powell * Licensed under the Apache License, Version 2.0 (the "License");
528a5834185ace02fed8b61989532a6516f30f04eAdam Powell * you may not use this file except in compliance with the License.
628a5834185ace02fed8b61989532a6516f30f04eAdam Powell * You may obtain a copy of the License at
728a5834185ace02fed8b61989532a6516f30f04eAdam Powell *
828a5834185ace02fed8b61989532a6516f30f04eAdam Powell *      http://www.apache.org/licenses/LICENSE-2.0
928a5834185ace02fed8b61989532a6516f30f04eAdam Powell *
1028a5834185ace02fed8b61989532a6516f30f04eAdam Powell * Unless required by applicable law or agreed to in writing, software
1128a5834185ace02fed8b61989532a6516f30f04eAdam Powell * distributed under the License is distributed on an "AS IS" BASIS,
1228a5834185ace02fed8b61989532a6516f30f04eAdam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1328a5834185ace02fed8b61989532a6516f30f04eAdam Powell * See the License for the specific language governing permissions and
1428a5834185ace02fed8b61989532a6516f30f04eAdam Powell * limitations under the License.
1528a5834185ace02fed8b61989532a6516f30f04eAdam Powell */
1628a5834185ace02fed8b61989532a6516f30f04eAdam Powell
1728a5834185ace02fed8b61989532a6516f30f04eAdam Powellpackage com.example.android.supportv4.app;
1828a5834185ace02fed8b61989532a6516f30f04eAdam Powell
1928a5834185ace02fed8b61989532a6516f30f04eAdam Powellimport com.example.android.supportv4.R;
2028a5834185ace02fed8b61989532a6516f30f04eAdam Powell
2128a5834185ace02fed8b61989532a6516f30f04eAdam Powellimport android.app.Activity;
2228a5834185ace02fed8b61989532a6516f30f04eAdam Powellimport android.graphics.drawable.Drawable;
2328a5834185ace02fed8b61989532a6516f30f04eAdam Powellimport android.net.Uri;
2428a5834185ace02fed8b61989532a6516f30f04eAdam Powellimport android.os.Bundle;
2528a5834185ace02fed8b61989532a6516f30f04eAdam Powellimport android.support.v4.app.ShareCompat;
2628a5834185ace02fed8b61989532a6516f30f04eAdam Powellimport android.util.Log;
2728a5834185ace02fed8b61989532a6516f30f04eAdam Powellimport android.widget.TextView;
2828a5834185ace02fed8b61989532a6516f30f04eAdam Powell
2928a5834185ace02fed8b61989532a6516f30f04eAdam Powellimport java.io.BufferedReader;
3028a5834185ace02fed8b61989532a6516f30f04eAdam Powellimport java.io.FileNotFoundException;
3128a5834185ace02fed8b61989532a6516f30f04eAdam Powellimport java.io.IOException;
3228a5834185ace02fed8b61989532a6516f30f04eAdam Powellimport java.io.InputStreamReader;
3328a5834185ace02fed8b61989532a6516f30f04eAdam Powell
3428a5834185ace02fed8b61989532a6516f30f04eAdam Powell/**
3528a5834185ace02fed8b61989532a6516f30f04eAdam Powell * This example shows a simple way to handle data shared with your app through the
3628a5834185ace02fed8b61989532a6516f30f04eAdam Powell * use of the support library's ShareCompat features. It will display shared text
3728a5834185ace02fed8b61989532a6516f30f04eAdam Powell * content as well as the application label and icon of the app that shared the content.
3828a5834185ace02fed8b61989532a6516f30f04eAdam Powell */
3928a5834185ace02fed8b61989532a6516f30f04eAdam Powellpublic class SharingReceiverSupport extends Activity {
4028a5834185ace02fed8b61989532a6516f30f04eAdam Powell    private static final String TAG = "SharingReceiverSupport";
4128a5834185ace02fed8b61989532a6516f30f04eAdam Powell    private static final int ICON_SIZE = 32; // dip
4228a5834185ace02fed8b61989532a6516f30f04eAdam Powell
4328a5834185ace02fed8b61989532a6516f30f04eAdam Powell    @Override
4428a5834185ace02fed8b61989532a6516f30f04eAdam Powell    protected void onCreate(Bundle b) {
4528a5834185ace02fed8b61989532a6516f30f04eAdam Powell        super.onCreate(b);
4628a5834185ace02fed8b61989532a6516f30f04eAdam Powell        setContentView(R.layout.sharing_receiver_support);
4728a5834185ace02fed8b61989532a6516f30f04eAdam Powell
4828a5834185ace02fed8b61989532a6516f30f04eAdam Powell        final float density = getResources().getDisplayMetrics().density;
4928a5834185ace02fed8b61989532a6516f30f04eAdam Powell        final int iconSize = (int) (ICON_SIZE * density + 0.5f);
5028a5834185ace02fed8b61989532a6516f30f04eAdam Powell
5128a5834185ace02fed8b61989532a6516f30f04eAdam Powell        ShareCompat.IntentReader intentReader = ShareCompat.IntentReader.from(this);
5228a5834185ace02fed8b61989532a6516f30f04eAdam Powell
5328a5834185ace02fed8b61989532a6516f30f04eAdam Powell        // The following provides attribution for the app that shared the data with us.
54fa2e2acf79d791a90410025daad438968550d18cAlan Viverette        TextView info = findViewById(R.id.app_info);
5528a5834185ace02fed8b61989532a6516f30f04eAdam Powell        Drawable d = intentReader.getCallingActivityIcon();
5628a5834185ace02fed8b61989532a6516f30f04eAdam Powell        d.setBounds(0, 0, iconSize, iconSize);
5728a5834185ace02fed8b61989532a6516f30f04eAdam Powell        info.setCompoundDrawables(d, null, null, null);
5828a5834185ace02fed8b61989532a6516f30f04eAdam Powell        info.setText(intentReader.getCallingApplicationLabel());
5928a5834185ace02fed8b61989532a6516f30f04eAdam Powell
60fa2e2acf79d791a90410025daad438968550d18cAlan Viverette        TextView tv = findViewById(R.id.text);
6128a5834185ace02fed8b61989532a6516f30f04eAdam Powell        StringBuilder txt = new StringBuilder("Received share!\nText was: ");
6228a5834185ace02fed8b61989532a6516f30f04eAdam Powell
6328a5834185ace02fed8b61989532a6516f30f04eAdam Powell        txt.append(intentReader.getText());
6428a5834185ace02fed8b61989532a6516f30f04eAdam Powell        txt.append("\n");
6528a5834185ace02fed8b61989532a6516f30f04eAdam Powell
6628a5834185ace02fed8b61989532a6516f30f04eAdam Powell        txt.append("Streams included:\n");
6728a5834185ace02fed8b61989532a6516f30f04eAdam Powell        final int N = intentReader.getStreamCount();
6828a5834185ace02fed8b61989532a6516f30f04eAdam Powell        for (int i = 0; i < N; i++) {
6928a5834185ace02fed8b61989532a6516f30f04eAdam Powell            Uri uri = intentReader.getStream(i);
7028a5834185ace02fed8b61989532a6516f30f04eAdam Powell            txt.append("Share included stream " + i + ": " + uri + "\n");
7128a5834185ace02fed8b61989532a6516f30f04eAdam Powell            try {
7228a5834185ace02fed8b61989532a6516f30f04eAdam Powell                BufferedReader reader = new BufferedReader(new InputStreamReader(
7328a5834185ace02fed8b61989532a6516f30f04eAdam Powell                        getContentResolver().openInputStream(uri)));
7428a5834185ace02fed8b61989532a6516f30f04eAdam Powell                try {
7528a5834185ace02fed8b61989532a6516f30f04eAdam Powell                    txt.append(reader.readLine() + "\n");
7628a5834185ace02fed8b61989532a6516f30f04eAdam Powell                } catch (IOException e) {
7728a5834185ace02fed8b61989532a6516f30f04eAdam Powell                    Log.e(TAG, "Reading stream threw exception", e);
7828a5834185ace02fed8b61989532a6516f30f04eAdam Powell                } finally {
7928a5834185ace02fed8b61989532a6516f30f04eAdam Powell                    reader.close();
8028a5834185ace02fed8b61989532a6516f30f04eAdam Powell                }
8128a5834185ace02fed8b61989532a6516f30f04eAdam Powell            } catch (FileNotFoundException e) {
8270fc82cd2d35e12c4f41162690505397af2e96acAdam Powell                Log.e(TAG, "File not found from share.", e);
8370fc82cd2d35e12c4f41162690505397af2e96acAdam Powell            } catch (IOException e) {
8470fc82cd2d35e12c4f41162690505397af2e96acAdam Powell                Log.d(TAG, "I/O Error", e);
8528a5834185ace02fed8b61989532a6516f30f04eAdam Powell            }
8628a5834185ace02fed8b61989532a6516f30f04eAdam Powell        }
8728a5834185ace02fed8b61989532a6516f30f04eAdam Powell
8828a5834185ace02fed8b61989532a6516f30f04eAdam Powell        tv.setText(txt.toString());
8928a5834185ace02fed8b61989532a6516f30f04eAdam Powell    }
9028a5834185ace02fed8b61989532a6516f30f04eAdam Powell}
91