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 Powellimport com.example.android.supportv4.content.SharingSupportProvider;
2128a5834185ace02fed8b61989532a6516f30f04eAdam Powell
2228a5834185ace02fed8b61989532a6516f30f04eAdam Powellimport android.app.Activity;
2328a5834185ace02fed8b61989532a6516f30f04eAdam Powellimport android.net.Uri;
2428a5834185ace02fed8b61989532a6516f30f04eAdam Powellimport android.os.Bundle;
2528a5834185ace02fed8b61989532a6516f30f04eAdam Powellimport android.support.v4.app.ShareCompat;
2628a5834185ace02fed8b61989532a6516f30f04eAdam Powellimport android.support.v4.view.MenuItemCompat;
2728a5834185ace02fed8b61989532a6516f30f04eAdam Powellimport android.view.Menu;
2828a5834185ace02fed8b61989532a6516f30f04eAdam Powellimport android.view.MenuItem;
2928a5834185ace02fed8b61989532a6516f30f04eAdam Powellimport android.view.View;
3028a5834185ace02fed8b61989532a6516f30f04eAdam Powell
3128a5834185ace02fed8b61989532a6516f30f04eAdam Powellimport java.io.FileNotFoundException;
3228a5834185ace02fed8b61989532a6516f30f04eAdam Powellimport java.io.FileWriter;
3328a5834185ace02fed8b61989532a6516f30f04eAdam Powellimport java.io.IOException;
3428a5834185ace02fed8b61989532a6516f30f04eAdam Powell
3528a5834185ace02fed8b61989532a6516f30f04eAdam Powell/**
3628a5834185ace02fed8b61989532a6516f30f04eAdam Powell * This example illustrates the use of the ShareCompat feature of the support library.
3728a5834185ace02fed8b61989532a6516f30f04eAdam Powell * ShareCompat offers several pieces of functionality to assist in sharing content between
3828a5834185ace02fed8b61989532a6516f30f04eAdam Powell * apps and is especially suited for sharing content to social apps that the user has installed.
3928a5834185ace02fed8b61989532a6516f30f04eAdam Powell *
4028a5834185ace02fed8b61989532a6516f30f04eAdam Powell * <p>Two other classes are relevant to this code sample: {@link SharingReceiverSupport} is
4128a5834185ace02fed8b61989532a6516f30f04eAdam Powell * an activity that has been configured to receive ACTION_SEND and ACTION_SEND_MULTIPLE
4228a5834185ace02fed8b61989532a6516f30f04eAdam Powell * sharing intents with a type of text/plain. It provides an example of writing a sharing
4328a5834185ace02fed8b61989532a6516f30f04eAdam Powell * target using ShareCompat features. {@link SharingSupportProvider} is a simple
4428a5834185ace02fed8b61989532a6516f30f04eAdam Powell * {@link android.content.ContentProvider} that provides access to two text files
4528a5834185ace02fed8b61989532a6516f30f04eAdam Powell * created by this app to share as content streams.</p>
4628a5834185ace02fed8b61989532a6516f30f04eAdam Powell */
4728a5834185ace02fed8b61989532a6516f30f04eAdam Powellpublic class SharingSupport extends Activity {
4828a5834185ace02fed8b61989532a6516f30f04eAdam Powell    @Override
4928a5834185ace02fed8b61989532a6516f30f04eAdam Powell    protected void onCreate(Bundle b) {
5028a5834185ace02fed8b61989532a6516f30f04eAdam Powell        super.onCreate(b);
5128a5834185ace02fed8b61989532a6516f30f04eAdam Powell        setContentView(R.layout.sharing_support);
5228a5834185ace02fed8b61989532a6516f30f04eAdam Powell    }
5328a5834185ace02fed8b61989532a6516f30f04eAdam Powell
5428a5834185ace02fed8b61989532a6516f30f04eAdam Powell    @Override
5528a5834185ace02fed8b61989532a6516f30f04eAdam Powell    public boolean onCreateOptionsMenu(Menu menu) {
5628a5834185ace02fed8b61989532a6516f30f04eAdam Powell        ShareCompat.IntentBuilder b = ShareCompat.IntentBuilder.from(this);
5728a5834185ace02fed8b61989532a6516f30f04eAdam Powell        b.setType("text/plain").setText("Share from menu");
5828a5834185ace02fed8b61989532a6516f30f04eAdam Powell        MenuItem item = menu.add("Share");
5928a5834185ace02fed8b61989532a6516f30f04eAdam Powell        ShareCompat.configureMenuItem(item, b);
6028a5834185ace02fed8b61989532a6516f30f04eAdam Powell        MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
6128a5834185ace02fed8b61989532a6516f30f04eAdam Powell        return true;
6228a5834185ace02fed8b61989532a6516f30f04eAdam Powell    }
6328a5834185ace02fed8b61989532a6516f30f04eAdam Powell
6428a5834185ace02fed8b61989532a6516f30f04eAdam Powell    public void onShareTextClick(View v) {
6528a5834185ace02fed8b61989532a6516f30f04eAdam Powell        ShareCompat.IntentBuilder.from(this)
6628a5834185ace02fed8b61989532a6516f30f04eAdam Powell                .setType("text/plain")
6728a5834185ace02fed8b61989532a6516f30f04eAdam Powell                .setText("I'm sharing!")
688895d250320d796ef2d95881c104a11d81ca415fAdam Powell                .startChooser();
6928a5834185ace02fed8b61989532a6516f30f04eAdam Powell    }
7028a5834185ace02fed8b61989532a6516f30f04eAdam Powell
7128a5834185ace02fed8b61989532a6516f30f04eAdam Powell    public void onShareFileClick(View v) {
7228a5834185ace02fed8b61989532a6516f30f04eAdam Powell        try {
7328a5834185ace02fed8b61989532a6516f30f04eAdam Powell            // This file will be accessed by the target of the share through
7428a5834185ace02fed8b61989532a6516f30f04eAdam Powell            // the ContentProvider SharingSupportProvider.
7528a5834185ace02fed8b61989532a6516f30f04eAdam Powell            FileWriter fw = new FileWriter(getFilesDir() + "/foo.txt");
7628a5834185ace02fed8b61989532a6516f30f04eAdam Powell            fw.write("This is a file share");
7728a5834185ace02fed8b61989532a6516f30f04eAdam Powell            fw.close();
7828a5834185ace02fed8b61989532a6516f30f04eAdam Powell
7928a5834185ace02fed8b61989532a6516f30f04eAdam Powell            ShareCompat.IntentBuilder.from(this)
8028a5834185ace02fed8b61989532a6516f30f04eAdam Powell                    .setType("text/plain")
8128a5834185ace02fed8b61989532a6516f30f04eAdam Powell                    .setStream(Uri.parse(SharingSupportProvider.CONTENT_URI + "/foo.txt"))
828895d250320d796ef2d95881c104a11d81ca415fAdam Powell                    .startChooser();
8328a5834185ace02fed8b61989532a6516f30f04eAdam Powell        } catch (FileNotFoundException e) {
8428a5834185ace02fed8b61989532a6516f30f04eAdam Powell            e.printStackTrace();
8528a5834185ace02fed8b61989532a6516f30f04eAdam Powell        } catch (IOException e) {
8628a5834185ace02fed8b61989532a6516f30f04eAdam Powell            e.printStackTrace();
8728a5834185ace02fed8b61989532a6516f30f04eAdam Powell        }
8828a5834185ace02fed8b61989532a6516f30f04eAdam Powell    }
8928a5834185ace02fed8b61989532a6516f30f04eAdam Powell
9028a5834185ace02fed8b61989532a6516f30f04eAdam Powell    public void onShareMultipleFileClick(View v) {
9128a5834185ace02fed8b61989532a6516f30f04eAdam Powell        try {
9228a5834185ace02fed8b61989532a6516f30f04eAdam Powell            // These files will be accessed by the target of the share through
9328a5834185ace02fed8b61989532a6516f30f04eAdam Powell            // the ContentProvider SharingSupportProvider.
9428a5834185ace02fed8b61989532a6516f30f04eAdam Powell            FileWriter fw = new FileWriter(getFilesDir() + "/foo.txt");
9528a5834185ace02fed8b61989532a6516f30f04eAdam Powell            fw.write("This is a file share");
9628a5834185ace02fed8b61989532a6516f30f04eAdam Powell            fw.close();
9728a5834185ace02fed8b61989532a6516f30f04eAdam Powell
9828a5834185ace02fed8b61989532a6516f30f04eAdam Powell            fw = new FileWriter(getFilesDir() + "/bar.txt");
9928a5834185ace02fed8b61989532a6516f30f04eAdam Powell            fw.write("This is another file share");
10028a5834185ace02fed8b61989532a6516f30f04eAdam Powell            fw.close();
10128a5834185ace02fed8b61989532a6516f30f04eAdam Powell
10228a5834185ace02fed8b61989532a6516f30f04eAdam Powell            ShareCompat.IntentBuilder.from(this)
10328a5834185ace02fed8b61989532a6516f30f04eAdam Powell                    .setType("text/plain")
10428a5834185ace02fed8b61989532a6516f30f04eAdam Powell                    .addStream(Uri.parse(SharingSupportProvider.CONTENT_URI + "/foo.txt"))
10528a5834185ace02fed8b61989532a6516f30f04eAdam Powell                    .addStream(Uri.parse(SharingSupportProvider.CONTENT_URI + "/bar.txt"))
1068895d250320d796ef2d95881c104a11d81ca415fAdam Powell                    .startChooser();
10728a5834185ace02fed8b61989532a6516f30f04eAdam Powell        } catch (FileNotFoundException e) {
10828a5834185ace02fed8b61989532a6516f30f04eAdam Powell            e.printStackTrace();
10928a5834185ace02fed8b61989532a6516f30f04eAdam Powell        } catch (IOException e) {
11028a5834185ace02fed8b61989532a6516f30f04eAdam Powell            e.printStackTrace();
11128a5834185ace02fed8b61989532a6516f30f04eAdam Powell        }
11228a5834185ace02fed8b61989532a6516f30f04eAdam Powell    }
11328a5834185ace02fed8b61989532a6516f30f04eAdam Powell}
114