/*
 * Android Scan, an app for scanning and configuring HBM devices.
 *
 * The MIT License (MIT)
 *
 * Copyright (C) Stephan Gatzka
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.hbm.devices.scan.ui.android;

import android.net.Uri;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import com.hbm.devices.scan.announce.Announce;

public final class DeviceZipper {

    static Uri saveAnnounce(Announce announce, AppCompatActivity activity) {
        List<Announce> list = new ArrayList<>();
        list.add(announce);
        return saveAnnounces(list, activity);
    }

    static Uri saveAnnounces(List<Announce> announces, AppCompatActivity activity) {
        TimeZone tz = TimeZone.getTimeZone("UTC");
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'", Locale.US);
        df.setTimeZone(tz);
        String isoDate = df.format(new Date());
        Charset charSet = Charset.forName("UTF-8");

        try {
            File cacheDir = activity.getCacheDir();
            File subDir = new File(cacheDir, "devices");
            if ( !subDir.exists() ) {
                if (!subDir.mkdirs()) {
                    return null;
                }
            }
            File file = new File(subDir, "devices.zip");
            file.createNewFile();
            FileOutputStream fos = new FileOutputStream(file);
            ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos));
            ZipEntry entry = new ZipEntry("devices.json");
            zos.putNextEntry(entry);
            zos.write(("{\"date\":\"" + isoDate + "\",").getBytes(charSet)); 
            zos.write("\"devices\":[".getBytes(charSet));

            Iterator<Announce> iterator = announces.iterator();
            while (iterator.hasNext()) {
                final Announce announce = iterator.next();
                zos.write(announce.getJSONString().getBytes(charSet));
                if (iterator.hasNext()) {
                    zos.write(",\n".getBytes(charSet));
                }
            }

            zos.write("]}".getBytes(charSet));
            zos.closeEntry();
            zos.close();
            fos.close();
            return FileProvider.getUriForFile(activity, "com.hbm.devices.scan.ui.android.fileprovider", file);

        } catch (IOException e) {
            return null;
        }
    }
}


@@ -2786,16 +2786,20 @@
         
+if (
 file.cre
@@ -2810,18 +2810,22 @@
 ewFile()
-;%0A
+) %7B%0A  
         
@@ -2824,24 +2824,26 @@
             
+  
 FileOutputSt
@@ -2877,24 +2877,28 @@
 ream(file);%0A
+    
             
@@ -2983,16 +2983,20 @@
         
+   
+ 
 ZipEntry
@@ -3030,24 +3030,28 @@
 ces.json%22);%0A
+    
             
@@ -3079,32 +3079,36 @@
 y);%0A            
+    
 zos.write((%22%7B%5C%22d
@@ -3159,10 +3159,13 @@
 t));
- 
 %0A
+    
     
@@ -3231,16 +3231,20 @@
         
+    
 Iterator
@@ -3299,16 +3299,20 @@
         
+ 
+   
 while (i
@@ -3324,32 +3324,36 @@
 or.hasNext()) %7B%0A
+    
                 
@@ -3403,32 +3403,36 @@
                 
+   
+ 
 zos.write(announ
@@ -3482,24 +3482,28 @@
             
+    
 if (iterator
@@ -3508,32 +3508,36 @@
 or.hasNext()) %7B%0A
+    
                 
@@ -3584,34 +3584,42 @@
                 
-%7D%0A
+    %7D%0A    
             %7D%0A%0A 
@@ -3613,24 +3613,28 @@
          %7D%0A%0A
+    
             
@@ -3672,32 +3672,36 @@
 ));%0A            
+    
 zos.closeEntry()
@@ -3706,32 +3706,36 @@
 ();%0A            
+  
+  
 zos.close();%0A   
@@ -3723,32 +3723,36 @@
    zos.close();%0A
+    
             fos.
@@ -3752,32 +3752,36 @@
    fos.close();%0A
+    
             retu
@@ -3874,16 +3874,80 @@
 , file);
+%0A            %7D else %7B%0A                return null;%0A            %7D
 %0A%0A      

Check if file was successfully created.