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
.view
.LayoutInflater
;
30 import android
.view
.View
;
31 import android
.view
.ViewGroup
;
32 import android
.widget
.Button
;
33 import android
.widget
.ImageView
;
34 import android
.widget
.ListView
;
35 import android
.widget
.TextView
;
36 import android
.widget
.Toast
;
38 import com
.owncloud
.android
.R
;
39 import com
.owncloud
.android
.authentication
.AccountUtils
;
40 import com
.owncloud
.android
.datamodel
.OCFile
;
41 import com
.owncloud
.android
.datamodel
.ThumbnailsCacheManager
;
42 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
43 import com
.owncloud
.android
.lib
.resources
.shares
.OCShare
;
44 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
45 import com
.owncloud
.android
.ui
.activity
.ShareActivity
;
46 import com
.owncloud
.android
.ui
.adapter
.ShareUserListAdapter
;
47 import com
.owncloud
.android
.utils
.DisplayUtils
;
48 import com
.owncloud
.android
.utils
.MimetypeIconUtil
;
50 import java
.util
.ArrayList
;
53 * Fragment for Sharing a file with sharees (users or groups)
55 * A simple {@link Fragment} subclass.
57 * Activities that contain this fragment must implement the
58 * {@link ShareFileFragment.OnShareFragmentInteractionListener} interface
59 * to handle interaction events.
61 * Use the {@link ShareFileFragment#newInstance} factory method to
62 * create an instance of this fragment.
64 public class ShareFileFragment
extends Fragment
65 implements ShareUserListAdapter
.ShareUserAdapterListener
{
67 private static final String TAG
= ShareFileFragment
.class.getSimpleName();
69 // the fragment initialization parameters
70 private static final String ARG_FILE
= "FILE";
71 private static final String ARG_ACCOUNT
= "ACCOUNT";
75 private Account mAccount
;
78 private ArrayList
<OCShare
> mShares
;
79 private ShareUserListAdapter mUserGroupsAdapter
= null
;
80 private OnShareFragmentInteractionListener mListener
;
83 * Public factory method to create new ShareFileFragment instances.
85 * @param fileToShare An {@link OCFile} to show in the fragment
86 * @param account An ownCloud account
87 * @return A new instance of fragment ShareFileFragment.
89 public static ShareFileFragment
newInstance(OCFile fileToShare
, Account account
) {
90 ShareFileFragment fragment
= new ShareFileFragment();
91 Bundle args
= new Bundle();
92 args
.putParcelable(ARG_FILE
, fileToShare
);
93 args
.putParcelable(ARG_ACCOUNT
, account
);
94 fragment
.setArguments(args
);
98 public ShareFileFragment() {
99 // Required empty public constructor
106 public void onCreate(Bundle savedInstanceState
) {
107 super.onCreate(savedInstanceState
);
108 if (getArguments() != null
) {
109 mFile
= getArguments().getParcelable(ARG_FILE
);
110 mAccount
= getArguments().getParcelable(ARG_ACCOUNT
);
118 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
119 Bundle savedInstanceState
) {
120 // Inflate the layout for this fragment
121 View view
= inflater
.inflate(R
.layout
.share_file_layout
, container
, false
);
125 ImageView icon
= (ImageView
) view
.findViewById(R
.id
.shareFileIcon
);
126 icon
.setImageResource(MimetypeIconUtil
.getFileTypeIconId(mFile
.getMimetype(),
127 mFile
.getFileName()));
128 if (mFile
.isImage()) {
129 String remoteId
= String
.valueOf(mFile
.getRemoteId());
130 Bitmap thumbnail
= ThumbnailsCacheManager
.getBitmapFromDiskCache(remoteId
);
131 if (thumbnail
!= null
) {
132 icon
.setImageBitmap(thumbnail
);
136 TextView filename
= (TextView
) view
.findViewById(R
.id
.shareFileName
);
137 filename
.setText(mFile
.getFileName());
139 TextView size
= (TextView
) view
.findViewById(R
.id
.shareFileSize
);
140 if (mFile
.isFolder()) {
141 size
.setVisibility(View
.GONE
);
143 size
.setText(DisplayUtils
.bytesToHumanReadable(mFile
.getFileLength()));
147 Button addUserGroupButton
= (Button
)
148 view
.findViewById(R
.id
.addUserButton
);
149 addUserGroupButton
.setOnClickListener(new View
.OnClickListener() {
151 public void onClick(View view
) {
152 boolean shareWithUsersEnable
= AccountUtils
.hasSearchUsersSupport(mAccount
);
153 if (shareWithUsersEnable
) {
154 // Show Search Fragment
155 mListener
.showSearchUsersAndGroups();
157 String message
= getString(R
.string
.share_sharee_unavailable
);
158 Toast
.makeText(getActivity(), message
, Toast
.LENGTH_LONG
).show();
167 public void onActivityCreated(Bundle savedInstanceState
) {
168 super.onActivityCreated(savedInstanceState
);
170 // Load data into the list
171 refreshUsersOrGroupsListFromDB();
175 public void onAttach(Activity activity
) {
176 super.onAttach(activity
);
178 mListener
= (OnShareFragmentInteractionListener
) activity
;
179 } catch (ClassCastException e
) {
180 throw new ClassCastException(activity
.toString()
181 + " must implement OnShareFragmentInteractionListener");
186 public void onDetach() {
192 * Get users and groups from the DB to fill in the "share with" list
194 * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
195 * instance ready to use. If not ready, does nothing.
197 public void refreshUsersOrGroupsListFromDB (){
198 if (((FileActivity
) mListener
).getStorageManager() != null
) {
199 // Get Users and Groups
200 mShares
= ((FileActivity
) mListener
).getStorageManager().getSharesWithForAFile(
201 mFile
.getRemotePath(),
205 // Update list of users/groups
206 updateListOfUserGroups();
210 private void updateListOfUserGroups() {
211 // Update list of users/groups
212 // TODO Refactoring: create a new {@link ShareUserListAdapter} instance with every call should not be needed
213 mUserGroupsAdapter
= new ShareUserListAdapter(
215 R
.layout
.share_user_item
,
221 TextView noShares
= (TextView
) getView().findViewById(R
.id
.shareNoUsers
);
222 ListView usersList
= (ListView
) getView().findViewById(R
.id
.shareUsersList
);
224 if (mShares
.size() > 0) {
225 noShares
.setVisibility(View
.GONE
);
226 usersList
.setVisibility(View
.VISIBLE
);
227 usersList
.setAdapter(mUserGroupsAdapter
);
230 noShares
.setVisibility(View
.VISIBLE
);
231 usersList
.setVisibility(View
.GONE
);
236 public void unshareButtonPressed(OCShare share
) {
238 mListener
.unshareWith(share
);
239 Log_OC
.d(TAG
, "Unshare - " + share
.getSharedWithDisplayName());
244 * This interface must be implemented by activities that contain this
245 * fragment to allow an interaction in this fragment to be communicated
246 * to the activity and potentially other fragments contained in that
249 * See the Android Training lesson <a href=
250 * "http://developer.android.com/training/basics/fragments/communicating.html"
251 * >Communicating with Other Fragments</a> for more information.
253 public interface OnShareFragmentInteractionListener
{
254 void showSearchUsersAndGroups();
255 void refreshUsersOrGroupsListFromServer();
256 void unshareWith(OCShare share
);