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
.content
.Intent
;
27 import android
.graphics
.Bitmap
;
28 import android
.os
.Bundle
;
29 import android
.support
.v4
.app
.DialogFragment
;
30 import android
.support
.v4
.app
.Fragment
;
31 import android
.support
.v7
.widget
.AppCompatButton
;
32 import android
.view
.LayoutInflater
;
33 import android
.view
.View
;
34 import android
.view
.ViewGroup
;
35 import android
.widget
.Button
;
36 import android
.widget
.CompoundButton
;
37 import android
.widget
.ImageView
;
38 import android
.widget
.ListAdapter
;
39 import android
.widget
.ListView
;
40 import android
.widget
.ScrollView
;
41 import android
.widget
.Switch
;
42 import android
.widget
.TextView
;
43 import android
.widget
.Toast
;
45 import com
.owncloud
.android
.R
;
46 import com
.owncloud
.android
.authentication
.AccountUtils
;
47 import com
.owncloud
.android
.datamodel
.OCFile
;
48 import com
.owncloud
.android
.datamodel
.ThumbnailsCacheManager
;
49 import com
.owncloud
.android
.files
.FileOperationsHelper
;
50 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
51 import com
.owncloud
.android
.lib
.resources
.shares
.OCShare
;
52 import com
.owncloud
.android
.lib
.resources
.shares
.ShareType
;
53 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
54 import com
.owncloud
.android
.ui
.adapter
.ShareUserListAdapter
;
55 import com
.owncloud
.android
.ui
.dialog
.ExpirationDatePickerDialogFragment
;
56 import com
.owncloud
.android
.utils
.DisplayUtils
;
57 import com
.owncloud
.android
.utils
.MimetypeIconUtil
;
59 import java
.text
.SimpleDateFormat
;
61 import java
.util
.ArrayList
;
62 import java
.util
.Date
;
65 * Fragment for Sharing a file with sharees (users or groups) or creating
68 * A simple {@link Fragment} subclass.
70 * Activities that contain this fragment must implement the
71 * {@link ShareFileFragment.OnShareFragmentInteractionListener} interface
72 * to handle interaction events.
74 * Use the {@link ShareFileFragment#newInstance} factory method to
75 * create an instance of this fragment.
77 public class ShareFileFragment
extends Fragment
78 implements ShareUserListAdapter
.ShareUserAdapterListener
{
80 private static final String TAG
= ShareFileFragment
.class.getSimpleName();
82 /** The fragment initialization parameters */
83 private static final String ARG_FILE
= "FILE";
84 private static final String ARG_ACCOUNT
= "ACCOUNT";
86 // /** Tag for dialog */
87 // private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG";
89 /** File to share, received as a parameter in construction time */
92 /** OC account holding the file to share, received as a parameter in construction time */
93 private Account mAccount
;
95 /** Reference to parent listener */
96 private OnShareFragmentInteractionListener mListener
;
98 /** List of private shares bound to the file */
99 private ArrayList
<OCShare
> mPrivateShares
;
101 /** Adapter to show private shares */
102 private ShareUserListAdapter mUserGroupsAdapter
= null
;
104 /** Public share bound to the file */
105 private OCShare mPublicShare
;
107 /** Listener for changes on switch to share / unshare publicly */
108 private CompoundButton
.OnCheckedChangeListener mOnShareViaLinkSwitchCheckedChangeListener
;
111 * Listener for user actions to set, update or clear password on public link
113 private OnPasswordInteractionListener mOnPasswordInteractionListener
= null
;
116 * Listener for user actions to set, update or clear expiration date on public link
118 private OnExpirationDateInteractionListener mOnExpirationDateInteractionListener
= null
;
121 * Public factory method to create new ShareFileFragment instances.
123 * @param fileToShare An {@link OCFile} to show in the fragment
124 * @param account An ownCloud account
125 * @return A new instance of fragment ShareFileFragment.
127 public static ShareFileFragment
newInstance(OCFile fileToShare
, Account account
) {
128 ShareFileFragment fragment
= new ShareFileFragment();
129 Bundle args
= new Bundle();
130 args
.putParcelable(ARG_FILE
, fileToShare
);
131 args
.putParcelable(ARG_ACCOUNT
, account
);
132 fragment
.setArguments(args
);
136 public ShareFileFragment() {
137 // Required empty public constructor
144 public void onCreate(Bundle savedInstanceState
) {
145 super.onCreate(savedInstanceState
);
146 if (getArguments() != null
) {
147 mFile
= getArguments().getParcelable(ARG_FILE
);
148 mAccount
= getArguments().getParcelable(ARG_ACCOUNT
);
157 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
158 Bundle savedInstanceState
) {
159 // Inflate the layout for this fragment
160 View view
= inflater
.inflate(R
.layout
.share_file_layout
, container
, false
);
164 ImageView icon
= (ImageView
) view
.findViewById(R
.id
.shareFileIcon
);
165 icon
.setImageResource(MimetypeIconUtil
.getFileTypeIconId(mFile
.getMimetype(),
166 mFile
.getFileName()));
167 if (mFile
.isImage()) {
168 String remoteId
= String
.valueOf(mFile
.getRemoteId());
169 Bitmap thumbnail
= ThumbnailsCacheManager
.getBitmapFromDiskCache(remoteId
);
170 if (thumbnail
!= null
) {
171 icon
.setImageBitmap(thumbnail
);
175 TextView filename
= (TextView
) view
.findViewById(R
.id
.shareFileName
);
176 filename
.setText(mFile
.getFileName());
178 TextView size
= (TextView
) view
.findViewById(R
.id
.shareFileSize
);
179 if (mFile
.isFolder()) {
180 size
.setVisibility(View
.GONE
);
182 size
.setText(DisplayUtils
.bytesToHumanReadable(mFile
.getFileLength()));
186 Button addUserGroupButton
= (Button
)
187 view
.findViewById(R
.id
.addUserButton
);
188 addUserGroupButton
.setOnClickListener(new View
.OnClickListener() {
190 public void onClick(View view
) {
191 boolean shareWithUsersEnable
= AccountUtils
.hasSearchUsersSupport(mAccount
);
192 if (shareWithUsersEnable
) {
193 // Show Search Fragment
194 mListener
.showSearchUsersAndGroups();
196 String message
= getString(R
.string
.share_sharee_unavailable
);
197 Toast
.makeText(getActivity(), message
, Toast
.LENGTH_LONG
).show();
202 // Switch to create public share
203 mOnShareViaLinkSwitchCheckedChangeListener
= new CompoundButton
.OnCheckedChangeListener() {
205 public void onCheckedChanged(CompoundButton buttonView
, boolean isChecked
) {
207 // very important, setCheched(...) is called automatically during
208 // Fragment recreation on device rotations
212 ((FileActivity
) getActivity()).getFileOperationsHelper().
213 shareFileViaLink(mFile
);
216 ((FileActivity
) getActivity()).getFileOperationsHelper().
217 unshareFileViaLink(mFile
);
221 Switch shareViaLinkSwitch
= (Switch
) view
.findViewById(R
.id
.shareViaLinkSectionSwitch
);
222 shareViaLinkSwitch
.setOnCheckedChangeListener(mOnShareViaLinkSwitchCheckedChangeListener
);
224 // Set listener for user actions on expiration date
225 initExpirationListener(view
);
227 // Set listener for user actions on password
228 initPasswordListener(view
);
235 * Binds listener for user actions that start any update on a expiration date
236 * for the public link to the views receiving the user events.
238 * @param shareView Root view in the fragment.
240 private void initExpirationListener(View shareView
) {
241 mOnExpirationDateInteractionListener
= new OnExpirationDateInteractionListener();
243 ((Switch
) shareView
.findViewById(R
.id
.shareViaLinkExpirationSwitch
)).
244 setOnCheckedChangeListener(mOnExpirationDateInteractionListener
);
246 shareView
.findViewById(R
.id
.shareViaLinkExpirationLabel
).
247 setOnClickListener(mOnExpirationDateInteractionListener
);
249 shareView
.findViewById(R
.id
.shareViaLinkExpirationValue
).
250 setOnClickListener(mOnExpirationDateInteractionListener
);
254 * Listener for user actions that start any update on the expiration date for the public link.
256 private class OnExpirationDateInteractionListener
257 implements CompoundButton
.OnCheckedChangeListener
, View
.OnClickListener
{
260 * Called by R.id.shareViaLinkExpirationSwitch to set or clear the expiration date.
262 * @param switchView {@link Switch} toggled by the user, R.id.shareViaLinkExpirationSwitch
263 * @param isChecked New switch state.
266 public void onCheckedChanged(CompoundButton switchView
, boolean isChecked
) {
268 // very important, setCheched(...) is called automatically during
269 // Fragment recreation on device rotations
273 ExpirationDatePickerDialogFragment dialog
=
274 ExpirationDatePickerDialogFragment
.newInstance(mFile
, -1);
276 getActivity().getSupportFragmentManager(),
277 ExpirationDatePickerDialogFragment
.DATE_PICKER_DIALOG
281 ((FileActivity
) getActivity()).getFileOperationsHelper().
282 setExpirationDateToShareViaLink(mFile
, -1);
285 // undo the toggle to grant the view will be correct if the dialog is cancelled
286 switchView
.setOnCheckedChangeListener(null
);
288 switchView
.setOnCheckedChangeListener(mOnExpirationDateInteractionListener
);
292 * Called by R.id.shareViaLinkExpirationLabel or R.id.shareViaLinkExpirationValue
293 * to change the current expiration date.
295 * @param expirationView Label or value view touched by the user.
298 public void onClick(View expirationView
) {
299 if (mPublicShare
!= null
&& mPublicShare
.getExpirationDate() > 0) {
300 long chosenDateInMillis
= -1;
301 if (mPublicShare
!= null
) {
302 chosenDateInMillis
= mPublicShare
.getExpirationDate();
304 ExpirationDatePickerDialogFragment dialog
=
305 ExpirationDatePickerDialogFragment
.newInstance(
310 getActivity().getSupportFragmentManager(),
311 ExpirationDatePickerDialogFragment
.DATE_PICKER_DIALOG
319 * Binds listener for user actions that start any update on a password for the public link
320 * to the views receiving the user events.
322 * @param shareView Root view in the fragment.
324 private void initPasswordListener(View shareView
) {
325 mOnPasswordInteractionListener
= new OnPasswordInteractionListener();
327 ((Switch
) shareView
.findViewById(R
.id
.shareViaLinkPasswordSwitch
)).
328 setOnCheckedChangeListener(mOnPasswordInteractionListener
);
330 shareView
.findViewById(R
.id
.shareViaLinkPasswordLabel
).
331 setOnClickListener(mOnPasswordInteractionListener
);
333 shareView
.findViewById(R
.id
.shareViaLinkPasswordValue
).
334 setOnClickListener(mOnPasswordInteractionListener
);
339 * Listener for user actions that start any update on a password for the public link.
341 private class OnPasswordInteractionListener
342 implements CompoundButton
.OnCheckedChangeListener
, View
.OnClickListener
{
345 * Called by R.id.shareViaLinkPasswordSwitch to set or clear the password.
347 * @param switchView {@link Switch} toggled by the user, R.id.shareViaLinkPasswordSwitch
348 * @param isChecked New switch state.
351 public void onCheckedChanged(CompoundButton switchView
, boolean isChecked
) {
353 // very important, setCheched(...) is called automatically during
354 // Fragment recreation on device rotations
358 ((FileActivity
) getActivity()).getFileOperationsHelper().
359 requestPasswordForShareViaLink(mFile
);
361 ((FileActivity
) getActivity()).getFileOperationsHelper().
362 setPasswordToShareViaLink(mFile
, ""); // "" clears
365 // undo the toggle to grant the view will be correct if the dialog is cancelled
366 switchView
.setOnCheckedChangeListener(null
);
368 switchView
.setOnCheckedChangeListener(mOnPasswordInteractionListener
);
372 * Called by R.id.shareViaLinkPasswordLabel or R.id.shareViaLinkPasswordValue
373 * to change the current password.
375 * @param passwordView Label or value view touched by the user.
378 public void onClick(View passwordView
) {
379 if (mPublicShare
!= null
&& mPublicShare
.isPasswordProtected()) {
380 ((FileActivity
) getActivity()).getFileOperationsHelper().
381 requestPasswordForShareViaLink(mFile
);
388 public void onActivityCreated(Bundle savedInstanceState
) {
389 super.onActivityCreated(savedInstanceState
);
391 // Load data into the list of private shares
392 refreshUsersOrGroupsListFromDB();
394 // Load data of public share, if exists
395 refreshPublicShareFromDB();
399 public void onAttach(Activity activity
) {
400 super.onAttach(activity
);
402 mListener
= (OnShareFragmentInteractionListener
) activity
;
403 } catch (ClassCastException e
) {
404 throw new ClassCastException(activity
.toString()
405 + " must implement OnShareFragmentInteractionListener");
410 public void onDetach() {
416 * Get users and groups from the DB to fill in the "share with" list.
418 * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
419 * instance ready to use. If not ready, does nothing.
421 public void refreshUsersOrGroupsListFromDB (){
422 if (((FileActivity
) mListener
).getStorageManager() != null
) {
423 // Get Users and Groups
424 mPrivateShares
= ((FileActivity
) mListener
).getStorageManager().getSharesWithForAFile(
425 mFile
.getRemotePath(),
429 // Update list of users/groups
430 updateListOfUserGroups();
434 private void updateListOfUserGroups() {
435 // Update list of users/groups
436 // TODO Refactoring: create a new {@link ShareUserListAdapter} instance with every call should not be needed
437 mUserGroupsAdapter
= new ShareUserListAdapter(
439 R
.layout
.share_user_item
,
445 TextView noShares
= (TextView
) getView().findViewById(R
.id
.shareNoUsers
);
446 ListView usersList
= (ListView
) getView().findViewById(R
.id
.shareUsersList
);
448 if (mPrivateShares
.size() > 0) {
449 noShares
.setVisibility(View
.GONE
);
450 usersList
.setVisibility(View
.VISIBLE
);
451 usersList
.setAdapter(mUserGroupsAdapter
);
452 setListViewHeightBasedOnChildren(usersList
);
454 noShares
.setVisibility(View
.VISIBLE
);
455 usersList
.setVisibility(View
.GONE
);
458 // Set Scroll to initial position
459 ScrollView scrollView
= (ScrollView
) getView().findViewById(R
.id
.shareScroll
);
460 scrollView
.scrollTo(0, 0);
464 public void unshareButtonPressed(OCShare share
) {
466 mListener
.unshareWith(share
);
467 Log_OC
.d(TAG
, "Unshare - " + share
.getSharedWithDisplayName());
473 * Get public link from the DB to fill in the "Share link" section in the UI.
475 * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
476 * instance ready to use. If not ready, does nothing.
478 public void refreshPublicShareFromDB() {
479 if (((FileActivity
) mListener
).getStorageManager() != null
) {
481 mPublicShare
= ((FileActivity
) mListener
).getStorageManager().getFirstShareByPathAndType(
482 mFile
.getRemotePath(),
483 ShareType
.PUBLIC_LINK
,
487 // Update public share section
488 updatePublicShareSection();
493 * Updates in the UI the section about public share with the information in the current
494 * public share bound to mFile, if any
496 private void updatePublicShareSection() {
497 if (mPublicShare
!= null
&& ShareType
.PUBLIC_LINK
.equals(mPublicShare
.getShareType())) {
498 /// public share bound -> expand section
499 Switch shareViaLinkSwitch
= getShareViaLinkSwitch();
500 if (!shareViaLinkSwitch
.isChecked()) {
501 // set null listener before setChecked() to prevent infinite loop of calls
502 shareViaLinkSwitch
.setOnCheckedChangeListener(null
);
503 shareViaLinkSwitch
.setChecked(true
);
504 shareViaLinkSwitch
.setOnCheckedChangeListener(
505 mOnShareViaLinkSwitchCheckedChangeListener
508 getExpirationDateSection().setVisibility(View
.VISIBLE
);
509 getPasswordSection().setVisibility(View
.VISIBLE
);
511 AppCompatButton getLinkButton
= getGetLinkButton();
512 getLinkButton
.setVisibility(View
.VISIBLE
);
513 getLinkButton
.setOnClickListener(new View
.OnClickListener() {
515 public void onClick(View v
) {
516 //GetLink from the server and show ShareLinkToDialog
517 ((FileActivity
) getActivity()).getFileOperationsHelper().
518 getFileWithLink(mFile
);
523 /// update state of expiration date switch and message depending on expiration date
524 /// update state of expiration date switch and message depending on expiration date
525 Switch expirationDateSwitch
= getExpirationDateSwitch();
526 // set null listener before setChecked() to prevent infinite loop of calls
527 expirationDateSwitch
.setOnCheckedChangeListener(null
);
528 long expirationDate
= mPublicShare
.getExpirationDate();
529 if (expirationDate
> 0) {
530 if (!expirationDateSwitch
.isChecked()) {
531 expirationDateSwitch
.toggle();
533 String formattedDate
=
534 SimpleDateFormat
.getDateInstance().format(
535 new Date(expirationDate
)
537 getExpirationDateValue().setText(formattedDate
);
539 if (expirationDateSwitch
.isChecked()) {
540 expirationDateSwitch
.toggle();
542 getExpirationDateValue().setText(R
.string
.empty
);
545 expirationDateSwitch
.setOnCheckedChangeListener(
546 mOnExpirationDateInteractionListener
549 /// update state of password switch and message depending on password protection
550 Switch passwordSwitch
= getPasswordSwitch();
551 // set null listener before setChecked() to prevent infinite loop of calls
552 passwordSwitch
.setOnCheckedChangeListener(null
);
553 if (mPublicShare
.isPasswordProtected()) {
554 if (!passwordSwitch
.isChecked()) {
555 passwordSwitch
.toggle();
557 getPasswordValue().setVisibility(View
.VISIBLE
);
559 if (passwordSwitch
.isChecked()) {
560 passwordSwitch
.toggle();
562 getPasswordValue().setVisibility(View
.INVISIBLE
);
565 passwordSwitch
.setOnCheckedChangeListener(
566 mOnPasswordInteractionListener
571 /// no public share -> collapse section
572 Switch shareViaLinkSwitch
= getShareViaLinkSwitch();
573 if (shareViaLinkSwitch
.isChecked()) {
574 shareViaLinkSwitch
.setOnCheckedChangeListener(null
);
575 getShareViaLinkSwitch().setChecked(false
);
576 shareViaLinkSwitch
.setOnCheckedChangeListener(
577 mOnShareViaLinkSwitchCheckedChangeListener
580 getExpirationDateSection().setVisibility(View
.GONE
);
581 getPasswordSection().setVisibility(View
.GONE
);
582 getGetLinkButton().setVisibility(View
.GONE
);
587 /// BEWARE: next methods will failed with NullPointerException if called before onCreateView() finishes
589 private Switch
getShareViaLinkSwitch() {
590 return (Switch
) getView().findViewById(R
.id
.shareViaLinkSectionSwitch
);
593 private View
getExpirationDateSection() {
594 return getView().findViewById(R
.id
.shareViaLinkExpirationSection
);
597 private Switch
getExpirationDateSwitch() {
598 return (Switch
) getView().findViewById(R
.id
.shareViaLinkExpirationSwitch
);
601 private TextView
getExpirationDateValue() {
602 return (TextView
) getView().findViewById(R
.id
.shareViaLinkExpirationValue
);
605 private View
getPasswordSection() {
606 return getView().findViewById(R
.id
.shareViaLinkPasswordSection
);
609 private Switch
getPasswordSwitch() {
610 return (Switch
) getView().findViewById(R
.id
.shareViaLinkPasswordSwitch
);
613 private TextView
getPasswordValue() {
614 return (TextView
) getView().findViewById(R
.id
.shareViaLinkPasswordValue
);
617 private AppCompatButton
getGetLinkButton() {
618 return (AppCompatButton
) getView().findViewById(R
.id
.shareViewLinkGetLinkButton
);
621 public static void setListViewHeightBasedOnChildren(ListView listView
) {
622 ListAdapter listAdapter
= listView
.getAdapter();
623 if (listAdapter
== null
) {
626 int desiredWidth
= View
.MeasureSpec
.makeMeasureSpec(listView
.getWidth(), View
.MeasureSpec
.AT_MOST
);
629 for (int i
= 0; i
< listAdapter
.getCount(); i
++) {
630 view
= listAdapter
.getView(i
, view
, listView
);
632 view
.setLayoutParams(new ViewGroup
.LayoutParams(desiredWidth
, ViewGroup
.LayoutParams
.WRAP_CONTENT
));
634 view
.measure(desiredWidth
, View
.MeasureSpec
.UNSPECIFIED
);
635 totalHeight
+= view
.getMeasuredHeight();
637 ViewGroup
.LayoutParams params
= listView
.getLayoutParams();
638 params
.height
= totalHeight
+ (listView
.getDividerHeight() * (listAdapter
.getCount() - 1));
639 listView
.setLayoutParams(params
);
640 listView
.requestLayout();
644 * This interface must be implemented by activities that contain this
645 * fragment to allow an interaction in this fragment to be communicated
646 * to the activity and potentially other fragments contained in that
649 * See the Android Training lesson <a href=
650 * "http://developer.android.com/training/basics/fragments/communicating.html"
651 * >Communicating with Other Fragments</a> for more information.
653 public interface OnShareFragmentInteractionListener
{
654 void showSearchUsersAndGroups();
655 void refreshUsersOrGroupsListFromServer();
656 void unshareWith(OCShare share
);