2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.photos;
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;
28 import com.android.gallery3d.common.ApiHelper;
29 import com.android.gallery3d.data.MediaObject;
30 import com.android.gallery3d.util.GalleryUtils;
32 import java.util.ArrayList;
34 public class SelectionManager {
35 private Activity mActivity;
36 private NfcAdapter mNfcAdapter;
37 private SelectedUriSource mUriSource;
38 private Intent mShareIntent = new Intent();
40 public interface SelectedUriSource {
41 public ArrayList<Uri> getSelectedShareableUris();
44 public interface Client {
45 public void setSelectionManager(SelectionManager manager);
48 public SelectionManager(Activity activity) {
50 if (ApiHelper.AT_LEAST_16) {
51 mNfcAdapter = NfcAdapter.getDefaultAdapter(mActivity);
52 mNfcAdapter.setBeamPushUrisCallback(new CreateBeamUrisCallback() {
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()]);
64 public void setSelectedUriSource(SelectedUriSource source) {
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;
74 private ArrayList<Uri> mCachedShareableUris = null;
76 public void onItemSelectedStateChanged(ShareActionProvider share,
77 int itemType, int itemSupportedOperations, boolean selected) {
78 int increment = selected ? 1 : -1;
80 mSelectedTotalCount += increment;
81 mCachedShareableUris = null;
83 if ((itemSupportedOperations & MediaObject.SUPPORT_DELETE) > 0) {
84 mSelectedDeletableCount += increment;
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;
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);
103 if (mSelectedShareableImageCount == mSelectedShareableCount) {
104 mShareIntent.setType(GalleryUtils.MIME_TYPE_IMAGE);
105 } else if (mSelectedShareableVideoCount == mSelectedShareableCount) {
106 mShareIntent.setType(GalleryUtils.MIME_TYPE_VIDEO);
108 mShareIntent.setType(GalleryUtils.MIME_TYPE_ALL);
110 if (mCachedShareableUris.size() == 1) {
111 mShareIntent.setAction(Intent.ACTION_SEND);
112 mShareIntent.putExtra(Intent.EXTRA_STREAM, mCachedShareableUris.get(0));
114 mShareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
115 mShareIntent.putExtra(Intent.EXTRA_STREAM, mCachedShareableUris);
119 share.setShareIntent(mShareIntent);
121 // TODO update editability, etc.
124 public int getSupportedOperations() {
125 if (mSelectedTotalCount == 0) {
129 if (mSelectedDeletableCount == mSelectedTotalCount) {
130 supported |= MediaObject.SUPPORT_DELETE;
132 if (mSelectedShareableCount > 0) {
133 supported |= MediaObject.SUPPORT_SHARE;
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);