2 * ownCloud Android client application
5 * Copyright (C) 2015 ownCloud Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 package com
.owncloud
.android
.ui
.fragment
;
23 import android
.accounts
.Account
;
24 import android
.app
.Activity
;
25 import android
.graphics
.Bitmap
;
26 import android
.os
.Bundle
;
27 import android
.support
.v4
.app
.Fragment
;
28 import android
.view
.LayoutInflater
;
29 import android
.view
.View
;
30 import android
.view
.ViewGroup
;
31 import android
.widget
.Button
;
32 import android
.widget
.ImageView
;
33 import android
.widget
.ListView
;
34 import android
.widget
.TextView
;
35 import android
.widget
.Toast
;
37 import com
.owncloud
.android
.R
;
38 import com
.owncloud
.android
.authentication
.AccountUtils
;
39 import com
.owncloud
.android
.datamodel
.OCFile
;
40 import com
.owncloud
.android
.datamodel
.ThumbnailsCacheManager
;
41 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
42 import com
.owncloud
.android
.lib
.resources
.shares
.OCShare
;
43 import com
.owncloud
.android
.ui
.activity
.ShareActivity
;
44 import com
.owncloud
.android
.ui
.adapter
.ShareUserListAdapter
;
45 import com
.owncloud
.android
.utils
.DisplayUtils
;
46 import com
.owncloud
.android
.utils
.MimetypeIconUtil
;
48 import java
.util
.ArrayList
;
51 * Fragment for Sharing a file with users
53 * A simple {@link Fragment} subclass.
54 * Activities that contain this fragment must implement the
55 * {@link ShareFileFragment.OnShareFragmentInteractionListener} interface
56 * to handle interaction events.
57 * Use the {@link ShareFileFragment#newInstance} factory method to
58 * create an instance of this fragment.
60 public class ShareFileFragment
extends Fragment
61 implements ShareUserListAdapter
.ShareUserAdapterListener
{
63 private static final String TAG
= ShareFileFragment
.class.getSimpleName();
65 // the fragment initialization parameters
66 private static final String ARG_FILE
= "FILE";
67 private static final String ARG_ACCOUNT
= "ACCOUNT";
71 private Account mAccount
;
73 private ArrayList
<OCShare
> mShares
;
74 private ShareUserListAdapter mUserGroupsAdapter
= null
;
76 private OnShareFragmentInteractionListener mListener
;
79 * Public factory method to create new ShareFileFragment instances.
81 * @param fileToShare An {@link OCFile} to show in the fragment
82 * @param account An ownCloud account
83 * @return A new instance of fragment ShareFileFragment.
85 public static ShareFileFragment
newInstance(OCFile fileToShare
, Account account
) {
86 ShareFileFragment fragment
= new ShareFileFragment();
87 Bundle args
= new Bundle();
88 args
.putParcelable(ARG_FILE
, fileToShare
);
89 args
.putParcelable(ARG_ACCOUNT
, account
);
90 fragment
.setArguments(args
);
94 public ShareFileFragment() {
95 // Required empty public constructor
99 public void onCreate(Bundle savedInstanceState
) {
100 super.onCreate(savedInstanceState
);
101 if (getArguments() != null
) {
102 mFile
= getArguments().getParcelable(ARG_FILE
);
103 mAccount
= getArguments().getParcelable(ARG_ACCOUNT
);
108 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
109 Bundle savedInstanceState
) {
110 // Inflate the layout for this fragment
111 View view
= inflater
.inflate(R
.layout
.share_file_layout
, container
, false
);
115 ImageView icon
= (ImageView
) view
.findViewById(R
.id
.shareFileIcon
);
116 icon
.setImageResource(MimetypeIconUtil
.getFileTypeIconId(mFile
.getMimetype(),
117 mFile
.getFileName()));
118 if (mFile
.isImage()) {
119 String remoteId
= String
.valueOf(mFile
.getRemoteId());
120 Bitmap thumbnail
= ThumbnailsCacheManager
.getBitmapFromDiskCache(remoteId
);
121 if (thumbnail
!= null
) {
122 icon
.setImageBitmap(thumbnail
);
126 TextView filename
= (TextView
) view
.findViewById(R
.id
.shareFileName
);
127 filename
.setText(mFile
.getFileName());
129 TextView size
= (TextView
) view
.findViewById(R
.id
.shareFileSize
);
130 if (mFile
.isFolder()) {
131 size
.setVisibility(View
.GONE
);
133 size
.setText(DisplayUtils
.bytesToHumanReadable(mFile
.getFileLength()));
137 Button addUserGroupButton
= (Button
)
138 view
.findViewById(R
.id
.addUserButton
);
139 addUserGroupButton
.setOnClickListener(new View
.OnClickListener() {
141 public void onClick(View view
) {
142 boolean shareWithUsersEnable
= AccountUtils
.hasSearchUsersSupport(mAccount
);
143 if (shareWithUsersEnable
) {
144 // Show Search Fragment
145 mListener
.showSearchUsersAndGroups(mShares
);
147 String message
= getString(R
.string
.share_sharee_unavailable
);
148 Toast
.makeText(getActivity(), message
, Toast
.LENGTH_LONG
).show();
157 public void onActivityCreated(Bundle savedInstanceState
) {
158 super.onActivityCreated(savedInstanceState
);
160 // Load data to the list (start process with an Async Task)
161 mListener
.refreshUsersOrGroupsListFromServer();
165 public void onAttach(Activity activity
) {
166 super.onAttach(activity
);
168 mListener
= (OnShareFragmentInteractionListener
) activity
;
169 } catch (ClassCastException e
) {
170 throw new ClassCastException(activity
.toString()
171 + " must implement OnShareFragmentInteractionListener");
176 public void onDetach() {
182 * Get users and groups fromn the DB to fill in the "share with" list
184 public void refreshUsersOrGroupsListFromDB (){
185 // Get Users and Groups
186 mShares
= ((ShareActivity
) mListener
).getStorageManager().getSharesWithForAFile(mFile
.getRemotePath(),
189 // Update list of users/groups
190 updateListOfUserGroups();
193 private void updateListOfUserGroups() {
194 // Update list of users/groups
195 mUserGroupsAdapter
= new ShareUserListAdapter(getActivity().getApplicationContext(),
196 R
.layout
.share_user_item
, mShares
, this);
199 TextView noShares
= (TextView
) getView().findViewById(R
.id
.shareNoUsers
);
200 ListView usersList
= (ListView
) getView().findViewById(R
.id
.shareUsersList
);
202 if (mShares
.size() > 0) {
203 noShares
.setVisibility(View
.GONE
);
204 usersList
.setVisibility(View
.VISIBLE
);
205 usersList
.setAdapter(mUserGroupsAdapter
);
208 noShares
.setVisibility(View
.VISIBLE
);
209 usersList
.setVisibility(View
.GONE
);
214 public void unshareButtonPressed(OCShare share
) {
216 mListener
.unshareWith(share
);
217 Log_OC
.d(TAG
, "Unshare - " + share
.getSharedWithDisplayName());
222 * This interface must be implemented by activities that contain this
223 * fragment to allow an interaction in this fragment to be communicated
224 * to the activity and potentially other fragments contained in that
227 * See the Android Training lesson <a href=
228 * "http://developer.android.com/training/basics/fragments/communicating.html"
229 * >Communicating with Other Fragments</a> for more information.
231 public interface OnShareFragmentInteractionListener
{
232 void showSearchUsersAndGroups(ArrayList
<OCShare
> shares
);
233 void refreshUsersOrGroupsListFromServer();
234 void unshareWith(OCShare share
);