Browse Source

Use exif tags

Mario Danic 8 years ago
parent
commit
79fb4e884b

+ 35 - 2
src/com/owncloud/android/services/FileAlterationMagicListener.java

@@ -23,8 +23,10 @@ import android.app.job.JobInfo;
 import android.app.job.JobScheduler;
 import android.content.ComponentName;
 import android.content.Context;
+import android.media.ExifInterface;
 import android.os.Handler;
 import android.os.PersistableBundle;
+import android.text.TextUtils;
 
 import com.owncloud.android.MainApp;
 import com.owncloud.android.datamodel.SyncedFolder;
@@ -35,9 +37,14 @@ import org.apache.commons.io.monitor.FileAlterationListener;
 import org.apache.commons.io.monitor.FileAlterationObserver;
 
 import java.io.File;
+import java.io.IOException;
+import java.text.ParsePosition;
+import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.Locale;
 import java.util.Map;
+import java.util.TimeZone;
 
 /**
  * Magical file alteration listener
@@ -84,14 +91,40 @@ public class FileAlterationMagicListener implements FileAlterationListener {
     @Override
     public void onFileCreate(final File file) {
         if (file != null) {
-            final Runnable runnable = new Runnable() {
+
+            String mimetypeString = FileStorageUtils.getMimeTypeFromName(file.getAbsolutePath());
+            Long dateFolder = file.lastModified();
+            final Locale current = context.getResources().getConfiguration().locale;
+
+            if (mimetypeString.equalsIgnoreCase("image/jpeg") || mimetypeString.equals("image/tiff")) {
+                try {
+                    ExifInterface exifInterface = new ExifInterface(file.getAbsolutePath());
+                    if (!TextUtils.isEmpty(exifInterface.getAttribute(ExifInterface.TAG_DATETIME))) {
+                        String exifDate = exifInterface.getAttribute(ExifInterface.TAG_DATETIME);
+                        ParsePosition pos = new ParsePosition(0);
+                        SimpleDateFormat sFormatter = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss", current);
+                        sFormatter.setTimeZone(TimeZone.getTimeZone(TimeZone.getDefault().getID()));
+                        Date datetime = sFormatter.parse(exifDate, pos);
+                        dateFolder = datetime.getTime();
+                    }
+
+                } catch (IOException e) {
+                    Log_OC.d(TAG, "Failed to get the proper time " + e.getLocalizedMessage());
+                }
+            }
+
+
+            final Long finalDateFolder = dateFolder;
+
+                final Runnable runnable = new Runnable() {
                 @Override
                 public void run() {
                     PersistableBundle bundle = new PersistableBundle();
                     bundle.putString(SyncedFolderJobService.LOCAL_PATH, file.getAbsolutePath());
                     bundle.putString(SyncedFolderJobService.REMOTE_PATH, FileStorageUtils.getInstantUploadFilePath(
+                            current,
                             syncedFolder.getRemotePath(), file.getName(),
-                            file.lastModified(),
+                            finalDateFolder,
                             syncedFolder.getSubfolderByDate()));
                     bundle.putString(SyncedFolderJobService.ACCOUNT, syncedFolder.getAccount());
                     bundle.putInt(SyncedFolderJobService.UPLOAD_BEHAVIOUR, syncedFolder.getUploadAction());

+ 29 - 2
src/com/owncloud/android/utils/FileStorageUtils.java

@@ -47,6 +47,8 @@ import java.util.Collections;
 import java.util.Comparator;
 import java.util.Date;
 import java.util.List;
+import java.util.Locale;
+import java.util.TimeZone;
 import java.util.Vector;
 
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
@@ -124,17 +126,32 @@ public class FileStorageUtils {
      * @param date: date in microseconds since 1st January 1970
      * @return string: yyyy/mm/
      */
+    private static String getSubpathFromDate(long date, Locale currentLocale) {
+        if (date == 0) {
+            return "";
+        }
+
+        Date d = new Date(date);
+
+        DateFormat df = new SimpleDateFormat("yyyy/MM/", currentLocale);
+        df.setTimeZone(TimeZone.getTimeZone(TimeZone.getDefault().getID()));
+
+        return df.format(d);
+
+
+    }
+
     private static String getSubpathFromDate(long date) {
         if (date == 0) {
             return "";
         }
 
         Date d = new Date(date);
+
         DateFormat df = new SimpleDateFormat("yyyy/MM/");
 
         return df.format(d);
 
-
     }
 
     /**
@@ -144,15 +161,25 @@ public class FileStorageUtils {
      * @param dateTaken: Time in milliseconds since 1970 when the picture was taken.
      * @return instantUpload path, eg. /Camera/2017/01/fileName
      */
+    public static String getInstantUploadFilePath(Locale current, String remotePath, String fileName, long dateTaken,
+                                                  Boolean subfolderByDate) {
+        String subPath = "";
+        if (subfolderByDate) {
+           subPath = getSubpathFromDate(dateTaken, current);
+        }
+        return remotePath + OCFile.PATH_SEPARATOR + subPath + (fileName == null ? "" : fileName);
+    }
+
     public static String getInstantUploadFilePath(String remotePath, String fileName, long dateTaken,
                                                   Boolean subfolderByDate) {
         String subPath = "";
         if (subfolderByDate) {
-           subPath = getSubpathFromDate(dateTaken);
+            subPath = getSubpathFromDate(dateTaken);
         }
         return remotePath + OCFile.PATH_SEPARATOR + subPath + (fileName == null ? "" : fileName);
     }
 
+
     /**
      * Gets the composed path when video is or must be stored
      * @param context