Show users/groups in Search Fragment list
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / ShareFileFragment.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author masensio
5 * Copyright (C) 2015 ownCloud Inc.
6 *
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.
10 *
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.
15 *
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/>.
18 *
19 */
20
21 package com.owncloud.android.ui.fragment;
22
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
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;
45
46 import java.util.ArrayList;
47
48 /**
49 * Fragment for Sharing a file with users
50 *
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.
57 */
58 public class ShareFileFragment extends Fragment
59 implements ShareUserListAdapter.ShareUserAdapterListener{
60
61 private static final String TAG = ShareFileFragment.class.getSimpleName();
62
63 // the fragment initialization parameters
64 private static final String ARG_FILE = "FILE";
65 private static final String ARG_ACCOUNT = "ACCOUNT";
66
67 // Parameters
68 private OCFile mFile;
69 private Account mAccount;
70
71 private ArrayList<OCShare> mShares;
72 private ShareUserListAdapter mUserGroupsAdapter = null;
73
74 private OnShareFragmentInteractionListener mListener;
75
76 /**
77 * Public factory method to create new ShareFileFragment instances.
78 *
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.
82 */
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);
89 return fragment;
90 }
91
92 public ShareFileFragment() {
93 // Required empty public constructor
94 }
95
96 @Override
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);
102 }
103 }
104
105 @Override
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);
110
111 // Setup layout
112 // Image
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);
121 }
122 }
123 // Name
124 TextView filename = (TextView) view.findViewById(R.id.shareFileName);
125 filename.setText(mFile.getFileName());
126 // Size
127 TextView size = (TextView) view.findViewById(R.id.shareFileSize);
128 if (mFile.isFolder()) {
129 size.setVisibility(View.GONE);
130 } else {
131 size.setText(DisplayUtils.bytesToHumanReadable(mFile.getFileLength()));
132 }
133
134 // Add User Button
135 Button addUserGroupButton = (Button)
136 view.findViewById(R.id.addUserButton);
137 addUserGroupButton.setOnClickListener(new View.OnClickListener() {
138 @Override
139 public void onClick(View view) {
140 // Show Search Fragment
141 mListener.showSearchUsersAndGroups(mShares);
142 }
143 });
144
145 return view;
146 }
147
148 @Override
149 public void onActivityCreated(Bundle savedInstanceState) {
150 super.onActivityCreated(savedInstanceState);
151
152 // Load data to the list (start process with an Async Task)
153 mListener.refreshUsersOrGroupsListFromServer();
154 }
155
156 @Override
157 public void onAttach(Activity activity) {
158 super.onAttach(activity);
159 try {
160 mListener = (OnShareFragmentInteractionListener) activity;
161 } catch (ClassCastException e) {
162 throw new ClassCastException(activity.toString()
163 + " must implement OnShareFragmentInteractionListener");
164 }
165 }
166
167 @Override
168 public void onDetach() {
169 super.onDetach();
170 mListener = null;
171 }
172
173 /**
174 * Get users and groups fromn the DB to fill in the "share with" list
175 */
176 public void refreshUsersOrGroupsListFromDB (){
177 // Get Users and Groups
178 mShares = ((ShareActivity) mListener).getStorageManager().getSharesWithForAFile(mFile.getRemotePath(),
179 mAccount.name);
180
181 // Update list of users/groups
182 updateListOfUserGroups();
183 }
184
185 private void updateListOfUserGroups() {
186 // Update list of users/groups
187 mUserGroupsAdapter = new ShareUserListAdapter(getActivity().getApplicationContext(),
188 R.layout.share_user_item, mShares, this);
189
190 // Show data
191 TextView noShares = (TextView) getView().findViewById(R.id.shareNoUsers);
192 ListView usersList = (ListView) getView().findViewById(R.id.shareUsersList);
193
194 if (mShares.size() > 0) {
195 noShares.setVisibility(View.GONE);
196 usersList.setVisibility(View.VISIBLE);
197 usersList.setAdapter(mUserGroupsAdapter);
198
199 } else {
200 noShares.setVisibility(View.VISIBLE);
201 usersList.setVisibility(View.GONE);
202 }
203 }
204
205 @Override
206 public void unshareButtonPressed(OCShare share) {
207 // Unshare
208 mListener.unshareWith(share);
209 Log_OC.d(TAG, "Unshare - " + share.getSharedWithDisplayName());
210 }
211
212
213 /**
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
217 * activity.
218 * <p/>
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.
222 */
223 public interface OnShareFragmentInteractionListener {
224 void showSearchUsersAndGroups(ArrayList<OCShare> shares);
225 void refreshUsersOrGroupsListFromServer();
226 void unshareWith(OCShare share);
227 }
228
229 }