Blackboard Resources links

This commit is contained in:
Caesar2011
2019-02-01 23:51:35 +01:00
parent d5d4b680e8
commit c7dd46a1a2
5 changed files with 143 additions and 20 deletions

View File

@@ -34,7 +34,7 @@ import de.sebse.fuplanner.tools.ui.treeview.TreeViewAdapter;
* Use the {@link ModDetailResourceFragment#newInstance} factory method to * Use the {@link ModDetailResourceFragment#newInstance} factory method to
* create an instance of this fragment. * create an instance of this fragment.
*/ */
public class ModDetailResourceFragment extends Fragment { public class ModDetailResourceFragment extends Fragment implements Download.OnDownloadRequestInterface {
private static final String ARG_POSITION = "itemPosition"; private static final String ARG_POSITION = "itemPosition";
private String mItemPos; private String mItemPos;
@@ -82,7 +82,7 @@ public class ModDetailResourceFragment extends Fragment {
RecyclerView recyclerView = view.findViewById(R.id.list); RecyclerView recyclerView = view.findViewById(R.id.list);
recyclerView.setLayoutManager(new LinearLayoutManager(context)); recyclerView.setLayoutManager(new LinearLayoutManager(context));
adapter = new ModDetailResourceAdapter(Arrays.asList(new FileNodeBinder(), new DirectoryNodeBinder(), new DocumentNodeBinder())); adapter = new ModDetailResourceAdapter(Arrays.asList(new FileNodeBinder(), new DirectoryNodeBinder(), new DocumentNodeBinder(this)));
adapter.setOnTreeNodeListener(new TreeViewAdapter.OnTreeNodeListener() { adapter.setOnTreeNodeListener(new TreeViewAdapter.OnTreeNodeListener() {
@Override @Override
public boolean onClick(TreeNode node, RecyclerView.ViewHolder holder) { public boolean onClick(TreeNode node, RecyclerView.ViewHolder holder) {
@@ -119,6 +119,17 @@ public class ModDetailResourceFragment extends Fragment {
return view; return view;
} }
@Override
public void request(String title, String url) {
if (mListener == null)
return;
mListener.getKVV().modules().list().find(mItemPos, (Modules.Module module) -> {
String folderName = "FU-"+module.title.replaceAll("[:*<>|/\"\\\\]", "-");
folderName += "/Announcement";
getDownload().openDownloadDialog(title, url, folderName);
}, log::e);
}
@Override @Override
public void onAttach(Context context) { public void onAttach(Context context) {
super.onAttach(context); super.onAttach(context);

View File

@@ -178,14 +178,43 @@ public class ModulesResources extends PartModules<ArrayList<Resource>> {
} else { } else {
String bodyText = resource.optString("body", ""); String bodyText = resource.optString("body", "");
bodyText = String.valueOf(PartModules.fromHtml(bodyText)); bodyText = String.valueOf(PartModules.fromHtml(bodyText));
resources.add(new Resource.Document("", title, createdDate, true, "", bodyText)); Resource.Document document = new Resource.Document("", title, createdDate, true, "", bodyText);
if (--latch[0] == 0) callback.onResponse(resources); resources.add(document);
get(String.format("https://lms.fu-berlin.de/learn/api/public/v1/courses/%s/contents/%s/attachments", ID, resid), mLogin.getLoginTokenBB().getCookies(), response1 -> {
String body1 = response1.getParsed();
if (body1 == null) {
errorCallback.onError(new NetworkError(101617, 403, "No resource attachments retrieved!"));
return;
}
JSONArray attachments;
try {
JSONObject json = new JSONObject(body1);
attachments = json.getJSONArray("results");
} catch (JSONException e) {
e.printStackTrace();
errorCallback.onError(new NetworkError(101618, 400, "Cannot parse resource attachments!"));
return;
}
for (int j = 0; j < attachments.length(); j++) {
try {
JSONObject attachment = attachments.getJSONObject(j);
String attachid = attachment.getString("id");
String attachname = attachment.getString("fileName");
document.addUrl(String.format("https://lms.fu-berlin.de/learn/api/public/v1/courses/%s/contents/%s/attachments/%s/download", ID, resid, attachid), attachname);
} catch (JSONException e) {
log.e(new NetworkError(101619, 400, "Cannot parse resource attachments!"));
e.printStackTrace();
log.e("ID:", j, "JSON:", attachments);
}
}
if (--latch[0] == 0) callback.onResponse(resources);
}, error -> errorCallback.onError(new NetworkError(101616, error.networkResponse.statusCode, "Cannot get resource attachments!")));
} }
} catch (JSONException e) { } catch (JSONException e) {
log.e(new NetworkError(101615, 400, "Cannot parse resources!")); log.e(new NetworkError(101615, 400, "Cannot parse resources!"));
e.printStackTrace(); e.printStackTrace();
log.e("ID:", i, "JSON:", sites); log.e("ID:", i, "JSON:", sites);
return;
} }
} }
}, error -> { }, error -> {
@@ -238,7 +267,15 @@ public class ModulesResources extends PartModules<ArrayList<Resource>> {
} }
private void fileUpgrade(String filename, String url, String modulename, final NetworkCallback<String> callback, final NetworkErrorCallback errorCallback) { private void fileUpgrade(String filename, String url, String modulename, final NetworkCallback<String> callback, final NetworkErrorCallback errorCallback) {
if (!mLogin.isInOnlineMode() || mLogin.getLoginTokenKVV() == null) { if (url.contains("lms.fu-berlin.de")) {
fileUpgradeBB(filename, url, modulename, callback, errorCallback);
} else {
fileUpgradeKVV(filename, url, modulename, callback, errorCallback);
}
}
private void fileUpgradeKVV(String filename, String url, String modulename, final NetworkCallback<String> callback, final NetworkErrorCallback errorCallback) {
if (!mLogin.isInOnlineMode() || mLogin.getLoginTokenKVV() == null || !mLogin.getLoginTokenKVV().isAvailable()) {
errorCallback.onError(new NetworkError(101604, 500, "Currently running in offline mode!")); errorCallback.onError(new NetworkError(101604, 500, "Currently running in offline mode!"));
return; return;
} }
@@ -256,11 +293,52 @@ public class ModulesResources extends PartModules<ArrayList<Resource>> {
String path = saveFileInDownloads(filename, response.getBytes(), modulename); String path = saveFileInDownloads(filename, response.getBytes(), modulename);
callback.onResponse(path); callback.onResponse(path);
} else { } else {
errorCallback.onError(new NetworkError(101704, 403, "External storage not writable!")); errorCallback.onError(new NetworkError(101704, 400, "External storage not writable!"));
} }
}, error -> errorCallback.onError(new NetworkError(101702, error.networkResponse.statusCode, "Cannot get file!"))); }, error -> errorCallback.onError(new NetworkError(101702, error.networkResponse.statusCode, "Cannot get file!")));
}
private void fileUpgradeBB(String filename, String url, String modulename, final NetworkCallback<String> callback, final NetworkErrorCallback errorCallback) {
if (!mLogin.isInOnlineMode() || mLogin.getLoginTokenBB() == null || !mLogin.getLoginTokenBB().isAvailable()) {
errorCallback.onError(new NetworkError(101615, 500, "Currently running in offline mode!"));
return;
}
get(url, mLogin.getLoginTokenBB().getCookies(), response -> {
if (response.getHeaders().containsKey("Location")) {
// Return redirected URL
String redirectUrl = response.getHeaders().get("Location");
if (redirectUrl == null){
redirectUrl = "";
}
log.d("redirectUrl", redirectUrl);
get(redirectUrl, mLogin.getLoginTokenBB().getCookies(), response1 -> {
if (response1.getHeaders().containsKey("Location")) {
// Return redirected URL
String redirectUrl2 = response1.getHeaders().get("Location");
if (redirectUrl2 == null) {
redirectUrl2 = "";
} else {
redirectUrl2 = "https://lms.fu-berlin.de" + redirectUrl2;
}
log.d("redirectUrl2", redirectUrl2);
get(redirectUrl2, mLogin.getLoginTokenBB().getCookies(), response2 -> {
if (response2.getBytes() == null) {
errorCallback.onError(new NetworkError(101714, 400, "Cannot get file!"));
} else if (isExternalStorageWritable()) {
String path = saveFileInDownloads(filename, response2.getBytes(), modulename);
callback.onResponse(path);
} else {
errorCallback.onError(new NetworkError(101713, 400, "External storage not writable!"));
}
}, error -> errorCallback.onError(new NetworkError(101717, error.networkResponse.statusCode, "Cannot get file!")));
} else {
errorCallback.onError(new NetworkError(101716, 400, "External storage not writable!"));
}
}, error -> errorCallback.onError(new NetworkError(101712, error.networkResponse.statusCode, "Cannot get file!")));
} else {
errorCallback.onError(new NetworkError(101711, 400, "External storage not writable!"));
}
}, error -> errorCallback.onError(new NetworkError(101710, error.networkResponse.statusCode, "Cannot get file!")));
} }
@@ -293,7 +371,7 @@ public class ModulesResources extends PartModules<ArrayList<Resource>> {
// Saves file in folder: DOWNLOADS/moduleName // Saves file in folder: DOWNLOADS/moduleName
File folder = new File(Environment.getExternalStoragePublicDirectory( File folder = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), moduleName); Environment.DIRECTORY_DOWNLOADS), moduleName);
if (!folder.mkdir()) { if (!folder.mkdirs()) {
log.w( "Directory not created"); log.w( "Directory not created");
} }
String path = ""; String path = "";

