/
ImageDetailActivityViewPager.java
1341 lines (1131 loc) · 54.5 KB
/
ImageDetailActivityViewPager.java
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright 2011, 2012 Chris Banes.
* Copyright (c) 2015-2019 by k3b.
*
* This file is part of AndroFotoFinder / #APhotoManager.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>
*/
package de.k3b.android.androFotoFinder.imagedetail;
import android.app.ActionBar;
import android.app.Activity;
import android.app.LoaderManager;
import android.content.Context;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.PopupMenu;
import android.widget.Toast;
import org.osmdroid.api.IGeoPoint;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import de.k3b.android.androFotoFinder.AffUtils;
import de.k3b.android.androFotoFinder.Common;
import de.k3b.android.androFotoFinder.FotoGalleryActivity;
import de.k3b.android.androFotoFinder.Global;
import de.k3b.android.androFotoFinder.LockScreen;
import de.k3b.android.androFotoFinder.PhotoPropertiesEditActivity;
import de.k3b.android.androFotoFinder.R;
import de.k3b.android.androFotoFinder.SettingsActivity;
import de.k3b.android.androFotoFinder.directory.DirectoryPickerFragment;
import de.k3b.android.androFotoFinder.locationmap.GeoEditActivity;
import de.k3b.android.androFotoFinder.locationmap.MapGeoPickerActivity;
import de.k3b.android.androFotoFinder.queries.FotoSql;
import de.k3b.android.androFotoFinder.tagDB.TagSql;
import de.k3b.android.androFotoFinder.tagDB.TagTask;
import de.k3b.android.androFotoFinder.tagDB.TagsPickerFragment;
import de.k3b.android.util.AndroidFileCommands;
import de.k3b.android.util.FileManagerUtil;
import de.k3b.android.util.IntentUtil;
import de.k3b.android.util.OsUtils;
import de.k3b.android.util.PhotoPropertiesMediaFilesScanner;
import de.k3b.android.util.PhotoPropertiesMediaFilesScannerAsyncTask;
import de.k3b.android.widget.AboutDialogPreference;
import de.k3b.android.widget.ActivityWithAutoCloseDialogs;
import de.k3b.android.widget.ActivityWithCallContext;
import de.k3b.android.widget.Dialogs;
import de.k3b.database.QueryParameter;
import de.k3b.geo.api.GeoPointDto;
import de.k3b.geo.io.GeoUri;
import de.k3b.io.FileProcessor;
import de.k3b.io.FileUtils;
import de.k3b.io.GalleryFilterParameter;
import de.k3b.io.IDirectory;
import de.k3b.io.StringUtils;
import de.k3b.io.collections.SelectedFiles;
import de.k3b.media.PhotoPropertiesUtil;
import de.k3b.tagDB.Tag;
// import com.squareup.leakcanary.RefWatcher;
/**
* Shows a zoomable imagee.<br>
* Swipe left/right to show previous/next image.
*/
public class ImageDetailActivityViewPager extends ActivityWithAutoCloseDialogs implements Common, TagsPickerFragment.ITagsPicker {
private static final String INSTANCE_STATE_MODIFY_COUNT = "mModifyCount";
private static final String INSTANCE_STATE_LAST_SCROLL_POSITION = "lastScrollPosition";
/** #70: remember on config change (screen rotation) */
private static final String INSTANCE_STATE_ContextName = "ContextName";
private static final String INSTANCE_STATE_ContextMenuId = "ContextMenuId";
/** #70: part of sql column expression for ExtraDetailExpression used to find old defintion ot delete */
private static final String CONTEXT_COLUMN_ALIAS = " as " + ImagePagerAdapterFromCursor.CONTEXT_COLUMN_FIELD;
public static final int ACTIVITY_ID = 76621;
/** activityRequestCode: in forward mode: intent is forwarded to gallery */
private static final int ACTION_RESULT_FORWARD = 471102;
// #64: after image edit rescan file
private static final int ACTION_RESULT_MUST_MEDIA_SCAN = 47103;
private static final int DEFAULT_SORT = FotoSql.SORT_BY_NAME_LEN;
private static final QueryParameter DEFAULT_QUERY = FotoSql.queryDetail;
private static final int NO_INITIAL_SCROLL_POSITION = -1;
/** if smaller that these millisecs then the actionbar autohide is disabled */
private static final int DISABLE_HIDE_ACTIONBAR = 700;
private static final int NOMEDIA_GALLERY = 8227;
// how many changes have been made. if != 0 parent activity must invalidate cached data
private static int mModifyCount = 0;
private MenuItem mMenuSlideshow = null;
private boolean mWaitingForMediaScannerResult = false;
MoveOrCopyDestDirPicker mDestDirPicker = null;
private LocalCursorLoader mCurorLoader;
// private static final String ISLOCKED_ARG = "isLocked";
private LockableViewPager mViewPager = null;
private ImagePagerAdapterFromCursorArray mAdapter = null;
private TagUpdateTask mTagWorflow = null;
private final AndroidFileCommands mFileCommands = new LocalFileCommands();
// for debugging
private static int id = 1;
private String mDebugPrefix;
/** where data comes from */
private QueryParameter mGalleryContentQuery = null;
private GalleryFilterParameter mFilter = null;
/** if >= 0 after load cursor scroll to this offset */
private int mInitialScrollPosition = NO_INITIAL_SCROLL_POSITION;
/** if not null: after load cursor scroll to this path */
private String mInitialFilePath = null;
/** after 2 secs of user inactive the actionbar is hidden until the screen is touched */
private Handler mActionBarHideTimer = null;
/** #70: handles ExtraDetail definition */
private ImageContextController mImageContextController = null;
/** #70: optinal sql expression to be shown in detailview */
private String mContextColumnExpression = null; // sql field expression. Result will be displayed in ImageView Context area
private String mContextName; // name of current ImageView Context persisted in bundle
private boolean mMustReplaceMenue = false;
private boolean locked = false; // if != Global.locked : must update menu
// if not null this one image that cannot be translated to a file uri will be shown
private Uri imageUri = null;
/** executes sql to load image detail data in a background task that may survive
* conriguration change (i.e. device rotation) */
class LocalCursorLoader implements LoaderManager.LoaderCallbacks<Cursor> {
/** incremented every time a new curster/query is generated */
private int mRequeryInstanceCount = 0;
/** called by LoaderManager.getLoader(ACTIVITY_ID) to (re)create loader
* that attaches to last query/cursor if it still exist i.e. after rotation */
@Override
public Loader<Cursor> onCreateLoader(int loaderID, Bundle bundle) {
switch (loaderID) {
case ACTIVITY_ID:
mRequeryInstanceCount++;
mWaitingForMediaScannerResult = false;
if (Global.debugEnabledSql) {
Log.i(Global.LOG_CONTEXT, mDebugPrefix + " onCreateLoader" +
getDebugContext() +
" : query = " + mGalleryContentQuery);
}
return FotoSql.createCursorLoader(getApplicationContext(), mGalleryContentQuery);
default:
// An invalid id was passed in
return null;
}
}
/** called after media db content has changed */
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// to be restored after refreshLocal if there is no mInitialFilePath
if ((mInitialScrollPosition == NO_INITIAL_SCROLL_POSITION) && (mViewPager != null)) {
mInitialScrollPosition = mViewPager.getCurrentItem();
}
// do change the data
mAdapter.swapCursor(data);
// restore position is invalid
final int newItemCount = mAdapter.getCount();
if (((newItemCount == 0)) || (mInitialScrollPosition >= newItemCount)) mInitialScrollPosition = NO_INITIAL_SCROLL_POSITION;
if (Global.debugEnabledSql) {
Log.i(Global.LOG_CONTEXT, mDebugPrefix + " onLoadFinished" +
getDebugContext() +
" found " + ((data == null) ? 0 : newItemCount) + " rows");
}
// do change the data
mAdapter.notifyDataSetChanged();
mViewPager.setAdapter(mAdapter);
// show the changes
onLoadCompleted();
}
/** called by LoaderManager. after search criteria were changed or if activity is destroyed. */
@Override
public void onLoaderReset(Loader<Cursor> loader) {
// rember position where we have to scroll to after refreshLocal is finished.
mInitialScrollPosition = mViewPager.getCurrentItem();
mAdapter.swapCursor(null);
if (Global.debugEnabledSql) {
Log.i(Global.LOG_CONTEXT, mDebugPrefix + " onLoaderReset" +
getDebugContext());
}
mAdapter.notifyDataSetChanged();
}
@NonNull
private String getDebugContext() {
return "(#" + mRequeryInstanceCount
+ ", mScrollPosition=" + mInitialScrollPosition +
", Path='" + mInitialFilePath +
"')";
}
}
class LocalFileCommands extends AndroidFileCommands {
@Override
protected void onPostProcess(String what, int opCode, SelectedFiles selectedFiles, int modifyCount, int itemCount, String[] oldPathNames, String[] newPathNames) {
mInitialFilePath = null;
switch (opCode) {
case OP_MOVE:
case OP_RENAME:
if ((newPathNames!= null) && (newPathNames.length > 0)) {
// so selection will be restored to this after load complete
mInitialFilePath = newPathNames[0];
}
break;
case OP_COPY:
if ((oldPathNames!= null) && (oldPathNames.length > 0)) {
// so selection will be restored to this after load complete
mInitialFilePath = oldPathNames[0];
}
break;
default:break;
}
super.onPostProcess(what, opCode, selectedFiles, modifyCount, itemCount, oldPathNames, newPathNames);
if ((opCode == OP_RENAME) || (opCode == OP_MOVE) || (opCode == OP_DELETE)) {
refreshIfNecessary();
}
}
}
public static class MoveOrCopyDestDirPicker extends DirectoryPickerFragment {
protected static AndroidFileCommands sFileCommands = null;
public static MoveOrCopyDestDirPicker newInstance(boolean move, final SelectedFiles srcFotos) {
MoveOrCopyDestDirPicker f = new MoveOrCopyDestDirPicker();
// Supply index input as an argument.
Bundle args = new Bundle();
args.putBoolean("move", move);
AffUtils.putSelectedFiles(args, srcFotos);
f.setArguments(args);
return f;
}
/* do not use activity callback */
@Override
protected void setDirectoryListener(Activity activity) {/* do not use activity callback */}
public boolean getMove() {
return getArguments().getBoolean("move", false);
}
public SelectedFiles getSrcFotos() {
return AffUtils.getSelectedFiles(getArguments());
}
/**
* To be overwritten to check if a path can be picked.
*
* @param path to be checked if it cannot be handled
* @return null if no error else error message with the reason why it cannot be selected
*/
@Override
protected String getStatusErrorMessage(String path) {
String errorMessage = (sFileCommands == null) ? null : sFileCommands.checkWriteProtected(0, new File(path));
if (errorMessage != null) {
int pos = errorMessage.indexOf('\n');
return (pos > 0) ? errorMessage.substring(0,pos) : errorMessage;
}
return super.getStatusErrorMessage(path);
}
@Override
protected void onDirectoryPick(IDirectory selection) {
// super.onDirectoryPick(selection);
mModifyCount++; // copy or move initiated
getActivity().setResult((mModifyCount == 0) ? RESULT_NOCHANGE : RESULT_CHANGE);
sFileCommands.onMoveOrCopyDirectoryPick(getMove(), getSrcFotos(), selection);
dismiss();
}
}
private class TagUpdateTask extends TagTask<List<String>> {
TagUpdateTask(SelectedFiles fotos) {
super(ImageDetailActivityViewPager.this,R.string.tags_activity_title);
this.getWorkflow().init(ImageDetailActivityViewPager.this, fotos, null);
}
@Override
protected Integer doInBackground(List<String>... params) {
return getWorkflow().updateTags(params[0], params[1]);
}
}
/**
* shows a new instance of ImageDetailActivityViewPager.
*
* @param debugContext
* @param context calling activity
* @param imageUri if != null initial image to show
* @param position offset of image to display in query or current file directory
* @param imageDetailQuery if != null set initial filter to new FotoGalleryActivity
* @param requestCode if != 0 start for result. else start without result
*/
public static void showActivity(String debugContext, Activity context, Uri imageUri,
int position, QueryParameter imageDetailQuery, int requestCode) {
Intent intent;
//Create intent
intent = new Intent(context, ImageDetailActivityViewPager.class);
if (imageDetailQuery != null) {
intent.putExtra(ImageDetailActivityViewPager.EXTRA_QUERY, imageDetailQuery.toReParseableString());
}
intent.putExtra(ImageDetailActivityViewPager.EXTRA_POSITION, position);
intent.setData(imageUri);
IntentUtil.startActivity(debugContext, context, requestCode, intent);
}
@Override
public void onCreate(Bundle savedInstanceState) {
mDebugPrefix = "ImageDetailActivityViewPager#" + (id++) + " ";
Global.debugMemory(mDebugPrefix, "onCreate");
this.mWaitingForMediaScannerResult = false;
// #17: let actionbar overlap image so there is no need to resize main view item
// http://stackoverflow.com/questions/6749261/custom-translucent-android-actionbar
getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
super.onCreate(savedInstanceState);
SettingsActivity.prefs2Global(this);
Intent intent = getIntent();
if (Global.debugEnabled && (intent != null)){
Log.d(Global.LOG_CONTEXT, mDebugPrefix + "onCreate " + intent.toUri(Intent.URI_INTENT_SCHEME));
}
if (mustForward(intent)) {
// cannot handle myself. Forward to FotoGalleryActivity
Intent childIntent = new Intent();
childIntent.setClass(this, FotoGalleryActivity.class);
childIntent.setAction(intent.getAction());
IntentUtil.setDataAndTypeAndNormalize(childIntent, intent.getData(), intent.getType());
copyExtras(childIntent, intent.getExtras(),
EXTRA_FILTER, EXTRA_POSITION, EXTRA_QUERY, AffUtils.EXTRA_SELECTED_ITEM_IDS, AffUtils.EXTRA_SELECTED_ITEM_DATES,
AffUtils.EXTRA_SELECTED_ITEM_PATHS, EXTRA_STREAM, EXTRA_TITLE);
ActivityWithCallContext.additionalCallContext = "-must-forward-";
startActivityForResult(childIntent, ACTION_RESULT_FORWARD);
} else { // not in forward mode
setContentView(R.layout.activity_image_view_pager);
mViewPager = (LockableViewPager) findViewById(R.id.view_pager);
setContentView(mViewPager);
// extra parameter
getParameter(intent);
mAdapter = new ImagePagerAdapterFromCursorArray(this, mDebugPrefix, mInitialFilePath);
if (savedInstanceState != null) {
String querySql = savedInstanceState.getString(EXTRA_QUERY);
if (querySql != null) {
this.mGalleryContentQuery = QueryParameter.parse(querySql);
}
mContextName = savedInstanceState.getString(INSTANCE_STATE_ContextName, mContextName);
int lastMenuId = savedInstanceState.getInt(INSTANCE_STATE_ContextMenuId, 0);
if (lastMenuId != 0) {
PopupMenu menu = new PopupMenu(this, mViewPager);
onCreateOptionsMenu(menu.getMenu());
MenuItem menuItem = menu.getMenu().findItem(lastMenuId);
if (menuItem != null) {
mContextName = menuItem.getTitle().toString();
mAdapter.setContext(menuItem);
}
}
setContextMode(mContextName);
}
mViewPager.setAdapter(mAdapter);
mViewPager.setOnInterceptTouchEvent(new View.OnClickListener() {
@Override
public void onClick(View v) {
onGuiTouched();
}
});
if (savedInstanceState != null) {
mInitialScrollPosition = savedInstanceState.getInt(INSTANCE_STATE_LAST_SCROLL_POSITION, this.mInitialScrollPosition);
mModifyCount = savedInstanceState.getInt(INSTANCE_STATE_ContextMenuId, mModifyCount);
} else {
mModifyCount = 0;
}
if ((mInitialFilePath != null) && (mInitialScrollPosition ==-1)) {
mInitialScrollPosition = mAdapter.getPositionFromPath(mInitialFilePath);
}
if ((mInitialScrollPosition >= 0) && (mInitialScrollPosition < mAdapter.getCount())) {
mViewPager.setCurrentItem(mInitialScrollPosition);
}
setResult((mModifyCount == 0) ? RESULT_NOCHANGE : RESULT_CHANGE);
mFileCommands.setContext(this);
mFileCommands.setLogFilePath(mFileCommands.getDefaultLogFile());
if (Global.debugEnabledMemory) {
Log.d(Global.LOG_CONTEXT, mDebugPrefix + " - onCreate cmd (" +
MoveOrCopyDestDirPicker.sFileCommands + ") => (" + mFileCommands +
")");
}
MoveOrCopyDestDirPicker.sFileCommands = mFileCommands;
if (mAdapter.getCount() == 0) {
// if cursor does not contain data via file
mCurorLoader = new LocalCursorLoader();
getLoaderManager().initLoader(ACTIVITY_ID, null, mCurorLoader);
}
// #17: make actionbar background nearly transparent
// http://stackoverflow.com/questions/6749261/custom-translucent-android-actionbar
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.argb(128, 0, 0, 0)));
}
unhideActionBar(Global.actionBarHideTimeInMilliSecs, "onCreate");
}
private void onGuiTouched() {
// stop slideshow if active
if (mSlideShowStarted) startStopSlideShow(false);
unhideActionBar(Global.actionBarHideTimeInMilliSecs, "onGuiTouched");
}
private void unhideActionBar(int milliSecsUntilHide, String why) {
final int ACTIONBAR_HIDE_HANDLER_ID = 3;
boolean timer = milliSecsUntilHide >= DISABLE_HIDE_ACTIONBAR;
ActionBar bar = getActionBar();
if ((bar != null) && (!bar.isShowing())) {
if (Global.debugEnabled) {
Log.d(Global.LOG_CONTEXT, mDebugPrefix + "unhiding ActionBar(timer=" + timer +
") " + why);
}
bar.show();
}
if (timer) {
if (mActionBarHideTimer == null) {
mActionBarHideTimer = new Handler() {
public void handleMessage(Message m) {
ActionBar bar = getActionBar();
if ((bar != null) && (bar.isShowing())) {
if (Global.debugEnabled) {
Log.d(Global.LOG_CONTEXT, mDebugPrefix + "hiding ActionBar");
}
bar.hide();
}
}
};
}
mActionBarHideTimer.removeMessages(ACTIONBAR_HIDE_HANDLER_ID);
mActionBarHideTimer.sendMessageDelayed(Message.obtain(mActionBarHideTimer, ACTIONBAR_HIDE_HANDLER_ID), milliSecsUntilHide);
} else {
if (mActionBarHideTimer != null) {
mActionBarHideTimer.removeMessages(ACTIONBAR_HIDE_HANDLER_ID);
mActionBarHideTimer = null;
}
}
}
private void copyExtras(Intent dest, Bundle source, String... keys) {
if ((dest != null) && (source != null) && (keys != null)) {
for (String key : keys) {
Object item = source.get(key);
if (item != null) {
dest.putExtra(key, item.toString());
}
}
}
}
@Override
protected void onActivityResult(final int requestCode,
final int resultCode, final Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (mDestDirPicker != null) mDestDirPicker.onActivityResult(requestCode,resultCode,intent);
final boolean locked = LockScreen.isLocked(this);
if (this.locked != locked) {
this.locked = locked;
mMustReplaceMenue = true;
invalidateOptionsMenu();
}
if (requestCode == ACTION_RESULT_FORWARD) {
// forward result from child-activity to parent-activity
setResult(resultCode, intent);
finish();
} else if (requestCode == ACTION_RESULT_MUST_MEDIA_SCAN) {
// #64 after edit the content might have been changed. update media DB.
String orgiginalFileToScan = getCurrentFilePath();
if (orgiginalFileToScan != null) {
PhotoPropertiesMediaFilesScanner.getInstance(this).updateMediaDatabase_Android42(this, null, orgiginalFileToScan);
}
}
refreshIfNecessary();
}
private static boolean mustForward(Intent intent) {
Uri uri = IntentUtil.getUri(intent);
File file = IntentUtil.getFile(uri);
// probably content: url
if (file == null) return IntentUtil.isFileUri(uri); // true if file:// with wildcards "%" or "*" goto gallery;
// file with wildcard, directory or no read permissions
return (!file.exists() || !file.isFile() || !file.canRead());
}
private void setFilter(GalleryFilterParameter value) {
if (value != null) {
QueryParameter query = new QueryParameter(DEFAULT_QUERY);
FotoSql.setSort(query, DEFAULT_SORT, true);
TagSql.filter2QueryEx(query, value, true);
addContextColumn(query, mContextColumnExpression);
mGalleryContentQuery = query;
}
this.mFilter = value; // #34
}
/** query from EXTRA_QUERY, EXTRA_FILTER, fileParentDir , defaultQuery */
private void getParameter(Intent intent) {
this.mInitialScrollPosition = intent.getIntExtra(EXTRA_POSITION, this.mInitialScrollPosition);
this.mGalleryContentQuery = QueryParameter.parse(intent.getStringExtra(EXTRA_QUERY));
this.mFilter = null;
if (mGalleryContentQuery == null) {
String filterValue = intent.getStringExtra(EXTRA_FILTER);
if (filterValue != null) {
setFilter(GalleryFilterParameter.parse(filterValue, new GalleryFilterParameter()));
}
}
if (mGalleryContentQuery == null) {
this.mInitialScrollPosition = NO_INITIAL_SCROLL_POSITION;
final Uri conentUri = IntentUtil.getUri(intent);
if (conentUri != null) {
String path = IntentUtil.getFilePath(this, conentUri);
if (path != null) {
setFilter(getParameterFromPath(path));
} else {
this.imageUri = conentUri;
}
}
}
if ((mGalleryContentQuery == null) && (mFilter == null)) {
Log.e(Global.LOG_CONTEXT, mDebugPrefix + " onCreate() : parameter Not found " + EXTRA_QUERY +
", " + EXTRA_FILTER + ", ] not found. Using default.");
mGalleryContentQuery = new QueryParameter(DEFAULT_QUERY);
this.mInitialScrollPosition = NO_INITIAL_SCROLL_POSITION;
addContextColumn(mGalleryContentQuery, mContextColumnExpression);
//mInitialFilePath = path;
//this.mInitialScrollPosition = NO_INITIAL_SCROLL_POSITION;
} else if (Global.debugEnabledSql) {
Log.i(Global.LOG_CONTEXT, mDebugPrefix + " onCreate() : query = " + mGalleryContentQuery + "; filter = " + mFilter);
}
}
private GalleryFilterParameter getParameterFromPath(String path) {
if ((path == null) || (path.length() == 0)) return null;
File selectedPhoto = new File(path);
this.mInitialFilePath = path;
this.mInitialScrollPosition = NO_INITIAL_SCROLL_POSITION;
GalleryFilterParameter filter = new GalleryFilterParameter().setPath(selectedPhoto.getParent() + "/%");
return filter;
}
/* these doe not work yet (tested with for android 4.0)
manifest <activity ... android:ellipsize="middle" />
@Override
public void setTitle(CharSequence title) {
super.setTitle(title);
// http://stackoverflow.com/questions/10779037/set-activity-title-ellipse-to-middle
final int ActionBarTitle = android.R.id.title; // Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
final TextView titleView = (TextView) this.getWindow().findViewById(ActionBarTitle);
if ( titleView != null ) {
titleView.setEllipsize(TextUtils.TruncateAt.MIDDLE);
}
}
*/
@Override
protected void onPause () {
unhideActionBar(DISABLE_HIDE_ACTIONBAR, "onPause");
Global.debugMemory(mDebugPrefix, "onPause");
startStopSlideShow(false);
super.onPause();
}
@Override
protected void onResume () {
unhideActionBar(Global.actionBarHideTimeInMilliSecs, "onResume");
Global.debugMemory(mDebugPrefix, "onResume");
super.onResume();
final boolean locked = LockScreen.isLocked(this);
if (this.locked != locked) {
this.locked = locked;
mMustReplaceMenue = true;
invalidateOptionsMenu();
}
if (Global.debugEnabledMemory) {
Log.d(Global.LOG_CONTEXT, mDebugPrefix + " - onResume cmd (" +
MoveOrCopyDestDirPicker.sFileCommands + ") => (" + mFileCommands +
")");
}
// workaround fragment lifecycle is newFragment.attach oldFragment.detach.
// this makes shure that the visible fragment has commands
MoveOrCopyDestDirPicker.sFileCommands = mFileCommands;
}
@Override
protected void onDestroy() {
Global.debugMemory(mDebugPrefix, "onDestroy");
mDestDirPicker = null;
unhideActionBar(DISABLE_HIDE_ACTIONBAR, "onDestroy");
if (mImageContextController != null) mImageContextController.close();
mImageContextController = null;
// getLoaderManager().destroyLoader(ACTIVITY_ID);
if (mAdapter != null) {
mViewPager.setAdapter(null);
mFileCommands.closeLogFile();
mFileCommands.closeAll();
mFileCommands.setContext(null);
}
// kill this instance only if not an other instance is active
if (MoveOrCopyDestDirPicker.sFileCommands == mFileCommands) {
if (Global.debugEnabledMemory) {
Log.d(Global.LOG_CONTEXT, mDebugPrefix + " - onDestroy cmd (" +
MoveOrCopyDestDirPicker.sFileCommands + ") => (null) ");
}
MoveOrCopyDestDirPicker.sFileCommands = null;
} else if (Global.debugEnabledMemory) {
Log.d(Global.LOG_CONTEXT, mDebugPrefix + " - onDestroy cmd [ignore] (" +
MoveOrCopyDestDirPicker.sFileCommands + ")");
}
if (mSlideShowTimer != null) {
startStopSlideShow(false);
mSlideShowTimer = null;
}
super.onDestroy();
// RefWatcher refWatcher = AndroFotoFinderApp.getRefWatcher(this);
// refWatcher.watch(this);
}
private void onLoadCompleted() {
if (mAdapter.getCount() == 0) {
// image not found in media database
if (checkForIncompleteMediaDatabase(mInitialFilePath, "onLoadCompleted().count=0")) {
// this.finish();
} else {
// close activity if last image of current selection has been deleted
String message = getString(R.string.image_err_not_found_format, mInitialFilePath);
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
this.finish();
}
} else if (mInitialFilePath != null) {
// try to find selection by text
mViewPager.invalidate();
int positionFound = mAdapter.getPositionFromPath(mInitialFilePath);
if (positionFound < 0) {
// not found
checkForIncompleteMediaDatabase(mInitialFilePath, "onLoadCompleted(Selection='" +
mInitialFilePath + "' not found)");
if (mInitialScrollPosition >= 0) mViewPager.setCurrentItem(mInitialScrollPosition);
// mInitialFilePath = null; keep path so next requery/rotatate the selected image will be displayed
} else {
mViewPager.setCurrentItem(positionFound);
mInitialFilePath = null;
}
mInitialScrollPosition = NO_INITIAL_SCROLL_POSITION;
} else if (mInitialScrollPosition >= 0) {
// after initial load select correct image
mViewPager.invalidate();
mViewPager.setCurrentItem(mInitialScrollPosition);
mInitialScrollPosition = NO_INITIAL_SCROLL_POSITION;
mInitialFilePath = null;
}
}
private void refreshIfNecessary() {
if ((mAdapter != null) && (mViewPager != null) && (mAdapter.isInArrayMode())) {
mAdapter.refreshLocal();
mViewPager.setAdapter(mAdapter);
// show the changes
onLoadCompleted();
}
}
/**
* gets called if no file is found by a db-query or if jpgFullFilePath is not found in media db
* return false; activity must me closed
*/
private boolean checkForIncompleteMediaDatabase(String jpgFullFilePath, String why) {
if (!PhotoPropertiesMediaFilesScanner.isNoMedia(jpgFullFilePath, PhotoPropertiesMediaFilesScanner.DEFAULT_SCAN_DEPTH)) {
File fileToLoad = (jpgFullFilePath != null) ? new File(jpgFullFilePath) : null;
if ((!this.mWaitingForMediaScannerResult) && (fileToLoad != null) && (fileToLoad.exists()) && (fileToLoad.canRead())) {
// file exists => must update media database
this.mWaitingForMediaScannerResult = true;
int numberOfNewItems = updateIncompleteMediaDatabase(mDebugPrefix, this,
mDebugPrefix + "checkForIncompleteMediaDatabase-" + why,
fileToLoad.getParentFile());
String message = getString(R.string.image_err_not_in_db_format, jpgFullFilePath, numberOfNewItems);
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
return true;
}
this.mWaitingForMediaScannerResult = false;
}
return false;
}
private static int updateIncompleteMediaDatabase(String debugPrefix, Context context, String why, File dirToScan) {
if (dirToScan == null) return 0;
String dbPathSearch = null;
ArrayList<String> missing = new ArrayList<String>();
dbPathSearch = dirToScan.getPath() + "/%";
List<String> known = FotoSql.execGetFotoPaths(dbPathSearch);
File[] existing = dirToScan.listFiles();
if (existing != null) {
for (File file : existing) {
String found = file.getAbsolutePath();
if (PhotoPropertiesUtil.isImage(found, PhotoPropertiesUtil.IMG_TYPE_ALL) && !known.contains(found)) {
missing.add(found);
}
}
}
if (Global.debugEnabled) {
StringBuilder message = new StringBuilder();
message.append(debugPrefix).append("updateIncompleteMediaDatabase('")
.append(dbPathSearch).append("') : \n\t");
for(String s : missing) {
message.append(s).append("; ");
}
Log.d(Global.LOG_CONTEXT, message.toString());
}
PhotoPropertiesMediaFilesScannerAsyncTask scanner = new PhotoPropertiesMediaFilesScannerAsyncTask(PhotoPropertiesMediaFilesScanner.getInstance(context), context, why);
scanner.execute(null, missing.toArray(new String[missing.size()]));
return missing.size();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
defineMenu(menu);
boolean result = super.onCreateOptionsMenu(menu);
return result;
}
private void defineMenu(Menu menu) {
if (LockScreen.isLocked(this)) {
getMenuInflater().inflate(R.menu.menu_image_detail_locked, menu);
LockScreen.removeDangerousCommandsFromMenu(menu);
} else {
getMenuInflater().inflate(R.menu.menu_image_detail, menu);
getMenuInflater().inflate(R.menu.menu_image_commands, menu);
MenuItem item = menu.findItem(R.id.cmd_filemanager);
final boolean hasShowInFilemanager = FileManagerUtil.hasShowInFilemanager(this, "/a/b/");
if ((item != null) && !hasShowInFilemanager) {
// no filemanager installed
item.setVisible(false);
}
Global.fixMenu(this, menu);
}
mMenuSlideshow = menu.findItem(R.id.action_slideshow);
if (mAdapter != null) mAdapter.setMenu(menu);
getOrCreateContextTextController().setMenu(menu);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
final boolean locked = LockScreen.isLocked(this);
if (mMustReplaceMenue || (locked != this.locked)) {
this.locked = locked;
mMustReplaceMenue = false;
menu.clear();
defineMenu(menu);
}
// have more time to find and press the menu
unhideActionBar(Global.actionBarHideTimeInMilliSecs * 3, "onPrepareOptionsMenu");
AboutDialogPreference.onPrepareOptionsMenu(this, menu);
MenuItem item = menu.findItem(R.id.cmd_show_geo);
if (item != null) {
item.setVisible(hasCurrentGeo());
}
item = menu.findItem(R.id.cmd_show_geo_as);
if (item != null) {
item.setVisible(hasCurrentGeo());
}
if (mAdapter != null) mAdapter.setMenu(menu);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
boolean reloadContext = true;
boolean result = true;
boolean slideShowStarted = mSlideShowStarted;
onGuiTouched();
if (LockScreen.onOptionsItemSelected(this, menuItem)) {
mMustReplaceMenue = true;
this.invalidateOptionsMenu();
return true;
}
if (mFileCommands.onOptionsItemSelected(menuItem, getCurrentFoto())) {
mModifyCount++;
} else {
// Handle presses on the action bar items
switch (menuItem.getItemId()) {
case R.id.action_details:
cmdShowDetails(getCurrentFilePath(), getCurrentImageId());
break;
case R.id.action_view_context_mode:
pickContextDefinition();
break;
case R.id.cmd_filemanager:
FileManagerUtil.showInFilemanager(this, getCurrentDir());
break;
case R.id.action_slideshow:
reloadContext = false;
// only if not started
if (!slideShowStarted) startStopSlideShow(true);
break;
case R.id.action_edit:
// #64: (not) open editor via chooser
IntentUtil.cmdStartIntent("edit", this, getCurrentFilePath(), null, null,
Intent.ACTION_EDIT,
(Global.showEditChooser) ? R.string.edit_chooser_title : 0,
R.string.edit_err_editor_not_found, ACTION_RESULT_MUST_MEDIA_SCAN);
break;
case R.id.menu_item_share:
reloadContext = false;
IntentUtil.cmdStartIntent("share", this, null, null, getCurrentFilePath(), Intent.ACTION_SEND, R.string.share_menu_title, R.string.share_err_not_found, 0);
break;
case R.id.cmd_copy:
result = cmdMoveOrCopyWithDestDirPicker(false, mFileCommands.getLastCopyToPath(), getCurrentFoto());
break;
case R.id.cmd_move:
result = cmdMoveOrCopyWithDestDirPicker(true, mFileCommands.getLastCopyToPath(), getCurrentFoto());
break;
case R.id.menu_item_rename:
result = onRenameDirQueston(getCurrentFoto(), getCurrentImageId(), getCurrentFilePath(), null);
break;
case R.id.menu_exif:
result = onEditExif(menuItem, getCurrentFoto(), getCurrentImageId(), getCurrentFilePath());
break;
case R.id.cmd_gallery: {
reloadContext = false;
String dirPath = getCurrentFilePath(); // PhotoPropertiesMediaFilesScanner.getDir().getAbsolutePath();
if (dirPath != null) {
dirPath = FileUtils.getDir(dirPath).getAbsolutePath();
GalleryFilterParameter newFilter = new GalleryFilterParameter();
newFilter.setPath(dirPath);
// int callBackId = (PhotoPropertiesMediaFilesScanner.isNoMedia(dirPath,PhotoPropertiesMediaFilesScanner.DEFAULT_SCAN_DEPTH)) ? NOMEDIA_GALLERY : 0;
QueryParameter query = TagSql.filter2NewQuery(this.mFilter);
FotoGalleryActivity.showActivity(" menu " + menuItem.getTitle() + "[13]" + dirPath,
this, query, 0);
}
break;
}
case R.id.cmd_show_geo:
MapGeoPickerActivity.showActivity(" menu " + menuItem.getTitle(),
this, getCurrentFoto(), null, null, 0);
break;
case R.id.cmd_show_geo_as: {
final long imageId = getCurrentImageId();
IGeoPoint _geo = FotoSql.execGetPosition(null,
null, imageId, mDebugPrefix, "on cmd_show_geo_as");
final String currentFilePath = getCurrentFilePath();
GeoPointDto geo = new GeoPointDto(_geo.getLatitude(), _geo.getLongitude(), GeoPointDto.NO_ZOOM);
geo.setDescription(currentFilePath);
geo.setId(""+imageId);
geo.setName("#"+imageId);
GeoUri PARSER = new GeoUri(GeoUri.OPT_PARSE_INFER_MISSING);
String uri = PARSER.toUriString(geo);
IntentUtil.cmdStartIntent("cmd_show_geo_as", this, null, uri, null, Intent.ACTION_VIEW, R.string.geo_show_as_menu_title, R.string.geo_picker_err_not_found, 0);
break;
}
case R.id.cmd_edit_geo: {
SelectedFiles selectedItem = getCurrentFoto();
GeoEditActivity.showActivity(" menu " + menuItem.getTitle() + " " + selectedItem,
this, selectedItem, GeoEditActivity.RESULT_ID);
break;
}
case R.id.cmd_edit_tags: {
SelectedFiles selectedItem = getCurrentFoto();
tagsShowEditDialog(selectedItem);
break;