summaryrefslogtreecommitdiffstats
path: root/src/com/android/photos/MultiChoiceManager.java
blob: 49519ca6315e42596178c45f843df7f40dad4580 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.photos;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.MediaStore.Files.FileColumns;
import android.util.SparseBooleanArray;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.AbsListView.MultiChoiceModeListener;
import android.widget.ShareActionProvider;
import android.widget.ShareActionProvider.OnShareTargetSelectedListener;

import com.android.gallery3d.R;
import com.android.gallery3d.app.TrimVideo;
import com.android.gallery3d.data.MediaObject;
import com.android.gallery3d.filtershow.FilterShowActivity;
import com.android.gallery3d.filtershow.crop.CropActivity;
import com.android.gallery3d.util.GalleryUtils;

import java.util.ArrayList;
import java.util.List;

public class MultiChoiceManager implements MultiChoiceModeListener,
    OnShareTargetSelectedListener, SelectionManager.SelectedUriSource {

    public interface Provider {
        public MultiChoiceManager getMultiChoiceManager();
    }

    public interface Delegate {
        public SparseBooleanArray getSelectedItemPositions();
        public int getSelectedItemCount();
        public int getItemMediaType(Object item);
        public int getItemSupportedOperations(Object item);
        public ArrayList<Uri> getSubItemUrisForItem(Object item);
        public Uri getItemUri(Object item);
        public Object getItemAtPosition(int position);
        public Object getPathForItemAtPosition(int position);
        public void deleteItemWithPath(Object itemPath);
    }

    private SelectionManager mSelectionManager;
    private ShareActionProvider mShareActionProvider;
    private ActionMode mActionMode;
    private Context mContext;
    private Delegate mDelegate;

    private ArrayList<Uri> mSelectedShareableUrisArray = new ArrayList<Uri>();

    public MultiChoiceManager(Activity activity) {
        mContext = activity;
        mSelectionManager = new SelectionManager(activity);
    }

    public void setDelegate(Delegate delegate) {
        if (mDelegate == delegate) {
            return;
        }
        if (mActionMode != null) {
            mActionMode.finish();
        }
        mDelegate = delegate;
    }

    @Override
    public ArrayList<Uri> getSelectedShareableUris() {
        return mSelectedShareableUrisArray;
    }

    private void updateSelectedTitle(ActionMode mode) {
        int count = mDelegate.getSelectedItemCount();
        mode.setTitle(mContext.getResources().getQuantityString(
                R.plurals.number_of_items_selected, count, count));
    }

    private String getItemMimetype(Object item) {
        int type = mDelegate.getItemMediaType(item);
        if (type == FileColumns.MEDIA_TYPE_IMAGE) {
            return GalleryUtils.MIME_TYPE_IMAGE;
        } else if (type == FileColumns.MEDIA_TYPE_VIDEO) {
            return GalleryUtils.MIME_TYPE_VIDEO;
        } else {
            return GalleryUtils.MIME_TYPE_ALL;
        }
    }

    @Override
    public void onItemCheckedStateChanged(ActionMode mode, int position, long id,
            boolean checked) {
        updateSelectedTitle(mode);
        Object item = mDelegate.getItemAtPosition(position);

        int supported = mDelegate.getItemSupportedOperations(item);

        if ((supported & MediaObject.SUPPORT_SHARE) > 0) {
            ArrayList<Uri> subItems = mDelegate.getSubItemUrisForItem(item);
            if (checked) {
                mSelectedShareableUrisArray.addAll(subItems);
            } else {
                mSelectedShareableUrisArray.removeAll(subItems);
            }
        }

        mSelectionManager.onItemSelectedStateChanged(mShareActionProvider,
                mDelegate.getItemMediaType(item),
                supported,
                checked);
        updateActionItemVisibilities(mode.getMenu(),
                mSelectionManager.getSupportedOperations());
    }

    private void updateActionItemVisibilities(Menu menu, int supportedOperations) {
        MenuItem editItem = menu.findItem(R.id.menu_edit);
        MenuItem deleteItem = menu.findItem(R.id.menu_delete);
        MenuItem shareItem = menu.findItem(R.id.menu_share);
        MenuItem cropItem = menu.findItem(R.id.menu_crop);
        MenuItem trimItem = menu.findItem(R.id.menu_trim);
        MenuItem muteItem = menu.findItem(R.id.menu_mute);
        MenuItem setAsItem = menu.findItem(R.id.menu_set_as);

        editItem.setVisible((supportedOperations & MediaObject.SUPPORT_EDIT) > 0);
        deleteItem.setVisible((supportedOperations & MediaObject.SUPPORT_DELETE) > 0);
        shareItem.setVisible((supportedOperations & MediaObject.SUPPORT_SHARE) > 0);
        cropItem.setVisible((supportedOperations & MediaObject.SUPPORT_CROP) > 0);
        trimItem.setVisible((supportedOperations & MediaObject.SUPPORT_TRIM) > 0);
        muteItem.setVisible((supportedOperations & MediaObject.SUPPORT_MUTE) > 0);
        setAsItem.setVisible((supportedOperations & MediaObject.SUPPORT_SETAS) > 0);
    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        mSelectionManager.setSelectedUriSource(this);
        mActionMode = mode;
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.gallery_multiselect, menu);
        MenuItem menuItem = menu.findItem(R.id.menu_share);
        mShareActionProvider = (ShareActionProvider) menuItem.getActionProvider();
        mShareActionProvider.setOnShareTargetSelectedListener(this);
        updateSelectedTitle(mode);
        return true;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
        // onDestroyActionMode gets called when the share target was selected,
        // but apparently before the ArrayList is serialized in the intent
        // so we can't clear the old one here.
        mSelectedShareableUrisArray = new ArrayList<Uri>();
        mSelectionManager.onClearSelection();
        mSelectionManager.setSelectedUriSource(null);
        mShareActionProvider = null;
        mActionMode = null;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        updateSelectedTitle(mode);
        return false;
    }

    @Override
    public boolean onShareTargetSelected(ShareActionProvider provider, Intent intent) {
        mActionMode.finish();
        return false;
    }

    private static class BulkDeleteTask extends AsyncTask<Void, Void, Void> {
        private Delegate mDelegate;
        private List<Object> mPaths;

        public BulkDeleteTask(Delegate delegate, List<Object> paths) {
            mDelegate = delegate;
            mPaths = paths;
        }

        @Override
        protected Void doInBackground(Void... ignored) {
            for (Object path : mPaths) {
                mDelegate.deleteItemWithPath(path);
            }
            return null;
        }
    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        int actionItemId = item.getItemId();
        switch (actionItemId) {
            case R.id.menu_delete:
                BulkDeleteTask deleteTask = new BulkDeleteTask(mDelegate,
                        getPathsForSelectedItems());
                deleteTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                mode.finish();
                return true;
            case R.id.menu_edit:
            case R.id.menu_crop:
            case R.id.menu_trim:
            case R.id.menu_mute:
            case R.id.menu_set_as:
                singleItemAction(getSelectedItem(), actionItemId);
                mode.finish();
                return true;
            default:
                return false;
        }
    }

    private void singleItemAction(Object item, int actionItemId) {
        Intent intent = new Intent();
        String mime = getItemMimetype(item);
        Uri uri = mDelegate.getItemUri(item);
        switch (actionItemId) {
            case R.id.menu_edit:
                intent.setDataAndType(uri, mime)
                      .setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                      .setAction(Intent.ACTION_EDIT);
                mContext.startActivity(Intent.createChooser(intent, null));
                return;
            case R.id.menu_crop:
                intent.setDataAndType(uri, mime)
                      .setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                      .setAction(CropActivity.CROP_ACTION)
                      .setClass(mContext, FilterShowActivity.class);
                mContext.startActivity(intent);
                return;
            case R.id.menu_trim:
                intent.setData(uri)
                      .setClass(mContext, TrimVideo.class);
                mContext.startActivity(intent);
                return;
            case R.id.menu_mute:
                /* TODO need a way to get the file path of an item
                MuteVideo muteVideo = new MuteVideo(filePath,
                        uri, (Activity) mContext);
                muteVideo.muteInBackground();
                */
                return;
            case R.id.menu_set_as:
                intent.setDataAndType(uri, mime)
                      .setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                      .setAction(Intent.ACTION_ATTACH_DATA)
                      .putExtra("mimeType", mime);
                mContext.startActivity(Intent.createChooser(
                        intent, mContext.getString(R.string.set_as)));
                return;
            default:
                return;
        }
    }

    private List<Object> getPathsForSelectedItems() {
        List<Object> paths = new ArrayList<Object>();
        SparseBooleanArray selected = mDelegate.getSelectedItemPositions();
        for (int i = 0; i < selected.size(); i++) {
            if (selected.valueAt(i)) {
                paths.add(mDelegate.getPathForItemAtPosition(i));
            }
        }
        return paths;
    }

    public Object getSelectedItem() {
        if (mDelegate.getSelectedItemCount() != 1) {
            return null;
        }
        SparseBooleanArray selected = mDelegate.getSelectedItemPositions();
        for (int i = 0; i < selected.size(); i++) {
            if (selected.valueAt(i)) {
                return mDelegate.getItemAtPosition(selected.keyAt(i));
            }
        }
        return null;
    }
}