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
.DialogInterface
;
27 import android
.graphics
.Bitmap
;
28 import android
.os
.Bundle
;
29 import android
.support
.v4
.app
.Fragment
;
30 import android
.support
.v7
.widget
.AppCompatButton
;
31 import android
.view
.LayoutInflater
;
32 import android
.view
.View
;
33 import android
.view
.ViewGroup
;
34 import android
.widget
.Button
;
35 import android
.widget
.CompoundButton
;
36 import android
.widget
.ImageView
;
37 import android
.widget
.ListView
;
38 import android
.widget
.Switch
;
39 import android
.widget
.TextView
;
40 import android
.widget
.Toast
;
42 import com
.owncloud
.android
.R
;
43 import com
.owncloud
.android
.authentication
.AccountUtils
;
44 import com
.owncloud
.android
.datamodel
.OCFile
;
45 import com
.owncloud
.android
.datamodel
.ThumbnailsCacheManager
;
46 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
47 import com
.owncloud
.android
.lib
.resources
.shares
.OCShare
;
48 import com
.owncloud
.android
.lib
.resources
.shares
.ShareType
;
49 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
50 import com
.owncloud
.android
.ui
.adapter
.ShareUserListAdapter
;
51 import com
.owncloud
.android
.ui
.dialog
.ExpirationDatePickerDialogFragment
;
52 import com
.owncloud
.android
.utils
.DisplayUtils
;
53 import com
.owncloud
.android
.utils
.MimetypeIconUtil
;
55 import java
.text
.SimpleDateFormat
;
56 import java
.util
.ArrayList
;
57 import java
.util
.Date
;
60 * Fragment for Sharing a file with sharees (users or groups) or creating
63 * A simple {@link Fragment} subclass.
65 * Activities that contain this fragment must implement the
66 * {@link ShareFileFragment.OnShareFragmentInteractionListener} interface
67 * to handle interaction events.
69 * Use the {@link ShareFileFragment#newInstance} factory method to
70 * create an instance of this fragment.
72 public class ShareFileFragment
extends Fragment
73 implements ShareUserListAdapter
.ShareUserAdapterListener
{
75 private static final String TAG
= ShareFileFragment
.class.getSimpleName();
77 // the fragment initialization parameters
78 private static final String ARG_FILE
= "FILE";
79 private static final String ARG_ACCOUNT
= "ACCOUNT";
81 /** File to share, received as a parameter in construction time */
84 /** OC account holding the file to share, received as a parameter in construction time */
85 private Account mAccount
;
87 /** Reference to parent listener */
88 private OnShareFragmentInteractionListener mListener
;
90 /** List of private shares bound to the file */
91 private ArrayList
<OCShare
> mPrivateShares
;
93 /** Adapter to show private shares */
94 private ShareUserListAdapter mUserGroupsAdapter
= null
;
96 /** Public share bound to the file */
97 private OCShare mPublicShare
;
99 /** Listener for changes on switch to share / unshare publicly */
100 private CompoundButton
.OnCheckedChangeListener mOnShareViaLinkSwitchCheckedChangeListener
;
103 * Listener for user actions to set, update or clear password on public link
105 //private CompoundButton.OnCheckedChangeListener mOnPasswordSwitchCheckedChangeListener;
106 private OnPasswordInteractionListener mOnPasswordInteractionListener
;
109 * Listener for changes on switch to set / clear expiration date on public link
111 private CompoundButton
.OnCheckedChangeListener mOnExpirationDateSwitchCheckedChangeListener
;
115 * Public factory method to create new ShareFileFragment instances.
117 * @param fileToShare An {@link OCFile} to show in the fragment
118 * @param account An ownCloud account
119 * @return A new instance of fragment ShareFileFragment.
121 public static ShareFileFragment
newInstance(OCFile fileToShare
, Account account
) {
122 ShareFileFragment fragment
= new ShareFileFragment();
123 Bundle args
= new Bundle();
124 args
.putParcelable(ARG_FILE
, fileToShare
);
125 args
.putParcelable(ARG_ACCOUNT
, account
);
126 fragment
.setArguments(args
);
130 public ShareFileFragment() {
131 // Required empty public constructor
138 public void onCreate(Bundle savedInstanceState
) {
139 super.onCreate(savedInstanceState
);
140 if (getArguments() != null
) {
141 mFile
= getArguments().getParcelable(ARG_FILE
);
142 mAccount
= getArguments().getParcelable(ARG_ACCOUNT
);
151 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
152 Bundle savedInstanceState
) {
153 // Inflate the layout for this fragment
154 View view
= inflater
.inflate(R
.layout
.share_file_layout
, container
, false
);
158 ImageView icon
= (ImageView
) view
.findViewById(R
.id
.shareFileIcon
);
159 icon
.setImageResource(MimetypeIconUtil
.getFileTypeIconId(mFile
.getMimetype(),
160 mFile
.getFileName()));
161 if (mFile
.isImage()) {
162 String remoteId
= String
.valueOf(mFile
.getRemoteId());
163 Bitmap thumbnail
= ThumbnailsCacheManager
.getBitmapFromDiskCache(remoteId
);
164 if (thumbnail
!= null
) {
165 icon
.setImageBitmap(thumbnail
);
169 TextView filename
= (TextView
) view
.findViewById(R
.id
.shareFileName
);
170 filename
.setText(mFile
.getFileName());
172 TextView size
= (TextView
) view
.findViewById(R
.id
.shareFileSize
);
173 if (mFile
.isFolder()) {
174 size
.setVisibility(View
.GONE
);
176 size
.setText(DisplayUtils
.bytesToHumanReadable(mFile
.getFileLength()));
180 Button addUserGroupButton
= (Button
)
181 view
.findViewById(R
.id
.addUserButton
);
182 addUserGroupButton
.setOnClickListener(new View
.OnClickListener() {
184 public void onClick(View view
) {
185 boolean shareWithUsersEnable
= AccountUtils
.hasSearchUsersSupport(mAccount
);
186 if (shareWithUsersEnable
) {
187 // Show Search Fragment
188 mListener
.showSearchUsersAndGroups();
190 String message
= getString(R
.string
.share_sharee_unavailable
);
191 Toast
.makeText(getActivity(), message
, Toast
.LENGTH_LONG
).show();
196 // Switch to create public share
197 mOnShareViaLinkSwitchCheckedChangeListener
= new CompoundButton
.OnCheckedChangeListener() {
199 public void onCheckedChanged(CompoundButton buttonView
, boolean isChecked
) {
201 // very important, setCheched(...) is called automatically during
202 // Fragment recreation on device rotations
206 ((FileActivity
) getActivity()).getFileOperationsHelper().
207 shareFileViaLink(mFile
);
210 ((FileActivity
) getActivity()).getFileOperationsHelper().
211 unshareFileViaLink(mFile
);
215 Switch shareViaLinkSwitch
= (Switch
) view
.findViewById(R
.id
.shareViaLinkSectionSwitch
);
216 shareViaLinkSwitch
.setOnCheckedChangeListener(mOnShareViaLinkSwitchCheckedChangeListener
);
218 // Switch for expiration date
219 mOnExpirationDateSwitchCheckedChangeListener
= new CompoundButton
.OnCheckedChangeListener() {
221 public void onCheckedChanged(CompoundButton buttonView
, boolean isChecked
) {
223 // very important, setCheched(...) is called automatically during
224 // Fragment recreation on device rotations
228 ExpirationDatePickerDialogFragment dialog
=
229 ExpirationDatePickerDialogFragment
.newInstance(mFile
);
231 getActivity().getSupportFragmentManager(),
232 ExpirationDatePickerDialogFragment
.DATE_PICKER_DIALOG
236 ((FileActivity
) getActivity()).getFileOperationsHelper().
237 setExpirationDateToShareViaLink(mFile
, -1, -1, -1);
240 // undo the toggle to grant the view will be correct if the dialog is cancelled
241 buttonView
.setOnCheckedChangeListener(null
);
243 buttonView
.setOnCheckedChangeListener(mOnExpirationDateSwitchCheckedChangeListener
);
246 Switch shareViaLinkExpirationSwitch
= (Switch
) view
.findViewById(R
.id
.shareViaLinkExpirationSwitch
);
247 shareViaLinkExpirationSwitch
.setOnCheckedChangeListener(mOnExpirationDateSwitchCheckedChangeListener
);
249 // Set listener for user actions on password
250 initPasswordListener(view
);
256 * Binds listener for user actions that start any update on a password for the public link
257 * to the views receiving the user events.
259 * @param shareView Root view in the fragment.
261 private void initPasswordListener(View shareView
) {
262 mOnPasswordInteractionListener
= new OnPasswordInteractionListener();
263 Switch shareViaLinkPasswordSwitch
= (Switch
) shareView
.findViewById(R
.id
.shareViaLinkPasswordSwitch
);
264 shareViaLinkPasswordSwitch
.setOnCheckedChangeListener(mOnPasswordInteractionListener
);
265 TextView shareViaLinkPasswordLabel
= (TextView
) shareView
.findViewById(R
.id
.shareViaLinkPasswordLabel
);
266 shareViaLinkPasswordLabel
.setOnClickListener(mOnPasswordInteractionListener
);
267 TextView shareViaLinkPasswordValue
= (TextView
) shareView
.findViewById(R
.id
.shareViaLinkPasswordValue
);
268 shareViaLinkPasswordValue
.setOnClickListener(mOnPasswordInteractionListener
);
273 * Listener for user actions that start any update on a password for the public link.
275 private class OnPasswordInteractionListener
276 implements CompoundButton
.OnCheckedChangeListener
, View
.OnClickListener
{
279 public void onCheckedChanged(CompoundButton buttonView
, boolean isChecked
) {
281 // very important, setCheched(...) is called automatically during
282 // Fragment recreation on device rotations
286 ((FileActivity
) getActivity()).getFileOperationsHelper().
287 requestPasswordForShareViaLink(mFile
);
289 ((FileActivity
) getActivity()).getFileOperationsHelper().
290 setPasswordToShareViaLink(mFile
, ""); // "" clears
293 // undo the toggle to grant the view will be correct if the dialog is cancelled
294 buttonView
.setOnCheckedChangeListener(null
);
296 buttonView
.setOnCheckedChangeListener(mOnPasswordInteractionListener
);
300 public void onClick(View v
) {
301 if (mPublicShare
!= null
&& mPublicShare
.isPasswordProtected()) {
302 ((FileActivity
) getActivity()).getFileOperationsHelper().
303 requestPasswordForShareViaLink(mFile
);
310 public void onActivityCreated(Bundle savedInstanceState
) {
311 super.onActivityCreated(savedInstanceState
);
313 // Load data into the list of private shares
314 refreshUsersOrGroupsListFromDB();
316 // Load data of public share, if exists
317 refreshPublicShareFromDB();
321 public void onAttach(Activity activity
) {
322 super.onAttach(activity
);
324 mListener
= (OnShareFragmentInteractionListener
) activity
;
325 } catch (ClassCastException e
) {
326 throw new ClassCastException(activity
.toString()
327 + " must implement OnShareFragmentInteractionListener");
332 public void onDetach() {
338 * Get users and groups from the DB to fill in the "share with" list.
340 * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
341 * instance ready to use. If not ready, does nothing.
343 public void refreshUsersOrGroupsListFromDB (){
344 if (((FileActivity
) mListener
).getStorageManager() != null
) {
345 // Get Users and Groups
346 mPrivateShares
= ((FileActivity
) mListener
).getStorageManager().getSharesWithForAFile(
347 mFile
.getRemotePath(),
351 // Update list of users/groups
352 updateListOfUserGroups();
356 private void updateListOfUserGroups() {
357 // Update list of users/groups
358 // TODO Refactoring: create a new {@link ShareUserListAdapter} instance with every call should not be needed
359 mUserGroupsAdapter
= new ShareUserListAdapter(
361 R
.layout
.share_user_item
,
367 TextView noShares
= (TextView
) getView().findViewById(R
.id
.shareNoUsers
);
368 ListView usersList
= (ListView
) getView().findViewById(R
.id
.shareUsersList
);
370 if (mPrivateShares
.size() > 0) {
371 noShares
.setVisibility(View
.GONE
);
372 usersList
.setVisibility(View
.VISIBLE
);
373 usersList
.setAdapter(mUserGroupsAdapter
);
376 noShares
.setVisibility(View
.VISIBLE
);
377 usersList
.setVisibility(View
.GONE
);
382 public void unshareButtonPressed(OCShare share
) {
384 mListener
.unshareWith(share
);
385 Log_OC
.d(TAG
, "Unshare - " + share
.getSharedWithDisplayName());
391 * Get public link from the DB to fill in the "Share link" section in the UI.
393 * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
394 * instance ready to use. If not ready, does nothing.
396 public void refreshPublicShareFromDB() {
397 if (((FileActivity
) mListener
).getStorageManager() != null
) {
399 mPublicShare
= ((FileActivity
) mListener
).getStorageManager().getFirstShareByPathAndType(
400 mFile
.getRemotePath(),
401 ShareType
.PUBLIC_LINK
,
405 // Update public share section
406 updatePublicShareSection();
411 * Updates in the UI the section about public share with the information in the current
412 * public share bound to mFile, if any
414 private void updatePublicShareSection() {
415 if (mPublicShare
!= null
&& ShareType
.PUBLIC_LINK
.equals(mPublicShare
.getShareType())) {
416 /// public share bound -> expand section
417 Switch shareViaLinkSwitch
= getShareViaLinkSwitch();
418 if (!shareViaLinkSwitch
.isChecked()) {
419 // set null listener before setChecked() to prevent infinite loop of calls
420 shareViaLinkSwitch
.setOnCheckedChangeListener(null
);
421 shareViaLinkSwitch
.setChecked(true
);
422 shareViaLinkSwitch
.setOnCheckedChangeListener(
423 mOnShareViaLinkSwitchCheckedChangeListener
426 getExpirationDateSection().setVisibility(View
.VISIBLE
);
427 getPasswordSection().setVisibility(View
.VISIBLE
);
428 getGetLinkButton().setVisibility(View
.VISIBLE
);
430 /// update state of expiration date switch and message depending on expiration date
431 /// update state of expiration date switch and message depending on expiration date
432 Switch expirationDateSwitch
= getExpirationDateSwitch();
433 // set null listener before setChecked() to prevent infinite loop of calls
434 expirationDateSwitch
.setOnCheckedChangeListener(null
);
435 long expirationDate
= mPublicShare
.getExpirationDate();
436 if (expirationDate
> 0) {
437 if (!expirationDateSwitch
.isChecked()) {
438 expirationDateSwitch
.toggle();
440 String formattedDate
=
441 SimpleDateFormat
.getDateInstance().format(
442 new Date(expirationDate
)
444 getExpirationDateValue().setText(formattedDate
);
446 if (expirationDateSwitch
.isChecked()) {
447 expirationDateSwitch
.toggle();
449 getExpirationDateValue().setText(R
.string
.empty
);
452 expirationDateSwitch
.setOnCheckedChangeListener(
453 mOnExpirationDateSwitchCheckedChangeListener
456 /// update state of password switch and message depending on password protection
457 Switch passwordSwitch
= getPasswordSwitch();
458 // set null listener before setChecked() to prevent infinite loop of calls
459 passwordSwitch
.setOnCheckedChangeListener(null
);
460 if (mPublicShare
.isPasswordProtected()) {
461 if (!passwordSwitch
.isChecked()) {
462 passwordSwitch
.toggle();
464 getPasswordValue().setVisibility(View
.VISIBLE
);
466 if (passwordSwitch
.isChecked()) {
467 passwordSwitch
.toggle();
469 getPasswordValue().setVisibility(View
.INVISIBLE
);
472 passwordSwitch
.setOnCheckedChangeListener(
473 mOnPasswordInteractionListener
478 /// no public share -> collapse section
479 Switch shareViaLinkSwitch
= getShareViaLinkSwitch();
480 if (shareViaLinkSwitch
.isChecked()) {
481 shareViaLinkSwitch
.setOnCheckedChangeListener(null
);
482 getShareViaLinkSwitch().setChecked(false
);
483 shareViaLinkSwitch
.setOnCheckedChangeListener(
484 mOnShareViaLinkSwitchCheckedChangeListener
487 getExpirationDateSection().setVisibility(View
.GONE
);
488 getPasswordSection().setVisibility(View
.GONE
);
489 getGetLinkButton().setVisibility(View
.GONE
);
494 /// BEWARE: next methods will failed with NullPointerException if called before onCreateView() finishes
496 private Switch
getShareViaLinkSwitch() {
497 return (Switch
) getView().findViewById(R
.id
.shareViaLinkSectionSwitch
);
500 private View
getExpirationDateSection() {
501 return getView().findViewById(R
.id
.shareViaLinkExpirationSection
);
504 private Switch
getExpirationDateSwitch() {
505 return (Switch
) getView().findViewById(R
.id
.shareViaLinkExpirationSwitch
);
508 private TextView
getExpirationDateValue() {
509 return (TextView
) getView().findViewById(R
.id
.shareViaLinkExpirationValue
);
512 private View
getPasswordSection() {
513 return getView().findViewById(R
.id
.shareViaLinkPasswordSection
);
516 private Switch
getPasswordSwitch() {
517 return (Switch
) getView().findViewById(R
.id
.shareViaLinkPasswordSwitch
);
520 private TextView
getPasswordValue() {
521 return (TextView
) getView().findViewById(R
.id
.shareViaLinkPasswordValue
);
524 private AppCompatButton
getGetLinkButton() {
525 return (AppCompatButton
) getView().findViewById(R
.id
.shareViewLinkGetLinkButton
);
530 * This interface must be implemented by activities that contain this
531 * fragment to allow an interaction in this fragment to be communicated
532 * to the activity and potentially other fragments contained in that
535 * See the Android Training lesson <a href=
536 * "http://developer.android.com/training/basics/fragments/communicating.html"
537 * >Communicating with Other Fragments</a> for more information.
539 public interface OnShareFragmentInteractionListener
{
540 void showSearchUsersAndGroups();
541 void refreshUsersOrGroupsListFromServer();
542 void unshareWith(OCShare share
);