Get users/group from Server, to fill in 'Share with' 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.AdapterView;
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;
37
38 import com.owncloud.android.R;
39 import com.owncloud.android.datamodel.FileDataStorageManager;
40 import com.owncloud.android.datamodel.OCFile;
41 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
42 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
43 import com.owncloud.android.lib.common.utils.Log_OC;
44 import com.owncloud.android.lib.resources.shares.OCShare;
45 import com.owncloud.android.lib.resources.shares.ShareType;
46 import com.owncloud.android.ui.activity.FileActivity;
47 import com.owncloud.android.ui.activity.ShareActivity;
48 import com.owncloud.android.ui.adapter.ShareUserListAdapter;
49 import com.owncloud.android.utils.DisplayUtils;
50 import com.owncloud.android.utils.GetShareWithUsersAsyncTask;
51 import com.owncloud.android.utils.MimetypeIconUtil;
52
53 import java.util.ArrayList;
54
55 /**
56 * Fragment for Sharing a file with users
57 *
58 * A simple {@link Fragment} subclass.
59 * Activities that contain this fragment must implement the
60 * {@link ShareFileFragment.OnShareFragmentInteractionListener} interface
61 * to handle interaction events.
62 * Use the {@link ShareFileFragment#newInstance} factory method to
63 * create an instance of this fragment.
64 */
65 public class ShareFileFragment extends Fragment
66 implements GetShareWithUsersAsyncTask.OnGetSharesWithUsersTaskListener {
67
68 private static final String TAG = ShareFileFragment.class.getSimpleName();
69
70 // the fragment initialization parameters
71 private static final String ARG_FILE = "FILE";
72 private static final String ARG_ACCOUNT = "ACCOUNT";
73
74 // Parameters
75 private OCFile mFile;
76 private Account mAccount;
77
78 private ArrayList<OCShare> mShares;
79 private ShareUserListAdapter mUserGroupsAdapter = null;
80
81 private OnShareFragmentInteractionListener mListener;
82
83 /**
84 * Public factory method to create new ShareFileFragment instances.
85 *
86 * @param fileToShare An {@link OCFile} to show in the fragment
87 * @param account An ownCloud account
88 * @return A new instance of fragment ShareFileFragment.
89 */
90 public static ShareFileFragment newInstance(OCFile fileToShare, Account account) {
91 ShareFileFragment fragment = new ShareFileFragment();
92 Bundle args = new Bundle();
93 args.putParcelable(ARG_FILE, fileToShare);
94 args.putParcelable(ARG_ACCOUNT, account);
95 fragment.setArguments(args);
96 return fragment;
97 }
98
99 public ShareFileFragment() {
100 // Required empty public constructor
101 }
102
103 @Override
104 public void onCreate(Bundle savedInstanceState) {
105 super.onCreate(savedInstanceState);
106 if (getArguments() != null) {
107 mFile = getArguments().getParcelable(ARG_FILE);
108 mAccount = getArguments().getParcelable(ARG_ACCOUNT);
109 }
110 }
111
112 @Override
113 public View onCreateView(LayoutInflater inflater, ViewGroup container,
114 Bundle savedInstanceState) {
115 // Inflate the layout for this fragment
116 View view = inflater.inflate(R.layout.share_file_layout, container, false);
117
118 // Setup layout
119 // Image
120 ImageView icon = (ImageView) view.findViewById(R.id.shareFileIcon);
121 icon.setImageResource(MimetypeIconUtil.getFileTypeIconId(mFile.getMimetype(),
122 mFile.getFileName()));
123 if (mFile.isImage()) {
124 String remoteId = String.valueOf(mFile.getRemoteId());
125 Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(remoteId);
126 if (thumbnail != null) {
127 icon.setImageBitmap(thumbnail);
128 }
129 }
130 // Name
131 TextView filename = (TextView) view.findViewById(R.id.shareFileName);
132 filename.setText(mFile.getFileName());
133 // Size
134 TextView size = (TextView) view.findViewById(R.id.shareFileSize);
135 if (mFile.isFolder()) {
136 size.setVisibility(View.GONE);
137 } else {
138 size.setText(DisplayUtils.bytesToHumanReadable(mFile.getFileLength()));
139 }
140
141 // Add User Button
142 Button addUserGroupButton = (Button)
143 view.findViewById(R.id.addUserButton);
144 addUserGroupButton.setOnClickListener(new View.OnClickListener() {
145 @Override
146 public void onClick(View view) {
147 // Show Search Fragment
148 mListener.showSearchUsersAndGroups();
149 }
150 });
151
152 return view;
153 }
154
155 @Override
156 public void onActivityCreated(Bundle savedInstanceState) {
157 super.onActivityCreated(savedInstanceState);
158
159 // Load data to the list (start process with an Async Task)
160 refreshUsersOrGroupsListFromServer();
161
162 }
163
164 @Override
165 public void onAttach(Activity activity) {
166 super.onAttach(activity);
167 try {
168 mListener = (OnShareFragmentInteractionListener) activity;
169 } catch (ClassCastException e) {
170 throw new ClassCastException(activity.toString()
171 + " must implement OnShareFragmentInteractionListener");
172 }
173 }
174
175 @Override
176 public void onDetach() {
177 super.onDetach();
178 mListener = null;
179 }
180
181 /**
182 * Get users and groups fromn the DB to fill in the "share with" list
183 */
184 public void refreshUsersOrGroupsListFromDB (){
185 // Get Users and Groups
186 FileDataStorageManager fileDataStorageManager =
187 new FileDataStorageManager(mAccount, getActivity().getContentResolver());
188 mShares = fileDataStorageManager.getSharesWithForAFile(mFile.getRemotePath(),
189 mAccount.name);
190
191 // Update list of users/groups
192 updateListOfUserGroups();
193 }
194
195 private void updateListOfUserGroups() {
196 // Update list of users/groups
197 mUserGroupsAdapter = new ShareUserListAdapter(getActivity().getApplicationContext(),
198 R.layout.share_user_item, mShares);
199
200 // Show data
201 TextView noShares = (TextView) getView().findViewById(R.id.shareNoUsers);
202 ListView usersList = (ListView) getView().findViewById(R.id.shareUsersList);
203
204 if (mShares.size() > 0) {
205 noShares.setVisibility(View.GONE);
206 usersList.setVisibility(View.VISIBLE);
207 usersList.setAdapter(mUserGroupsAdapter);
208
209 // Add unshare options
210 registerLongClickListener(usersList);
211
212 } else {
213 noShares.setVisibility(View.VISIBLE);
214 usersList.setVisibility(View.GONE);
215 }
216 }
217
218 /**
219 * Get users and groups from the server to fill in the "share with" list
220 */
221 public void refreshUsersOrGroupsListFromServer(){
222 mShares = new ArrayList<>();
223
224 // Show loading
225 ((ShareActivity) getActivity()).showLoadingDialog(getString(R.string.common_loading));
226 // Get Users and Groups
227 GetShareWithUsersAsyncTask getTask = new GetShareWithUsersAsyncTask(this);
228 FileDataStorageManager fileDataStorageManager = ((ShareActivity) getActivity()).getStorageManager();
229 Object[] params = { mFile, mAccount, fileDataStorageManager};
230 getTask.execute(params);
231 }
232
233 private void registerLongClickListener(final ListView listView) {
234 listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
235 @Override
236 public boolean onItemLongClick(AdapterView<?> parent, View view, final int position,
237 long id) {
238 // Show unshare button
239 ImageView unshareButton = (ImageView) view.findViewById(R.id.unshareButton);
240 if (unshareButton.getVisibility() == View.GONE) {
241 unshareButton.setVisibility(View.VISIBLE);
242 unshareButton.setOnClickListener(new View.OnClickListener() {
243 @Override
244 public void onClick(View v) {
245 // Unshare
246 unshareWith(mShares.get(position));
247 Log_OC.d(TAG, "Unshare - " +
248 mShares.get(position).getSharedWithDisplayName());
249 }
250 });
251
252 } else {
253 unshareButton.setVisibility(View.GONE);
254 }
255 view.setAlpha(0);
256 view.animate().alpha(1).setDuration(500).start();
257 return false;
258 }
259 });
260 }
261
262 // Call to Unshare operation
263 private void unshareWith(OCShare share){
264 OCFile file = ((FileActivity) getActivity()).getFile();
265
266 ((FileActivity) getActivity()).getFileOperationsHelper().
267 unshareFileWithUserOrGroup(
268 file, share.getShareType(), share.getShareWith()
269 );
270 }
271
272 @Override
273 public void onGetDataShareWithFinish(RemoteOperationResult result) {
274 // Remove loading
275 ((ShareActivity) getActivity()).dismissLoadingDialog();
276 if (result != null && result.isSuccess()) {
277 // update local database
278 for(Object obj: result.getData()) {
279 if ( ((OCShare) obj).getShareType() == ShareType.USER ||
280 ((OCShare) obj).getShareType() == ShareType.GROUP ){
281 mShares.add((OCShare) obj);
282 }
283 }
284
285 // Update list of users/groups
286 mUserGroupsAdapter = new ShareUserListAdapter(getActivity().getApplicationContext(),
287 R.layout.share_user_item, mShares);
288
289 // Show data
290 TextView noShares = (TextView) getView().findViewById(R.id.shareNoUsers);
291 ListView usersList = (ListView) getView().findViewById(R.id.shareUsersList);
292
293 if (mShares.size() > 0) {
294 noShares.setVisibility(View.GONE);
295 usersList.setVisibility(View.VISIBLE);
296 usersList.setAdapter(mUserGroupsAdapter);
297
298 } else {
299 noShares.setVisibility(View.VISIBLE);
300 usersList.setVisibility(View.GONE);
301 }
302 } else {
303 Toast.makeText(getActivity(), result.getLogMessage(), Toast.LENGTH_SHORT).show();
304 }
305 }
306
307
308 // TODO: review if it is necessary
309 /**
310 * This interface must be implemented by activities that contain this
311 * fragment to allow an interaction in this fragment to be communicated
312 * to the activity and potentially other fragments contained in that
313 * activity.
314 * <p/>
315 * See the Android Training lesson <a href=
316 * "http://developer.android.com/training/basics/fragments/communicating.html"
317 * >Communicating with Other Fragments</a> for more information.
318 */
319 public interface OnShareFragmentInteractionListener {
320 void showSearchUsersAndGroups();
321 }
322
323 }