Home | History | Annotate | Download | only in photos
      1 /*
      2  * Copyright (C) 2013 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.android.photos;
     18 
     19 import android.app.Activity;
     20 import android.content.Intent;
     21 import android.net.Uri;
     22 import android.nfc.NfcAdapter;
     23 import android.nfc.NfcAdapter.CreateBeamUrisCallback;
     24 import android.nfc.NfcEvent;
     25 import android.provider.MediaStore.Files.FileColumns;
     26 import android.widget.ShareActionProvider;
     27 
     28 import com.android.gallery3d.common.ApiHelper;
     29 import com.android.gallery3d.data.MediaObject;
     30 import com.android.gallery3d.util.GalleryUtils;
     31 
     32 import java.util.ArrayList;
     33 
     34 public class SelectionManager {
     35     private Activity mActivity;
     36     private NfcAdapter mNfcAdapter;
     37     private SelectedUriSource mUriSource;
     38     private Intent mShareIntent = new Intent();
     39 
     40     public interface SelectedUriSource {
     41         public ArrayList<Uri> getSelectedShareableUris();
     42     }
     43 
     44     public SelectionManager(Activity activity) {
     45         mActivity = activity;
     46         if (ApiHelper.AT_LEAST_16) {
     47             mNfcAdapter = NfcAdapter.getDefaultAdapter(mActivity);
     48             mNfcAdapter.setBeamPushUrisCallback(new CreateBeamUrisCallback() {
     49                 @Override
     50                 public Uri[] createBeamUris(NfcEvent arg0) {
     51                  // This will have been preceded by a call to onItemSelectedStateChange
     52                     if (mCachedShareableUris == null) return null;
     53                     return mCachedShareableUris.toArray(
     54                             new Uri[mCachedShareableUris.size()]);
     55                 }
     56             }, mActivity);
     57         }
     58     }
     59 
     60     public void setSelectedUriSource(SelectedUriSource source) {
     61         mUriSource = source;
     62     }
     63 
     64     private int mSelectedTotalCount = 0;
     65     private int mSelectedShareableCount = 0;
     66     private int mSelectedShareableImageCount = 0;
     67     private int mSelectedShareableVideoCount = 0;
     68     private int mSelectedDeletableCount = 0;
     69     private int mSelectedEditableCount = 0;
     70     private int mSelectedCroppableCount = 0;
     71     private int mSelectedSetableCount = 0;
     72     private int mSelectedTrimmableCount = 0;
     73     private int mSelectedMuteableCount = 0;
     74 
     75     private ArrayList<Uri> mCachedShareableUris = null;
     76 
     77     public void onItemSelectedStateChanged(ShareActionProvider share,
     78             int itemType, int itemSupportedOperations, boolean selected) {
     79         int increment = selected ? 1 : -1;
     80 
     81         mSelectedTotalCount += increment;
     82         mCachedShareableUris = null;
     83 
     84         if ((itemSupportedOperations & MediaObject.SUPPORT_DELETE) > 0) {
     85             mSelectedDeletableCount += increment;
     86         }
     87         if ((itemSupportedOperations & MediaObject.SUPPORT_EDIT) > 0) {
     88             mSelectedEditableCount += increment;
     89         }
     90         if ((itemSupportedOperations & MediaObject.SUPPORT_CROP) > 0) {
     91             mSelectedCroppableCount += increment;
     92         }
     93         if ((itemSupportedOperations & MediaObject.SUPPORT_SETAS) > 0) {
     94             mSelectedSetableCount += increment;
     95         }
     96         if ((itemSupportedOperations & MediaObject.SUPPORT_TRIM) > 0) {
     97             mSelectedTrimmableCount += increment;
     98         }
     99         if ((itemSupportedOperations & MediaObject.SUPPORT_MUTE) > 0) {
    100             mSelectedMuteableCount += increment;
    101         }
    102         if ((itemSupportedOperations & MediaObject.SUPPORT_SHARE) > 0) {
    103             mSelectedShareableCount += increment;
    104             if (itemType == FileColumns.MEDIA_TYPE_IMAGE) {
    105                 mSelectedShareableImageCount += increment;
    106             } else if (itemType == FileColumns.MEDIA_TYPE_VIDEO) {
    107                 mSelectedShareableVideoCount += increment;
    108             }
    109         }
    110 
    111         mShareIntent.removeExtra(Intent.EXTRA_STREAM);
    112         if (mSelectedShareableCount == 0) {
    113             mShareIntent.setAction(null).setType(null);
    114         } else if (mSelectedShareableCount >= 1) {
    115             mCachedShareableUris = mUriSource.getSelectedShareableUris();
    116             if (mCachedShareableUris.size() == 0) {
    117                 mShareIntent.setAction(null).setType(null);
    118             } else {
    119                 if (mSelectedShareableImageCount == mSelectedShareableCount) {
    120                     mShareIntent.setType(GalleryUtils.MIME_TYPE_IMAGE);
    121                 } else if (mSelectedShareableVideoCount == mSelectedShareableCount) {
    122                     mShareIntent.setType(GalleryUtils.MIME_TYPE_VIDEO);
    123                 } else {
    124                     mShareIntent.setType(GalleryUtils.MIME_TYPE_ALL);
    125                 }
    126                 if (mCachedShareableUris.size() == 1) {
    127                     mShareIntent.setAction(Intent.ACTION_SEND);
    128                     mShareIntent.putExtra(Intent.EXTRA_STREAM, mCachedShareableUris.get(0));
    129                 } else {
    130                     mShareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
    131                     mShareIntent.putExtra(Intent.EXTRA_STREAM, mCachedShareableUris);
    132                 }
    133             }
    134         }
    135         share.setShareIntent(mShareIntent);
    136     }
    137 
    138     public int getSupportedOperations() {
    139         if (mSelectedTotalCount == 0) {
    140             return 0;
    141         }
    142         int supported = 0;
    143         if (mSelectedTotalCount == 1) {
    144             if (mSelectedCroppableCount == 1) {
    145                 supported |= MediaObject.SUPPORT_CROP;
    146             }
    147             if (mSelectedEditableCount == 1) {
    148                 supported |= MediaObject.SUPPORT_EDIT;
    149             }
    150             if (mSelectedSetableCount == 1) {
    151                 supported |= MediaObject.SUPPORT_SETAS;
    152             }
    153             if (mSelectedTrimmableCount == 1) {
    154                 supported |= MediaObject.SUPPORT_TRIM;
    155             }
    156             if (mSelectedMuteableCount == 1) {
    157                 supported |= MediaObject.SUPPORT_MUTE;
    158             }
    159         }
    160         if (mSelectedDeletableCount == mSelectedTotalCount) {
    161             supported |= MediaObject.SUPPORT_DELETE;
    162         }
    163         if (mSelectedShareableCount > 0) {
    164             supported |= MediaObject.SUPPORT_SHARE;
    165         }
    166         return supported;
    167     }
    168 
    169     public void onClearSelection() {
    170         mSelectedTotalCount = 0;
    171         mSelectedShareableCount = 0;
    172         mSelectedShareableImageCount = 0;
    173         mSelectedShareableVideoCount = 0;
    174         mSelectedDeletableCount = 0;
    175         mSelectedEditableCount = 0;
    176         mSelectedCroppableCount = 0;
    177         mSelectedSetableCount = 0;
    178         mSelectedTrimmableCount = 0;
    179         mSelectedMuteableCount = 0;
    180         mCachedShareableUris = null;
    181         mShareIntent.removeExtra(Intent.EXTRA_STREAM);
    182         mShareIntent.setAction(null).setType(null);
    183     }
    184 }
    185