2ed5d0b2c5ebd1d473ba192262e0fddab6152afc
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / ShareFileFragment.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author masensio
5 * @author David A. Velasco
6 * Copyright (C) 2015 ownCloud Inc.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2,
10 * as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22 package com.owncloud.android.ui.fragment;
23
24 import android.accounts.Account;
25 import android.app.Activity;
26 import android.graphics.Bitmap;
27 import android.os.Bundle;
28 import android.support.v4.app.Fragment;
29 import android.support.v7.widget.AppCompatButton;
30 import android.view.LayoutInflater;
31 import android.view.View;
32 import android.view.ViewGroup;
33 import android.widget.Button;
34 import android.widget.CompoundButton;
35 import android.widget.ImageView;
36 import android.widget.ListView;
37 import android.widget.Switch;
38 import android.widget.TextView;
39 import android.widget.Toast;
40
41 import com.owncloud.android.R;
42 import com.owncloud.android.authentication.AccountUtils;
43 import com.owncloud.android.datamodel.OCFile;
44 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
45 import com.owncloud.android.lib.common.utils.Log_OC;
46 import com.owncloud.android.lib.resources.shares.OCShare;
47 import com.owncloud.android.lib.resources.shares.ShareType;
48 import com.owncloud.android.ui.activity.FileActivity;
49 import com.owncloud.android.ui.adapter.ShareUserListAdapter;
50 import com.owncloud.android.utils.DisplayUtils;
51 import com.owncloud.android.utils.MimetypeIconUtil;
52
53 import java.util.ArrayList;
54
55 /**
56 * Fragment for Sharing a file with sharees (users or groups) or creating
57 * a public link.
58 *
59 * A simple {@link Fragment} subclass.
60 *
61 * Activities that contain this fragment must implement the
62 * {@link ShareFileFragment.OnShareFragmentInteractionListener} interface
63 * to handle interaction events.
64 *
65 * Use the {@link ShareFileFragment#newInstance} factory method to
66 * create an instance of this fragment.
67 */
68 public class ShareFileFragment extends Fragment
69 implements ShareUserListAdapter.ShareUserAdapterListener{
70
71 private static final String TAG = ShareFileFragment.class.getSimpleName();
72
73 // the fragment initialization parameters
74 private static final String ARG_FILE = "FILE";
75 private static final String ARG_ACCOUNT = "ACCOUNT";
76
77 /** File to share, received as a parameter in construction time */
78 private OCFile mFile;
79
80 /** OC account holding the file to share, received as a parameter in construction time */
81 private Account mAccount;
82
83 /** Reference to parent listener */
84 private OnShareFragmentInteractionListener mListener;
85
86 /** List of private shares bound to the file */
87 private ArrayList<OCShare> mPrivateShares;
88
89 /** Adapter to show private shares */
90 private ShareUserListAdapter mUserGroupsAdapter = null;
91
92 /** Public share bound to the file */
93 private OCShare mPublicShare;
94
95 /** Listener for changes on switch to share / unshare publicly */
96 private CompoundButton.OnCheckedChangeListener mOnShareViaLinkSwitchCheckedChangeListener;
97
98 /** Listener for changes on switch to set / clear password on public link */
99 private CompoundButton.OnCheckedChangeListener mOnPasswordSwitchCheckedChangeListener;
100
101
102 /**
103 * Public factory method to create new ShareFileFragment instances.
104 *
105 * @param fileToShare An {@link OCFile} to show in the fragment
106 * @param account An ownCloud account
107 * @return A new instance of fragment ShareFileFragment.
108 */
109 public static ShareFileFragment newInstance(OCFile fileToShare, Account account) {
110 ShareFileFragment fragment = new ShareFileFragment();
111 Bundle args = new Bundle();
112 args.putParcelable(ARG_FILE, fileToShare);
113 args.putParcelable(ARG_ACCOUNT, account);
114 fragment.setArguments(args);
115 return fragment;
116 }
117
118 public ShareFileFragment() {
119 // Required empty public constructor
120 }
121
122 /**
123 * {@inheritDoc}
124 */
125 @Override
126 public void onCreate(Bundle savedInstanceState) {
127 super.onCreate(savedInstanceState);
128 if (getArguments() != null) {
129 mFile = getArguments().getParcelable(ARG_FILE);
130 mAccount = getArguments().getParcelable(ARG_ACCOUNT);
131 }
132 }
133
134 /**
135 * {@inheritDoc}
136 */
137 @Override
138 public View onCreateView(LayoutInflater inflater, ViewGroup container,
139 Bundle savedInstanceState) {
140 // Inflate the layout for this fragment
141 View view = inflater.inflate(R.layout.share_file_layout, container, false);
142
143 // Setup layout
144 // Image
145 ImageView icon = (ImageView) view.findViewById(R.id.shareFileIcon);
146 icon.setImageResource(MimetypeIconUtil.getFileTypeIconId(mFile.getMimetype(),
147 mFile.getFileName()));
148 if (mFile.isImage()) {
149 String remoteId = String.valueOf(mFile.getRemoteId());
150 Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(remoteId);
151 if (thumbnail != null) {
152 icon.setImageBitmap(thumbnail);
153 }
154 }
155 // Name
156 TextView filename = (TextView) view.findViewById(R.id.shareFileName);
157 filename.setText(mFile.getFileName());
158 // Size
159 TextView size = (TextView) view.findViewById(R.id.shareFileSize);
160 if (mFile.isFolder()) {
161 size.setVisibility(View.GONE);
162 } else {
163 size.setText(DisplayUtils.bytesToHumanReadable(mFile.getFileLength()));
164 }
165
166 // Add User Button
167 Button addUserGroupButton = (Button)
168 view.findViewById(R.id.addUserButton);
169 addUserGroupButton.setOnClickListener(new View.OnClickListener() {
170 @Override
171 public void onClick(View view) {
172 boolean shareWithUsersEnable = AccountUtils.hasSearchUsersSupport(mAccount);
173 if (shareWithUsersEnable) {
174 // Show Search Fragment
175 mListener.showSearchUsersAndGroups();
176 } else {
177 String message = getString(R.string.share_sharee_unavailable);
178 Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
179 }
180 }
181 });
182
183 // Switch to create public share
184 mOnShareViaLinkSwitchCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
185 @Override
186 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
187 if (!isResumed()) {
188 // very important, setCheched(...) is called automatically during
189 // Fragment recreation on device rotations
190 return;
191 }
192 if (isChecked) {
193 ((FileActivity) getActivity()).getFileOperationsHelper().
194 shareFileViaLink(mFile);
195
196 } else {
197 ((FileActivity) getActivity()).getFileOperationsHelper().
198 unshareFileViaLink(mFile);
199 }
200 }
201 };
202 Switch shareViaLinkSwitch = (Switch) view.findViewById(R.id.shareViaLinkSectionSwitch);
203 shareViaLinkSwitch.setOnCheckedChangeListener(mOnShareViaLinkSwitchCheckedChangeListener);
204
205 // Switch for expiration date
206 Switch shareViaLinkExpirationSwitch = (Switch) view.findViewById(R.id.shareViaLinkExpirationSwitch);
207 shareViaLinkExpirationSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
208 @Override
209 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
210 if (!isResumed()) {
211 // very important, setCheched(...) is called automatically during
212 // Fragment recreation on device rotations
213 return;
214 }
215 if (isChecked) {
216 // TODO real implementation: update share with expiration date
217 // show value of expiration date
218 getExpirationDateValue().setText(R.string.placeholder_timestamp);
219
220 } else {
221 // TODO real implementation: update share without expiration date
222 // empty value
223 getExpirationDateValue().setText(R.string.empty);
224 }
225 }
226 });
227
228 // Switch for password
229 mOnPasswordSwitchCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
230 @Override
231 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
232 if (!isResumed()) {
233 // very important, setCheched(...) is called automatically during
234 // Fragment recreation on device rotations
235 return;
236 }
237 if (isChecked) {
238 ((FileActivity) getActivity()).getFileOperationsHelper().
239 requestPasswordForShareViaLink(mFile);
240 } else {
241 ((FileActivity) getActivity()).getFileOperationsHelper().
242 setPasswordToShareViaLink(mFile, ""); // "" clears
243 }
244 }
245 };
246 Switch shareViaLinkPasswordSwitch = (Switch) view.findViewById(R.id.shareViaLinkPasswordSwitch);
247 shareViaLinkPasswordSwitch.setOnCheckedChangeListener(mOnShareViaLinkSwitchCheckedChangeListener);
248
249 return view;
250 }
251
252 @Override
253 public void onActivityCreated(Bundle savedInstanceState) {
254 super.onActivityCreated(savedInstanceState);
255
256 // Load data into the list of private shares
257 refreshUsersOrGroupsListFromDB();
258
259 // Load data of public share, if exists
260 refreshPublicShareFromDB();
261 }
262
263 @Override
264 public void onAttach(Activity activity) {
265 super.onAttach(activity);
266 try {
267 mListener = (OnShareFragmentInteractionListener) activity;
268 } catch (ClassCastException e) {
269 throw new ClassCastException(activity.toString()
270 + " must implement OnShareFragmentInteractionListener");
271 }
272 }
273
274 @Override
275 public void onDetach() {
276 super.onDetach();
277 mListener = null;
278 }
279
280 /**
281 * Get users and groups from the DB to fill in the "share with" list
282 *
283 * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
284 * instance ready to use. If not ready, does nothing.
285 */
286 public void refreshUsersOrGroupsListFromDB (){
287 if (((FileActivity) mListener).getStorageManager() != null) {
288 // Get Users and Groups
289 mPrivateShares = ((FileActivity) mListener).getStorageManager().getSharesWithForAFile(
290 mFile.getRemotePath(),
291 mAccount.name
292 );
293
294 // Update list of users/groups
295 updateListOfUserGroups();
296 }
297 }
298
299 private void updateListOfUserGroups() {
300 // Update list of users/groups
301 // TODO Refactoring: create a new {@link ShareUserListAdapter} instance with every call should not be needed
302 mUserGroupsAdapter = new ShareUserListAdapter(
303 getActivity(),
304 R.layout.share_user_item,
305 mPrivateShares,
306 this
307 );
308
309 // Show data
310 TextView noShares = (TextView) getView().findViewById(R.id.shareNoUsers);
311 ListView usersList = (ListView) getView().findViewById(R.id.shareUsersList);
312
313 if (mPrivateShares.size() > 0) {
314 noShares.setVisibility(View.GONE);
315 usersList.setVisibility(View.VISIBLE);
316 usersList.setAdapter(mUserGroupsAdapter);
317
318 } else {
319 noShares.setVisibility(View.VISIBLE);
320 usersList.setVisibility(View.GONE);
321 }
322 }
323
324 @Override
325 public void unshareButtonPressed(OCShare share) {
326 // Unshare
327 mListener.unshareWith(share);
328 Log_OC.d(TAG, "Unshare - " + share.getSharedWithDisplayName());
329 }
330
331
332
333 /**
334 * Get public link from the DB to fill in the "Share link" section
335 *
336 * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
337 * instance ready to use. If not ready, does nothing.
338 */
339 public void refreshPublicShareFromDB() {
340 if (((FileActivity) mListener).getStorageManager() != null) {
341 // Get public share
342 mPublicShare = ((FileActivity) mListener).getStorageManager().getFirstShareByPathAndType(
343 mFile.getRemotePath(),
344 ShareType.PUBLIC_LINK,
345 ""
346 );
347
348 // Update list of users/groups
349 updatePublicShareSection();
350 }
351 }
352
353 /**
354 * Updates in the UI the section about public share with the information in the current
355 * public share bound to mFile, if any
356 */
357 private void updatePublicShareSection() {
358 if (mPublicShare != null && ShareType.PUBLIC_LINK.equals(mPublicShare.getShareType())) {
359 /// public share bound -> expand section
360 Switch shareViaLinkSwitch = getShareViaLinkSwitch();
361 if (!shareViaLinkSwitch.isChecked()) {
362 // set null listener before setChecked() to prevent infinite loop of calls
363 shareViaLinkSwitch.setOnCheckedChangeListener(null);
364 shareViaLinkSwitch.setChecked(true);
365 shareViaLinkSwitch.setOnCheckedChangeListener(
366 mOnShareViaLinkSwitchCheckedChangeListener
367 );
368 }
369 getExpirationDateSection().setVisibility(View.VISIBLE);
370 getPasswordSection().setVisibility(View.VISIBLE);
371 getGetLinkButton().setVisibility(View.VISIBLE);
372
373 /// update state of password switch and message depending on password protection
374 Switch passwordSwitch = getPasswordSwitch();
375 // set null listener before setChecked() to prevent infinite loop of calls
376 passwordSwitch.setOnCheckedChangeListener(null);
377 if (mPublicShare.isPasswordProtected()) {
378 if (!passwordSwitch.isChecked()) {
379 passwordSwitch.toggle();
380 }
381 getPasswordValue().setVisibility(View.VISIBLE);
382 } else {
383 if (passwordSwitch.isChecked()) {
384 passwordSwitch.toggle();
385 }
386 getPasswordValue().setVisibility(View.INVISIBLE);
387 }
388 // recover listener
389 passwordSwitch.setOnCheckedChangeListener(
390 mOnPasswordSwitchCheckedChangeListener
391 );
392
393
394 } else {
395 /// no public share -> collapse section
396 Switch shareViaLinkSwitch = getShareViaLinkSwitch();
397 if (shareViaLinkSwitch.isChecked()) {
398 shareViaLinkSwitch.setOnCheckedChangeListener(null);
399 getShareViaLinkSwitch().setChecked(false);
400 shareViaLinkSwitch.setOnCheckedChangeListener(
401 mOnShareViaLinkSwitchCheckedChangeListener
402 );
403 }
404 getExpirationDateSection().setVisibility(View.GONE);
405 getPasswordSection().setVisibility(View.GONE);
406 getGetLinkButton().setVisibility(View.GONE);
407 }
408 }
409
410 /// BEWARE: next methods will failed with NullPointerException if called before onCreateView() finishes
411
412 private Switch getShareViaLinkSwitch() {
413 return (Switch) getView().findViewById(R.id.shareViaLinkSectionSwitch);
414 }
415
416 private View getExpirationDateSection() {
417 return getView().findViewById(R.id.shareViaLinkExpirationSection);
418 }
419
420 private TextView getExpirationDateValue() {
421 return (TextView) getView().findViewById(R.id.shareViaLinkExpirationValue);
422 }
423
424 private View getPasswordSection() {
425 return getView().findViewById(R.id.shareViaLinkPasswordSection);
426 }
427
428 private Switch getPasswordSwitch() {
429 return (Switch) getView().findViewById(R.id.shareViaLinkPasswordSwitch);
430 }
431
432 private TextView getPasswordValue() {
433 return (TextView) getView().findViewById(R.id.shareViaLinkPasswordValue);
434 }
435
436 private AppCompatButton getGetLinkButton() {
437 return (AppCompatButton) getView().findViewById(R.id.shareViewLinkGetLinkButton);
438 }
439
440
441 /**
442 * This interface must be implemented by activities that contain this
443 * fragment to allow an interaction in this fragment to be communicated
444 * to the activity and potentially other fragments contained in that
445 * activity.
446 * <p/>
447 * See the Android Training lesson <a href=
448 * "http://developer.android.com/training/basics/fragments/communicating.html"
449 * >Communicating with Other Fragments</a> for more information.
450 */
451 public interface OnShareFragmentInteractionListener {
452 void showSearchUsersAndGroups();
453 void refreshUsersOrGroupsListFromServer();
454 void unshareWith(OCShare share);
455 }
456
457 }