-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdcim-compliant-1.patch
More file actions
417 lines (409 loc) · 15.2 KB
/
Copy pathdcim-compliant-1.patch
File metadata and controls
417 lines (409 loc) · 15.2 KB
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
diff --git a/src/com/android/camera/Camera.java b/src/com/android/camera/Camera.java
index 66b1805..f7e9a43 100644
--- a/src/com/android/camera/Camera.java
+++ b/src/com/android/camera/Camera.java
@@ -70,6 +70,7 @@ import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
+import com.android.camera.dcim.DCIMHelper;
import com.android.camera.gallery.IImage;
import com.android.camera.gallery.IImageList;
import com.android.camera.ui.CameraHeadUpDisplay;
@@ -356,6 +357,7 @@ public class Camera extends NoSearchActivity implements View.OnClickListener,
queue.addIdleHandler(new MessageQueue.IdleHandler() {
public boolean queueIdle() {
ImageManager.ensureOSXCompatibleFolder();
+ DCIMHelper.convertToDCIM();
return false;
}
});
@@ -719,7 +721,7 @@ public class Camera extends NoSearchActivity implements View.OnClickListener,
private int storeImage(byte[] data, Location loc) {
try {
long dateTaken = System.currentTimeMillis();
- String title = createName(dateTaken);
+ String title = /*createName(dateTaken)*/DCIMHelper.getNameForNewImage();
String filename = title + ".jpg";
int[] degree = new int[1];
mLastContentUri = ImageManager.addImage(
@@ -727,7 +729,7 @@ public class Camera extends NoSearchActivity implements View.OnClickListener,
title,
dateTaken,
loc, // location from gps/network
- ImageManager.CAMERA_IMAGE_BUCKET_NAME, filename,
+ /*ImageManager.CAMERA_IMAGE_BUCKET_NAME*/DCIMHelper.getDirectoryForNewImage(), filename,
null, data,
degree);
return degree[0];
@@ -1614,7 +1616,7 @@ public class Camera extends NoSearchActivity implements View.OnClickListener,
dataLocation(),
ImageManager.INCLUDE_IMAGES,
ImageManager.SORT_ASCENDING,
- ImageManager.CAMERA_IMAGE_BUCKET_ID);
+ ImageManager.CAMERA_IMAGE_BUCKET_ID());
int count = list.getCount();
if (count > 0) {
IImage image = list.getImageAt(count - 1);
diff --git a/src/com/android/camera/ImageManager.java b/src/com/android/camera/ImageManager.java
index 76a6d1d..b1585d3 100644
--- a/src/com/android/camera/ImageManager.java
+++ b/src/com/android/camera/ImageManager.java
@@ -16,6 +16,7 @@
package com.android.camera;
+import com.android.camera.dcim.DCIMHelper;
import com.android.camera.gallery.BaseImageList;
import com.android.camera.gallery.IImage;
import com.android.camera.gallery.IImageList;
@@ -127,13 +128,25 @@ public class ImageManager {
public static final int SORT_ASCENDING = 1;
public static final int SORT_DESCENDING = 2;
- public static final String CAMERA_IMAGE_BUCKET_NAME =
- Environment.getExternalStorageDirectory().toString()
- + "/DCIM/Camera";
- public static final String CAMERA_IMAGE_BUCKET_ID =
- getBucketId(CAMERA_IMAGE_BUCKET_NAME);
+// public static final String CAMERA_IMAGE_BUCKET_NAME =
+// Environment.getExternalStorageDirectory().toString()
+// + "/DCIM/Camera";
+// public static final String CAMERA_IMAGE_BUCKET_ID =
+// getBucketId(CAMERA_IMAGE_BUCKET_NAME);
+ /**
+ * DCIM standard compliant.
+ */
+ public static String CAMERA_IMAGE_BUCKET_NAME() {
+ return DCIMHelper.getCurrentDirectory();
+ }
/**
+ * DCIM standard compliant. We don't want to use a final variable for this, since CAMERA_IMAGE_BUCKET_NAME() returns a different value when the limit of images is reached.
+ */
+ public static String CAMERA_IMAGE_BUCKET_ID() {
+ return getBucketId(DCIMHelper.getCurrentDirectory());
+ }
+ /**
* Matches code in MediaProvider.computeBucketValues. Should be a common
* function.
*/
diff --git a/src/com/android/camera/MenuHelper.java b/src/com/android/camera/MenuHelper.java
index 629cf87..0fe8fb9 100644
--- a/src/com/android/camera/MenuHelper.java
+++ b/src/com/android/camera/MenuHelper.java
@@ -166,7 +166,7 @@ public class MenuHelper {
int mediaTypes) {
Uri target = Images.Media.EXTERNAL_CONTENT_URI.buildUpon()
.appendQueryParameter("bucketId",
- ImageManager.CAMERA_IMAGE_BUCKET_ID).build();
+ ImageManager.CAMERA_IMAGE_BUCKET_ID()).build();
Intent intent = new Intent(Intent.ACTION_VIEW, target);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("windowTitle", activity.getString(windowTitleId));
diff --git a/src/com/android/camera/VideoCamera.java b/src/com/android/camera/VideoCamera.java
index ee05f9e..d14fcec 100644
--- a/src/com/android/camera/VideoCamera.java
+++ b/src/com/android/camera/VideoCamera.java
@@ -16,6 +16,7 @@
package com.android.camera;
+import com.android.camera.dcim.DCIMHelper;
import com.android.camera.gallery.IImage;
import com.android.camera.gallery.IImageList;
import com.android.camera.ui.CamcorderHeadUpDisplay;
@@ -1044,9 +1045,9 @@ public class VideoCamera extends NoSearchActivity
private void createVideoPath() {
long dateTaken = System.currentTimeMillis();
- String title = createName(dateTaken);
+ String title = /*createName(dateTaken)*/DCIMHelper.getNameForNewVideo();
String filename = title + ".3gp"; // Used when emailing.
- String cameraDirPath = ImageManager.CAMERA_IMAGE_BUCKET_NAME;
+ String cameraDirPath = /*ImageManager.CAMERA_IMAGE_BUCKET_NAME*/DCIMHelper.getDirectoryForNewImage();
String filePath = cameraDirPath + "/" + filename;
File cameraDir = new File(cameraDirPath);
cameraDir.mkdirs();
@@ -1416,7 +1417,7 @@ public class VideoCamera extends NoSearchActivity
dataLocation(),
ImageManager.INCLUDE_VIDEOS,
ImageManager.SORT_ASCENDING,
- ImageManager.CAMERA_IMAGE_BUCKET_ID);
+ ImageManager.CAMERA_IMAGE_BUCKET_ID());
int count = list.getCount();
if (count > 0) {
IImage image = list.getImageAt(count - 1);
diff --git a/src/com/android/camera/dcim/DCIMHelper.java b/src/com/android/camera/dcim/DCIMHelper.java
new file mode 100644
index 0000000..17f1450
--- /dev/null
+++ b/src/com/android/camera/dcim/DCIMHelper.java
@@ -0,0 +1,275 @@
+/*
+ * Copyright (C) 2007 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.camera.dcim;
+
+import java.io.File;
+import java.io.FileFilter;
+import java.text.NumberFormat;
+import java.text.SimpleDateFormat;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import android.os.Environment;
+import android.util.Log;
+
+/**
+ * {@code DCIMHelper} is used to provide DCIM-compliant informations regarding
+ * directory and file naming.
+ */
+public class DCIMHelper {
+
+ private final static String TAG = DCIMHelper.class.getSimpleName();
+ /**
+ * See spec {@literal "4.2.2 DCF directories"}
+ */
+ private final static String RE_DCIM_DIRECTORY = "([1-9][0-9]{2})([A-Z_0-9]{5})(\\..+)?";
+
+ /**
+ * See spec {@literal "4.3.1 DCF file names"}
+ */
+ private final static String RE_DCIM_FILE = "([A-Z_0-9]{4})([0-9]{4})(\\..+)";
+
+ /**
+ * 512 is the limit for FAT(16), but OSX creates hidden (._xxx) files for each
+ * image, plus a "._DSStore" file and Windows creates a "Thumbs.db". A value
+ * of 254 gives us room for another 2 junk files lying around (Linus, not more
+ * than 2, ok?).
+ */
+ private final static int MAX_IMAGES_PER_DIRECTORY = 254;
+
+ private static String dcimPath = Environment.getExternalStorageDirectory().toString() + "/DCIM/";
+
+ private static DCIMDirectoryFileFilter dcimDirectoryFileFilter = new DCIMDirectoryFileFilter();
+ private static DCIMDirectoryComparator dcimDirectoryComparator = new DCIMDirectoryComparator();
+ private static DCIMImageFileFilter dcimImageFileFilter = new DCIMImageFileFilter();
+
+ private static NumberFormat nfDirectory;
+ private static NumberFormat nfFile;
+
+ static {
+ nfDirectory = NumberFormat.getIntegerInstance();
+ nfDirectory.setGroupingUsed(false);
+ nfDirectory.setMinimumIntegerDigits(3);
+ nfDirectory.setMaximumIntegerDigits(3);
+
+ nfFile = NumberFormat.getIntegerInstance();
+ nfFile.setGroupingUsed(false);
+ nfFile.setMinimumIntegerDigits(4);
+ nfFile.setMaximumIntegerDigits(4);
+
+ /*
+ * Ensure that the first directory exists. Makes remaining code simpler.
+ */
+ File firstDir = new File(dcimPath, "100ANDRO");
+ if (!firstDir.exists()) {
+ firstDir.mkdirs();
+ }
+ }
+
+ private static File currentDir = null;
+
+ /**
+ * Returns the full path to the directory currently in use.
+ *
+ * @return the full path, DCIM-compliant (NNNAAAAA)
+ */
+ public static String getCurrentDirectory() {
+ File dcimDirectory = new File(dcimPath);
+ if (currentDir == null) {
+ File[] dirs = dcimDirectory.listFiles(dcimDirectoryFileFilter);
+ if (dirs.length > 1) {
+ List<File> dirsList = Arrays.asList(dirs);
+ Collections.sort(dirsList, dcimDirectoryComparator);
+ currentDir = dirsList.get(dirs.length - 1);
+ } else {
+ currentDir = dirs[0];
+ }
+ }
+ return currentDir.getAbsolutePath();
+ }
+
+ /**
+ * Returns the full path to the directory to be used to store a new image.
+ *
+ * @return the full path, DCIM-compliant (NNNAAAAA)
+ */
+ public static String getDirectoryForNewImage() {
+ if (currentDir == null) {
+ getCurrentDirectory();
+ }
+
+ File[] files = currentDir.listFiles(dcimImageFileFilter);
+ if (files.length >= MAX_IMAGES_PER_DIRECTORY) {
+ makeNewDirectory();
+ }
+ return currentDir.getAbsolutePath();
+ }
+
+ /**
+ * Returns the name (without extension) for the next available "slot" in the
+ * current directory. The name is DCIM compliant, in the form "IMG_NNNN".
+ *
+ * @return the name, DCIM compliant (AAAANNNN), without extension.
+ */
+ public static String getNameForNewImage() {
+ return getNameForNewItem("IMG_");
+ }
+
+ /**
+ * Returns the name (without extension) for the next available "slot" in the
+ * current directory. The name is DCIM compliant, in the form "VID_NNNN".
+ *
+ * @return the name, DCIM compliant (AAAANNNN), without extension.
+ */
+ public static String getNameForNewVideo() {
+ return getNameForNewItem("VID_");
+ }
+
+ /**
+ * Returns the name (without extension) for the next available "slot" in the
+ * current directory.
+ *
+ * @return the name, DCIM compliant (AAAANNNN), without extension.
+ */
+ private static String getNameForNewItem(String prefix) {
+ if (currentDir == null) {
+ getCurrentDirectory();
+ }
+ File[] files = currentDir.listFiles(dcimImageFileFilter);
+ if (files.length >= MAX_IMAGES_PER_DIRECTORY) {
+ makeNewDirectory();
+ files = currentDir.listFiles(dcimImageFileFilter);
+ }
+ if (files.length == 0) {
+ return prefix + "0001";
+ }
+ List<File> filesList = Arrays.asList(files);
+ Collections.sort(filesList);
+ File lastFile = filesList.get(files.length - 1);
+ int nnnn = Integer.parseInt(lastFile.getName().substring(4, 8));
+ if (nnnn >= 9999) {
+ /*
+ * This should not happen, since out MAX_IMAGES_PER_DIRECTORY limit is way
+ * below 9999 and we are creating the images using consecutive numbers.
+ */
+ Log.e(TAG, "Unable to create new image name: already reached limit of 9999");
+ return null;
+ }
+ return prefix + nfFile.format(nnnn + 1);
+ }
+
+ private static void makeNewDirectory() {
+ int nnn = Integer.parseInt(currentDir.getName().substring(0, 3));
+ if (nnn < 999) {
+ nnn++;
+ File newDir = new File(dcimPath, nfDirectory.format(nnn) + "ANDRO");
+ if (newDir.mkdirs()) {
+ currentDir = newDir;
+ } else {
+ Log.e(TAG, "Unable to create NNNAAAAA directory: unable to create " + newDir.getName());
+ }
+ } else {
+ Log.e(TAG, "Unable to create NNNAAAAA directory: reached 999 directories.");
+ }
+ }
+
+ private static class DCIMDirectoryComparator implements Comparator<File> {
+ public int compare(File dir1, File dir2) {
+ return dir1.getName().substring(0, 3).compareTo(dir2.getName().substring(0, 3));
+ }
+ }
+
+ private static class DCIMDirectoryFileFilter implements FileFilter {
+ public boolean accept(File pathname) {
+ if (pathname.isDirectory()) {
+ return pathname.getName().matches(RE_DCIM_DIRECTORY);
+ }
+ return false;
+ }
+ }
+
+ private static class DCIMImageFileFilter implements FileFilter {
+ public boolean accept(File pathname) {
+ if (pathname.isFile()) {
+ return pathname.getName().matches(RE_DCIM_FILE);
+ }
+ return false;
+ }
+ }
+
+ /**
+ * Scans the "/DCIM/Camera" directory renaming files into a proper DCIM
+ * structure.
+ */
+ public static void convertToDCIM() {
+ File cameraDir = new File(dcimPath, "Camera");
+ File[] files = cameraDir.listFiles();
+ List<File> filesList = Arrays.asList(files);
+ Pattern p1 = Pattern.compile("\\d{4}-\\d{2}-\\d{2} \\d{2}.\\d{2}.\\d{2}");
+ SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");
+ Pattern p2 = Pattern.compile("\\d{8}_\\d{6}");
+ SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd_HHmmss");
+ for (File file : filesList) {
+ try {
+ Matcher m1 = p1.matcher(file.getName());
+ if (m1.find()) {
+ long dateTaken = sdf1.parse(m1.group()).getTime();
+ file.setLastModified(dateTaken);
+ } else {
+ Matcher m2 = p2.matcher(file.getName());
+ if (m2.find()) {
+ long dateTaken = sdf2.parse(m2.group()).getTime();
+ file.setLastModified(dateTaken);
+ }
+ }
+ } catch (java.text.ParseException pex) {
+ pex.printStackTrace();
+ }
+ }
+
+ Collections.sort(filesList, new Comparator<File>() {
+ public int compare(File object1, File object2) {
+ if (object1.lastModified() < object2.lastModified()) {
+ return -1;
+ } else if (object1.lastModified() > object2.lastModified()) {
+ return 1;
+ } else {
+ return 0;
+ }
+ }
+ });
+ for (File file : filesList) {
+ String name = file.getName().toLowerCase();
+ String newName = null;
+ if (name.endsWith("jpg")) {
+ newName = getNameForNewImage() + ".jpg";
+ } else if (name.endsWith("3gp")) {
+ newName = getNameForNewVideo() + ".3gp";
+ } else {
+ continue;
+ }
+ File newFile = new File(getDirectoryForNewImage(), newName);
+ if (!file.renameTo(newFile)) {
+ Log.e(TAG, "Error renaming " + file.getAbsolutePath() + " to " + newFile.getAbsolutePath());
+ }
+ }
+ }
+}