Android: handle read-only documents, use ACLs for sharing

This commit is contained in:
jendib 2015-05-09 21:51:06 +02:00
parent 42320dc9b9
commit 072dd7b280
4 changed files with 49 additions and 21 deletions

View File

@ -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);
}

View File

@ -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<JSONObject> 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));
}
});

View File

@ -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;
}
}

View File

@ -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();