* ownCloud Android client application
*
* @author masensio
+ * @author David A. Velasco
* Copyright (C) 2015 ownCloud Inc.
*
* This program is free software: you can redistribute it and/or modify
import android.accounts.Account;
import android.app.Activity;
import android.graphics.Bitmap;
-import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
+import android.support.v7.widget.AppCompatButton;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
-import android.widget.AdapterView;
import android.widget.Button;
+import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
+import android.widget.Switch;
import android.widget.TextView;
+import android.widget.Toast;
import com.owncloud.android.R;
-import com.owncloud.android.datamodel.FileDataStorageManager;
+import com.owncloud.android.authentication.AccountUtils;
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.datamodel.ThumbnailsCacheManager;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.lib.resources.shares.OCShare;
+import com.owncloud.android.lib.resources.shares.ShareType;
import com.owncloud.android.ui.activity.FileActivity;
import com.owncloud.android.ui.adapter.ShareUserListAdapter;
import com.owncloud.android.utils.DisplayUtils;
import java.util.ArrayList;
/**
- * Fragment for Sharing a file with users
+ * Fragment for Sharing a file with sharees (users or groups) or creating
+ * a public link.
*
* A simple {@link Fragment} subclass.
+ *
* Activities that contain this fragment must implement the
* {@link ShareFileFragment.OnShareFragmentInteractionListener} interface
* to handle interaction events.
+ *
* Use the {@link ShareFileFragment#newInstance} factory method to
* create an instance of this fragment.
*/
-public class ShareFileFragment extends Fragment {
+public class ShareFileFragment extends Fragment
+ implements ShareUserListAdapter.ShareUserAdapterListener{
private static final String TAG = ShareFileFragment.class.getSimpleName();
private static final String ARG_FILE = "FILE";
private static final String ARG_ACCOUNT = "ACCOUNT";
- // Parameters
+ /** File to share, received as a parameter in construction time */
private OCFile mFile;
+
+ /** OC account holding the file to share, received as a parameter in construction time */
private Account mAccount;
- private ArrayList<OCShare> mShares;
+ /** Reference to parent listener */
+ private OnShareFragmentInteractionListener mListener;
+
+ /** List of private shares bound to the file */
+ private ArrayList<OCShare> mPrivateShares;
+
+ /** Adapter to show private shares */
private ShareUserListAdapter mUserGroupsAdapter = null;
- private OnShareFragmentInteractionListener mListener;
+ /** Public share bound to the file */
+ private OCShare mPublicShare;
+
+ /** Listener for changes on switch to share / unshare publicly */
+ private CompoundButton.OnCheckedChangeListener mOnShareViaLinkSwitchCheckedChangeListener;
+
/**
* Public factory method to create new ShareFileFragment instances.
// Required empty public constructor
}
+ /**
+ * {@inheritDoc}
+ */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
+ /**
+ * {@inheritDoc}
+ */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
addUserGroupButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
- // Show Search Fragment
- mListener.showSearchUsersAndGroups();
+ boolean shareWithUsersEnable = AccountUtils.hasSearchUsersSupport(mAccount);
+ if (shareWithUsersEnable) {
+ // Show Search Fragment
+ mListener.showSearchUsersAndGroups();
+ } else {
+ String message = getString(R.string.share_sharee_unavailable);
+ Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
+ }
+ }
+ });
+
+ // Switch to create public share
+ mOnShareViaLinkSwitchCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
+ @Override
+ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
+ if (isResumed()) {
+ if (isChecked) {
+ ((FileActivity) getActivity()).getFileOperationsHelper().shareFileViaLink(mFile);
+
+ } else {
+ ((FileActivity) getActivity()).getFileOperationsHelper().unshareFileViaLink(mFile);
+ }
+ } // else, nothing; very important, setCheched(...) is called automatically during Fragment
+ // recreation on device rotations
+ }
+ };
+ Switch shareViaLinkSwitch = (Switch) view.findViewById(R.id.shareViaLinkSectionSwitch);
+ shareViaLinkSwitch.setOnCheckedChangeListener(mOnShareViaLinkSwitchCheckedChangeListener);
+
+ // Switch for expiration date
+ Switch shareViaLinkExpirationSwitch = (Switch) view.findViewById(R.id.shareViaLinkExpirationSwitch);
+ shareViaLinkExpirationSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
+ @Override
+ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
+ if (isChecked) {
+ // TODO real implementation: update share with expiration date
+ // show value of expiration date
+ getExpirationDateValue().setText(R.string.placeholder_timestamp);
+
+ } else {
+ // TODO real implementation: update share without expiration date
+ // empty value
+ getExpirationDateValue().setText(R.string.empty);
+ }
}
});
+ // Switch for password
+ Switch shareViaLinkPasswordSwitch = (Switch) view.findViewById(R.id.shareViaLinkPasswordSwitch);
+ shareViaLinkPasswordSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
+ @Override
+ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
+ if (isChecked) {
+ // TODO real implementation: update share with password
+ // show
+ getExpirationPasswordValue().setVisibility(View.VISIBLE);
+
+ } else {
+ // TODO real implementation: update share without password
+ // empty value
+ getExpirationPasswordValue().setVisibility(View.INVISIBLE);
+ }
+ }
+ });
+
+
return view;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
- refreshUsersOrGroupsList();
- }
+ // Load data into the list of private shares
+ refreshUsersOrGroupsListFromDB();
- // TODO: Rename method, update argument and hook method into UI event
- public void onButtonPressed(Uri uri) {
- if (mListener != null) {
- mListener.onShareFragmentInteraction(uri);
- }
+ // Load data of public share, if exists
+ refreshPublicShareFromDB();
}
@Override
mListener = null;
}
- // Get users and groups to fill the "share with" list
- public void refreshUsersOrGroupsList(){
- mShares = new ArrayList<>();
-
- // Get Users and Groups
- FileDataStorageManager fileDataStorageManager =
- new FileDataStorageManager(mAccount, getActivity().getContentResolver());
- mShares = fileDataStorageManager.getSharesWithForAFile(mFile.getRemotePath(),
- mAccount.name);
-
- // Update list of users/groups
- updateListOfUserGroups();
+ /**
+ * Get users and groups from the DB to fill in the "share with" list
+ *
+ * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
+ * instance ready to use. If not ready, does nothing.
+ */
+ public void refreshUsersOrGroupsListFromDB (){
+ if (((FileActivity) mListener).getStorageManager() != null) {
+ // Get Users and Groups
+ mPrivateShares = ((FileActivity) mListener).getStorageManager().getSharesWithForAFile(
+ mFile.getRemotePath(),
+ mAccount.name
+ );
+
+ // Update list of users/groups
+ updateListOfUserGroups();
+ }
}
private void updateListOfUserGroups() {
// Update list of users/groups
- mUserGroupsAdapter = new ShareUserListAdapter(getActivity().getApplicationContext(),
- R.layout.share_user_item, mShares);
+ // TODO Refactoring: create a new {@link ShareUserListAdapter} instance with every call should not be needed
+ mUserGroupsAdapter = new ShareUserListAdapter(
+ getActivity(),
+ R.layout.share_user_item,
+ mPrivateShares,
+ this
+ );
// Show data
TextView noShares = (TextView) getView().findViewById(R.id.shareNoUsers);
ListView usersList = (ListView) getView().findViewById(R.id.shareUsersList);
- if (mShares.size() > 0) {
+ if (mPrivateShares.size() > 0) {
noShares.setVisibility(View.GONE);
usersList.setVisibility(View.VISIBLE);
usersList.setAdapter(mUserGroupsAdapter);
- // Add unshare options
- registerLongClickListener(usersList);
-
} else {
noShares.setVisibility(View.VISIBLE);
usersList.setVisibility(View.GONE);
}
}
- private void registerLongClickListener(final ListView listView) {
- listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
- @Override
- public boolean onItemLongClick(AdapterView<?> parent, View view, final int position,
- long id) {
- // Show unshare button
- ImageView unshareButton = (ImageView) view.findViewById(R.id.unshareButton);
- if (unshareButton.getVisibility() == View.GONE) {
- unshareButton.setVisibility(View.VISIBLE);
- unshareButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // Unshare
- unshareWith(mShares.get(position));
- Log_OC.d(TAG, "Unshare - " +
- mShares.get(position).getSharedWithDisplayName());
- }
- });
+ @Override
+ public void unshareButtonPressed(OCShare share) {
+ // Unshare
+ mListener.unshareWith(share);
+ Log_OC.d(TAG, "Unshare - " + share.getSharedWithDisplayName());
+ }
- } else {
- unshareButton.setVisibility(View.GONE);
- }
- view.setAlpha(0);
- view.animate().alpha(1).setDuration(500).start();
- return false;
- }
- });
+
+
+ /**
+ * Get public link from the DB to fill in the "Share link" section
+ *
+ * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
+ * instance ready to use. If not ready, does nothing.
+ */
+ public void refreshPublicShareFromDB() {
+ if (((FileActivity) mListener).getStorageManager() != null) {
+ // Get public share
+ mPublicShare = ((FileActivity) mListener).getStorageManager().getFirstShareByPathAndType(
+ mFile.getRemotePath(),
+ ShareType.PUBLIC_LINK,
+ ""
+ );
+
+ // Update list of users/groups
+ updatePublicShareSection();
+ }
}
- private void unshareWith(OCShare share){
- OCFile file = ((FileActivity) getActivity()).getFile();
+ /**
+ * Updates in the UI the section about public share with the information in the current
+ * public share bound to mFile, if any
+ */
+ private void updatePublicShareSection() {
+ if (mPublicShare != null && ShareType.PUBLIC_LINK.equals(mPublicShare.getShareType())) {
+ // public share bound -> expand section
+ Switch shareViaLinkSwitch = getShareViaLinkSwitch();
+ if (!shareViaLinkSwitch.isChecked()) {
+ // set null listener before setChecked() to prevent infinite loop of calls
+ shareViaLinkSwitch.setOnCheckedChangeListener(null);
+ getShareViaLinkSwitch().setChecked(true);
+ shareViaLinkSwitch.setOnCheckedChangeListener(
+ mOnShareViaLinkSwitchCheckedChangeListener
+ );
+ }
+ getExpirationDateSection().setVisibility(View.VISIBLE);
+ getPasswordSection().setVisibility(View.VISIBLE);
+ getGetLinkButton().setVisibility(View.VISIBLE);
- ((FileActivity) getActivity()).getFileOperationsHelper().
- unshareFileWithUserOrGroup(
- file, share.getShareType(), share.getShareWith()
+ } else {
+ // no public share -> collapse section
+ Switch shareViaLinkSwitch = getShareViaLinkSwitch();
+ if (shareViaLinkSwitch.isChecked()) {
+ shareViaLinkSwitch.setOnCheckedChangeListener(null);
+ getShareViaLinkSwitch().setChecked(false);
+ shareViaLinkSwitch.setOnCheckedChangeListener(
+ mOnShareViaLinkSwitchCheckedChangeListener
);
+ }
+ getExpirationDateSection().setVisibility(View.GONE);
+ getPasswordSection().setVisibility(View.GONE);
+ getGetLinkButton().setVisibility(View.GONE);
+ }
+ }
+
+ private Switch getShareViaLinkSwitch() {
+ return (Switch) getView().findViewById(R.id.shareViaLinkSectionSwitch);
+ }
+
+ private View getExpirationDateSection() {
+ return getView().findViewById(R.id.shareViaLinkExpirationSection);
+ }
+
+ private TextView getExpirationDateValue() {
+ return (TextView) getView().findViewById(R.id.shareViaLinkExpirationValue);
+ }
+
+ private View getPasswordSection() {
+ return getView().findViewById(R.id.shareViaLinkPasswordSection);
+ }
+
+ private TextView getExpirationPasswordValue() {
+ return (TextView) getView().findViewById(R.id.shareViaLinkPasswordValue);
+ }
+
+ private AppCompatButton getGetLinkButton() {
+ return (AppCompatButton) getView().findViewById(R.id.shareViewLinkGetLinkButton);
}
- // TODO: review if it is necessary
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
*/
public interface OnShareFragmentInteractionListener {
void showSearchUsersAndGroups();
-
- void onShareFragmentInteraction(Uri uri);
+ void refreshUsersOrGroupsListFromServer();
+ void unshareWith(OCShare share);
}
}