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
;
36 import com
.owncloud
.android
.R
;
37 import com
.owncloud
.android
.datamodel
.OCFile
;
38 import com
.owncloud
.android
.datamodel
.ThumbnailsCacheManager
;
39 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
40 import com
.owncloud
.android
.lib
.resources
.shares
.OCShare
;
41 import com
.owncloud
.android
.ui
.activity
.ShareActivity
;
42 import com
.owncloud
.android
.ui
.adapter
.ShareUserListAdapter
;
43 import com
.owncloud
.android
.utils
.DisplayUtils
;
44 import com
.owncloud
.android
.utils
.MimetypeIconUtil
;
46 import java
.util
.ArrayList
;
49 * Fragment for Sharing a file with users
51 * A simple {@link Fragment} subclass.
52 * Activities that contain this fragment must implement the
53 * {@link ShareFileFragment.OnShareFragmentInteractionListener} interface
54 * to handle interaction events.
55 * Use the {@link ShareFileFragment#newInstance} factory method to
56 * create an instance of this fragment.
58 public class ShareFileFragment
extends Fragment
59 implements ShareUserListAdapter
.ShareUserAdapterListener
{
61 private static final String TAG
= ShareFileFragment
.class.getSimpleName();
63 // the fragment initialization parameters
64 private static final String ARG_FILE
= "FILE";
65 private static final String ARG_ACCOUNT
= "ACCOUNT";
69 private Account mAccount
;
71 private ArrayList
<OCShare
> mShares
;
72 private ShareUserListAdapter mUserGroupsAdapter
= null
;
74 private OnShareFragmentInteractionListener mListener
;
77 * Public factory method to create new ShareFileFragment instances.
79 * @param fileToShare An {@link OCFile} to show in the fragment
80 * @param account An ownCloud account
81 * @return A new instance of fragment ShareFileFragment.
83 public static ShareFileFragment
newInstance(OCFile fileToShare
, Account account
) {
84 ShareFileFragment fragment
= new ShareFileFragment();
85 Bundle args
= new Bundle();
86 args
.putParcelable(ARG_FILE
, fileToShare
);
87 args
.putParcelable(ARG_ACCOUNT
, account
);
88 fragment
.setArguments(args
);
92 public ShareFileFragment() {
93 // Required empty public constructor
97 public void onCreate(Bundle savedInstanceState
) {
98 super.onCreate(savedInstanceState
);
99 if (getArguments() != null
) {
100 mFile
= getArguments().getParcelable(ARG_FILE
);
101 mAccount
= getArguments().getParcelable(ARG_ACCOUNT
);
106 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
107 Bundle savedInstanceState
) {
108 // Inflate the layout for this fragment
109 View view
= inflater
.inflate(R
.layout
.share_file_layout
, container
, false
);
113 ImageView icon
= (ImageView
) view
.findViewById(R
.id
.shareFileIcon
);
114 icon
.setImageResource(MimetypeIconUtil
.getFileTypeIconId(mFile
.getMimetype(),
115 mFile
.getFileName()));
116 if (mFile
.isImage()) {
117 String remoteId
= String
.valueOf(mFile
.getRemoteId());
118 Bitmap thumbnail
= ThumbnailsCacheManager
.getBitmapFromDiskCache(remoteId
);
119 if (thumbnail
!= null
) {
120 icon
.setImageBitmap(thumbnail
);
124 TextView filename
= (TextView
) view
.findViewById(R
.id
.shareFileName
);
125 filename
.setText(mFile
.getFileName());
127 TextView size
= (TextView
) view
.findViewById(R
.id
.shareFileSize
);
128 if (mFile
.isFolder()) {
129 size
.setVisibility(View
.GONE
);
131 size
.setText(DisplayUtils
.bytesToHumanReadable(mFile
.getFileLength()));
135 Button addUserGroupButton
= (Button
)
136 view
.findViewById(R
.id
.addUserButton
);
137 addUserGroupButton
.setOnClickListener(new View
.OnClickListener() {
139 public void onClick(View view
) {
140 // Show Search Fragment
141 mListener
.showSearchUsersAndGroups(mShares
);
149 public void onActivityCreated(Bundle savedInstanceState
) {
150 super.onActivityCreated(savedInstanceState
);
152 // Load data to the list (start process with an Async Task)
153 mListener
.refreshUsersOrGroupsListFromServer();
157 public void onAttach(Activity activity
) {
158 super.onAttach(activity
);
160 mListener
= (OnShareFragmentInteractionListener
) activity
;
161 } catch (ClassCastException e
) {
162 throw new ClassCastException(activity
.toString()
163 + " must implement OnShareFragmentInteractionListener");
168 public void onDetach() {
174 * Get users and groups fromn the DB to fill in the "share with" list
176 public void refreshUsersOrGroupsListFromDB (){
177 // Get Users and Groups
178 mShares
= ((ShareActivity
) mListener
).getStorageManager().getSharesWithForAFile(mFile
.getRemotePath(),
181 // Update list of users/groups
182 updateListOfUserGroups();
185 private void updateListOfUserGroups() {
186 // Update list of users/groups
187 mUserGroupsAdapter
= new ShareUserListAdapter(getActivity().getApplicationContext(),
188 R
.layout
.share_user_item
, mShares
, this);
191 TextView noShares
= (TextView
) getView().findViewById(R
.id
.shareNoUsers
);
192 ListView usersList
= (ListView
) getView().findViewById(R
.id
.shareUsersList
);
194 if (mShares
.size() > 0) {
195 noShares
.setVisibility(View
.GONE
);
196 usersList
.setVisibility(View
.VISIBLE
);
197 usersList
.setAdapter(mUserGroupsAdapter
);
200 noShares
.setVisibility(View
.VISIBLE
);
201 usersList
.setVisibility(View
.GONE
);
206 public void unshareButtonPressed(OCShare share
) {
208 mListener
.unshareWith(share
);
209 Log_OC
.d(TAG
, "Unshare - " + share
.getSharedWithDisplayName());
214 * This interface must be implemented by activities that contain this
215 * fragment to allow an interaction in this fragment to be communicated
216 * to the activity and potentially other fragments contained in that
219 * See the Android Training lesson <a href=
220 * "http://developer.android.com/training/basics/fragments/communicating.html"
221 * >Communicating with Other Fragments</a> for more information.
223 public interface OnShareFragmentInteractionListener
{
224 void showSearchUsersAndGroups(ArrayList
<OCShare
> shares
);
225 void refreshUsersOrGroupsListFromServer();
226 void unshareWith(OCShare share
);