db1e98d36f616e0370df3b7b5c8ec5b376669020
[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
99 /**
100 * Public factory method to create new ShareFileFragment instances.
101 *
102 * @param fileToShare An {@link OCFile} to show in the fragment
103 * @param account An ownCloud account
104 * @return A new instance of fragment ShareFileFragment.
105 */
106 public static ShareFileFragment newInstance(OCFile fileToShare, Account account) {
107 ShareFileFragment fragment = new ShareFileFragment();
108 Bundle args = new Bundle();
109 args.putParcelable(ARG_FILE, fileToShare);
110 args.putParcelable(ARG_ACCOUNT, account);
111 fragment.setArguments(args);
112 return fragment;
113 }
114
115 public ShareFileFragment() {
116 // Required empty public constructor
117 }
118
119 /**
120 * {@inheritDoc}
121 */
122 @Override
123 public void onCreate(Bundle savedInstanceState) {
124 super.onCreate(savedInstanceState);
125 if (getArguments() != null) {
126 mFile = getArguments().getParcelable(ARG_FILE);
127 mAccount = getArguments().getParcelable(ARG_ACCOUNT);
128 }
129 }
130
131 /**
132 * {@inheritDoc}
133 */
134 @Override
135 public View onCreateView(LayoutInflater inflater, ViewGroup container,
136 Bundle savedInstanceState) {
137 // Inflate the layout for this fragment
138 View view = inflater.inflate(R.layout.share_file_layout, container, false);
139
140 // Setup layout
141 // Image
142 ImageView icon = (ImageView) view.findViewById(R.id.shareFileIcon);
143 icon.setImageResource(MimetypeIconUtil.getFileTypeIconId(mFile.getMimetype(),
144 mFile.getFileName()));
145 if (mFile.isImage()) {
146 String remoteId = String.valueOf(mFile.getRemoteId());
147 Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(remoteId);
148 if (thumbnail != null) {
149 icon.setImageBitmap(thumbnail);
150 }
151 }
152 // Name
153 TextView filename = (TextView) view.findViewById(R.id.shareFileName);
154 filename.setText(mFile.getFileName());
155 // Size
156 TextView size = (TextView) view.findViewById(R.id.shareFileSize);
157 if (mFile.isFolder()) {
158 size.setVisibility(View.GONE);
159 } else {
160 size.setText(DisplayUtils.bytesToHumanReadable(mFile.getFileLength()));
161 }
162
163 // Add User Button
164 Button addUserGroupButton = (Button)
165 view.findViewById(R.id.addUserButton);
166 addUserGroupButton.setOnClickListener(new View.OnClickListener() {
167 @Override
168 public void onClick(View view) {
169 boolean shareWithUsersEnable = AccountUtils.hasSearchUsersSupport(mAccount);
170 if (shareWithUsersEnable) {
171 // Show Search Fragment
172 mListener.showSearchUsersAndGroups();
173 } else {
174 String message = getString(R.string.share_sharee_unavailable);
175 Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
176 }
177 }
178 });
179
180 // Switch to create public share
181 mOnShareViaLinkSwitchCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
182 @Override
183 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
184 if (isResumed()) {
185 if (isChecked) {
186 ((FileActivity) getActivity()).getFileOperationsHelper().shareFileViaLink(mFile);
187
188 } else {
189 ((FileActivity) getActivity()).getFileOperationsHelper().unshareFileViaLink(mFile);
190 }
191 } // else, nothing; very important, setCheched(...) is called automatically during Fragment
192 // recreation on device rotations
193 }
194 };
195 Switch shareViaLinkSwitch = (Switch) view.findViewById(R.id.shareViaLinkSectionSwitch);
196 shareViaLinkSwitch.setOnCheckedChangeListener(mOnShareViaLinkSwitchCheckedChangeListener);
197
198 // Switch for expiration date
199 Switch shareViaLinkExpirationSwitch = (Switch) view.findViewById(R.id.shareViaLinkExpirationSwitch);
200 shareViaLinkExpirationSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
201 @Override
202 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
203 if (isChecked) {
204 // TODO real implementation: update share with expiration date
205 // show value of expiration date
206 getExpirationDateValue().setText(R.string.placeholder_timestamp);
207
208 } else {
209 // TODO real implementation: update share without expiration date
210 // empty value
211 getExpirationDateValue().setText(R.string.empty);
212 }
213 }
214 });
215
216 // Switch for password
217 Switch shareViaLinkPasswordSwitch = (Switch) view.findViewById(R.id.shareViaLinkPasswordSwitch);
218 shareViaLinkPasswordSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
219 @Override
220 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
221 if (isChecked) {
222 // TODO real implementation: update share with password
223 // show
224 getExpirationPasswordValue().setVisibility(View.VISIBLE);
225
226 } else {
227 // TODO real implementation: update share without password
228 // empty value
229 getExpirationPasswordValue().setVisibility(View.INVISIBLE);
230 }
231 }
232 });
233
234
235 return view;
236 }
237
238 @Override
239 public void onActivityCreated(Bundle savedInstanceState) {
240 super.onActivityCreated(savedInstanceState);
241
242 // Load data into the list of private shares
243 refreshUsersOrGroupsListFromDB();
244
245 // Load data of public share, if exists
246 refreshPublicShareFromDB();
247 }
248
249 @Override
250 public void onAttach(Activity activity) {
251 super.onAttach(activity);
252 try {
253 mListener = (OnShareFragmentInteractionListener) activity;
254 } catch (ClassCastException e) {
255 throw new ClassCastException(activity.toString()
256 + " must implement OnShareFragmentInteractionListener");
257 }
258 }
259
260 @Override
261 public void onDetach() {
262 super.onDetach();
263 mListener = null;
264 }
265
266 /**
267 * Get users and groups from the DB to fill in the "share with" list
268 *
269 * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
270 * instance ready to use. If not ready, does nothing.
271 */
272 public void refreshUsersOrGroupsListFromDB (){
273 if (((FileActivity) mListener).getStorageManager() != null) {
274 // Get Users and Groups
275 mPrivateShares = ((FileActivity) mListener).getStorageManager().getSharesWithForAFile(
276 mFile.getRemotePath(),
277 mAccount.name
278 );
279
280 // Update list of users/groups
281 updateListOfUserGroups();
282 }
283 }
284
285 private void updateListOfUserGroups() {
286 // Update list of users/groups
287 // TODO Refactoring: create a new {@link ShareUserListAdapter} instance with every call should not be needed
288 mUserGroupsAdapter = new ShareUserListAdapter(
289 getActivity(),
290 R.layout.share_user_item,
291 mPrivateShares,
292 this
293 );
294
295 // Show data
296 TextView noShares = (TextView) getView().findViewById(R.id.shareNoUsers);
297 ListView usersList = (ListView) getView().findViewById(R.id.shareUsersList);
298
299 if (mPrivateShares.size() > 0) {
300 noShares.setVisibility(View.GONE);
301 usersList.setVisibility(View.VISIBLE);
302 usersList.setAdapter(mUserGroupsAdapter);
303
304 } else {
305 noShares.setVisibility(View.VISIBLE);
306 usersList.setVisibility(View.GONE);
307 }
308 }
309
310 @Override
311 public void unshareButtonPressed(OCShare share) {
312 // Unshare
313 mListener.unshareWith(share);
314 Log_OC.d(TAG, "Unshare - " + share.getSharedWithDisplayName());
315 }
316
317
318
319 /**
320 * Get public link from the DB to fill in the "Share link" section
321 *
322 * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
323 * instance ready to use. If not ready, does nothing.
324 */
325 public void refreshPublicShareFromDB() {
326 if (((FileActivity) mListener).getStorageManager() != null) {
327 // Get public share
328 mPublicShare = ((FileActivity) mListener).getStorageManager().getFirstShareByPathAndType(
329 mFile.getRemotePath(),
330 ShareType.PUBLIC_LINK,
331 ""
332 );
333
334 // Update list of users/groups
335 updatePublicShareSection();
336 }
337 }
338
339 /**
340 * Updates in the UI the section about public share with the information in the current
341 * public share bound to mFile, if any
342 */
343 private void updatePublicShareSection() {
344 if (mPublicShare != null && ShareType.PUBLIC_LINK.equals(mPublicShare.getShareType())) {
345 // public share bound -> expand section
346 Switch shareViaLinkSwitch = getShareViaLinkSwitch();
347 if (!shareViaLinkSwitch.isChecked()) {
348 // set null listener before setChecked() to prevent infinite loop of calls
349 shareViaLinkSwitch.setOnCheckedChangeListener(null);
350 getShareViaLinkSwitch().setChecked(true);
351 shareViaLinkSwitch.setOnCheckedChangeListener(
352 mOnShareViaLinkSwitchCheckedChangeListener
353 );
354 }
355 getExpirationDateSection().setVisibility(View.VISIBLE);
356 getPasswordSection().setVisibility(View.VISIBLE);
357 getGetLinkButton().setVisibility(View.VISIBLE);
358
359 } else {
360 // no public share -> collapse section
361 Switch shareViaLinkSwitch = getShareViaLinkSwitch();
362 if (shareViaLinkSwitch.isChecked()) {
363 shareViaLinkSwitch.setOnCheckedChangeListener(null);
364 getShareViaLinkSwitch().setChecked(false);
365 shareViaLinkSwitch.setOnCheckedChangeListener(
366 mOnShareViaLinkSwitchCheckedChangeListener
367 );
368 }
369 getExpirationDateSection().setVisibility(View.GONE);
370 getPasswordSection().setVisibility(View.GONE);
371 getGetLinkButton().setVisibility(View.GONE);
372 }
373 }
374
375 private Switch getShareViaLinkSwitch() {
376 return (Switch) getView().findViewById(R.id.shareViaLinkSectionSwitch);
377 }
378
379 private View getExpirationDateSection() {
380 return getView().findViewById(R.id.shareViaLinkExpirationSection);
381 }
382
383 private TextView getExpirationDateValue() {
384 return (TextView) getView().findViewById(R.id.shareViaLinkExpirationValue);
385 }
386
387 private View getPasswordSection() {
388 return getView().findViewById(R.id.shareViaLinkPasswordSection);
389 }
390
391 private TextView getExpirationPasswordValue() {
392 return (TextView) getView().findViewById(R.id.shareViaLinkPasswordValue);
393 }
394
395 private AppCompatButton getGetLinkButton() {
396 return (AppCompatButton) getView().findViewById(R.id.shareViewLinkGetLinkButton);
397 }
398
399
400 /**
401 * This interface must be implemented by activities that contain this
402 * fragment to allow an interaction in this fragment to be communicated
403 * to the activity and potentially other fragments contained in that
404 * activity.
405 * <p/>
406 * See the Android Training lesson <a href=
407 * "http://developer.android.com/training/basics/fragments/communicating.html"
408 * >Communicating with Other Fragments</a> for more information.
409 */
410 public interface OnShareFragmentInteractionListener {
411 void showSearchUsersAndGroups();
412 void refreshUsersOrGroupsListFromServer();
413 void unshareWith(OCShare share);
414 }
415
416 }