/*
 * 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");
            if (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);
            } else {
                return null;
            }

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


@@ -2590,17 +2590,16 @@
     if (
- 
 !subDir.
@@ -2785,20 +2785,16 @@
         
-if (
 file.cre
@@ -2805,24 +2805,18 @@
 ewFile()
-) %7B%0A    
+;%0A
         
@@ -2863,24 +2863,31 @@
 tStream(file
+, false
 );%0A         
@@ -2889,20 +2889,16 @@
         
- 
-   
 ZipOutpu
@@ -2975,20 +2975,16 @@
         
-    
 ZipEntry
@@ -3030,28 +3030,24 @@
             
-   
- 
 zos.putNextE
@@ -3059,20 +3059,16 @@
 entry);%0A
-    
         
@@ -3128,36 +3128,32 @@
 ytes(charSet));%0A
-    
             zos.
@@ -3207,20 +3207,16 @@
         
-    
 Iterator
@@ -3271,20 +3271,16 @@
         
-    
 while (i
@@ -3316,20 +3316,16 @@
         
-  
-  
 final An
@@ -3355,28 +3355,24 @@
 tor.next();%0A
-    
             
@@ -3422,36 +3422,32 @@
 ytes(charSet));%0A
-    
                 
@@ -3484,36 +3484,32 @@
                 
-    
 zos.write(%22,%5Cn%22.
@@ -3540,32 +3540,26 @@
             
-    %7D%0A  
+%7D%0A
             
@@ -3554,33 +3554,27 @@
             
-  
 %7D%0A%0A
-    
             
@@ -3612,36 +3612,32 @@
 ));%0A            
-   
- 
 zos.closeEntry()
@@ -3642,36 +3642,32 @@
 ();%0A            
-    
 zos.close();%0A   
@@ -3675,20 +3675,16 @@
         
-  
-  
 fos.clos
@@ -3680,36 +3680,32 @@
    fos.close();%0A
-    
             retu
@@ -3803,73 +3803,8 @@
 e);%0A
-            %7D else %7B%0A                return null;%0A            %7D%0A%0A
     

Allow sharing of device list multiple times.