/
PhotoPropertiesMediaDBCsvImportActivity.java
276 lines (240 loc) · 10.7 KB
/
PhotoPropertiesMediaDBCsvImportActivity.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
/*
* Copyright (c) 2016-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.media;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Date;
import de.k3b.android.androFotoFinder.Global;
import de.k3b.android.androFotoFinder.R;
import de.k3b.android.androFotoFinder.tagDB.TagSql;
import de.k3b.android.util.AndroidFileCommands;
import de.k3b.android.util.IntentUtil;
import de.k3b.csv2db.csv.CsvLoader;
import de.k3b.io.FileUtils;
import de.k3b.io.VISIBILITY;
import de.k3b.media.PhotoPropertiesCsvItem;
import de.k3b.media.PhotoPropertiesUtil;
import de.k3b.tagDB.Tag;
import de.k3b.tagDB.TagRepository;
public class PhotoPropertiesMediaDBCsvImportActivity extends Activity {
public static final String DBG_CONTEXT = "MediaDB-Import(Csv): ";
/** Does the import into MediaDB in background task */
private AsyncTask<Uri,String,String> mTask = null;
private TextView mStatus;
private TextView mFolder;
@Override protected void onDestroy() {
if (mTask != null) {
// cancel background import if active
mTask.cancel(false);
mTask = null;
}
super.onDestroy();
}
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Uri uri = getUri();
if (uri == null) {
finish();
} else {
setContentView(R.layout.activity_media_db);
setTitle(R.string.scanner_menu_title);
mFolder = (TextView) findViewById(R.id.folder);
mStatus = (TextView) findViewById(R.id.status);
mFolder.setText(uri.toString());
mStatus.setText("");
findViewById(R.id.cmd_cancel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mTask != null) mTask.cancel(false);
finish();
}
});
findViewById(R.id.cmd_ok).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startScanner(uri);
}
});
}
}
private void startScanner(Uri uri) {
if ((AndroidFileCommands.canProcessFile(this, false)) && (uri != null)) {
mTask = new TaskLoadMediaDB();
mTask.execute(uri);
}
}
private Uri getUri() {
final String action = getIntent().getAction();
if(Intent.ACTION_VIEW.equals(action)) {
return IntentUtil.getUri(getIntent());
}
return null;
}
/** Does the import into MediaDB in background task */
private class TaskLoadMediaDB extends AsyncTask<Uri,String,String> {
private int mItemCount = 0;
private int mUpdateCount = 0;
private int mProgressCountDown = 0;
protected MediaCsvLoader mLoader = null;
/**
* Override this method to perform a computation on a background thread. The
* specified parameters are the parameters passed to {@link #execute}
* by the caller of this task.
* <p>
* This method can call {@link #publishProgress} to publish updates
* on the UI thread.
*
* @param params The parameters of the task.
* @return A result, defined by the subclass of this task.
* @see #onPreExecute()
* @see #onPostExecute
* @see #publishProgress
*/
@Override
protected String doInBackground(Uri... params) {
return processUri(params[0]);
}
protected String processUri(Uri uri) {
File file = IntentUtil.getFile(uri);
if (file != null) {
Reader reader = null;
try {
File csvRootDir = FileUtils.tryGetCanonicalFile(file.getParentFile(), null);
if ((csvRootDir != null) && file.getName().endsWith(".csv")) {
Log.i(Global.LOG_CONTEXT, DBG_CONTEXT + "start form " + uri.toString());
reader = new InputStreamReader(getContentResolver().openInputStream(uri));
mLoader = new MediaCsvLoader(csvRootDir);
mLoader.load(reader, new PhotoPropertiesCsvItem());
if (Global.Media.enableXmpNone) {
// set all xmp-file-dates to EXT_LAST_EXT_SCAN_NO_XMP_IN_CSV if null
mProgressCountDown = 10;
publishProgress(getString(R.string.scanner_menu_title) + " (" + mItemCount + ", +" + mUpdateCount + ") " + uri.toString());
updateDB("set all xmp-file-dates to EXT_LAST_EXT_SCAN_NO_XMP_IN_CSV", csvRootDir.getAbsolutePath() + "/%", TagSql.EXT_LAST_EXT_SCAN_NO_XMP_IN_CSV, new ContentValues());
}
}
mLoader = null;
if (reader != null) reader.close();
} catch (Exception e) {
String msg = DBG_CONTEXT +
"[" + e.getClass().getSimpleName() +
"]: Error processing " + uri + ": " + e.getMessage();
Log.e(Global.LOG_CONTEXT, msg, e);
mLoader = null;
mTask = null;
return msg;
}
}
mTask = null;
String message = getString(R.string.image_success_update_format, mUpdateCount);
Log.i(Global.LOG_CONTEXT, DBG_CONTEXT + uri.toString() + ": " + message);
return message;
}
private void updateDB(String dbgContext, String _path, long xmlLastFileModifyDate, ContentValues dbValues) {
String path = _path;
if (path != null) {
if (!path.contains("%")) {
if (PhotoPropertiesUtil.isImage(path, PhotoPropertiesUtil.IMG_TYPE_ALL)) {
// non xmp-file: do not update file modify date
xmlLastFileModifyDate = (Global.Media.enableXmpNone)
? TagSql.EXT_LAST_EXT_SCAN_NO_XMP_IN_CSV
: TagSql.EXT_LAST_EXT_SCAN_UNKNOWN;
} else {
// xmp-file: find path without extension
path = FileUtils.replaceExtension(path, ".%");
}
}
if (--mProgressCountDown <= 0) {
mProgressCountDown = 10;
publishProgress("(" + mItemCount + ", +" + mUpdateCount + ") " + path);
if (isCancelled() && (mLoader != null)) {
mLoader.cancel();
}
}
if (xmlLastFileModifyDate != TagSql.EXT_LAST_EXT_SCAN_UNKNOWN) {
TagSql.setXmpFileModifyDate(dbValues, xmlLastFileModifyDate);
}
TagSql.setFileModifyDate(dbValues, new Date().getTime() / 1000);
mUpdateCount += TagSql.execUpdate(dbgContext, path, xmlLastFileModifyDate, dbValues, VISIBILITY.PRIVATE_PUBLIC);
mItemCount++;
}
}
@Override
protected void onProgressUpdate(String... values) {
mStatus.setText(values[0]);
}
@Override
protected void onPostExecute(String errorMessage) {
if (Global.Media.enableIptcMediaScanner) {
TagRepository.getInstance().save();
}
if (!isCancelled()) {
if (errorMessage != null) {
Toast.makeText(PhotoPropertiesMediaDBCsvImportActivity.this, errorMessage, Toast.LENGTH_LONG).show();
}
finish();
}
}
private class MediaCsvLoader extends CsvLoader<PhotoPropertiesCsvItem> {
/**
* path-s in csv are relative to mCsvRootDir
*/
private final File mCsvRootDir;
private ContentValues mDbValues = new ContentValues();
private PhotoPropertiesMediaDBContentValues mMediaValueAdapter = new PhotoPropertiesMediaDBContentValues();
private Tag mImportRoot = null;
public MediaCsvLoader(File csvRootDir) {
mCsvRootDir = csvRootDir;
}
@Override
protected void onNextItem(PhotoPropertiesCsvItem next, int lineNumber, int recordNumber) {
if (next != null) {
String path = next.getPath();
next.setPath(null);
mDbValues.clear();
if ((path != null) && (PhotoPropertiesUtil.copy(mMediaValueAdapter.set(mDbValues, null), next, false, false) >= 1)) {
// >= 1 means at least one extra attribute (excluding path that has been set to null)
Date fileModifyDate = next.getFileModifyDate();
long fileModifyDateMilliSecs = (fileModifyDate != null) ? fileModifyDate.getTime() : TagSql.EXT_LAST_EXT_SCAN_NO_XMP_IN_CSV;
String canonicalPath = FileUtils.tryGetCanonicalPath(new File(mCsvRootDir, path), null);
updateDB("MediaCsvLoader.onNextItem", canonicalPath, fileModifyDateMilliSecs, mDbValues);
TagRepository.getInstance().includeTagNamesIfNotFound(mMediaValueAdapter.getTags());
}
}
}
/** get or create parent-tag where alle imports are appendend as children */
public Tag getImportRoot() {
if (mImportRoot == null) {
mImportRoot = TagRepository.getInstance().getImportRoot();
}
return mImportRoot;
}
}
}
}