OSDN Git Service

Sharing support for albums, bulk operations infrastructure
[android-x86/packages-apps-Gallery2.git] / src / com / android / photos / SelectionManager.java
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 interface Client {
45         public void setSelectionManager(SelectionManager manager);
46     }
47
48     public SelectionManager(Activity activity) {
49         mActivity = activity;
50         if (ApiHelper.AT_LEAST_16) {
51             mNfcAdapter = NfcAdapter.getDefaultAdapter(mActivity);
52             mNfcAdapter.setBeamPushUrisCallback(new CreateBeamUrisCallback() {
53                 @Override
54                 public Uri[] createBeamUris(NfcEvent arg0) {
55                  // This will have been preceded by a call to onItemSelectedStateChange
56                     if (mCachedShareableUris == null) return null;
57                     return mCachedShareableUris.toArray(
58                             new Uri[mCachedShareableUris.size()]);
59                 }
60             }, mActivity);
61         }
62     }
63
64     public void setSelectedUriSource(SelectedUriSource source) {
65         mUriSource = source;
66     }
67
68     private int mSelectedTotalCount = 0;
69     private int mSelectedShareableCount = 0;
70     private int mSelectedShareableImageCount = 0;
71     private int mSelectedShareableVideoCount = 0;
72     private int mSelectedDeletableCount = 0;
73
74     private ArrayList<Uri> mCachedShareableUris = null;
75
76     public void onItemSelectedStateChanged(ShareActionProvider share,
77             int itemType, int itemSupportedOperations, boolean selected) {
78         int increment = selected ? 1 : -1;
79
80         mSelectedTotalCount += increment;
81         mCachedShareableUris = null;
82
83         if ((itemSupportedOperations & MediaObject.SUPPORT_DELETE) > 0) {
84             mSelectedDeletableCount += increment;
85         }
86         if ((itemSupportedOperations & MediaObject.SUPPORT_SHARE) > 0) {
87             mSelectedShareableCount += increment;
88             if (itemType == FileColumns.MEDIA_TYPE_IMAGE) {
89                 mSelectedShareableImageCount += increment;
90             } else if (itemType == FileColumns.MEDIA_TYPE_VIDEO) {
91                 mSelectedShareableVideoCount += increment;
92             }
93         }
94
95         mShareIntent.removeExtra(Intent.EXTRA_STREAM);
96         if (mSelectedShareableCount == 0) {
97             mShareIntent.setAction(null).setType(null);
98         } else if (mSelectedShareableCount >= 1) {
99             mCachedShareableUris = mUriSource.getSelectedShareableUris();
100             if (mCachedShareableUris.size() == 0) {
101                 mShareIntent.setAction(null).setType(null);
102             } else {
103                 if (mSelectedShareableImageCount == mSelectedShareableCount) {
104                     mShareIntent.setType(GalleryUtils.MIME_TYPE_IMAGE);
105                 } else if (mSelectedShareableVideoCount == mSelectedShareableCount) {
106                     mShareIntent.setType(GalleryUtils.MIME_TYPE_VIDEO);
107                 } else {
108                     mShareIntent.setType(GalleryUtils.MIME_TYPE_ALL);
109                 }
110                 if (mCachedShareableUris.size() == 1) {
111                     mShareIntent.setAction(Intent.ACTION_SEND);
112                     mShareIntent.putExtra(Intent.EXTRA_STREAM, mCachedShareableUris.get(0));
113                 } else {
114                     mShareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
115                     mShareIntent.putExtra(Intent.EXTRA_STREAM, mCachedShareableUris);
116                 }
117             }
118         }
119         share.setShareIntent(mShareIntent);
120
121         // TODO update editability, etc.
122     }
123
124     public int getSupportedOperations() {
125         if (mSelectedTotalCount == 0) {
126             return 0;
127         }
128         int supported = 0;
129         if (mSelectedDeletableCount == mSelectedTotalCount) {
130             supported |= MediaObject.SUPPORT_DELETE;
131         }
132         if (mSelectedShareableCount > 0) {
133             supported |= MediaObject.SUPPORT_SHARE;
134         }
135         return supported;
136     }
137
138     public void onClearSelection() {
139         mSelectedTotalCount = 0;
140         mSelectedShareableCount = 0;
141         mSelectedShareableImageCount = 0;
142         mSelectedShareableVideoCount = 0;
143         mSelectedDeletableCount = 0;
144         mCachedShareableUris = null;
145         mShareIntent.removeExtra(Intent.EXTRA_STREAM);
146         mShareIntent.setAction(null).setType(null);
147     }
148 }