summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/ingest/IngestActivity.java
blob: 4e603bed36173f54fa5499aa4322900188fb2443 (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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
 * 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.gallery3d.ingest;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.mtp.MtpObjectInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.SparseBooleanArray;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView.MultiChoiceModeListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;

import com.android.gallery3d.R;
import com.android.gallery3d.ingest.adapter.MtpAdapter;
import com.android.gallery3d.ingest.ui.DateTileView;

import java.lang.ref.WeakReference;
import java.util.Collection;

public class IngestActivity extends Activity implements
        MtpDeviceIndex.ProgressListener, ImportTask.Listener {

    private IngestService mHelperService;
    private boolean mActive = false;
    private GridView mGridView;
    private MtpAdapter mAdapter;
    private Handler mHandler;
    private ProgressDialog mProgressDialog;
    private ActionMode mActiveActionMode;

    private View mWarningOverlay;
    private TextView mWarningOverlayText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        doBindHelperService();

        setContentView(R.layout.ingest_activity_item_list);
        mGridView = (GridView) findViewById(R.id.ingest_gridview);
        mAdapter = new MtpAdapter(this);
        mGridView.setAdapter(mAdapter);
        mGridView.setMultiChoiceModeListener(mMultiChoiceModeListener);
        mGridView.setOnItemClickListener(mOnItemClickListener);

        mHandler = new ItemListHandler(this);
    }

    private OnItemClickListener mOnItemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View itemView, int position, long arg3) {
            mGridView.setItemChecked(position, !mGridView.getCheckedItemPositions().get(position));
        }
    };

    private MultiChoiceModeListener mMultiChoiceModeListener = new MultiChoiceModeListener() {
        private boolean mIgnoreItemCheckedStateChanges = false;

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

        @Override
        public void onItemCheckedStateChanged(ActionMode mode, int position, long id,
                boolean checked) {
            if (mIgnoreItemCheckedStateChanges) return;
            if (mAdapter.itemAtPositionIsBucket(position)) {
                SparseBooleanArray checkedItems = mGridView.getCheckedItemPositions();
                mIgnoreItemCheckedStateChanges = true;
                mGridView.setItemChecked(position, false);

                // Takes advantage of the fact that SectionIndexer imposes the
                // need to clamp to the valid range
                int nextSectionStart = mAdapter.getPositionForSection(
                        mAdapter.getSectionForPosition(position) + 1);
                if (nextSectionStart == position)
                    nextSectionStart = mAdapter.getCount();

                boolean rangeValue = false; // Value we want to set all of the bucket items to

                // Determine if all the items in the bucket are currently checked, so that we
                // can uncheck them, otherwise we will check all items in the bucket.
                for (int i = position + 1; i < nextSectionStart; i++) {
                    if (checkedItems.get(i) == false) {
                        rangeValue = true;
                        break;
                    }
                }

                // Set all items in the bucket to the desired state
                for (int i = position + 1; i < nextSectionStart; i++) {
                    if (checkedItems.get(i) != rangeValue)
                        mGridView.setItemChecked(i, rangeValue);
                }

                mIgnoreItemCheckedStateChanges = false;
            }
            updateSelectedTitle(mode);
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId()) {
                case R.id.import_items:
                    mHelperService.importSelectedItems(
                            mGridView.getCheckedItemPositions(),
                            mAdapter);
                    mode.finish();
                    return true;
                default:
                    return false;
            }
        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.ingest_menu_item_list_selection, menu);
            updateSelectedTitle(mode);
            mActiveActionMode = mode;
            return true;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            mActiveActionMode = null;
        }

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

    @Override
    protected void onDestroy() {
        super.onDestroy();
        doUnbindHelperService();
    }

    @Override
    protected void onResume() {
        DateTileView.refreshLocale();
        mActive = true;
        if (mHelperService != null) mHelperService.setClientActivity(this);
        updateWarningOverlay();
        super.onResume();
    }

    @Override
    protected void onPause() {
        if (mHelperService != null) mHelperService.setClientActivity(null);
        mActive = false;
        cleanupProgressDialog();
        super.onPause();
    }

    private void showWarningOverlay(int textResId) {
        if (mWarningOverlay == null) {
            mWarningOverlay = findViewById(R.id.ingest_warning_overlay);
            mWarningOverlayText =
                    (TextView)mWarningOverlay.findViewById(R.id.ingest_warning_overlay_text);
        }
        mWarningOverlayText.setText(textResId);
        mWarningOverlay.setVisibility(View.VISIBLE);
        mGridView.setVisibility(View.GONE);
    }

    private void hideWarningOverlay() {
        if (mWarningOverlay != null) {
            mWarningOverlay.setVisibility(View.GONE);
            mGridView.setVisibility(View.VISIBLE);
        }
    }

    private void updateWarningOverlay() {
        if (!mAdapter.deviceConnected()) {
            showWarningOverlay(R.string.ingest_no_device);
        } else if (mAdapter.indexReady() && mAdapter.getCount() == 0) {
            showWarningOverlay(R.string.ingest_empty_device);
        } else {
            hideWarningOverlay();
        }
    }

    private void UiThreadNotifyIndexChanged() {
        mAdapter.notifyDataSetChanged();
        if (mActiveActionMode != null) {
            mActiveActionMode.finish();
            mActiveActionMode = null;
        }
        updateWarningOverlay();
    }

    protected void notifyIndexChanged() {
        mHandler.sendEmptyMessage(ItemListHandler.MSG_NOTIFY_CHANGED);
    }

    private static class ProgressState {
        String message;
        String title;
        int current;
        int max;

        public void reset() {
            title = null;
            message = null;
            current = 0;
            max = 0;
        }
    }

    private ProgressState mProgressState = new ProgressState();

    @Override
    public void onObjectIndexed(MtpObjectInfo object, int numVisited) {
        // Not guaranteed to be called on the UI thread
        mProgressState.reset();
        mProgressState.max = 0;
        mProgressState.message = getResources().getQuantityString(
                R.plurals.ingest_number_of_items_scanned, numVisited, numVisited);
        mHandler.sendEmptyMessage(ItemListHandler.MSG_PROGRESS_UPDATE);
    }

    @Override
    public void onSorting() {
        // Not guaranteed to be called on the UI thread
        mProgressState.reset();
        mProgressState.max = 0;
        mProgressState.message = getResources().getString(R.string.ingest_sorting);
        mHandler.sendEmptyMessage(ItemListHandler.MSG_PROGRESS_UPDATE);
    }

    @Override
    public void onIndexFinish() {
        // Not guaranteed to be called on the UI thread
        mHandler.sendEmptyMessage(ItemListHandler.MSG_PROGRESS_HIDE);
        mHandler.sendEmptyMessage(ItemListHandler.MSG_NOTIFY_CHANGED);
    }

    @Override
    public void onImportProgress(final int visitedCount, final int totalCount,
            String pathIfSuccessful) {
        // Not guaranteed to be called on the UI thread
        mProgressState.reset();
        mProgressState.max = totalCount;
        mProgressState.current = visitedCount;
        mProgressState.title = getResources().getString(R.string.ingest_importing);
        mHandler.sendEmptyMessage(ItemListHandler.MSG_PROGRESS_UPDATE);
    }

    @Override
    public void onImportFinish(Collection<MtpObjectInfo> objectsNotImported) {
        // Not guaranteed to be called on the UI thread
        mHandler.sendEmptyMessage(ItemListHandler.MSG_PROGRESS_HIDE);
        // TODO: maybe show an extra dialog listing the ones that failed
        // importing, if any?
    }

    private ProgressDialog getProgressDialog() {
        if (mProgressDialog == null || !mProgressDialog.isShowing()) {
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setCancelable(false);
        }
        return mProgressDialog;
    }

    private void updateProgressDialog() {
        ProgressDialog dialog = getProgressDialog();
        boolean indeterminate = (mProgressState.max == 0);
        dialog.setIndeterminate(indeterminate);
        dialog.setProgressStyle(indeterminate ? ProgressDialog.STYLE_SPINNER
                : ProgressDialog.STYLE_HORIZONTAL);
        if (mProgressState.title != null) {
            dialog.setTitle(mProgressState.title);
        }
        if (mProgressState.message != null) {
            dialog.setMessage(mProgressState.message);
        }
        if (!indeterminate) {
            dialog.setProgress(mProgressState.current);
            dialog.setMax(mProgressState.max);
        }
        if (!dialog.isShowing()) {
            dialog.show();
        }
    }

    private void cleanupProgressDialog() {
        if (mProgressDialog != null) {
            mProgressDialog.hide();
            mProgressDialog = null;
        }
    }

    // This is static and uses a WeakReference in order to avoid leaking the Activity
    private static class ItemListHandler extends Handler {
        public static final int MSG_PROGRESS_UPDATE = 0;
        public static final int MSG_PROGRESS_HIDE = 1;
        public static final int MSG_NOTIFY_CHANGED = 2;

        WeakReference<IngestActivity> mParentReference;

        public ItemListHandler(IngestActivity parent) {
            super();
            mParentReference = new WeakReference<IngestActivity>(parent);
        }

        public void handleMessage(Message message) {
            IngestActivity parent = mParentReference.get();
            if (parent == null || !parent.mActive)
                return;
            switch (message.what) {
                case MSG_PROGRESS_HIDE:
                    parent.cleanupProgressDialog();
                    break;
                case MSG_PROGRESS_UPDATE:
                    parent.updateProgressDialog();
                    break;
                case MSG_NOTIFY_CHANGED:
                    parent.UiThreadNotifyIndexChanged();
                    break;
                default:
                    break;
            }
        }
    }

    private ServiceConnection mHelperServiceConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            mHelperService = ((IngestService.LocalBinder) service).getService();
            mHelperService.setClientActivity(IngestActivity.this);
            mAdapter.setMtpDeviceIndex(mHelperService.getIndex());
        }

        public void onServiceDisconnected(ComponentName className) {
            mHelperService = null;
        }
    };

    private void doBindHelperService() {
        bindService(new Intent(getApplicationContext(), IngestService.class),
                mHelperServiceConnection, Context.BIND_AUTO_CREATE);
    }

    private void doUnbindHelperService() {
        if (mHelperService != null) {
            mHelperService.setClientActivity(null);
            unbindService(mHelperServiceConnection);
        }
    }
}