X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/20b0ec8977b845032214f31a9733aa8decd8cd07..ad7666370f248124a6c49846f78cd3cef71f19f0:/src/com/owncloud/android/ui/fragment/ShareFileFragment.java diff --git a/src/com/owncloud/android/ui/fragment/ShareFileFragment.java b/src/com/owncloud/android/ui/fragment/ShareFileFragment.java index 3a194a84..838635ae 100644 --- a/src/com/owncloud/android/ui/fragment/ShareFileFragment.java +++ b/src/com/owncloud/android/ui/fragment/ShareFileFragment.java @@ -2,6 +2,7 @@ * 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 @@ -21,83 +22,101 @@ package com.owncloud.android.ui.fragment; import android.accounts.Account; -import android.accounts.AuthenticatorException; -import android.accounts.OperationCanceledException; 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.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.MainApp; 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.OwnCloudAccount; -import com.owncloud.android.lib.common.OwnCloudClient; -import com.owncloud.android.lib.common.OwnCloudClientManagerFactory; -import com.owncloud.android.lib.common.accounts.AccountUtils; -import com.owncloud.android.lib.common.operations.RemoteOperationResult; 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.operations.GetSharesForFileOperation; import com.owncloud.android.ui.activity.FileActivity; -import com.owncloud.android.ui.activity.ShareActivity; -import com.owncloud.android.ui.adapter.LocalFileListAdapter; import com.owncloud.android.ui.adapter.ShareUserListAdapter; -import com.owncloud.android.utils.CopyTmpFileAsyncTask; +import com.owncloud.android.ui.dialog.ExpirationDatePickerDialogFragment; import com.owncloud.android.utils.DisplayUtils; -import com.owncloud.android.utils.GetShareWithUserAsyncTask; import com.owncloud.android.utils.MimetypeIconUtil; -import java.io.IOException; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; /** - * 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 - implements GetShareWithUserAsyncTask.OnGetSharesWithUserTaskListener{ + implements ShareUserListAdapter.ShareUserAdapterListener{ + private static final String TAG = ShareFileFragment.class.getSimpleName(); // the fragment initialization parameters 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 mShares; + /** Reference to parent listener */ + private OnShareFragmentInteractionListener mListener; + + /** List of private shares bound to the file */ + private ArrayList 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; + + /** + * Listener for changes on switch to set / clear password on public link + */ + private CompoundButton.OnCheckedChangeListener mOnPasswordSwitchCheckedChangeListener; + + /** + * Listener for changes on switch to set / clear expiration date on public link + */ + private CompoundButton.OnCheckedChangeListener mOnExpirationDateSwitchCheckedChangeListener; + /** * Public factory method to create new ShareFileFragment instances. * - * @param fileToShare An {@link OCFile} to show in the fragment - * @param account An ownCloud account + * @param fileToShare An {@link OCFile} to show in the fragment + * @param account An ownCloud account * @return A new instance of fragment ShareFileFragment. */ - public static ShareFileFragment newInstance(OCFile fileToShare, Account account) { + public static ShareFileFragment newInstance(OCFile fileToShare, Account account) { ShareFileFragment fragment = new ShareFileFragment(); Bundle args = new Bundle(); args.putParcelable(ARG_FILE, fileToShare); @@ -110,6 +129,9 @@ public class ShareFileFragment extends Fragment // Required empty public constructor } + /** + * {@inheritDoc} + */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -119,6 +141,9 @@ public class ShareFileFragment extends Fragment } } + /** + * {@inheritDoc} + */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -133,7 +158,7 @@ public class ShareFileFragment extends Fragment if (mFile.isImage()) { String remoteId = String.valueOf(mFile.getRemoteId()); Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(remoteId); - if (thumbnail != null){ + if (thumbnail != null) { icon.setImageBitmap(thumbnail); } } @@ -142,29 +167,108 @@ public class ShareFileFragment extends Fragment filename.setText(mFile.getFileName()); // Size TextView size = (TextView) view.findViewById(R.id.shareFileSize); - if (mFile.isFolder()){ + if (mFile.isFolder()) { size.setVisibility(View.GONE); } else { size.setText(DisplayUtils.bytesToHumanReadable(mFile.getFileLength())); } - // List of share with users - TextView noShares = (TextView) view.findViewById(R.id.shareNoUsers); - - // TODO: Get shares from DB and show - - // Add User Button Button addUserGroupButton = (Button) view.findViewById(R.id.addUserButton); 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()) { + // very important, setCheched(...) is called automatically during + // Fragment recreation on device rotations + return; + } + if (isChecked) { + ((FileActivity) getActivity()).getFileOperationsHelper(). + shareFileViaLink(mFile); + + } else { + ((FileActivity) getActivity()).getFileOperationsHelper(). + unshareFileViaLink(mFile); + } + } + }; + Switch shareViaLinkSwitch = (Switch) view.findViewById(R.id.shareViaLinkSectionSwitch); + shareViaLinkSwitch.setOnCheckedChangeListener(mOnShareViaLinkSwitchCheckedChangeListener); + + // Switch for expiration date + mOnExpirationDateSwitchCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + if (!isResumed()) { + // very important, setCheched(...) is called automatically during + // Fragment recreation on device rotations + return; + } + if (isChecked) { + ExpirationDatePickerDialogFragment dialog = + ExpirationDatePickerDialogFragment.newInstance(mFile); + dialog.show( + getActivity().getSupportFragmentManager(), + ExpirationDatePickerDialogFragment.DATE_PICKER_DIALOG + ); + + } else { + ((FileActivity) getActivity()).getFileOperationsHelper(). + setExpirationDateToShareViaLink(mFile, -1, -1, -1); + } + + // undo the toggle to grant the view will be correct if the dialog is cancelled + buttonView.setOnCheckedChangeListener(null); + buttonView.toggle(); + buttonView.setOnCheckedChangeListener(mOnExpirationDateSwitchCheckedChangeListener); + } + }; + Switch shareViaLinkExpirationSwitch = (Switch) view.findViewById(R.id.shareViaLinkExpirationSwitch); + shareViaLinkExpirationSwitch.setOnCheckedChangeListener(mOnExpirationDateSwitchCheckedChangeListener); + + // Switch for password + mOnPasswordSwitchCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + if (!isResumed()) { + // very important, setCheched(...) is called automatically during + // Fragment recreation on device rotations + return; + } + if (isChecked) { + ((FileActivity) getActivity()).getFileOperationsHelper(). + requestPasswordForShareViaLink(mFile); + } else { + ((FileActivity) getActivity()).getFileOperationsHelper(). + setPasswordToShareViaLink(mFile, ""); // "" clears + } + + // undo the toggle to grant the view will be correct if the dialog is cancelled + buttonView.setOnCheckedChangeListener(null); + buttonView.toggle(); + buttonView.setOnCheckedChangeListener(mOnPasswordSwitchCheckedChangeListener); + } + }; + Switch shareViaLinkPasswordSwitch = (Switch) view.findViewById(R.id.shareViaLinkPasswordSwitch); + shareViaLinkPasswordSwitch.setOnCheckedChangeListener(mOnShareViaLinkSwitchCheckedChangeListener); + return view; } @@ -172,14 +276,11 @@ public class ShareFileFragment extends Fragment public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); - getShares(mFile); - } + // 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 @@ -199,60 +300,198 @@ public class ShareFileFragment extends Fragment mListener = null; } - // Get users and groups to fill the "share with" list - private void getShares(OCFile file){ - mShares = new ArrayList<>(); + /** + * 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(); + } + } - RemoteOperationResult result = null; + private void updateListOfUserGroups() { + // Update list of users/groups + // 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 (mPrivateShares.size() > 0) { + noShares.setVisibility(View.GONE); + usersList.setVisibility(View.VISIBLE); + usersList.setAdapter(mUserGroupsAdapter); - // Show loading - ( (ShareActivity) getActivity()).showWaitingLoadDialog(); - // Get Users and Groups - GetShareWithUserAsyncTask getTask = new GetShareWithUserAsyncTask(this); - FileDataStorageManager fileDataStorageManager = - new FileDataStorageManager(mAccount, getActivity().getContentResolver()); - Object[] params = { mFile, mAccount, fileDataStorageManager}; - getTask.execute(params); + } else { + noShares.setVisibility(View.VISIBLE); + usersList.setVisibility(View.GONE); + } } @Override - public void onGetDataShareWithFinish(RemoteOperationResult result) { - // Remove loading - ((ShareActivity) getActivity()).dismissWaitingLoadDialog(); - if (result != null && result.isSuccess()) { - // update local database - for(Object obj: result.getData()) { - if ( ((OCShare) obj).getShareType() == ShareType.USER || - ((OCShare) obj).getShareType() == ShareType.GROUP ){ - mShares.add((OCShare) obj); - } - } + public void unshareButtonPressed(OCShare share) { + // Unshare + mListener.unshareWith(share); + Log_OC.d(TAG, "Unshare - " + share.getSharedWithDisplayName()); + } - // Update list of users/groups - mUserGroupsAdapter = new ShareUserListAdapter(getActivity().getApplicationContext(), - R.layout.share_user_item, mShares); - // Show data - TextView noShares = (TextView) getView().findViewById(R.id.shareNoUsers); - ListView usersList = (ListView) getView().findViewById(R.id.shareUsersList); - if (mShares.size() > 0) { - noShares.setVisibility(View.GONE); - usersList.setVisibility(View.VISIBLE); - usersList.setAdapter(mUserGroupsAdapter); + /** + * Get public link from the DB to fill in the "Share link" section in the UI. + * + * 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 public share section + updatePublicShareSection(); + } + } + /** + * 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); + shareViaLinkSwitch.setChecked(true); + shareViaLinkSwitch.setOnCheckedChangeListener( + mOnShareViaLinkSwitchCheckedChangeListener + ); + } + getExpirationDateSection().setVisibility(View.VISIBLE); + getPasswordSection().setVisibility(View.VISIBLE); + getGetLinkButton().setVisibility(View.VISIBLE); + + /// update state of expiration date switch and message depending on expiration date + /// update state of expiration date switch and message depending on expiration date + Switch expirationDateSwitch = getExpirationDateSwitch(); + // set null listener before setChecked() to prevent infinite loop of calls + expirationDateSwitch.setOnCheckedChangeListener(null); + long expirationDate = mPublicShare.getExpirationDate(); + if (expirationDate > 0) { + if (!expirationDateSwitch.isChecked()) { + expirationDateSwitch.toggle(); + } + String formattedDate = + SimpleDateFormat.getDateInstance().format( + new Date(expirationDate) + ); + getExpirationDateValue().setText(formattedDate); } else { - noShares.setVisibility(View.VISIBLE); - usersList.setVisibility(View.GONE); + if (expirationDateSwitch.isChecked()) { + expirationDateSwitch.toggle(); + } + getExpirationDateValue().setText(R.string.empty); } + // recover listener + expirationDateSwitch.setOnCheckedChangeListener( + mOnExpirationDateSwitchCheckedChangeListener + ); + + /// update state of password switch and message depending on password protection + Switch passwordSwitch = getPasswordSwitch(); + // set null listener before setChecked() to prevent infinite loop of calls + passwordSwitch.setOnCheckedChangeListener(null); + if (mPublicShare.isPasswordProtected()) { + if (!passwordSwitch.isChecked()) { + passwordSwitch.toggle(); + } + getPasswordValue().setVisibility(View.VISIBLE); + } else { + if (passwordSwitch.isChecked()) { + passwordSwitch.toggle(); + } + getPasswordValue().setVisibility(View.INVISIBLE); + } + // recover listener + passwordSwitch.setOnCheckedChangeListener( + mOnPasswordSwitchCheckedChangeListener + ); + } else { - Toast.makeText(getActivity(), result.getLogMessage(), Toast.LENGTH_SHORT).show(); + /// 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); } + } + + + /// BEWARE: next methods will failed with NullPointerException if called before onCreateView() finishes + + private Switch getShareViaLinkSwitch() { + return (Switch) getView().findViewById(R.id.shareViaLinkSectionSwitch); + } + + private View getExpirationDateSection() { + return getView().findViewById(R.id.shareViaLinkExpirationSection); + } + + private Switch getExpirationDateSwitch() { + return (Switch) getView().findViewById(R.id.shareViaLinkExpirationSwitch); + } + + private TextView getExpirationDateValue() { + return (TextView) getView().findViewById(R.id.shareViaLinkExpirationValue); + } + private View getPasswordSection() { + return getView().findViewById(R.id.shareViaLinkPasswordSection); } - // TODO: review if it is necessary + private Switch getPasswordSwitch() { + return (Switch) getView().findViewById(R.id.shareViaLinkPasswordSwitch); + } + + private TextView getPasswordValue() { + return (TextView) getView().findViewById(R.id.shareViaLinkPasswordValue); + } + + private AppCompatButton getGetLinkButton() { + return (AppCompatButton) getView().findViewById(R.id.shareViewLinkGetLinkButton); + } + + /** * This interface must be implemented by activities that contain this * fragment to allow an interaction in this fragment to be communicated @@ -265,8 +504,8 @@ public class ShareFileFragment extends Fragment */ public interface OnShareFragmentInteractionListener { void showSearchUsersAndGroups(); - - void onShareFragmentInteraction(Uri uri); + void refreshUsersOrGroupsListFromServer(); + void unshareWith(OCShare share); } }