From 7ad374dd5cdc40850b48b125f90047eb564e6773 Mon Sep 17 00:00:00 2001 From: Joshua <8Joshua@web.de> Date: Wed, 10 Oct 2018 18:10:52 +0200 Subject: [PATCH] Ressourcen Download implementiert, allerdings funktioneren bisher nur txt Dateien Problem los, bei PDFs sieht man irgentwie nur leere Folien --- app/src/main/AndroidManifest.xml | 3 + .../moddetails/ModDetailResourceFragment.java | 82 ++++++++++++++++- .../de/sebse/fuplanner/services/KVV/KVV.java | 4 + .../fuplanner/services/KVV/KVVModuleList.java | 90 +++++++++++++++++++ 4 files changed, 178 insertions(+), 1 deletion(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 68361a7..9948868 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -4,6 +4,9 @@ + + + dialog.cancel()) - .setPositiveButton(R.string.download, (dialog, id) -> log.d(file.getUrl())); + .setPositiveButton(R.string.download, (dialog, id) -> download(file)); AlertDialog alertDialog = alertDialogBuilder.create(); alertDialog.show(); @@ -128,6 +131,83 @@ public class ModDetailResourceFragment extends Fragment { return view; } + private void download(Resource.File file){ + KVV kvv = ((MainActivity) getActivity()).getKVV(); + kvv.getModule(mItemPos, (Modules.Module module) -> { + kvv.getResourceFile(success1 -> { + if (success1.equals("")){//Datei wurde nicht heruntergeladen + log.d("ResourceFile Datei nicht vorhanden"); + //todo Nutzer Info senden? + }else { + fileOpen(new File(success1)); + } + }, error -> { + + log.e(error); + }, file.getTitle(), file.getUrl(), module.title ); + }, error -> { + log.e(error); + }); + + + + + } + + private void fileOpen(File url){ + Uri uri = Uri.fromFile(url); + + Intent intent = new Intent();//Intent.ACTION_VIEW + // Check what kind of file you are trying to open, by comparing the url with extensions. + // When the if condition is matched, plugin sets the correct intent (mime) type, + // so Android knew what application to use to open the file + if (url.toString().contains(".doc") || url.toString().contains(".docx")) { + // Word document + intent.setDataAndType(uri, "application/msword"); + } else if(url.toString().contains(".pdf")) { + // PDF file + intent.setDataAndType(uri, "application/pdf"); + } else if(url.toString().contains(".ppt") || url.toString().contains(".pptx")) { + // Powerpoint file + intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); + } else if(url.toString().contains(".xls") || url.toString().contains(".xlsx")) { + // Excel file + intent.setDataAndType(uri, "application/vnd.ms-excel"); + } else if(url.toString().contains(".zip") || url.toString().contains(".rar")) { + // WAV audio file + intent.setDataAndType(uri, "application/x-wav"); + } else if(url.toString().contains(".rtf")) { + // RTF file + intent.setDataAndType(uri, "application/rtf"); + } else if(url.toString().contains(".wav") || url.toString().contains(".mp3")) { + // WAV audio file + intent.setDataAndType(uri, "audio/x-wav"); + } else if(url.toString().contains(".gif")) { + // GIF file + intent.setDataAndType(uri, "image/gif"); + } else if(url.toString().contains(".jpg") || url.toString().contains(".jpeg") || url.toString().contains(".png")) { + // JPG file + intent.setDataAndType(uri, "image/jpeg"); + } else if(url.toString().contains(".txt")) { + // Text file + intent.setDataAndType(uri, "text/plain"); + } else if(url.toString().contains(".3gp") || url.toString().contains(".mpg") || url.toString().contains(".mpeg") || url.toString().contains(".mpe") || url.toString().contains(".mp4") || url.toString().contains(".avi")) { + // Video files + intent.setDataAndType(uri, "video/*"); + } else { + //if you want you can also define the intent type for any other file + + //additionally use else clause below, to manage other unknown extensions + //in this case, Android will show all applications installed on the device + //so you can choose which application to use + intent.setDataAndType(uri, "*/*"); + } + + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + startActivity(intent); + + } + private void refresh(boolean forceRefresh) { if (getActivity() != null) { KVV kvv = ((MainActivity) getActivity()).getKVV(); diff --git a/app/src/main/java/de/sebse/fuplanner/services/KVV/KVV.java b/app/src/main/java/de/sebse/fuplanner/services/KVV/KVV.java index 517d315..c3dc543 100644 --- a/app/src/main/java/de/sebse/fuplanner/services/KVV/KVV.java +++ b/app/src/main/java/de/sebse/fuplanner/services/KVV/KVV.java @@ -137,6 +137,10 @@ public class KVV { getModulePart(modules -> modules.getResources(module, saveOnCallback(modules, callback, forceRefresh), errorOnCallback(error), forceRefresh)); } + public void getResourceFile(final NetworkCallback callback, final NetworkErrorCallback error, String filename, String url, String moduleName){ + getModulePart(modules -> modules.getResourceFile(callback, errorOnCallback(error),filename, url, moduleName)); + } + private void getModulePart(ModListFunction func) { diff --git a/app/src/main/java/de/sebse/fuplanner/services/KVV/KVVModuleList.java b/app/src/main/java/de/sebse/fuplanner/services/KVV/KVVModuleList.java index a88264d..c04ac89 100644 --- a/app/src/main/java/de/sebse/fuplanner/services/KVV/KVVModuleList.java +++ b/app/src/main/java/de/sebse/fuplanner/services/KVV/KVVModuleList.java @@ -1,6 +1,7 @@ package de.sebse.fuplanner.services.KVV; import android.content.Context; +import android.os.Environment; import android.text.Html; import android.util.Pair; @@ -8,6 +9,8 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.HashSet; @@ -31,6 +34,7 @@ import de.sebse.fuplanner.tools.network.HTTPService; import de.sebse.fuplanner.tools.network.NetworkCallback; import de.sebse.fuplanner.tools.network.NetworkError; import de.sebse.fuplanner.tools.network.NetworkErrorCallback; +import de.sebse.fuplanner.tools.network.Result; /** * Created by sebastian on 29.10.17. @@ -515,6 +519,48 @@ class KVVModuleList extends HTTPService { } + public void getResourceFile( final NetworkCallback callback, final NetworkErrorCallback errorCallback, String Filename, String url, String moduleName) { + + //queueModuleDetails.add(module.getID(), () -> { + if (isExternalStorageReadable()){ + File f = new File(Environment.getExternalStoragePublicDirectory( + Environment.DIRECTORY_DOWNLOADS)+"/"+moduleName+"/"+Filename); + if (f.exists()) {//prüffen ob bereits herunter gelagen... + callback.onResponse(f.getPath()); + log.d("ResourceFile laden aus Speicher"); + return; + } + } + getResourceFileUpgrade(Filename, url , moduleName, success -> { + callback.onResponse(success); + }, errorCallback); + //}); + } + + private void getResourceFileUpgrade(String Filename, String url , String moduleName, final NetworkCallback callback, final NetworkErrorCallback errorCallback) { + if (token == null) { + errorCallback.onError(new NetworkError(101701, 500, "Currently running in offline mode!")); + return; + } + get(url, token.getCookies(), response ->{ + String pfad=""; + if (response.getParsed()==null){//Prüfen ob Datei wirklich heruntergeladen wurde + log.d("ResourceFile erneut einlogen"); + //todo testLogin(token, token -> callback.onResponse(root), errorCallback); + } + else { + if (isExternalStorageWritable()){//Datei Speichern, falls schreibrechte + pfad=saveFileInDownloads(Filename, response, moduleName); + } + } + + //log.d("ResourceFile: ", token); + callback.onResponse(pfad); + }, error -> errorCallback.onError(new NetworkError(101702, error.networkResponse.statusCode, "Cannot get File!"))); + + + } + @@ -538,4 +584,48 @@ class KVVModuleList extends HTTPService { } + + /* Checks if external storage is available for read and write */ + public boolean isExternalStorageWritable() { + String state = Environment.getExternalStorageState(); + if (Environment.MEDIA_MOUNTED.equals(state)) { + return true; + } + log.d("Dateisystem: kein schreiben möglich"); + return false; + } + + /* Checks if external storage is available to at least read */ + public boolean isExternalStorageReadable() { + String state = Environment.getExternalStorageState(); + if (Environment.MEDIA_MOUNTED.equals(state) || + Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { + return true; + } + log.d("Dateisystem: kein lesen möglich"); + return false; + } + public String saveFileInDownloads(String filename, Result datei, String moduleName) { + //Speichert Datei im Ordner DOWNLOADS/moduleName + File ordner = new File(Environment.getExternalStoragePublicDirectory( + Environment.DIRECTORY_DOWNLOADS), moduleName); + if (!ordner.mkdir()) { + //Log.e(LOG_TAG, "Directory not created"); + log.d( "Directory not created"); + } + String pfad=""; + try { + //todo eventuel prüfen ob genügend speicher noch vorhanden ist + FileOutputStream out = new FileOutputStream(ordner.getPath()+"/"+filename); + out.write(datei.getParsed().getBytes()); + out.close(); + pfad=ordner.getPath()+"/"+filename; + } catch (Exception e) { + log.d("Datei nicht gespeichert!"); + e.printStackTrace(); + } + return pfad; + } + + }