2 * ownCloud Android client application
5 * @author David A. Velasco
6 * Copyright (C) 2015 ownCloud Inc.
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 package com
.owncloud
.android
.ui
.fragment
;
24 import android
.accounts
.Account
;
25 import android
.app
.Activity
;
26 import android
.graphics
.Bitmap
;
27 import android
.os
.Bundle
;
28 import android
.support
.v4
.app
.Fragment
;
29 import android
.support
.v7
.widget
.AppCompatButton
;
30 import android
.view
.LayoutInflater
;
31 import android
.view
.View
;
32 import android
.view
.ViewGroup
;
33 import android
.widget
.Button
;
34 import android
.widget
.CompoundButton
;
35 import android
.widget
.ImageView
;
36 import android
.widget
.ListAdapter
;
37 import android
.widget
.ListView
;
38 import android
.widget
.ScrollView
;
39 import android
.widget
.Switch
;
40 import android
.widget
.TextView
;
41 import android
.widget
.Toast
;
43 import com
.owncloud
.android
.R
;
44 import com
.owncloud
.android
.authentication
.AccountUtils
;
45 import com
.owncloud
.android
.datamodel
.OCFile
;
46 import com
.owncloud
.android
.datamodel
.ThumbnailsCacheManager
;
47 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
48 import com
.owncloud
.android
.lib
.resources
.shares
.OCShare
;
49 import com
.owncloud
.android
.lib
.resources
.shares
.ShareType
;
50 import com
.owncloud
.android
.lib
.resources
.status
.OCCapability
;
51 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
52 import com
.owncloud
.android
.ui
.adapter
.ShareUserListAdapter
;
53 import com
.owncloud
.android
.ui
.dialog
.ExpirationDatePickerDialogFragment
;
54 import com
.owncloud
.android
.utils
.DisplayUtils
;
55 import com
.owncloud
.android
.utils
.MimetypeIconUtil
;
57 import java
.text
.SimpleDateFormat
;
59 import java
.util
.ArrayList
;
60 import java
.util
.Date
;
63 * Fragment for Sharing a file with sharees (users or groups) or creating
66 * A simple {@link Fragment} subclass.
68 * Activities that contain this fragment must implement the
69 * {@link ShareFileFragment.OnShareFragmentInteractionListener} interface
70 * to handle interaction events.
72 * Use the {@link ShareFileFragment#newInstance} factory method to
73 * create an instance of this fragment.
75 public class ShareFileFragment
extends Fragment
76 implements ShareUserListAdapter
.ShareUserAdapterListener
{
78 private static final String TAG
= ShareFileFragment
.class.getSimpleName();
80 /** The fragment initialization parameters */
81 private static final String ARG_FILE
= "FILE";
82 private static final String ARG_ACCOUNT
= "ACCOUNT";
84 // /** Tag for dialog */
85 // private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG";
87 /** File to share, received as a parameter in construction time */
90 /** OC account holding the file to share, received as a parameter in construction time */
91 private Account mAccount
;
93 /** Reference to parent listener */
94 private OnShareFragmentInteractionListener mListener
;
96 /** List of private shares bound to the file */
97 private ArrayList
<OCShare
> mPrivateShares
;
99 /** Capabilities of the server */
100 private OCCapability mCapabilities
;
102 /** Adapter to show private shares */
103 private ShareUserListAdapter mUserGroupsAdapter
= null
;
105 /** Public share bound to the file */
106 private OCShare mPublicShare
;
108 /** Listener for changes on switch to share / unshare publicly */
109 private CompoundButton
.OnCheckedChangeListener mOnShareViaLinkSwitchCheckedChangeListener
;
112 * Listener for user actions to set, update or clear password on public link
114 private OnPasswordInteractionListener mOnPasswordInteractionListener
= null
;
117 * Listener for user actions to set, update or clear expiration date on public link
119 private OnExpirationDateInteractionListener mOnExpirationDateInteractionListener
= null
;
123 * Public factory method to create new ShareFileFragment instances.
125 * @param fileToShare An {@link OCFile} to show in the fragment
126 * @param account An ownCloud account
127 * @return A new instance of fragment ShareFileFragment.
129 public static ShareFileFragment
newInstance(OCFile fileToShare
, Account account
) {
130 ShareFileFragment fragment
= new ShareFileFragment();
131 Bundle args
= new Bundle();
132 args
.putParcelable(ARG_FILE
, fileToShare
);
133 args
.putParcelable(ARG_ACCOUNT
, account
);
134 fragment
.setArguments(args
);
138 public ShareFileFragment() {
139 // Required empty public constructor
146 public void onCreate(Bundle savedInstanceState
) {
147 super.onCreate(savedInstanceState
);
148 if (getArguments() != null
) {
149 mFile
= getArguments().getParcelable(ARG_FILE
);
150 mAccount
= getArguments().getParcelable(ARG_ACCOUNT
);
159 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
160 Bundle savedInstanceState
) {
161 // Inflate the layout for this fragment
162 View view
= inflater
.inflate(R
.layout
.share_file_layout
, container
, false
);
166 ImageView icon
= (ImageView
) view
.findViewById(R
.id
.shareFileIcon
);
167 icon
.setImageResource(MimetypeIconUtil
.getFileTypeIconId(mFile
.getMimetype(),
168 mFile
.getFileName()));
169 if (mFile
.isImage()) {
170 String remoteId
= String
.valueOf(mFile
.getRemoteId());
171 Bitmap thumbnail
= ThumbnailsCacheManager
.getBitmapFromDiskCache(remoteId
);
172 if (thumbnail
!= null
) {
173 icon
.setImageBitmap(thumbnail
);
177 TextView filename
= (TextView
) view
.findViewById(R
.id
.shareFileName
);
178 filename
.setText(mFile
.getFileName());
180 TextView size
= (TextView
) view
.findViewById(R
.id
.shareFileSize
);
181 if (mFile
.isFolder()) {
182 size
.setVisibility(View
.GONE
);
184 size
.setText(DisplayUtils
.bytesToHumanReadable(mFile
.getFileLength()));
188 Button addUserGroupButton
= (Button
)
189 view
.findViewById(R
.id
.addUserButton
);
190 addUserGroupButton
.setOnClickListener(new View
.OnClickListener() {
192 public void onClick(View view
) {
193 boolean shareWithUsersEnable
= AccountUtils
.hasSearchUsersSupport(mAccount
);
194 if (shareWithUsersEnable
) {
195 // Show Search Fragment
196 mListener
.showSearchUsersAndGroups();
198 String message
= getString(R
.string
.share_sharee_unavailable
);
199 Toast
.makeText(getActivity(), message
, Toast
.LENGTH_LONG
).show();
204 // Switch to create public share
205 mOnShareViaLinkSwitchCheckedChangeListener
= new CompoundButton
.OnCheckedChangeListener() {
207 public void onCheckedChanged(CompoundButton buttonView
, boolean isChecked
) {
209 // very important, setCheched(...) is called automatically during
210 // Fragment recreation on device rotations
214 ((FileActivity
) getActivity()).getFileOperationsHelper().
215 shareFileViaLink(mFile
);
218 ((FileActivity
) getActivity()).getFileOperationsHelper().
219 unshareFileViaLink(mFile
);
223 Switch shareViaLinkSwitch
= (Switch
) view
.findViewById(R
.id
.shareViaLinkSectionSwitch
);
224 shareViaLinkSwitch
.setOnCheckedChangeListener(mOnShareViaLinkSwitchCheckedChangeListener
);
226 // Set listener for user actions on expiration date
227 initExpirationListener(view
);
229 // Set listener for user actions on password
230 initPasswordListener(view
);
236 * Binds listener for user actions that start any update on a expiration date
237 * for the public link to the views receiving the user events.
239 * @param shareView Root view in the fragment.
241 private void initExpirationListener(View shareView
) {
242 mOnExpirationDateInteractionListener
= new OnExpirationDateInteractionListener();
244 ((Switch
) shareView
.findViewById(R
.id
.shareViaLinkExpirationSwitch
)).
245 setOnCheckedChangeListener(mOnExpirationDateInteractionListener
);
247 shareView
.findViewById(R
.id
.shareViaLinkExpirationLabel
).
248 setOnClickListener(mOnExpirationDateInteractionListener
);
250 shareView
.findViewById(R
.id
.shareViaLinkExpirationValue
).
251 setOnClickListener(mOnExpirationDateInteractionListener
);
255 * Listener for user actions that start any update on the expiration date for the public link.
257 private class OnExpirationDateInteractionListener
258 implements CompoundButton
.OnCheckedChangeListener
, View
.OnClickListener
{
261 * Called by R.id.shareViaLinkExpirationSwitch to set or clear the expiration date.
263 * @param switchView {@link Switch} toggled by the user, R.id.shareViaLinkExpirationSwitch
264 * @param isChecked New switch state.
267 public void onCheckedChanged(CompoundButton switchView
, boolean isChecked
) {
269 // very important, setCheched(...) is called automatically during
270 // Fragment recreation on device rotations
274 ExpirationDatePickerDialogFragment dialog
=
275 ExpirationDatePickerDialogFragment
.newInstance(mFile
, -1);
277 getActivity().getSupportFragmentManager(),
278 ExpirationDatePickerDialogFragment
.DATE_PICKER_DIALOG
282 ((FileActivity
) getActivity()).getFileOperationsHelper().
283 setExpirationDateToShareViaLink(mFile
, -1);
286 // undo the toggle to grant the view will be correct if the dialog is cancelled
287 switchView
.setOnCheckedChangeListener(null
);
289 switchView
.setOnCheckedChangeListener(mOnExpirationDateInteractionListener
);
293 * Called by R.id.shareViaLinkExpirationLabel or R.id.shareViaLinkExpirationValue
294 * to change the current expiration date.
296 * @param expirationView Label or value view touched by the user.
299 public void onClick(View expirationView
) {
300 if (mPublicShare
!= null
&& mPublicShare
.getExpirationDate() > 0) {
301 long chosenDateInMillis
= -1;
302 if (mPublicShare
!= null
) {
303 chosenDateInMillis
= mPublicShare
.getExpirationDate();
305 ExpirationDatePickerDialogFragment dialog
=
306 ExpirationDatePickerDialogFragment
.newInstance(
311 getActivity().getSupportFragmentManager(),
312 ExpirationDatePickerDialogFragment
.DATE_PICKER_DIALOG
320 * Binds listener for user actions that start any update on a password for the public link
321 * to the views receiving the user events.
323 * @param shareView Root view in the fragment.
325 private void initPasswordListener(View shareView
) {
326 mOnPasswordInteractionListener
= new OnPasswordInteractionListener();
328 ((Switch
) shareView
.findViewById(R
.id
.shareViaLinkPasswordSwitch
)).
329 setOnCheckedChangeListener(mOnPasswordInteractionListener
);
331 shareView
.findViewById(R
.id
.shareViaLinkPasswordLabel
).
332 setOnClickListener(mOnPasswordInteractionListener
);
334 shareView
.findViewById(R
.id
.shareViaLinkPasswordValue
).
335 setOnClickListener(mOnPasswordInteractionListener
);
340 * Listener for user actions that start any update on a password for the public link.
342 private class OnPasswordInteractionListener
343 implements CompoundButton
.OnCheckedChangeListener
, View
.OnClickListener
{
346 * Called by R.id.shareViaLinkPasswordSwitch to set or clear the password.
348 * @param switchView {@link Switch} toggled by the user, R.id.shareViaLinkPasswordSwitch
349 * @param isChecked New switch state.
352 public void onCheckedChanged(CompoundButton switchView
, boolean isChecked
) {
354 // very important, setCheched(...) is called automatically during
355 // Fragment recreation on device rotations
359 ((FileActivity
) getActivity()).getFileOperationsHelper().
360 requestPasswordForShareViaLink(mFile
);
362 ((FileActivity
) getActivity()).getFileOperationsHelper().
363 setPasswordToShareViaLink(mFile
, ""); // "" clears
366 // undo the toggle to grant the view will be correct if the dialog is cancelled
367 switchView
.setOnCheckedChangeListener(null
);
369 switchView
.setOnCheckedChangeListener(mOnPasswordInteractionListener
);
373 * Called by R.id.shareViaLinkPasswordLabel or R.id.shareViaLinkPasswordValue
374 * to change the current password.
376 * @param passwordView Label or value view touched by the user.
379 public void onClick(View passwordView
) {
380 if (mPublicShare
!= null
&& mPublicShare
.isPasswordProtected()) {
381 ((FileActivity
) getActivity()).getFileOperationsHelper().
382 requestPasswordForShareViaLink(mFile
);
389 public void onActivityCreated(Bundle savedInstanceState
) {
390 super.onActivityCreated(savedInstanceState
);
392 // Load known capabilities of the server from DB
393 refreshCapabilitiesFromDB();
395 // Load data into the list of private shares
396 refreshUsersOrGroupsListFromDB();
398 // Load data of public share, if exists
399 refreshPublicShareFromDB();
403 public void onAttach(Activity activity
) {
404 super.onAttach(activity
);
406 mListener
= (OnShareFragmentInteractionListener
) activity
;
407 } catch (ClassCastException e
) {
408 throw new ClassCastException(activity
.toString()
409 + " must implement OnShareFragmentInteractionListener");
414 public void onDetach() {
421 * Get known server capabilities from DB
423 * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
424 * instance ready to use. If not ready, does nothing.
426 public void refreshCapabilitiesFromDB() {
427 if (((FileActivity
)mListener
).getStorageManager() != null
) {
428 mCapabilities
= ((FileActivity
)mListener
).getStorageManager().
429 getCapability(mAccount
.name
);
435 * Get users and groups from the DB to fill in the "share with" list.
437 * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
438 * instance ready to use. If not ready, does nothing.
440 public void refreshUsersOrGroupsListFromDB (){
441 if (((FileActivity
) mListener
).getStorageManager() != null
) {
442 // Get Users and Groups
443 mPrivateShares
= ((FileActivity
) mListener
).getStorageManager().getSharesWithForAFile(
444 mFile
.getRemotePath(),
448 // Update list of users/groups
449 updateListOfUserGroups();
453 private void updateListOfUserGroups() {
454 // Update list of users/groups
455 // TODO Refactoring: create a new {@link ShareUserListAdapter} instance with every call should not be needed
456 mUserGroupsAdapter
= new ShareUserListAdapter(
458 R
.layout
.share_user_item
,
464 TextView noShares
= (TextView
) getView().findViewById(R
.id
.shareNoUsers
);
465 ListView usersList
= (ListView
) getView().findViewById(R
.id
.shareUsersList
);
467 if (mPrivateShares
.size() > 0) {
468 noShares
.setVisibility(View
.GONE
);
469 usersList
.setVisibility(View
.VISIBLE
);
470 usersList
.setAdapter(mUserGroupsAdapter
);
471 setListViewHeightBasedOnChildren(usersList
);
473 noShares
.setVisibility(View
.VISIBLE
);
474 usersList
.setVisibility(View
.GONE
);
477 // Set Scroll to initial position
478 ScrollView scrollView
= (ScrollView
) getView().findViewById(R
.id
.shareScroll
);
479 scrollView
.scrollTo(0, 0);
483 public void unshareButtonPressed(OCShare share
) {
485 mListener
.unshareWith(share
);
486 Log_OC
.d(TAG
, "Unshare - " + share
.getSharedWithDisplayName());
492 * Get public link from the DB to fill in the "Share link" section in the UI.
494 * Takes into account server capabilities before reading database.
496 * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
497 * instance ready to use. If not ready, does nothing.
499 public void refreshPublicShareFromDB() {
500 if (isPublicShareDisabled()) {
503 } else if (((FileActivity
) mListener
).getStorageManager() != null
) {
505 mPublicShare
= ((FileActivity
) mListener
).getStorageManager().getFirstShareByPathAndType(
506 mFile
.getRemotePath(),
507 ShareType
.PUBLIC_LINK
,
511 // Update public share section
512 updatePublicShareSection();
517 * @return 'True' when public share is disabled in the server
519 private boolean isPublicShareDisabled() {
520 return (mCapabilities
!= null
&&
521 mCapabilities
.getFilesSharingPublicEnabled().isFalse()
526 * Updates in the UI the section about public share with the information in the current
527 * public share bound to mFile, if any
529 private void updatePublicShareSection() {
530 if (mPublicShare
!= null
&& ShareType
.PUBLIC_LINK
.equals(mPublicShare
.getShareType())) {
531 /// public share bound -> expand section
532 Switch shareViaLinkSwitch
= getShareViaLinkSwitch();
533 if (!shareViaLinkSwitch
.isChecked()) {
534 // set null listener before setChecked() to prevent infinite loop of calls
535 shareViaLinkSwitch
.setOnCheckedChangeListener(null
);
536 shareViaLinkSwitch
.setChecked(true
);
537 shareViaLinkSwitch
.setOnCheckedChangeListener(
538 mOnShareViaLinkSwitchCheckedChangeListener
541 getExpirationDateSection().setVisibility(View
.VISIBLE
);
542 getPasswordSection().setVisibility(View
.VISIBLE
);
544 AppCompatButton getLinkButton
= getGetLinkButton();
545 getLinkButton
.setVisibility(View
.VISIBLE
);
546 getLinkButton
.setOnClickListener(new View
.OnClickListener() {
548 public void onClick(View v
) {
549 //GetLink from the server and show ShareLinkToDialog
550 ((FileActivity
) getActivity()).getFileOperationsHelper().
551 getFileWithLink(mFile
);
556 /// update state of expiration date switch and message depending on expiration date
557 Switch expirationDateSwitch
= getExpirationDateSwitch();
558 // set null listener before setChecked() to prevent infinite loop of calls
559 expirationDateSwitch
.setOnCheckedChangeListener(null
);
560 long expirationDate
= mPublicShare
.getExpirationDate();
561 if (expirationDate
> 0) {
562 if (!expirationDateSwitch
.isChecked()) {
563 expirationDateSwitch
.toggle();
565 String formattedDate
=
566 SimpleDateFormat
.getDateInstance().format(
567 new Date(expirationDate
)
569 getExpirationDateValue().setText(formattedDate
);
571 if (expirationDateSwitch
.isChecked()) {
572 expirationDateSwitch
.toggle();
574 getExpirationDateValue().setText(R
.string
.empty
);
577 expirationDateSwitch
.setOnCheckedChangeListener(
578 mOnExpirationDateInteractionListener
581 /// update state of password switch and message depending on password protection
582 Switch passwordSwitch
= getPasswordSwitch();
583 // set null listener before setChecked() to prevent infinite loop of calls
584 passwordSwitch
.setOnCheckedChangeListener(null
);
585 if (mPublicShare
.isPasswordProtected()) {
586 if (!passwordSwitch
.isChecked()) {
587 passwordSwitch
.toggle();
589 getPasswordValue().setVisibility(View
.VISIBLE
);
591 if (passwordSwitch
.isChecked()) {
592 passwordSwitch
.toggle();
594 getPasswordValue().setVisibility(View
.INVISIBLE
);
597 passwordSwitch
.setOnCheckedChangeListener(
598 mOnPasswordInteractionListener
603 /// no public share -> collapse section
604 Switch shareViaLinkSwitch
= getShareViaLinkSwitch();
605 if (shareViaLinkSwitch
.isChecked()) {
606 shareViaLinkSwitch
.setOnCheckedChangeListener(null
);
607 getShareViaLinkSwitch().setChecked(false
);
608 shareViaLinkSwitch
.setOnCheckedChangeListener(
609 mOnShareViaLinkSwitchCheckedChangeListener
612 getExpirationDateSection().setVisibility(View
.GONE
);
613 getPasswordSection().setVisibility(View
.GONE
);
614 getGetLinkButton().setVisibility(View
.GONE
);
619 /// BEWARE: next methods will failed with NullPointerException if called before onCreateView() finishes
621 private Switch
getShareViaLinkSwitch() {
622 return (Switch
) getView().findViewById(R
.id
.shareViaLinkSectionSwitch
);
625 private View
getExpirationDateSection() {
626 return getView().findViewById(R
.id
.shareViaLinkExpirationSection
);
629 private Switch
getExpirationDateSwitch() {
630 return (Switch
) getView().findViewById(R
.id
.shareViaLinkExpirationSwitch
);
633 private TextView
getExpirationDateValue() {
634 return (TextView
) getView().findViewById(R
.id
.shareViaLinkExpirationValue
);
637 private View
getPasswordSection() {
638 return getView().findViewById(R
.id
.shareViaLinkPasswordSection
);
641 private Switch
getPasswordSwitch() {
642 return (Switch
) getView().findViewById(R
.id
.shareViaLinkPasswordSwitch
);
645 private TextView
getPasswordValue() {
646 return (TextView
) getView().findViewById(R
.id
.shareViaLinkPasswordValue
);
649 private AppCompatButton
getGetLinkButton() {
650 return (AppCompatButton
) getView().findViewById(R
.id
.shareViaLinkGetLinkButton
);
654 * Hides all the UI elements related to public share
656 private void hidePublicShare() {
657 getShareViaLinkSwitch().setVisibility(View
.GONE
);
658 getExpirationDateSection().setVisibility(View
.GONE
);
659 getPasswordSection().setVisibility(View
.GONE
);
660 getGetLinkButton().setVisibility(View
.GONE
);
663 public static void setListViewHeightBasedOnChildren(ListView listView
) {
664 ListAdapter listAdapter
= listView
.getAdapter();
665 if (listAdapter
== null
) {
668 int desiredWidth
= View
.MeasureSpec
.makeMeasureSpec(listView
.getWidth(), View
.MeasureSpec
.AT_MOST
);
671 for (int i
= 0; i
< listAdapter
.getCount(); i
++) {
672 view
= listAdapter
.getView(i
, view
, listView
);
674 view
.setLayoutParams(new ViewGroup
.LayoutParams(desiredWidth
, ViewGroup
.LayoutParams
.WRAP_CONTENT
));
676 view
.measure(desiredWidth
, View
.MeasureSpec
.UNSPECIFIED
);
677 totalHeight
+= view
.getMeasuredHeight();
679 ViewGroup
.LayoutParams params
= listView
.getLayoutParams();
680 params
.height
= totalHeight
+ (listView
.getDividerHeight() * (listAdapter
.getCount() - 1));
681 listView
.setLayoutParams(params
);
682 listView
.requestLayout();
686 * This interface must be implemented by activities that contain this
687 * fragment to allow an interaction in this fragment to be communicated
688 * to the activity and potentially other fragments contained in that
691 * See the Android Training lesson <a href=
692 * "http://developer.android.com/training/basics/fragments/communicating.html"
693 * >Communicating with Other Fragments</a> for more information.
695 public interface OnShareFragmentInteractionListener
{
696 void showSearchUsersAndGroups();
697 void refreshUsersOrGroupsListFromServer();
698 void unshareWith(OCShare share
);