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
;
100 * Public factory method to create new ShareFileFragment instances.
102 * @param fileToShare An {@link OCFile} to show in the fragment
103 * @param account An ownCloud account
104 * @return A new instance of fragment ShareFileFragment.
106 public static ShareFileFragment
newInstance(OCFile fileToShare
, Account account
) {
107 ShareFileFragment fragment
= new ShareFileFragment();
108 Bundle args
= new Bundle();
109 args
.putParcelable(ARG_FILE
, fileToShare
);
110 args
.putParcelable(ARG_ACCOUNT
, account
);
111 fragment
.setArguments(args
);
115 public ShareFileFragment() {
116 // Required empty public constructor
123 public void onCreate(Bundle savedInstanceState
) {
124 super.onCreate(savedInstanceState
);
125 if (getArguments() != null
) {
126 mFile
= getArguments().getParcelable(ARG_FILE
);
127 mAccount
= getArguments().getParcelable(ARG_ACCOUNT
);
135 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
136 Bundle savedInstanceState
) {
137 // Inflate the layout for this fragment
138 View view
= inflater
.inflate(R
.layout
.share_file_layout
, container
, false
);
142 ImageView icon
= (ImageView
) view
.findViewById(R
.id
.shareFileIcon
);
143 icon
.setImageResource(MimetypeIconUtil
.getFileTypeIconId(mFile
.getMimetype(),
144 mFile
.getFileName()));
145 if (mFile
.isImage()) {
146 String remoteId
= String
.valueOf(mFile
.getRemoteId());
147 Bitmap thumbnail
= ThumbnailsCacheManager
.getBitmapFromDiskCache(remoteId
);
148 if (thumbnail
!= null
) {
149 icon
.setImageBitmap(thumbnail
);
153 TextView filename
= (TextView
) view
.findViewById(R
.id
.shareFileName
);
154 filename
.setText(mFile
.getFileName());
156 TextView size
= (TextView
) view
.findViewById(R
.id
.shareFileSize
);
157 if (mFile
.isFolder()) {
158 size
.setVisibility(View
.GONE
);
160 size
.setText(DisplayUtils
.bytesToHumanReadable(mFile
.getFileLength()));
164 Button addUserGroupButton
= (Button
)
165 view
.findViewById(R
.id
.addUserButton
);
166 addUserGroupButton
.setOnClickListener(new View
.OnClickListener() {
168 public void onClick(View view
) {
169 boolean shareWithUsersEnable
= AccountUtils
.hasSearchUsersSupport(mAccount
);
170 if (shareWithUsersEnable
) {
171 // Show Search Fragment
172 mListener
.showSearchUsersAndGroups();
174 String message
= getString(R
.string
.share_sharee_unavailable
);
175 Toast
.makeText(getActivity(), message
, Toast
.LENGTH_LONG
).show();
180 // Switch to create public share
181 mOnShareViaLinkSwitchCheckedChangeListener
= new CompoundButton
.OnCheckedChangeListener() {
183 public void onCheckedChanged(CompoundButton buttonView
, boolean isChecked
) {
186 ((FileActivity
) getActivity()).getFileOperationsHelper().shareFileViaLink(mFile
);
189 ((FileActivity
) getActivity()).getFileOperationsHelper().unshareFileViaLink(mFile
);
191 } // else, nothing; very important, setCheched(...) is called automatically during Fragment
192 // recreation on device rotations
195 Switch shareViaLinkSwitch
= (Switch
) view
.findViewById(R
.id
.shareViaLinkSectionSwitch
);
196 shareViaLinkSwitch
.setOnCheckedChangeListener(mOnShareViaLinkSwitchCheckedChangeListener
);
198 // Switch for expiration date
199 Switch shareViaLinkExpirationSwitch
= (Switch
) view
.findViewById(R
.id
.shareViaLinkExpirationSwitch
);
200 shareViaLinkExpirationSwitch
.setOnCheckedChangeListener(new CompoundButton
.OnCheckedChangeListener() {
202 public void onCheckedChanged(CompoundButton buttonView
, boolean isChecked
) {
204 // TODO real implementation: update share with expiration date
205 // show value of expiration date
206 getExpirationDateValue().setText(R
.string
.placeholder_timestamp
);
209 // TODO real implementation: update share without expiration date
211 getExpirationDateValue().setText(R
.string
.empty
);
216 // Switch for password
217 Switch shareViaLinkPasswordSwitch
= (Switch
) view
.findViewById(R
.id
.shareViaLinkPasswordSwitch
);
218 shareViaLinkPasswordSwitch
.setOnCheckedChangeListener(new CompoundButton
.OnCheckedChangeListener() {
220 public void onCheckedChanged(CompoundButton buttonView
, boolean isChecked
) {
222 // TODO real implementation: update share with password
224 getExpirationPasswordValue().setVisibility(View
.VISIBLE
);
227 // TODO real implementation: update share without password
229 getExpirationPasswordValue().setVisibility(View
.INVISIBLE
);
239 public void onActivityCreated(Bundle savedInstanceState
) {
240 super.onActivityCreated(savedInstanceState
);
242 // Load data into the list of private shares
243 refreshUsersOrGroupsListFromDB();
245 // Load data of public share, if exists
246 refreshPublicShareFromDB();
250 public void onAttach(Activity activity
) {
251 super.onAttach(activity
);
253 mListener
= (OnShareFragmentInteractionListener
) activity
;
254 } catch (ClassCastException e
) {
255 throw new ClassCastException(activity
.toString()
256 + " must implement OnShareFragmentInteractionListener");
261 public void onDetach() {
267 * Get users and groups from the DB to fill in the "share with" list
269 * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
270 * instance ready to use. If not ready, does nothing.
272 public void refreshUsersOrGroupsListFromDB (){
273 if (((FileActivity
) mListener
).getStorageManager() != null
) {
274 // Get Users and Groups
275 mPrivateShares
= ((FileActivity
) mListener
).getStorageManager().getSharesWithForAFile(
276 mFile
.getRemotePath(),
280 // Update list of users/groups
281 updateListOfUserGroups();
285 private void updateListOfUserGroups() {
286 // Update list of users/groups
287 // TODO Refactoring: create a new {@link ShareUserListAdapter} instance with every call should not be needed
288 mUserGroupsAdapter
= new ShareUserListAdapter(
290 R
.layout
.share_user_item
,
296 TextView noShares
= (TextView
) getView().findViewById(R
.id
.shareNoUsers
);
297 ListView usersList
= (ListView
) getView().findViewById(R
.id
.shareUsersList
);
299 if (mPrivateShares
.size() > 0) {
300 noShares
.setVisibility(View
.GONE
);
301 usersList
.setVisibility(View
.VISIBLE
);
302 usersList
.setAdapter(mUserGroupsAdapter
);
305 noShares
.setVisibility(View
.VISIBLE
);
306 usersList
.setVisibility(View
.GONE
);
311 public void unshareButtonPressed(OCShare share
) {
313 mListener
.unshareWith(share
);
314 Log_OC
.d(TAG
, "Unshare - " + share
.getSharedWithDisplayName());
320 * Get public link from the DB to fill in the "Share link" section
322 * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
323 * instance ready to use. If not ready, does nothing.
325 public void refreshPublicShareFromDB() {
326 if (((FileActivity
) mListener
).getStorageManager() != null
) {
328 mPublicShare
= ((FileActivity
) mListener
).getStorageManager().getFirstShareByPathAndType(
329 mFile
.getRemotePath(),
330 ShareType
.PUBLIC_LINK
,
334 // Update list of users/groups
335 updatePublicShareSection();
340 * Updates in the UI the section about public share with the information in the current
341 * public share bound to mFile, if any
343 private void updatePublicShareSection() {
344 if (mPublicShare
!= null
&& ShareType
.PUBLIC_LINK
.equals(mPublicShare
.getShareType())) {
345 // public share bound -> expand section
346 Switch shareViaLinkSwitch
= getShareViaLinkSwitch();
347 if (!shareViaLinkSwitch
.isChecked()) {
348 // set null listener before setChecked() to prevent infinite loop of calls
349 shareViaLinkSwitch
.setOnCheckedChangeListener(null
);
350 getShareViaLinkSwitch().setChecked(true
);
351 shareViaLinkSwitch
.setOnCheckedChangeListener(
352 mOnShareViaLinkSwitchCheckedChangeListener
355 getExpirationDateSection().setVisibility(View
.VISIBLE
);
356 getPasswordSection().setVisibility(View
.VISIBLE
);
357 getGetLinkButton().setVisibility(View
.VISIBLE
);
360 // no public share -> collapse section
361 Switch shareViaLinkSwitch
= getShareViaLinkSwitch();
362 if (shareViaLinkSwitch
.isChecked()) {
363 shareViaLinkSwitch
.setOnCheckedChangeListener(null
);
364 getShareViaLinkSwitch().setChecked(false
);
365 shareViaLinkSwitch
.setOnCheckedChangeListener(
366 mOnShareViaLinkSwitchCheckedChangeListener
369 getExpirationDateSection().setVisibility(View
.GONE
);
370 getPasswordSection().setVisibility(View
.GONE
);
371 getGetLinkButton().setVisibility(View
.GONE
);
375 private Switch
getShareViaLinkSwitch() {
376 return (Switch
) getView().findViewById(R
.id
.shareViaLinkSectionSwitch
);
379 private View
getExpirationDateSection() {
380 return getView().findViewById(R
.id
.shareViaLinkExpirationSection
);
383 private TextView
getExpirationDateValue() {
384 return (TextView
) getView().findViewById(R
.id
.shareViaLinkExpirationValue
);
387 private View
getPasswordSection() {
388 return getView().findViewById(R
.id
.shareViaLinkPasswordSection
);
391 private TextView
getExpirationPasswordValue() {
392 return (TextView
) getView().findViewById(R
.id
.shareViaLinkPasswordValue
);
395 private AppCompatButton
getGetLinkButton() {
396 return (AppCompatButton
) getView().findViewById(R
.id
.shareViewLinkGetLinkButton
);
401 * This interface must be implemented by activities that contain this
402 * fragment to allow an interaction in this fragment to be communicated
403 * to the activity and potentially other fragments contained in that
406 * See the Android Training lesson <a href=
407 * "http://developer.android.com/training/basics/fragments/communicating.html"
408 * >Communicating with Other Fragments</a> for more information.
410 public interface OnShareFragmentInteractionListener
{
411 void showSearchUsersAndGroups();
412 void refreshUsersOrGroupsListFromServer();
413 void unshareWith(OCShare share
);