package cl.uchile.ing.adi.quicklook;

import android.content.Intent;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;

import org.apache.commons.io.IOUtils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;

import cl.uchile.ing.adi.quicklooklib.QuicklookActivity;

public class MainActivity extends AppCompatActivity implements DemoAssetFragment.OnDemoAssetFragmentListener {
    private static String FILES_ASSETS_DIR = "files";
    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Fragment fragment = DemoAssetFragment.newInstance();
        FragmentTransaction t = getSupportFragmentManager().beginTransaction();
        t.replace(R.id.main_activity_fragment , fragment, "MainQuickLook");
        t.commit();
    }

    @Override public void onAssetSelected(String item) {
        String urlForAsset = urlForAsset(item);
        Intent i = new Intent(this, QuicklookActivity.class);
        String s = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
        i.putExtra("url", urlForAsset);
        startActivity(i);
    }

    private String urlForAsset(String assetName){
        File downloadAsset = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS + "/" + assetName);
        if(downloadAsset.exists()) {
            return downloadAsset.getAbsolutePath();
        }
        else{
            if(copyAssetToDownload(FILES_ASSETS_DIR+"/"+assetName,downloadAsset)) return downloadAsset.getAbsolutePath();
        }
        return null;
    }

    private boolean copyAssetToDownload(String assetName, File destinationFile){
        try {
            FileOutputStream fos = new FileOutputStream(destinationFile);
            int copied = IOUtils.copy(getAssets().open(assetName), fos);
            fos.close();
            if(copied>0) return true;
            else return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
}

@@ -50,22 +50,159 @@
 oid.
-content.Intent
+app.DownloadManager;%0Aimport android.content.Context;%0Aimport android.content.Intent;%0Aimport android.media.MediaScannerConnection;%0Aimport android.net.Uri
 ;%0Aim
@@ -919,16 +919,17 @@
 = %22files
+/
 %22;%0A    @
@@ -1269,17 +1269,16 @@
 fragment
- 
 , fragme
@@ -1497,125 +1497,8 @@
 s);%0A
-        String s = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();%0A
     
@@ -1513,16 +1513,21 @@
 tExtra(%22
+local
 url%22, ur
@@ -1744,24 +1744,25 @@
 assetName);%0A
+%0A
         if(d
@@ -1917,13 +1917,10 @@
 _DIR
-+%22/%22+
+, 
 asse
@@ -2058,32 +2058,50 @@
 Download(String 
+assetPath, String 
 assetName, File 
@@ -2261,16 +2261,26 @@
 ().open(
+assetPath+
 assetNam
@@ -2343,159 +2343,818 @@
 %3E0) 
-return true;%0A            else return false;%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A            return false;%0A        %7D%0A    %7D
+%7B%0A                DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);%0A                dm.addCompletedDownload(assetName,assetName, true, mime(destinationFile), destinationFile.getAbsolutePath(), copied, false);%0A                return true;%0A            %7D%0A            else return false;%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A            return false;%0A        %7D%0A    %7D%0A%0A    private String mime(File file)%7B%0A        String%5B%5D pieces = file.getName().split(%22%5C%5C.%22);%0A        String ext = pieces%5Bpieces.length-1%5D;%0A        if(ext.equals(%22png%22)) return %22image/png%22;%0A        if(ext.equals(%22pdf%22)) return %22application/pdf%22;%0A        if(ext.equals(%22txt%22)) return %22text/plain%22;%0A        if(ext.equals(%22zip%22)) return %22application/zip%22;%0A        return %22text/plain%22;%0A    %7D%0A
 %0A%7D%0A

rollback to previous state