Use values from attributes for DateTime tag.

The library uses values from attributes for DateTime tag if found in the attribute set. This is to
fix the bug when there is a DateTime tag from java side, jhead library will write multiple DateTime
tags.

bug:5766177
Change-Id: I98de235edabdd1de4fc7d111ca13f3b580c2e351
diff --git a/exif.c b/exif.c
index 8d8c0c0..381d9dc 100644
--- a/exif.c
+++ b/exif.c
@@ -311,6 +311,11 @@
     return -1;
 }
 
+int IsDateTimeTag(unsigned short tag)
+{
+    return ((tag == TAG_DATETIME)? TRUE: FALSE);
+}
+
 //--------------------------------------------------------------------------
 // Convert a 16 bit unsigned value to file's native byte order
 //--------------------------------------------------------------------------
@@ -1200,7 +1205,7 @@
 // Create minimal exif header - just date and thumbnail pointers,
 // so that date and thumbnail may be filled later.
 //--------------------------------------------------------------------------
-static void create_EXIF_internal(ExifElement_t* elements, int exifTagCount, int gpsTagCount, char* Buffer)
+static void create_EXIF_internal(ExifElement_t* elements, int exifTagCount, int gpsTagCount, int hasDateTimeTag, char* Buffer)
 {
     unsigned short NumEntries;
     int DataWriteIndex;
@@ -1221,21 +1226,26 @@
 
     {
         DirIndex = DataWriteIndex;
-        NumEntries = 2 + exifTagCount;  // the two extra are the datetime and the thumbnail
+        NumEntries = 1 + exifTagCount;  // the extra is the thumbnail
         if (gpsTagCount) {
             ++NumEntries;       // allow for the GPS info tag
         }
+        if (!hasDateTimeTag) {
+            // We have to write extra date time tag. The entry number should be
+            // adjusted.
+            ++NumEntries;
+        }
         DataWriteIndex += 2 + NumEntries*12 + 4;
 
         Put16u(Buffer+DirIndex, NumEntries); // Number of entries
         DirIndex += 2;
   
         // Entries go here...
-        {
+        if (!hasDateTimeTag) {
             // Date/time entry
             char* dateTime = NULL;
             char dateBuf[20];
-            if (ImageInfo.numDateTimeTags){
+            if (ImageInfo.numDateTimeTags) {
                 // If we had a pre-existing exif header, use time from that.
                 dateTime = ImageInfo.DateTime;
             } else {
@@ -1275,7 +1285,7 @@
                                     &DirIndex,
                                     &DataWriteIndex);
             }
-        
+
             if (gpsTagCount) {
                 // Link to gps dir entry
                 writeExifTagAndData(TAG_GPSINFO,
@@ -1412,7 +1422,7 @@
     }
 }
 
-void create_EXIF(ExifElement_t* elements, int exifTagCount, int gpsTagCount)
+void create_EXIF(ExifElement_t* elements, int exifTagCount, int gpsTagCount, int hasDateTimeTag)
 {
     // It is hard to calculate exact necessary size for editing the exif
     // header dynamically, so we are using the maximum size of EXIF, 64K
@@ -1420,7 +1430,7 @@
     char* Buffer = malloc(EXIF_MAX_SIZE);
 
     if (Buffer != NULL) {
-        create_EXIF_internal(elements, exifTagCount, gpsTagCount, Buffer);
+        create_EXIF_internal(elements, exifTagCount, gpsTagCount, hasDateTimeTag, Buffer);
         free(Buffer);
     } else {
         ErrFatal("Could not allocate memory");