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
.ListView
;
37 import android
.widget
.Switch
;
38 import android
.widget
.TextView
;
39 import android
.widget
.Toast
;
41 import com
.owncloud
.android
.R
;
42 import com
.owncloud
.android
.authentication
.AccountUtils
;
43 import com
.owncloud
.android
.datamodel
.OCFile
;
44 import com
.owncloud
.android
.datamodel
.ThumbnailsCacheManager
;
45 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
46 import com
.owncloud
.android
.lib
.resources
.shares
.OCShare
;
47 import com
.owncloud
.android
.lib
.resources
.shares
.ShareType
;
48 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
49 import com
.owncloud
.android
.ui
.adapter
.ShareUserListAdapter
;
50 import com
.owncloud
.android
.utils
.DisplayUtils
;
51 import com
.owncloud
.android
.utils
.MimetypeIconUtil
;
53 import java
.util
.ArrayList
;
56 * Fragment for Sharing a file with sharees (users or groups) or creating
59 * A simple {@link Fragment} subclass.
61 * Activities that contain this fragment must implement the
62 * {@link ShareFileFragment.OnShareFragmentInteractionListener} interface
63 * to handle interaction events.
65 * Use the {@link ShareFileFragment#newInstance} factory method to
66 * create an instance of this fragment.
68 public class ShareFileFragment
extends Fragment
69 implements ShareUserListAdapter
.ShareUserAdapterListener
{
71 private static final String TAG
= ShareFileFragment
.class.getSimpleName();
73 // the fragment initialization parameters
74 private static final String ARG_FILE
= "FILE";
75 private static final String ARG_ACCOUNT
= "ACCOUNT";
77 /** File to share, received as a parameter in construction time */
80 /** OC account holding the file to share, received as a parameter in construction time */
81 private Account mAccount
;
83 /** Reference to parent listener */
84 private OnShareFragmentInteractionListener mListener
;
86 /** List of private shares bound to the file */
87 private ArrayList
<OCShare
> mPrivateShares
;
89 /** Adapter to show private shares */
90 private ShareUserListAdapter mUserGroupsAdapter
= null
;
92 /** Public share bound to the file */
93 private OCShare mPublicShare
;
95 /** Listener for changes on switch to share / unshare publicly */
96 private CompoundButton
.OnCheckedChangeListener mOnShareViaLinkSwitchCheckedChangeListener
;
98 /** Listener for changes on switch to set / clear password on public link */
99 private CompoundButton
.OnCheckedChangeListener mOnPasswordSwitchCheckedChangeListener
;
103 * Public factory method to create new ShareFileFragment instances.
105 * @param fileToShare An {@link OCFile} to show in the fragment
106 * @param account An ownCloud account
107 * @return A new instance of fragment ShareFileFragment.
109 public static ShareFileFragment
newInstance(OCFile fileToShare
, Account account
) {
110 ShareFileFragment fragment
= new ShareFileFragment();
111 Bundle args
= new Bundle();
112 args
.putParcelable(ARG_FILE
, fileToShare
);
113 args
.putParcelable(ARG_ACCOUNT
, account
);
114 fragment
.setArguments(args
);
118 public ShareFileFragment() {
119 // Required empty public constructor
126 public void onCreate(Bundle savedInstanceState
) {
127 super.onCreate(savedInstanceState
);
128 if (getArguments() != null
) {
129 mFile
= getArguments().getParcelable(ARG_FILE
);
130 mAccount
= getArguments().getParcelable(ARG_ACCOUNT
);
138 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
139 Bundle savedInstanceState
) {
140 // Inflate the layout for this fragment
141 View view
= inflater
.inflate(R
.layout
.share_file_layout
, container
, false
);
145 ImageView icon
= (ImageView
) view
.findViewById(R
.id
.shareFileIcon
);
146 icon
.setImageResource(MimetypeIconUtil
.getFileTypeIconId(mFile
.getMimetype(),
147 mFile
.getFileName()));
148 if (mFile
.isImage()) {
149 String remoteId
= String
.valueOf(mFile
.getRemoteId());
150 Bitmap thumbnail
= ThumbnailsCacheManager
.getBitmapFromDiskCache(remoteId
);
151 if (thumbnail
!= null
) {
152 icon
.setImageBitmap(thumbnail
);
156 TextView filename
= (TextView
) view
.findViewById(R
.id
.shareFileName
);
157 filename
.setText(mFile
.getFileName());
159 TextView size
= (TextView
) view
.findViewById(R
.id
.shareFileSize
);
160 if (mFile
.isFolder()) {
161 size
.setVisibility(View
.GONE
);
163 size
.setText(DisplayUtils
.bytesToHumanReadable(mFile
.getFileLength()));
167 Button addUserGroupButton
= (Button
)
168 view
.findViewById(R
.id
.addUserButton
);
169 addUserGroupButton
.setOnClickListener(new View
.OnClickListener() {
171 public void onClick(View view
) {
172 boolean shareWithUsersEnable
= AccountUtils
.hasSearchUsersSupport(mAccount
);
173 if (shareWithUsersEnable
) {
174 // Show Search Fragment
175 mListener
.showSearchUsersAndGroups();
177 String message
= getString(R
.string
.share_sharee_unavailable
);
178 Toast
.makeText(getActivity(), message
, Toast
.LENGTH_LONG
).show();
183 // Switch to create public share
184 mOnShareViaLinkSwitchCheckedChangeListener
= new CompoundButton
.OnCheckedChangeListener() {
186 public void onCheckedChanged(CompoundButton buttonView
, boolean isChecked
) {
188 // very important, setCheched(...) is called automatically during
189 // Fragment recreation on device rotations
193 ((FileActivity
) getActivity()).getFileOperationsHelper().
194 shareFileViaLink(mFile
);
197 ((FileActivity
) getActivity()).getFileOperationsHelper().
198 unshareFileViaLink(mFile
);
202 Switch shareViaLinkSwitch
= (Switch
) view
.findViewById(R
.id
.shareViaLinkSectionSwitch
);
203 shareViaLinkSwitch
.setOnCheckedChangeListener(mOnShareViaLinkSwitchCheckedChangeListener
);
205 // Switch for expiration date
206 Switch shareViaLinkExpirationSwitch
= (Switch
) view
.findViewById(R
.id
.shareViaLinkExpirationSwitch
);
207 shareViaLinkExpirationSwitch
.setOnCheckedChangeListener(new CompoundButton
.OnCheckedChangeListener() {
209 public void onCheckedChanged(CompoundButton buttonView
, boolean isChecked
) {
211 // very important, setCheched(...) is called automatically during
212 // Fragment recreation on device rotations
216 // TODO real implementation: update share with expiration date
217 // show value of expiration date
218 getExpirationDateValue().setText(R
.string
.placeholder_timestamp
);
221 // TODO real implementation: update share without expiration date
223 getExpirationDateValue().setText(R
.string
.empty
);
228 // Switch for password
229 mOnPasswordSwitchCheckedChangeListener
= new CompoundButton
.OnCheckedChangeListener() {
231 public void onCheckedChanged(CompoundButton buttonView
, boolean isChecked
) {
233 // very important, setCheched(...) is called automatically during
234 // Fragment recreation on device rotations
238 ((FileActivity
) getActivity()).getFileOperationsHelper().
239 requestPasswordForShareViaLink(mFile
);
241 ((FileActivity
) getActivity()).getFileOperationsHelper().
242 setPasswordToShareViaLink(mFile
, ""); // "" clears
246 Switch shareViaLinkPasswordSwitch
= (Switch
) view
.findViewById(R
.id
.shareViaLinkPasswordSwitch
);
247 shareViaLinkPasswordSwitch
.setOnCheckedChangeListener(mOnShareViaLinkSwitchCheckedChangeListener
);
253 public void onActivityCreated(Bundle savedInstanceState
) {
254 super.onActivityCreated(savedInstanceState
);
256 // Load data into the list of private shares
257 refreshUsersOrGroupsListFromDB();
259 // Load data of public share, if exists
260 refreshPublicShareFromDB();
264 public void onAttach(Activity activity
) {
265 super.onAttach(activity
);
267 mListener
= (OnShareFragmentInteractionListener
) activity
;
268 } catch (ClassCastException e
) {
269 throw new ClassCastException(activity
.toString()
270 + " must implement OnShareFragmentInteractionListener");
275 public void onDetach() {
281 * Get users and groups from the DB to fill in the "share with" list
283 * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
284 * instance ready to use. If not ready, does nothing.
286 public void refreshUsersOrGroupsListFromDB (){
287 if (((FileActivity
) mListener
).getStorageManager() != null
) {
288 // Get Users and Groups
289 mPrivateShares
= ((FileActivity
) mListener
).getStorageManager().getSharesWithForAFile(
290 mFile
.getRemotePath(),
294 // Update list of users/groups
295 updateListOfUserGroups();
299 private void updateListOfUserGroups() {
300 // Update list of users/groups
301 // TODO Refactoring: create a new {@link ShareUserListAdapter} instance with every call should not be needed
302 mUserGroupsAdapter
= new ShareUserListAdapter(
304 R
.layout
.share_user_item
,
310 TextView noShares
= (TextView
) getView().findViewById(R
.id
.shareNoUsers
);
311 ListView usersList
= (ListView
) getView().findViewById(R
.id
.shareUsersList
);
313 if (mPrivateShares
.size() > 0) {
314 noShares
.setVisibility(View
.GONE
);
315 usersList
.setVisibility(View
.VISIBLE
);
316 usersList
.setAdapter(mUserGroupsAdapter
);
319 noShares
.setVisibility(View
.VISIBLE
);
320 usersList
.setVisibility(View
.GONE
);
325 public void unshareButtonPressed(OCShare share
) {
327 mListener
.unshareWith(share
);
328 Log_OC
.d(TAG
, "Unshare - " + share
.getSharedWithDisplayName());
334 * Get public link from the DB to fill in the "Share link" section
336 * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
337 * instance ready to use. If not ready, does nothing.
339 public void refreshPublicShareFromDB() {
340 if (((FileActivity
) mListener
).getStorageManager() != null
) {
342 mPublicShare
= ((FileActivity
) mListener
).getStorageManager().getFirstShareByPathAndType(
343 mFile
.getRemotePath(),
344 ShareType
.PUBLIC_LINK
,
348 // Update list of users/groups
349 updatePublicShareSection();
354 * Updates in the UI the section about public share with the information in the current
355 * public share bound to mFile, if any
357 private void updatePublicShareSection() {
358 if (mPublicShare
!= null
&& ShareType
.PUBLIC_LINK
.equals(mPublicShare
.getShareType())) {
359 /// public share bound -> expand section
360 Switch shareViaLinkSwitch
= getShareViaLinkSwitch();
361 if (!shareViaLinkSwitch
.isChecked()) {
362 // set null listener before setChecked() to prevent infinite loop of calls
363 shareViaLinkSwitch
.setOnCheckedChangeListener(null
);
364 shareViaLinkSwitch
.setChecked(true
);
365 shareViaLinkSwitch
.setOnCheckedChangeListener(
366 mOnShareViaLinkSwitchCheckedChangeListener
369 getExpirationDateSection().setVisibility(View
.VISIBLE
);
370 getPasswordSection().setVisibility(View
.VISIBLE
);
371 getGetLinkButton().setVisibility(View
.VISIBLE
);
373 /// update state of password switch and message depending on password protection
374 Switch passwordSwitch
= getPasswordSwitch();
375 // set null listener before setChecked() to prevent infinite loop of calls
376 passwordSwitch
.setOnCheckedChangeListener(null
);
377 if (mPublicShare
.isPasswordProtected()) {
378 if (!passwordSwitch
.isChecked()) {
379 passwordSwitch
.toggle();
381 getPasswordValue().setVisibility(View
.VISIBLE
);
383 if (passwordSwitch
.isChecked()) {
384 passwordSwitch
.toggle();
386 getPasswordValue().setVisibility(View
.INVISIBLE
);
389 passwordSwitch
.setOnCheckedChangeListener(
390 mOnPasswordSwitchCheckedChangeListener
395 /// no public share -> collapse section
396 Switch shareViaLinkSwitch
= getShareViaLinkSwitch();
397 if (shareViaLinkSwitch
.isChecked()) {
398 shareViaLinkSwitch
.setOnCheckedChangeListener(null
);
399 getShareViaLinkSwitch().setChecked(false
);
400 shareViaLinkSwitch
.setOnCheckedChangeListener(
401 mOnShareViaLinkSwitchCheckedChangeListener
404 getExpirationDateSection().setVisibility(View
.GONE
);
405 getPasswordSection().setVisibility(View
.GONE
);
406 getGetLinkButton().setVisibility(View
.GONE
);
410 /// BEWARE: next methods will failed with NullPointerException if called before onCreateView() finishes
412 private Switch
getShareViaLinkSwitch() {
413 return (Switch
) getView().findViewById(R
.id
.shareViaLinkSectionSwitch
);
416 private View
getExpirationDateSection() {
417 return getView().findViewById(R
.id
.shareViaLinkExpirationSection
);
420 private TextView
getExpirationDateValue() {
421 return (TextView
) getView().findViewById(R
.id
.shareViaLinkExpirationValue
);
424 private View
getPasswordSection() {
425 return getView().findViewById(R
.id
.shareViaLinkPasswordSection
);
428 private Switch
getPasswordSwitch() {
429 return (Switch
) getView().findViewById(R
.id
.shareViaLinkPasswordSwitch
);
432 private TextView
getPasswordValue() {
433 return (TextView
) getView().findViewById(R
.id
.shareViaLinkPasswordValue
);
436 private AppCompatButton
getGetLinkButton() {
437 return (AppCompatButton
) getView().findViewById(R
.id
.shareViewLinkGetLinkButton
);
442 * This interface must be implemented by activities that contain this
443 * fragment to allow an interaction in this fragment to be communicated
444 * to the activity and potentially other fragments contained in that
447 * See the Android Training lesson <a href=
448 * "http://developer.android.com/training/basics/fragments/communicating.html"
449 * >Communicating with Other Fragments</a> for more information.
451 public interface OnShareFragmentInteractionListener
{
452 void showSearchUsersAndGroups();
453 void refreshUsersOrGroupsListFromServer();
454 void unshareWith(OCShare share
);