Ressourcen Download implementiert, allerdings funktioneren bisher nur txt Dateien Problem los, bei PDFs sieht man irgentwie nur leere Folien
This commit is contained in:
@@ -4,6 +4,9 @@
|
|||||||
|
|
||||||
<uses-permission android:name="android.permission.INTERNET" />
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
|
|
||||||
|
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||||
|
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
android:icon="@mipmap/ic_launcher"
|
android:icon="@mipmap/ic_launcher"
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package de.sebse.fuplanner.fragments.moddetails;
|
|||||||
|
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
import android.support.v4.app.Fragment;
|
import android.support.v4.app.Fragment;
|
||||||
@@ -14,6 +16,7 @@ import android.view.View;
|
|||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import de.sebse.fuplanner.MainActivity;
|
import de.sebse.fuplanner.MainActivity;
|
||||||
@@ -99,7 +102,7 @@ public class ModDetailResourceFragment extends Fragment {
|
|||||||
)
|
)
|
||||||
.setCancelable(true)
|
.setCancelable(true)
|
||||||
.setNeutralButton(R.string.close, (dialog, id) -> dialog.cancel())
|
.setNeutralButton(R.string.close, (dialog, id) -> 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 alertDialog = alertDialogBuilder.create();
|
||||||
alertDialog.show();
|
alertDialog.show();
|
||||||
@@ -128,6 +131,83 @@ public class ModDetailResourceFragment extends Fragment {
|
|||||||
return view;
|
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) {
|
private void refresh(boolean forceRefresh) {
|
||||||
if (getActivity() != null) {
|
if (getActivity() != null) {
|
||||||
KVV kvv = ((MainActivity) getActivity()).getKVV();
|
KVV kvv = ((MainActivity) getActivity()).getKVV();
|
||||||
|
|||||||
@@ -137,6 +137,10 @@ public class KVV {
|
|||||||
getModulePart(modules -> modules.getResources(module, saveOnCallback(modules, callback, forceRefresh), errorOnCallback(error), forceRefresh));
|
getModulePart(modules -> modules.getResources(module, saveOnCallback(modules, callback, forceRefresh), errorOnCallback(error), forceRefresh));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void getResourceFile(final NetworkCallback<String> 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) {
|
private void getModulePart(ModListFunction func) {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package de.sebse.fuplanner.services.KVV;
|
package de.sebse.fuplanner.services.KVV;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.os.Environment;
|
||||||
import android.text.Html;
|
import android.text.Html;
|
||||||
import android.util.Pair;
|
import android.util.Pair;
|
||||||
|
|
||||||
@@ -8,6 +9,8 @@ import org.json.JSONArray;
|
|||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
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.NetworkCallback;
|
||||||
import de.sebse.fuplanner.tools.network.NetworkError;
|
import de.sebse.fuplanner.tools.network.NetworkError;
|
||||||
import de.sebse.fuplanner.tools.network.NetworkErrorCallback;
|
import de.sebse.fuplanner.tools.network.NetworkErrorCallback;
|
||||||
|
import de.sebse.fuplanner.tools.network.Result;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by sebastian on 29.10.17.
|
* Created by sebastian on 29.10.17.
|
||||||
@@ -515,6 +519,48 @@ class KVVModuleList extends HTTPService {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void getResourceFile( final NetworkCallback<String> 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<String> 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user