View File

@@ -1,5 +1,7 @@
package de.sebse.fuplanner.services.kvv.types; package de.sebse.fuplanner.services.kvv.types;
import android.util.Pair;
import com.google.android.gms.common.internal.Objects; import com.google.android.gms.common.internal.Objects;
import java.io.Serializable; import java.io.Serializable;
@@ -63,7 +65,8 @@ public abstract class Resource implements Serializable {
} }
public static class Document extends Resource implements LayoutItemType { public static class Document extends Resource implements LayoutItemType {
private final ArrayList<String> urls;
private final ArrayList<DownloadFile> urls;
private final String body; private final String body;
public Document(String author, String title, long modifiedDate, boolean visible, String container, String body) { public Document(String author, String title, long modifiedDate, boolean visible, String container, String body) {
@@ -89,11 +92,11 @@ public abstract class Resource implements Serializable {
return body; return body;
} }
public void addUrl(String res){ public void addUrl(String url, String name){
urls.add(res); urls.add(new DownloadFile(url, name));
} }
public String getUrl(int id){ public DownloadFile getUrl(int id){
return urls.get(id); return urls.get(id);
} }
@@ -115,6 +118,16 @@ public abstract class Resource implements Serializable {
public int hashCode() { public int hashCode() {
return Objects.hashCode(super.hashCode(), this.getClass().getSimpleName()); return Objects.hashCode(super.hashCode(), this.getClass().getSimpleName());
} }
public class DownloadFile implements Serializable {
public final String url;
public final String name;
public DownloadFile(String url, String name) {
this.url = url;
this.name = name;
}
}
} }
public static class File extends Resource implements LayoutItemType { public static class File extends Resource implements LayoutItemType {

View File

@@ -187,6 +187,8 @@ public class Download {
private void fileOpen(File url){ private void fileOpen(File url){
Context context = contextInterface.get(); Context context = contextInterface.get();
log.d("fileOpen", context);
log.d("url", url.toString());
if (context == null) if (context == null)
return; return;
Uri uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".my.provider", url); Uri uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".my.provider", url);

View File

@@ -8,7 +8,10 @@ import com.cunoraz.tagview.TagView;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import de.sebse.fuplanner.R; import de.sebse.fuplanner.R;
import de.sebse.fuplanner.fragments.moddetails.ModDetailAnnounceFragment;
import de.sebse.fuplanner.services.kvv.types.Resource; import de.sebse.fuplanner.services.kvv.types.Resource;
import de.sebse.fuplanner.services.kvv.ui.Download;
import de.sebse.fuplanner.tools.UtilsDate;
import de.sebse.fuplanner.tools.logging.Logger; import de.sebse.fuplanner.tools.logging.Logger;
import de.sebse.fuplanner.tools.ui.cardview.ExpandableCardView; import de.sebse.fuplanner.tools.ui.cardview.ExpandableCardView;
@@ -17,6 +20,14 @@ import de.sebse.fuplanner.tools.ui.cardview.ExpandableCardView;
*/ */
public class DocumentNodeBinder extends TreeViewBinder<DocumentNodeBinder.ViewHolder> { public class DocumentNodeBinder extends TreeViewBinder<DocumentNodeBinder.ViewHolder> {
private Download.OnDownloadRequestInterface requestInterface;
public DocumentNodeBinder(Download.OnDownloadRequestInterface requestInterface) {
super();
this.requestInterface = requestInterface;
}
@Override @Override
public ViewHolder provideViewHolder(View itemView) { public ViewHolder provideViewHolder(View itemView) {
return new ViewHolder(itemView); return new ViewHolder(itemView);
@@ -26,22 +37,28 @@ public class DocumentNodeBinder extends TreeViewBinder<DocumentNodeBinder.ViewHo
public void bindView(ViewHolder holder, int position, TreeNode node) { public void bindView(ViewHolder holder, int position, TreeNode node) {
Resource.Document fileNode = (Resource.Document) node.getContent(); Resource.Document fileNode = (Resource.Document) node.getContent();
holder.title.setText(fileNode.getTitle()); holder.title.setText(fileNode.getTitle());
holder.sub_title.setText(UtilsDate.getModifiedDateTime(holder.itemView.getContext(), fileNode.getModifiedDate()));
holder.notes.setText(fileNode.getBody()); holder.notes.setText(fileNode.getBody());
/*for (int i = 0, notesSize = fileNode.size(); i < notesSize; i++) { holder.mTagGroup.removeAll();
String name = urlToName(fileNode.getUrl(i), i, holder.itemView.getResources()); for (int i = 0, notesSize = fileNode.size(); i < notesSize; i++) {
Tag tag = new Tag(name); String name = fileNode.getUrl(i).name;
Tag tag = new Tag(name.substring(0, Math.min(40, name.length())));
tag.id = i; tag.id = i;
tag.layoutColor = ContextCompat.getColor(holder.itemView.getContext(), R.color.colorFUBlue); tag.layoutColor = ContextCompat.getColor(holder.itemView.getContext(), R.color.colorFUBlue);
holder.mTagGroup.addTag(tag); holder.mTagGroup.addTag(tag);
} }
holder.mTagGroup.setOnTagClickListener((tag, i) -> { holder.mTagGroup.setOnTagClickListener((tag, i) -> {
String s = fileNode.getUrl(i); String s = fileNode.getUrl(i).url;
if (s != null) { if (s != null) {
String name = urlToName(s, i, holder.itemView.getResources()); String name = fileNode.getUrl(i).name;
requestInterface.request(name, s); Logger log = new Logger(this);
//log.d(name);
//log.d(s);
this.requestInterface.request(name, s);
} }
});*/ });
holder.itemView.invalidate();
} }
@Override @Override
@@ -51,6 +68,7 @@ public class DocumentNodeBinder extends TreeViewBinder<DocumentNodeBinder.ViewHo
public class ViewHolder extends TreeViewBinder.ViewHolder { public class ViewHolder extends TreeViewBinder.ViewHolder {
final TextView title; final TextView title;
final TextView sub_title;
final TextView notes; final TextView notes;
final ExpandableCardView card_view; final ExpandableCardView card_view;
final TagView mTagGroup; final TagView mTagGroup;
@@ -59,6 +77,7 @@ public class DocumentNodeBinder extends TreeViewBinder<DocumentNodeBinder.ViewHo
super(rootView); super(rootView);
this.card_view = rootView.findViewById(R.id.card_view); this.card_view = rootView.findViewById(R.id.card_view);
this.title = this.card_view.getOuterView().findViewById(R.id.title); this.title = this.card_view.getOuterView().findViewById(R.id.title);
this.sub_title = this.card_view.getOuterView().findViewById(R.id.sub_title);
this.notes = this.card_view.getInnerView().findViewById(R.id.notes); this.notes = this.card_view.getInnerView().findViewById(R.id.notes);
this.mTagGroup = this.card_view.getInnerView().findViewById(R.id.tag_group); this.mTagGroup = this.card_view.getInnerView().findViewById(R.id.tag_group);
} }