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