diff --git a/docs-android/app/src/main/java/com/sismics/docs/activity/DocumentViewActivity.java b/docs-android/app/src/main/java/com/sismics/docs/activity/DocumentViewActivity.java index 6b982c6d..00abd2b0 100644 --- a/docs-android/app/src/main/java/com/sismics/docs/activity/DocumentViewActivity.java +++ b/docs-android/app/src/main/java/com/sismics/docs/activity/DocumentViewActivity.java @@ -184,9 +184,26 @@ public class DocumentViewActivity extends AppCompatActivity { } @Override - public boolean onCreateOptionsMenu(Menu menu) { + public boolean onCreateOptionsMenu(final Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.document_view_activity, menu); + + // Silently get the document to know if it is writable by the current user + // If this call fails or is slow and the document is read-only, + // write actions will be allowed and will fail + DocumentResource.get(this, document.optString("id"), new JsonHttpResponseHandler() { + @Override + public void onSuccess(int statusCode, Header[] headers, JSONObject response) { + boolean writable = response.optBoolean("writable"); + + menu.findItem(R.id.share).setVisible(writable); + menu.findItem(R.id.upload_file).setVisible(writable); + menu.findItem(R.id.edit).setVisible(writable); + menu.findItem(R.id.delete_file).setVisible(writable); + menu.findItem(R.id.delete_document).setVisible(writable); + } + }); + return super.onCreateOptionsMenu(menu); } diff --git a/docs-android/app/src/main/java/com/sismics/docs/adapter/ShareListAdapter.java b/docs-android/app/src/main/java/com/sismics/docs/adapter/ShareListAdapter.java index d0fffc76..6469866f 100644 --- a/docs-android/app/src/main/java/com/sismics/docs/adapter/ShareListAdapter.java +++ b/docs-android/app/src/main/java/com/sismics/docs/adapter/ShareListAdapter.java @@ -15,6 +15,9 @@ import com.sismics.docs.event.ShareSendEvent; import org.json.JSONArray; import org.json.JSONObject; +import java.util.ArrayList; +import java.util.List; + import de.greenrobot.event.EventBus; /** @@ -26,25 +29,33 @@ public class ShareListAdapter extends BaseAdapter { /** * Shares. */ - private JSONArray shares; + private List acls; /** * Share list adapter. * - * @param shares Shares + * @param acls ACLs */ - public ShareListAdapter(JSONArray shares) { - this.shares = shares; + public ShareListAdapter(JSONArray acls) { + this.acls = new ArrayList<>(); + + // Extract only share ACLs + for (int i = 0; i < acls.length(); i++) { + JSONObject acl = acls.optJSONObject(i); + if (acl.optString("type").equals("SHARE")) { + this.acls.add(acl); + } + } } @Override public int getCount() { - return shares.length(); + return acls.size(); } @Override public JSONObject getItem(int position) { - return shares.optJSONObject(position); + return acls.get(position); } @Override @@ -60,8 +71,8 @@ public class ShareListAdapter extends BaseAdapter { } // Fill the view - final JSONObject share = getItem(position); - String name = share.optString("name"); + final JSONObject acl = getItem(position); + String name = acl.optString("name"); TextView shareTextView = (TextView) view.findViewById(R.id.shareTextView); shareTextView.setText(name.isEmpty() ? parent.getContext().getString(R.string.share_default_name) : name); @@ -70,7 +81,7 @@ public class ShareListAdapter extends BaseAdapter { shareDeleteButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - EventBus.getDefault().post(new ShareDeleteEvent(share.optString("id"))); + EventBus.getDefault().post(new ShareDeleteEvent(acl.optString("id"))); } }); @@ -79,7 +90,7 @@ public class ShareListAdapter extends BaseAdapter { shareSendButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - EventBus.getDefault().post(new ShareSendEvent(share)); + EventBus.getDefault().post(new ShareSendEvent(acl)); } }); diff --git a/docs-android/app/src/main/java/com/sismics/docs/event/ShareSendEvent.java b/docs-android/app/src/main/java/com/sismics/docs/event/ShareSendEvent.java index 8e48e1ca..9d922648 100644 --- a/docs-android/app/src/main/java/com/sismics/docs/event/ShareSendEvent.java +++ b/docs-android/app/src/main/java/com/sismics/docs/event/ShareSendEvent.java @@ -9,20 +9,20 @@ import org.json.JSONObject; */ public class ShareSendEvent { /** - * Share data. + * ACL data. */ - private JSONObject share; + private JSONObject acl; /** * Create a share send event. * - * @param share Share data + * @param acl ACL data */ - public ShareSendEvent(JSONObject share) { - this.share = share; + public ShareSendEvent(JSONObject acl) { + this.acl = acl; } - public JSONObject getShare() { - return share; + public JSONObject getAcl() { + return acl; } } diff --git a/docs-android/app/src/main/java/com/sismics/docs/fragment/DocShareFragment.java b/docs-android/app/src/main/java/com/sismics/docs/fragment/DocShareFragment.java index a22ccc0d..93cb93d2 100644 --- a/docs-android/app/src/main/java/com/sismics/docs/fragment/DocShareFragment.java +++ b/docs-android/app/src/main/java/com/sismics/docs/fragment/DocShareFragment.java @@ -125,10 +125,10 @@ public class DocShareFragment extends DialogFragment { @Override public void onSuccess(int statusCode, Header[] headers, JSONObject response) { document = response; - JSONArray shares = response.optJSONArray("shares"); + JSONArray acls = response.optJSONArray("acls"); shareProgressBar.setVisibility(View.GONE); shareListView.setEmptyView(shareEmptyView); - shareListView.setAdapter(new ShareListAdapter(shares)); + shareListView.setAdapter(new ShareListAdapter(acls)); } @Override @@ -158,7 +158,7 @@ public class DocShareFragment extends DialogFragment { // Build the share link String serverUrl = PreferenceUtil.getServerUrl(getActivity()); - String link = serverUrl + "/share.html#/share/" + document.optString("id") + "/" + event.getShare().optString("id"); + String link = serverUrl + "/share.html#/share/" + document.optString("id") + "/" + event.getAcl().optString("id"); // Build the intent Context context = getActivity();