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.gallery3d.ingest;
19 import android.app.Activity;
20 import android.app.ProgressDialog;
21 import android.content.ComponentName;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.ServiceConnection;
25 import android.mtp.MtpObjectInfo;
26 import android.os.Bundle;
27 import android.os.Handler;
28 import android.os.IBinder;
29 import android.os.Message;
30 import android.util.SparseBooleanArray;
31 import android.view.ActionMode;
32 import android.view.Menu;
33 import android.view.MenuInflater;
34 import android.view.MenuItem;
35 import android.view.View;
36 import android.widget.AbsListView.MultiChoiceModeListener;
37 import android.widget.AdapterView;
38 import android.widget.AdapterView.OnItemClickListener;
39 import android.widget.GridView;
41 import com.android.gallery3d.R;
42 import com.android.gallery3d.ingest.adapter.MtpAdapter;
43 import com.android.gallery3d.ingest.ui.DateTileView;
45 import java.lang.ref.WeakReference;
46 import java.util.Collection;
48 public class IngestActivity extends Activity implements
49 MtpDeviceIndex.ProgressListener, ImportTask.Listener {
51 private IngestService mHelperService;
52 private boolean mActive = false;
53 private GridView mGridView;
54 private MtpAdapter mAdapter;
55 private Handler mHandler;
56 private ProgressDialog mProgressDialog;
57 private ActionMode mActiveActionMode;
60 protected void onCreate(Bundle savedInstanceState) {
61 super.onCreate(savedInstanceState);
62 doBindHelperService();
64 setContentView(R.layout.ingest_activity_item_list);
65 mGridView = (GridView) findViewById(R.id.ingest_gridview);
66 mAdapter = new MtpAdapter(this);
67 mGridView.setAdapter(mAdapter);
68 mGridView.setMultiChoiceModeListener(mMultiChoiceModeListener);
69 mGridView.setOnItemClickListener(mOnItemClickListener);
71 mHandler = new ItemListHandler(this);
74 private OnItemClickListener mOnItemClickListener = new OnItemClickListener() {
76 public void onItemClick(AdapterView<?> adapterView, View itemView, int position, long arg3) {
77 mGridView.setItemChecked(position, !mGridView.getCheckedItemPositions().get(position));
81 private MultiChoiceModeListener mMultiChoiceModeListener = new MultiChoiceModeListener() {
82 private boolean mIgnoreItemCheckedStateChanges = false;
84 private void updateSelectedTitle(ActionMode mode) {
85 int count = mGridView.getCheckedItemCount();
86 mode.setTitle(getResources().getQuantityString(
87 R.plurals.number_of_items_selected, count, count));
91 public void onItemCheckedStateChanged(ActionMode mode, int position, long id,
93 if (mIgnoreItemCheckedStateChanges) return;
94 if (mAdapter.itemAtPositionIsBucket(position)) {
95 SparseBooleanArray checkedItems = mGridView.getCheckedItemPositions();
96 mIgnoreItemCheckedStateChanges = true;
97 mGridView.setItemChecked(position, false);
99 // Takes advantage of the fact that SectionIndexer imposes the
100 // need to clamp to the valid range
101 int nextSectionStart = mAdapter.getPositionForSection(
102 mAdapter.getSectionForPosition(position) + 1);
103 if (nextSectionStart == position)
104 nextSectionStart = mAdapter.getCount();
106 boolean rangeValue = false; // Value we want to set all of the bucket items to
108 // Determine if all the items in the bucket are currently checked, so that we
109 // can uncheck them, otherwise we will check all items in the bucket.
110 for (int i = position + 1; i < nextSectionStart; i++) {
111 if (checkedItems.get(i) == false) {
117 // Set all items in the bucket to the desired state
118 for (int i = position + 1; i < nextSectionStart; i++) {
119 if (checkedItems.get(i) != rangeValue)
120 mGridView.setItemChecked(i, rangeValue);
123 mIgnoreItemCheckedStateChanges = false;
125 updateSelectedTitle(mode);
129 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
130 switch (item.getItemId()) {
131 case R.id.import_items:
132 mHelperService.importSelectedItems(
133 mGridView.getCheckedItemPositions(),
143 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
144 MenuInflater inflater = mode.getMenuInflater();
145 inflater.inflate(R.menu.ingest_menu_item_list_selection, menu);
146 updateSelectedTitle(mode);
147 mActiveActionMode = mode;
152 public void onDestroyActionMode(ActionMode mode) {
153 mActiveActionMode = null;
157 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
158 updateSelectedTitle(mode);
164 protected void onDestroy() {
166 doUnbindHelperService();
170 protected void onResume() {
171 DateTileView.refreshLocale();
173 if (mHelperService != null) mHelperService.setClientActivity(this);
178 protected void onPause() {
179 if (mHelperService != null) mHelperService.setClientActivity(null);
181 cleanupProgressDialog();
185 protected void notifyIndexChanged() {
186 mAdapter.notifyDataSetChanged();
187 if (mActiveActionMode != null) {
188 mActiveActionMode.finish();
189 mActiveActionMode = null;
193 private static class ProgressState {
199 public void reset() {
207 private ProgressState mProgressState = new ProgressState();
210 public void onObjectIndexed(MtpObjectInfo object, int numVisited) {
211 // Not guaranteed to be called on the UI thread
212 mProgressState.reset();
213 mProgressState.max = 0;
214 mProgressState.message = getResources().getQuantityString(
215 R.plurals.ingest_number_of_items_scanned, numVisited, numVisited);
216 mHandler.sendEmptyMessage(ItemListHandler.MSG_PROGRESS_UPDATE);
220 public void onSorting() {
221 // Not guaranteed to be called on the UI thread
222 mProgressState.reset();
223 mProgressState.max = 0;
224 mProgressState.message = getResources().getString(R.string.ingest_sorting);
225 mHandler.sendEmptyMessage(ItemListHandler.MSG_PROGRESS_UPDATE);
229 public void onIndexFinish() {
230 // Not guaranteed to be called on the UI thread
231 mHandler.sendEmptyMessage(ItemListHandler.MSG_PROGRESS_HIDE);
232 mHandler.sendEmptyMessage(ItemListHandler.MSG_ADAPTER_NOTIFY_CHANGED);
236 public void onImportProgress(final int visitedCount, final int totalCount,
237 String pathIfSuccessful) {
238 // Not guaranteed to be called on the UI thread
239 mProgressState.reset();
240 mProgressState.max = totalCount;
241 mProgressState.current = visitedCount;
242 mProgressState.title = getResources().getString(R.string.ingest_importing);
243 mHandler.sendEmptyMessage(ItemListHandler.MSG_PROGRESS_UPDATE);
247 public void onImportFinish(Collection<MtpObjectInfo> objectsNotImported) {
248 // Not guaranteed to be called on the UI thread
249 mHandler.sendEmptyMessage(ItemListHandler.MSG_PROGRESS_HIDE);
250 // TODO: maybe show an extra dialog listing the ones that failed
251 // importing, if any?
254 private ProgressDialog getProgressDialog() {
255 if (mProgressDialog == null || !mProgressDialog.isShowing()) {
256 mProgressDialog = new ProgressDialog(this);
257 mProgressDialog.setCancelable(false);
259 return mProgressDialog;
262 private void updateProgressDialog() {
263 ProgressDialog dialog = getProgressDialog();
264 boolean indeterminate = (mProgressState.max == 0);
265 dialog.setIndeterminate(indeterminate);
266 dialog.setProgressStyle(indeterminate ? ProgressDialog.STYLE_SPINNER
267 : ProgressDialog.STYLE_HORIZONTAL);
268 if (mProgressState.title != null) {
269 dialog.setTitle(mProgressState.title);
271 if (mProgressState.message != null) {
272 dialog.setMessage(mProgressState.message);
274 if (!indeterminate) {
275 dialog.setProgress(mProgressState.current);
276 dialog.setMax(mProgressState.max);
278 if (!dialog.isShowing()) {
283 private void cleanupProgressDialog() {
284 if (mProgressDialog != null) {
285 mProgressDialog.hide();
286 mProgressDialog = null;
290 // This is static and uses a WeakReference in order to avoid leaking the Activity
291 private static class ItemListHandler extends Handler {
292 public static final int MSG_PROGRESS_UPDATE = 0;
293 public static final int MSG_PROGRESS_HIDE = 1;
294 public static final int MSG_ADAPTER_NOTIFY_CHANGED = 2;
296 WeakReference<IngestActivity> mParentReference;
298 public ItemListHandler(IngestActivity parent) {
300 mParentReference = new WeakReference<IngestActivity>(parent);
303 public void handleMessage(Message message) {
304 IngestActivity parent = mParentReference.get();
305 if (parent == null || !parent.mActive)
307 switch (message.what) {
308 case MSG_PROGRESS_HIDE:
309 parent.cleanupProgressDialog();
311 case MSG_PROGRESS_UPDATE:
312 parent.updateProgressDialog();
314 case MSG_ADAPTER_NOTIFY_CHANGED:
315 parent.notifyIndexChanged();
323 private ServiceConnection mHelperServiceConnection = new ServiceConnection() {
324 public void onServiceConnected(ComponentName className, IBinder service) {
325 mHelperService = ((IngestService.LocalBinder) service).getService();
326 mHelperService.setClientActivity(IngestActivity.this);
327 mAdapter.setMtpDeviceIndex(mHelperService.getIndex());
330 public void onServiceDisconnected(ComponentName className) {
331 mHelperService = null;
335 private void doBindHelperService() {
336 bindService(new Intent(getApplicationContext(), IngestService.class),
337 mHelperServiceConnection, Context.BIND_AUTO_CREATE);
340 private void doUnbindHelperService() {
341 if (mHelperService != null) {
342 mHelperService.setClientActivity(null);
343 unbindService(mHelperServiceConnection);