• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
getSelectedShareableUris()41         public ArrayList<Uri> getSelectedShareableUris();
42     }
43 
SelectionManager(Activity activity)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 
setSelectedUriSource(SelectedUriSource source)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 
onItemSelectedStateChanged(ShareActionProvider share, int itemType, int itemSupportedOperations, boolean selected)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_TRIM) > 0) {
94             mSelectedTrimmableCount += increment;
95         }
96         if ((itemSupportedOperations & MediaObject.SUPPORT_MUTE) > 0) {
97             mSelectedMuteableCount += increment;
98         }
99         if ((itemSupportedOperations & MediaObject.SUPPORT_SHARE) > 0) {
100             mSelectedShareableCount += increment;
101             if (itemType == FileColumns.MEDIA_TYPE_IMAGE) {
102                 mSelectedShareableImageCount += increment;
103             } else if (itemType == FileColumns.MEDIA_TYPE_VIDEO) {
104                 mSelectedShareableVideoCount += increment;
105             }
106         }
107 
108         mShareIntent.removeExtra(Intent.EXTRA_STREAM);
109         if (mSelectedShareableCount == 0) {
110             mShareIntent.setAction(null).setType(null);
111         } else if (mSelectedShareableCount >= 1) {
112             mCachedShareableUris = mUriSource.getSelectedShareableUris();
113             if (mCachedShareableUris.size() == 0) {
114                 mShareIntent.setAction(null).setType(null);
115             } else {
116                 if (mSelectedShareableImageCount == mSelectedShareableCount) {
117                     mShareIntent.setType(GalleryUtils.MIME_TYPE_IMAGE);
118                 } else if (mSelectedShareableVideoCount == mSelectedShareableCount) {
119                     mShareIntent.setType(GalleryUtils.MIME_TYPE_VIDEO);
120                 } else {
121                     mShareIntent.setType(GalleryUtils.MIME_TYPE_ALL);
122                 }
123                 if (mCachedShareableUris.size() == 1) {
124                     mShareIntent.setAction(Intent.ACTION_SEND);
125                     mShareIntent.putExtra(Intent.EXTRA_STREAM, mCachedShareableUris.get(0));
126                 } else {
127                     mShareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
128                     mShareIntent.putExtra(Intent.EXTRA_STREAM, mCachedShareableUris);
129                 }
130             }
131         }
132         share.setShareIntent(mShareIntent);
133     }
134 
getSupportedOperations()135     public int getSupportedOperations() {
136         if (mSelectedTotalCount == 0) {
137             return 0;
138         }
139         int supported = 0;
140         if (mSelectedTotalCount == 1) {
141             if (mSelectedCroppableCount == 1) {
142                 supported |= MediaObject.SUPPORT_CROP;
143             }
144             if (mSelectedEditableCount == 1) {
145                 supported |= MediaObject.SUPPORT_EDIT;
146             }
147             if (mSelectedTrimmableCount == 1) {
148                 supported |= MediaObject.SUPPORT_TRIM;
149             }
150             if (mSelectedMuteableCount == 1) {
151                 supported |= MediaObject.SUPPORT_MUTE;
152             }
153         }
154         if (mSelectedDeletableCount == mSelectedTotalCount) {
155             supported |= MediaObject.SUPPORT_DELETE;
156         }
157         if (mSelectedShareableCount > 0) {
158             supported |= MediaObject.SUPPORT_SHARE;
159         }
160         return supported;
161     }
162 
onClearSelection()163     public void onClearSelection() {
164         mSelectedTotalCount = 0;
165         mSelectedShareableCount = 0;
166         mSelectedShareableImageCount = 0;
167         mSelectedShareableVideoCount = 0;
168         mSelectedDeletableCount = 0;
169         mSelectedEditableCount = 0;
170         mSelectedCroppableCount = 0;
171         mSelectedSetableCount = 0;
172         mSelectedTrimmableCount = 0;
173         mSelectedMuteableCount = 0;
174         mCachedShareableUris = null;
175         mShareIntent.removeExtra(Intent.EXTRA_STREAM);
176         mShareIntent.setAction(null).setType(null);
177     }
178 }
179