Add unshare with users/groups, from Share wiht view
[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.net.Uri;
27 import android.os.Bundle;
28 import android.support.v4.app.Fragment;
29 import android.view.LayoutInflater;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.widget.AdapterView;
33 import android.widget.Button;
34 import android.widget.ImageView;
35 import android.widget.ListView;
36 import android.widget.TextView;
37 import android.widget.Toast;
38
39 import com.owncloud.android.R;
40 import com.owncloud.android.datamodel.FileDataStorageManager;
41 import com.owncloud.android.datamodel.OCFile;
42 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
43 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
44 import com.owncloud.android.lib.common.utils.Log_OC;
45 import com.owncloud.android.lib.resources.shares.OCShare;
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.MimetypeIconUtil;
50 import com.owncloud.android.utils.UnshareWithUserAsyncTask;
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 UnshareWithUserAsyncTask.OnUnshareWithUserTaskListener{
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 /**
83 * Public factory method to create new ShareFileFragment instances.
84 *
85 * @param fileToShare An {@link OCFile} to show in the fragment
86 * @param account An ownCloud account
87 * @return A new instance of fragment ShareFileFragment.
88 */
89 public static ShareFileFragment newInstance(OCFile fileToShare, Account account) {
90 ShareFileFragment fragment = new ShareFileFragment();
91 Bundle args = new Bundle();
92 args.putParcelable(ARG_FILE, fileToShare);
93 args.putParcelable(ARG_ACCOUNT, account);
94 fragment.setArguments(args);
95 return fragment;
96 }
97
98 public ShareFileFragment() {
99 // Required empty public constructor
100 }
101
102 @Override
103 public void onCreate(Bundle savedInstanceState) {
104 super.onCreate(savedInstanceState);
105 if (getArguments() != null) {
106 mFile = getArguments().getParcelable(ARG_FILE);
107 mAccount = getArguments().getParcelable(ARG_ACCOUNT);
108 }
109 }
110
111 @Override
112 public View onCreateView(LayoutInflater inflater, ViewGroup container,
113 Bundle savedInstanceState) {
114 // Inflate the layout for this fragment
115 View view = inflater.inflate(R.layout.share_file_layout, container, false);
116
117 // Setup layout
118 // Image
119 ImageView icon = (ImageView) view.findViewById(R.id.shareFileIcon);
120 icon.setImageResource(MimetypeIconUtil.getFileTypeIconId(mFile.getMimetype(),
121 mFile.getFileName()));
122 if (mFile.isImage()) {
123 String remoteId = String.valueOf(mFile.getRemoteId());
124 Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(remoteId);
125 if (thumbnail != null) {
126 icon.setImageBitmap(thumbnail);
127 }
128 }
129 // Name
130 TextView filename = (TextView) view.findViewById(R.id.shareFileName);
131 filename.setText(mFile.getFileName());
132 // Size
133 TextView size = (TextView) view.findViewById(R.id.shareFileSize);
134 if (mFile.isFolder()) {
135 size.setVisibility(View.GONE);
136 } else {
137 size.setText(DisplayUtils.bytesToHumanReadable(mFile.getFileLength()));
138 }
139
140 // List of share with users
141 TextView noShares = (TextView) view.findViewById(R.id.shareNoUsers);
142
143 // TODO: Get shares from DB and show
144
145
146 // Add User Button
147 Button addUserGroupButton = (Button)
148 view.findViewById(R.id.addUserButton);
149 addUserGroupButton.setOnClickListener(new View.OnClickListener() {
150 @Override
151 public void onClick(View view) {
152 // Show Search Fragment
153 mListener.showSearchUsersAndGroups();
154 }
155 });
156
157 return view;
158 }
159
160 @Override
161 public void onActivityCreated(Bundle savedInstanceState) {
162 super.onActivityCreated(savedInstanceState);
163
164 getShares();
165 }
166
167 // TODO: Rename method, update argument and hook method into UI event
168 public void onButtonPressed(Uri uri) {
169 if (mListener != null) {
170 mListener.onShareFragmentInteraction(uri);
171 }
172 }
173
174 @Override
175 public void onAttach(Activity activity) {
176 super.onAttach(activity);
177 try {
178 mListener = (OnShareFragmentInteractionListener) activity;
179 } catch (ClassCastException e) {
180 throw new ClassCastException(activity.toString()
181 + " must implement OnShareFragmentInteractionListener");
182 }
183 }
184
185 @Override
186 public void onDetach() {
187 super.onDetach();
188 mListener = null;
189 }
190
191 // Get users and groups to fill the "share with" list
192 private void getShares() {
193 mShares = new ArrayList<>();
194
195 // Get Users and Groups
196 FileDataStorageManager fileDataStorageManager =
197 new FileDataStorageManager(mAccount, getActivity().getContentResolver());
198 mShares = fileDataStorageManager.getSharesWithForAFile(mFile.getRemotePath(), mAccount.name);
199
200 // Update list of users/groups
201 updateListOfUserGroups();
202 }
203
204 private void updateListOfUserGroups() {
205 // Update list of users/groups
206 mUserGroupsAdapter = new ShareUserListAdapter(getActivity().getApplicationContext(),
207 R.layout.share_user_item, mShares);
208
209 // Show data
210 TextView noShares = (TextView) getView().findViewById(R.id.shareNoUsers);
211 ListView usersList = (ListView) getView().findViewById(R.id.shareUsersList);
212
213 if (mShares.size() > 0) {
214 noShares.setVisibility(View.GONE);
215 usersList.setVisibility(View.VISIBLE);
216 usersList.setAdapter(mUserGroupsAdapter);
217
218 // Add unshare options
219 registerLongClickListener(usersList);
220
221 } else {
222 noShares.setVisibility(View.VISIBLE);
223 usersList.setVisibility(View.GONE);
224 }
225 }
226
227 private void registerLongClickListener(final ListView listView) {
228 listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
229 @Override
230 public boolean onItemLongClick(AdapterView<?> parent, View view, final int position,
231 long id) {
232 // Show unshare button
233 ImageView unshareButton = (ImageView) view.findViewById(R.id.unshareButton);
234 if (unshareButton.getVisibility() == View.GONE) {
235 unshareButton.setVisibility(View.VISIBLE);
236 unshareButton.setOnClickListener(new View.OnClickListener() {
237 @Override
238 public void onClick(View v) {
239 // Unshare
240 unshareWith((int)(mShares.get(position).getIdRemoteShared()));
241 Log_OC.d(TAG, "Unshare - " +
242 mShares.get(position).getSharedWithDisplayName());
243 }
244 });
245
246 } else {
247 unshareButton.setVisibility(View.GONE);
248 }
249 view.setAlpha(0);
250 view.animate().alpha(1).setDuration(500).start();
251 return false;
252 }
253 });
254 }
255
256 private void unshareWith(int shareId){
257 ( (ShareActivity) getActivity()).showWaitingLoadDialog();
258 // Remove Share with id
259 UnshareWithUserAsyncTask unshareTask = new UnshareWithUserAsyncTask(this);
260 FileDataStorageManager fileDataStorageManager =
261 new FileDataStorageManager(mAccount, getActivity().getContentResolver());
262 Object[] params = { shareId, mAccount, fileDataStorageManager};
263 unshareTask.execute(params);
264 }
265
266 @Override
267 public void onUnshareWithFinish(RemoteOperationResult result) {
268 // Remove loading
269 ((ShareActivity) getActivity()).dismissWaitingLoadDialog();
270
271 if (result != null && result.isSuccess()) {
272 // Refresh data
273 //TODO: Refresh file or delete the user from the list
274 updateListOfUserGroups();
275
276 } else {
277 Toast.makeText(getActivity(), result.getLogMessage(), Toast.LENGTH_SHORT).show();
278 }
279 }
280
281 // TODO: review if it is necessary
282 /**
283 * This interface must be implemented by activities that contain this
284 * fragment to allow an interaction in this fragment to be communicated
285 * to the activity and potentially other fragments contained in that
286 * activity.
287 * <p/>
288 * See the Android Training lesson <a href=
289 * "http://developer.android.com/training/basics/fragments/communicating.html"
290 * >Communicating with Other Fragments</a> for more information.
291 */
292 public interface OnShareFragmentInteractionListener {
293 void showSearchUsersAndGroups();
294
295 void onShareFragmentInteraction(Uri uri);
296 }
297
298 }