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
.ShareActivity
;
45 import com
.owncloud
.android
.ui
.adapter
.ShareUserListAdapter
;
46 import com
.owncloud
.android
.utils
.DisplayUtils
;
47 import com
.owncloud
.android
.utils
.MimetypeIconUtil
;
49 import java
.util
.ArrayList
;
52 * Fragment for Sharing a file with users
54 * A simple {@link Fragment} subclass.
55 * Activities that contain this fragment must implement the
56 * {@link ShareFileFragment.OnShareFragmentInteractionListener} interface
57 * to handle interaction events.
58 * Use the {@link ShareFileFragment#newInstance} factory method to
59 * create an instance of this fragment.
61 public class ShareFileFragment
extends Fragment
62 implements ShareUserListAdapter
.ShareUserAdapterListener
{
64 private static final String TAG
= ShareFileFragment
.class.getSimpleName();
66 // the fragment initialization parameters
67 private static final String ARG_FILE
= "FILE";
68 private static final String ARG_ACCOUNT
= "ACCOUNT";
72 private Account mAccount
;
74 private ArrayList
<OCShare
> mShares
;
75 private ShareUserListAdapter mUserGroupsAdapter
= null
;
77 private OnShareFragmentInteractionListener mListener
;
80 * Public factory method to create new ShareFileFragment instances.
82 * @param fileToShare An {@link OCFile} to show in the fragment
83 * @param account An ownCloud account
84 * @return A new instance of fragment ShareFileFragment.
86 public static ShareFileFragment
newInstance(OCFile fileToShare
, Account account
) {
87 ShareFileFragment fragment
= new ShareFileFragment();
88 Bundle args
= new Bundle();
89 args
.putParcelable(ARG_FILE
, fileToShare
);
90 args
.putParcelable(ARG_ACCOUNT
, account
);
91 fragment
.setArguments(args
);
95 public ShareFileFragment() {
96 // Required empty public constructor
100 public void onCreate(Bundle savedInstanceState
) {
101 super.onCreate(savedInstanceState
);
102 if (getArguments() != null
) {
103 mFile
= getArguments().getParcelable(ARG_FILE
);
104 mAccount
= getArguments().getParcelable(ARG_ACCOUNT
);
109 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
110 Bundle savedInstanceState
) {
111 // Inflate the layout for this fragment
112 View view
= inflater
.inflate(R
.layout
.share_file_layout
, container
, false
);
116 ImageView icon
= (ImageView
) view
.findViewById(R
.id
.shareFileIcon
);
117 icon
.setImageResource(MimetypeIconUtil
.getFileTypeIconId(mFile
.getMimetype(),
118 mFile
.getFileName()));
119 if (mFile
.isImage()) {
120 String remoteId
= String
.valueOf(mFile
.getRemoteId());
121 Bitmap thumbnail
= ThumbnailsCacheManager
.getBitmapFromDiskCache(remoteId
);
122 if (thumbnail
!= null
) {
123 icon
.setImageBitmap(thumbnail
);
127 TextView filename
= (TextView
) view
.findViewById(R
.id
.shareFileName
);
128 filename
.setText(mFile
.getFileName());
130 TextView size
= (TextView
) view
.findViewById(R
.id
.shareFileSize
);
131 if (mFile
.isFolder()) {
132 size
.setVisibility(View
.GONE
);
134 size
.setText(DisplayUtils
.bytesToHumanReadable(mFile
.getFileLength()));
138 Button addUserGroupButton
= (Button
)
139 view
.findViewById(R
.id
.addUserButton
);
140 addUserGroupButton
.setOnClickListener(new View
.OnClickListener() {
142 public void onClick(View view
) {
143 boolean shareWithUsersEnable
= AccountUtils
.hasSearchUsersSupport(mAccount
);
144 if (shareWithUsersEnable
) {
145 // Show Search Fragment
146 mListener
.showSearchUsersAndGroups();
148 String message
= getString(R
.string
.share_sharee_unavailable
);
149 Toast
.makeText(getActivity(), message
, Toast
.LENGTH_LONG
).show();
158 public void onActivityCreated(Bundle savedInstanceState
) {
159 super.onActivityCreated(savedInstanceState
);
161 // Load data into the list
162 refreshUsersOrGroupsListFromDB();
164 // Request for a refresh of the data through the server (starts an Async Task)
165 if (mListener
!= null
) {
166 mListener
.refreshUsersOrGroupsListFromServer();
171 public void onAttach(Activity activity
) {
172 super.onAttach(activity
);
174 mListener
= (OnShareFragmentInteractionListener
) activity
;
175 } catch (ClassCastException e
) {
176 throw new ClassCastException(activity
.toString()
177 + " must implement OnShareFragmentInteractionListener");
182 public void onDetach() {
188 * Get users and groups fromn the DB to fill in the "share with" list
190 public void refreshUsersOrGroupsListFromDB (){
191 // Get Users and Groups
192 mShares
= ((ShareActivity
) mListener
).getStorageManager().getSharesWithForAFile(mFile
.getRemotePath(),
195 // Update list of users/groups
196 updateListOfUserGroups();
199 private void updateListOfUserGroups() {
200 // Update list of users/groups
201 mUserGroupsAdapter
= new ShareUserListAdapter(
203 R
.layout
.share_user_item
,
209 TextView noShares
= (TextView
) getView().findViewById(R
.id
.shareNoUsers
);
210 ListView usersList
= (ListView
) getView().findViewById(R
.id
.shareUsersList
);
212 if (mShares
.size() > 0) {
213 noShares
.setVisibility(View
.GONE
);
214 usersList
.setVisibility(View
.VISIBLE
);
215 usersList
.setAdapter(mUserGroupsAdapter
);
218 noShares
.setVisibility(View
.VISIBLE
);
219 usersList
.setVisibility(View
.GONE
);
224 public void unshareButtonPressed(OCShare share
) {
226 mListener
.unshareWith(share
);
227 Log_OC
.d(TAG
, "Unshare - " + share
.getSharedWithDisplayName());
232 * This interface must be implemented by activities that contain this
233 * fragment to allow an interaction in this fragment to be communicated
234 * to the activity and potentially other fragments contained in that
237 * See the Android Training lesson <a href=
238 * "http://developer.android.com/training/basics/fragments/communicating.html"
239 * >Communicating with Other Fragments</a> for more information.
241 public interface OnShareFragmentInteractionListener
{
242 void showSearchUsersAndGroups();
243 void refreshUsersOrGroupsListFromServer();
244 void unshareWith(OCShare share
);