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