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.provider.MediaStore.Files.FileColumns; 23 import android.widget.ShareActionProvider; 24 25 import com.android.gallery3d.data.MediaObject; 26 import com.android.gallery3d.util.GalleryUtils; 27 28 import java.util.ArrayList; 29 30 public class SelectionManager { 31 private Activity mActivity; 32 private SelectedUriSource mUriSource; 33 private Intent mShareIntent = new Intent(); 34 35 public interface SelectedUriSource { getSelectedShareableUris()36 public ArrayList<Uri> getSelectedShareableUris(); 37 } 38 SelectionManager(Activity activity)39 public SelectionManager(Activity activity) { 40 mActivity = activity; 41 } 42 setSelectedUriSource(SelectedUriSource source)43 public void setSelectedUriSource(SelectedUriSource source) { 44 mUriSource = source; 45 } 46 47 private int mSelectedTotalCount = 0; 48 private int mSelectedShareableCount = 0; 49 private int mSelectedShareableImageCount = 0; 50 private int mSelectedShareableVideoCount = 0; 51 private int mSelectedDeletableCount = 0; 52 private int mSelectedEditableCount = 0; 53 private int mSelectedCroppableCount = 0; 54 private int mSelectedSetableCount = 0; 55 private int mSelectedTrimmableCount = 0; 56 private int mSelectedMuteableCount = 0; 57 58 private ArrayList<Uri> mCachedShareableUris = null; 59 onItemSelectedStateChanged(ShareActionProvider share, int itemType, int itemSupportedOperations, boolean selected)60 public void onItemSelectedStateChanged(ShareActionProvider share, 61 int itemType, int itemSupportedOperations, boolean selected) { 62 int increment = selected ? 1 : -1; 63 64 mSelectedTotalCount += increment; 65 mCachedShareableUris = null; 66 67 if ((itemSupportedOperations & MediaObject.SUPPORT_DELETE) > 0) { 68 mSelectedDeletableCount += increment; 69 } 70 if ((itemSupportedOperations & MediaObject.SUPPORT_EDIT) > 0) { 71 mSelectedEditableCount += increment; 72 } 73 if ((itemSupportedOperations & MediaObject.SUPPORT_CROP) > 0) { 74 mSelectedCroppableCount += increment; 75 } 76 if ((itemSupportedOperations & MediaObject.SUPPORT_TRIM) > 0) { 77 mSelectedTrimmableCount += increment; 78 } 79 if ((itemSupportedOperations & MediaObject.SUPPORT_MUTE) > 0) { 80 mSelectedMuteableCount += increment; 81 } 82 if ((itemSupportedOperations & MediaObject.SUPPORT_SHARE) > 0) { 83 mSelectedShareableCount += increment; 84 if (itemType == FileColumns.MEDIA_TYPE_IMAGE) { 85 mSelectedShareableImageCount += increment; 86 } else if (itemType == FileColumns.MEDIA_TYPE_VIDEO) { 87 mSelectedShareableVideoCount += increment; 88 } 89 } 90 91 mShareIntent.removeExtra(Intent.EXTRA_STREAM); 92 if (mSelectedShareableCount == 0) { 93 mShareIntent.setAction(null).setType(null); 94 } else if (mSelectedShareableCount >= 1) { 95 mCachedShareableUris = mUriSource.getSelectedShareableUris(); 96 if (mCachedShareableUris.size() == 0) { 97 mShareIntent.setAction(null).setType(null); 98 } else { 99 if (mSelectedShareableImageCount == mSelectedShareableCount) { 100 mShareIntent.setType(GalleryUtils.MIME_TYPE_IMAGE); 101 } else if (mSelectedShareableVideoCount == mSelectedShareableCount) { 102 mShareIntent.setType(GalleryUtils.MIME_TYPE_VIDEO); 103 } else { 104 mShareIntent.setType(GalleryUtils.MIME_TYPE_ALL); 105 } 106 if (mCachedShareableUris.size() == 1) { 107 mShareIntent.setAction(Intent.ACTION_SEND); 108 mShareIntent.putExtra(Intent.EXTRA_STREAM, mCachedShareableUris.get(0)); 109 } else { 110 mShareIntent.setAction(Intent.ACTION_SEND_MULTIPLE); 111 mShareIntent.putExtra(Intent.EXTRA_STREAM, mCachedShareableUris); 112 } 113 } 114 } 115 share.setShareIntent(mShareIntent); 116 } 117 getSupportedOperations()118 public int getSupportedOperations() { 119 if (mSelectedTotalCount == 0) { 120 return 0; 121 } 122 int supported = 0; 123 if (mSelectedTotalCount == 1) { 124 if (mSelectedCroppableCount == 1) { 125 supported |= MediaObject.SUPPORT_CROP; 126 } 127 if (mSelectedEditableCount == 1) { 128 supported |= MediaObject.SUPPORT_EDIT; 129 } 130 if (mSelectedTrimmableCount == 1) { 131 supported |= MediaObject.SUPPORT_TRIM; 132 } 133 if (mSelectedMuteableCount == 1) { 134 supported |= MediaObject.SUPPORT_MUTE; 135 } 136 } 137 if (mSelectedDeletableCount == mSelectedTotalCount) { 138 supported |= MediaObject.SUPPORT_DELETE; 139 } 140 if (mSelectedShareableCount > 0) { 141 supported |= MediaObject.SUPPORT_SHARE; 142 } 143 return supported; 144 } 145 onClearSelection()146 public void onClearSelection() { 147 mSelectedTotalCount = 0; 148 mSelectedShareableCount = 0; 149 mSelectedShareableImageCount = 0; 150 mSelectedShareableVideoCount = 0; 151 mSelectedDeletableCount = 0; 152 mSelectedEditableCount = 0; 153 mSelectedCroppableCount = 0; 154 mSelectedSetableCount = 0; 155 mSelectedTrimmableCount = 0; 156 mSelectedMuteableCount = 0; 157 mCachedShareableUris = null; 158 mShareIntent.removeExtra(Intent.EXTRA_STREAM); 159 mShareIntent.setAction(null).setType(null); 160 } 161 } 162