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.apis.app;
     18 
     19 import android.app.Activity;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.net.Uri;
     23 import android.os.Bundle;
     24 import android.view.Menu;
     25 import android.view.MenuItem;
     26 import android.widget.ShareActionProvider;
     27 
     28 import com.example.android.apis.R;
     29 
     30 import java.io.FileNotFoundException;
     31 import java.io.FileOutputStream;
     32 import java.io.IOException;
     33 import java.io.InputStream;
     34 
     35 /**
     36  * This activity demonstrates how to use an {@link android.view.ActionProvider}
     37  * for adding functionality to the Action Bar. In particular this demo is adding
     38  * a menu item with ShareActionProvider as its action provider. The
     39  * ShareActionProvider is responsible for managing the UI for sharing actions.
     40  */
     41 public class ActionBarShareActionProviderActivity extends Activity {
     42 
     43     private static final String SHARED_FILE_NAME = "shared.png";
     44 
     45     @Override
     46     public void onCreate(Bundle savedInstanceState) {
     47         super.onCreate(savedInstanceState);
     48         copyPrivateRawResuorceToPubliclyAccessibleFile();
     49     }
     50 
     51     @Override
     52     public boolean onCreateOptionsMenu(Menu menu) {
     53         // Inflate your menu.
     54         getMenuInflater().inflate(R.menu.action_bar_share_action_provider, menu);
     55 
     56         // Set file with share history to the provider and set the share intent.
     57         MenuItem actionItem = menu.findItem(R.id.menu_item_share_action_provider_action_bar);
     58         ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
     59         actionProvider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
     60         // Note that you can set/change the intent any time,
     61         // say when the user has selected an image.
     62         actionProvider.setShareIntent(createShareIntent());
     63 
     64         // Set file with share history to the provider and set the share intent.
     65         MenuItem overflowItem = menu.findItem(R.id.menu_item_share_action_provider_overflow);
     66         ShareActionProvider overflowProvider =
     67             (ShareActionProvider) overflowItem.getActionProvider();
     68         overflowProvider.setShareHistoryFileName(
     69             ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
     70         // Note that you can set/change the intent any time,
     71         // say when the user has selected an image.
     72         overflowProvider.setShareIntent(createShareIntent());
     73 
     74         return true;
     75     }
     76 
     77     /**
     78      * Creates a sharing {@link Intent}.
     79      *
     80      * @return The sharing intent.
     81      */
     82     private Intent createShareIntent() {
     83         Intent shareIntent = new Intent(Intent.ACTION_SEND);
     84         shareIntent.setType("image/*");
     85         Uri uri = Uri.fromFile(getFileStreamPath("shared.png"));
     86         shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
     87         return shareIntent;
     88     }
     89 
     90     /**
     91      * Copies a private raw resource content to a publicly readable
     92      * file such that the latter can be shared with other applications.
     93      */
     94     private void copyPrivateRawResuorceToPubliclyAccessibleFile() {
     95         InputStream inputStream = null;
     96         FileOutputStream outputStream = null;
     97         try {
     98             inputStream = getResources().openRawResource(R.raw.robot);
     99             outputStream = openFileOutput(SHARED_FILE_NAME,
    100                     Context.MODE_WORLD_READABLE | Context.MODE_APPEND);
    101             byte[] buffer = new byte[1024];
    102             int length = 0;
    103             try {
    104                 while ((length = inputStream.read(buffer)) > 0){
    105                     outputStream.write(buffer, 0, length);
    106                 }
    107             } catch (IOException ioe) {
    108                 /* ignore */
    109             }
    110         } catch (FileNotFoundException fnfe) {
    111             /* ignore */
    112         } finally {
    113             try {
    114                 inputStream.close();
    115             } catch (IOException ioe) {
    116                /* ignore */
    117             }
    118             try {
    119                 outputStream.close();
    120             } catch (IOException ioe) {
    121                /* ignore */
    122             }
    123         }
    124     }
    125 }
    126