Home | History | Annotate | Download | only in app
      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 
     17 package com.example.android.supportv4.app;
     18 
     19 import android.app.Activity;
     20 import android.net.Uri;
     21 import android.os.Bundle;
     22 import android.support.v4.app.ShareCompat;
     23 import android.view.Menu;
     24 import android.view.MenuItem;
     25 import android.view.View;
     26 
     27 import com.example.android.supportv4.R;
     28 import com.example.android.supportv4.content.SharingSupportProvider;
     29 
     30 import java.io.FileNotFoundException;
     31 import java.io.FileWriter;
     32 import java.io.IOException;
     33 
     34 /**
     35  * This example illustrates the use of the ShareCompat feature of the support library.
     36  * ShareCompat offers several pieces of functionality to assist in sharing content between
     37  * apps and is especially suited for sharing content to social apps that the user has installed.
     38  *
     39  * <p>Two other classes are relevant to this code sample: {@link SharingReceiverSupport} is
     40  * an activity that has been configured to receive ACTION_SEND and ACTION_SEND_MULTIPLE
     41  * sharing intents with a type of text/plain. It provides an example of writing a sharing
     42  * target using ShareCompat features. {@link SharingSupportProvider} is a simple
     43  * {@link android.content.ContentProvider} that provides access to two text files
     44  * created by this app to share as content streams.</p>
     45  */
     46 public class SharingSupport extends Activity {
     47     @Override
     48     protected void onCreate(Bundle b) {
     49         super.onCreate(b);
     50         setContentView(R.layout.sharing_support);
     51     }
     52 
     53     @Override
     54     public boolean onCreateOptionsMenu(Menu menu) {
     55         ShareCompat.IntentBuilder b = ShareCompat.IntentBuilder.from(this);
     56         b.setType("text/plain").setText("Share from menu");
     57         MenuItem item = menu.add("Share");
     58         ShareCompat.configureMenuItem(item, b);
     59         item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
     60         return true;
     61     }
     62 
     63     public void onShareTextClick(View v) {
     64         ShareCompat.IntentBuilder.from(this)
     65                 .setType("text/plain")
     66                 .setText("I'm sharing!")
     67                 .startChooser();
     68     }
     69 
     70     public void onShareFileClick(View v) {
     71         try {
     72             // This file will be accessed by the target of the share through
     73             // the ContentProvider SharingSupportProvider.
     74             FileWriter fw = new FileWriter(getFilesDir() + "/foo.txt");
     75             fw.write("This is a file share");
     76             fw.close();
     77 
     78             ShareCompat.IntentBuilder.from(this)
     79                     .setType("text/plain")
     80                     .setStream(Uri.parse(SharingSupportProvider.CONTENT_URI + "/foo.txt"))
     81                     .startChooser();
     82         } catch (FileNotFoundException e) {
     83             e.printStackTrace();
     84         } catch (IOException e) {
     85             e.printStackTrace();
     86         }
     87     }
     88 
     89     public void onShareMultipleFileClick(View v) {
     90         try {
     91             // These files will be accessed by the target of the share through
     92             // the ContentProvider SharingSupportProvider.
     93             FileWriter fw = new FileWriter(getFilesDir() + "/foo.txt");
     94             fw.write("This is a file share");
     95             fw.close();
     96 
     97             fw = new FileWriter(getFilesDir() + "/bar.txt");
     98             fw.write("This is another file share");
     99             fw.close();
    100 
    101             ShareCompat.IntentBuilder.from(this)
    102                     .setType("text/plain")
    103                     .addStream(Uri.parse(SharingSupportProvider.CONTENT_URI + "/foo.txt"))
    104                     .addStream(Uri.parse(SharingSupportProvider.CONTENT_URI + "/bar.txt"))
    105                     .startChooser();
    106         } catch (FileNotFoundException e) {
    107             e.printStackTrace();
    108         } catch (IOException e) {
    109             e.printStackTrace();
    110         }
    111     }
    112 }
    113