OSDN Git Service

Merge "Import translations. DO NOT MERGE" into gb-ub-photos-bryce
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / ingest / IngestActivity.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.gallery3d.ingest;
18
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;
40
41 import com.android.gallery3d.R;
42 import com.android.gallery3d.ingest.adapter.MtpAdapter;
43 import com.android.gallery3d.ingest.ui.DateTileView;
44
45 import java.lang.ref.WeakReference;
46 import java.util.Collection;
47
48 public class IngestActivity extends Activity implements
49         MtpDeviceIndex.ProgressListener, ImportTask.Listener {
50
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;
58
59     @Override
60     protected void onCreate(Bundle savedInstanceState) {
61         super.onCreate(savedInstanceState);
62         doBindHelperService();
63
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);
70
71         mHandler = new ItemListHandler(this);
72     }
73
74     private OnItemClickListener mOnItemClickListener = new OnItemClickListener() {
75         @Override
76         public void onItemClick(AdapterView<?> adapterView, View itemView, int position, long arg3) {
77             mGridView.setItemChecked(position, !mGridView.getCheckedItemPositions().get(position));
78         }
79     };
80
81     private MultiChoiceModeListener mMultiChoiceModeListener = new MultiChoiceModeListener() {
82         private boolean mIgnoreItemCheckedStateChanges = false;
83
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));
88         }
89
90         @Override
91         public void onItemCheckedStateChanged(ActionMode mode, int position, long id,
92                 boolean checked) {
93             if (mIgnoreItemCheckedStateChanges) return;
94             if (mAdapter.itemAtPositionIsBucket(position)) {
95                 SparseBooleanArray checkedItems = mGridView.getCheckedItemPositions();
96                 mIgnoreItemCheckedStateChanges = true;
97                 mGridView.setItemChecked(position, false);
98
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();
105
106                 boolean rangeValue = false; // Value we want to set all of the bucket items to
107
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) {
112                         rangeValue = true;
113                         break;
114                     }
115                 }
116
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);
121                 }
122
123                 mIgnoreItemCheckedStateChanges = false;
124             }
125             updateSelectedTitle(mode);
126         }
127
128         @Override
129         public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
130             switch (item.getItemId()) {
131                 case R.id.import_items:
132                     mHelperService.importSelectedItems(
133                             mGridView.getCheckedItemPositions(),
134                             mAdapter);
135                     mode.finish();
136                     return true;
137                 default:
138                     return false;
139             }
140         }
141
142         @Override
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;
148             return true;
149         }
150
151         @Override
152         public void onDestroyActionMode(ActionMode mode) {
153             mActiveActionMode = null;
154         }
155
156         @Override
157         public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
158             updateSelectedTitle(mode);
159             return false;
160         }
161     };
162
163     @Override
164     protected void onDestroy() {
165         super.onDestroy();
166         doUnbindHelperService();
167     }
168
169     @Override
170     protected void onResume() {
171         DateTileView.refreshLocale();
172         mActive = true;
173         if (mHelperService != null) mHelperService.setClientActivity(this);
174         super.onResume();
175     }
176
177     @Override
178     protected void onPause() {
179         if (mHelperService != null) mHelperService.setClientActivity(null);
180         mActive = false;
181         cleanupProgressDialog();
182         super.onPause();
183     }
184
185     protected void notifyIndexChanged() {
186         mAdapter.notifyDataSetChanged();
187         if (mActiveActionMode != null) {
188             mActiveActionMode.finish();
189             mActiveActionMode = null;
190         }
191     }
192
193     private static class ProgressState {
194         String message;
195         String title;
196         int current;
197         int max;
198
199         public void reset() {
200             title = null;
201             message = null;
202             current = 0;
203             max = 0;
204         }
205     }
206
207     private ProgressState mProgressState = new ProgressState();
208
209     @Override
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);
217     }
218
219     @Override
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);
226     }
227
228     @Override
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);
233     }
234
235     @Override
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);
244     }
245
246     @Override
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?
252     }
253
254     private ProgressDialog getProgressDialog() {
255         if (mProgressDialog == null || !mProgressDialog.isShowing()) {
256             mProgressDialog = new ProgressDialog(this);
257             mProgressDialog.setCancelable(false);
258         }
259         return mProgressDialog;
260     }
261
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);
270         }
271         if (mProgressState.message != null) {
272             dialog.setMessage(mProgressState.message);
273         }
274         if (!indeterminate) {
275             dialog.setProgress(mProgressState.current);
276             dialog.setMax(mProgressState.max);
277         }
278         if (!dialog.isShowing()) {
279             dialog.show();
280         }
281     }
282
283     private void cleanupProgressDialog() {
284         if (mProgressDialog != null) {
285             mProgressDialog.hide();
286             mProgressDialog = null;
287         }
288     }
289
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;
295
296         WeakReference<IngestActivity> mParentReference;
297
298         public ItemListHandler(IngestActivity parent) {
299             super();
300             mParentReference = new WeakReference<IngestActivity>(parent);
301         }
302
303         public void handleMessage(Message message) {
304             IngestActivity parent = mParentReference.get();
305             if (parent == null || !parent.mActive)
306                 return;
307             switch (message.what) {
308                 case MSG_PROGRESS_HIDE:
309                     parent.cleanupProgressDialog();
310                     break;
311                 case MSG_PROGRESS_UPDATE:
312                     parent.updateProgressDialog();
313                     break;
314                 case MSG_ADAPTER_NOTIFY_CHANGED:
315                     parent.notifyIndexChanged();
316                     break;
317                 default:
318                     break;
319             }
320         }
321     }
322
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());
328         }
329
330         public void onServiceDisconnected(ComponentName className) {
331             mHelperService = null;
332         }
333     };
334
335     private void doBindHelperService() {
336         bindService(new Intent(getApplicationContext(), IngestService.class),
337                 mHelperServiceConnection, Context.BIND_AUTO_CREATE);
338     }
339
340     private void doUnbindHelperService() {
341         if (mHelperService != null) {
342             mHelperService.setClientActivity(null);
343             unbindService(mHelperServiceConnection);
344         }
345     }
346 }