5ee2a87ca5bfce395ef8ef81f0ca9600eb276944
[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.view.LayoutInflater;
30 import android.view.View;
31 import android.view.ViewGroup;
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.authentication.AccountUtils;
40 import com.owncloud.android.datamodel.OCFile;
41 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
42 import com.owncloud.android.lib.common.utils.Log_OC;
43 import com.owncloud.android.lib.resources.shares.OCShare;
44 import com.owncloud.android.ui.activity.ShareActivity;
45 import com.owncloud.android.ui.adapter.ShareUserListAdapter;
46 import com.owncloud.android.utils.DisplayUtils;
47 import com.owncloud.android.utils.MimetypeIconUtil;
48
49 import java.util.ArrayList;
50
51 /**
52 * Fragment for Sharing a file with users
53 *
54 * A simple {@link Fragment} subclass.
55 * Activities that contain this fragment must implement the
56 * {@link ShareFileFragment.OnShareFragmentInteractionListener} interface
57 * to handle interaction events.
58 * Use the {@link ShareFileFragment#newInstance} factory method to
59 * create an instance of this fragment.
60 */
61 public class ShareFileFragment extends Fragment
62 implements ShareUserListAdapter.ShareUserAdapterListener{
63
64 private static final String TAG = ShareFileFragment.class.getSimpleName();
65
66 // the fragment initialization parameters
67 private static final String ARG_FILE = "FILE";
68 private static final String ARG_ACCOUNT = "ACCOUNT";
69
70 // Parameters
71 private OCFile mFile;
72 private Account mAccount;
73
74 private ArrayList<OCShare> mShares;
75 private ShareUserListAdapter mUserGroupsAdapter = null;
76
77 private OnShareFragmentInteractionListener mListener;
78
79 /**
80 * Public factory method to create new ShareFileFragment instances.
81 *
82 * @param fileToShare An {@link OCFile} to show in the fragment
83 * @param account An ownCloud account
84 * @return A new instance of fragment ShareFileFragment.
85 */
86 public static ShareFileFragment newInstance(OCFile fileToShare, Account account) {
87 ShareFileFragment fragment = new ShareFileFragment();
88 Bundle args = new Bundle();
89 args.putParcelable(ARG_FILE, fileToShare);
90 args.putParcelable(ARG_ACCOUNT, account);
91 fragment.setArguments(args);
92 return fragment;
93 }
94
95 public ShareFileFragment() {
96 // Required empty public constructor
97 }
98
99 @Override
100 public void onCreate(Bundle savedInstanceState) {
101 super.onCreate(savedInstanceState);
102 if (getArguments() != null) {
103 mFile = getArguments().getParcelable(ARG_FILE);
104 mAccount = getArguments().getParcelable(ARG_ACCOUNT);
105 }
106 }
107
108 @Override
109 public View onCreateView(LayoutInflater inflater, ViewGroup container,
110 Bundle savedInstanceState) {
111 // Inflate the layout for this fragment
112 View view = inflater.inflate(R.layout.share_file_layout, container, false);
113
114 // Setup layout
115 // Image
116 ImageView icon = (ImageView) view.findViewById(R.id.shareFileIcon);
117 icon.setImageResource(MimetypeIconUtil.getFileTypeIconId(mFile.getMimetype(),
118 mFile.getFileName()));
119 if (mFile.isImage()) {
120 String remoteId = String.valueOf(mFile.getRemoteId());
121 Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(remoteId);
122 if (thumbnail != null) {
123 icon.setImageBitmap(thumbnail);
124 }
125 }
126 // Name
127 TextView filename = (TextView) view.findViewById(R.id.shareFileName);
128 filename.setText(mFile.getFileName());
129 // Size
130 TextView size = (TextView) view.findViewById(R.id.shareFileSize);
131 if (mFile.isFolder()) {
132 size.setVisibility(View.GONE);
133 } else {
134 size.setText(DisplayUtils.bytesToHumanReadable(mFile.getFileLength()));
135 }
136
137 // Add User Button
138 Button addUserGroupButton = (Button)
139 view.findViewById(R.id.addUserButton);
140 addUserGroupButton.setOnClickListener(new View.OnClickListener() {
141 @Override
142 public void onClick(View view) {
143 boolean shareWithUsersEnable = AccountUtils.hasSearchUsersSupport(mAccount);
144 if (shareWithUsersEnable) {
145 // Show Search Fragment
146 mListener.showSearchUsersAndGroups();
147 } else {
148 String message = getString(R.string.share_sharee_unavailable);
149 Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
150 }
151 }
152 });
153
154 return view;
155 }
156
157 @Override
158 public void onActivityCreated(Bundle savedInstanceState) {
159 super.onActivityCreated(savedInstanceState);
160
161 // Load data into the list
162 refreshUsersOrGroupsListFromDB();
163
164 // Request for a refresh of the data through the server (starts an Async Task)
165 if (mListener != null) {
166 mListener.refreshUsersOrGroupsListFromServer();
167 }
168 }
169
170 @Override
171 public void onAttach(Activity activity) {
172 super.onAttach(activity);
173 try {
174 mListener = (OnShareFragmentInteractionListener) activity;
175 } catch (ClassCastException e) {
176 throw new ClassCastException(activity.toString()
177 + " must implement OnShareFragmentInteractionListener");
178 }
179 }
180
181 @Override
182 public void onDetach() {
183 super.onDetach();
184 mListener = null;
185 }
186
187 /**
188 * Get users and groups fromn the DB to fill in the "share with" list
189 */
190 public void refreshUsersOrGroupsListFromDB (){
191 // Get Users and Groups
192 mShares = ((ShareActivity) mListener).getStorageManager().getSharesWithForAFile(mFile.getRemotePath(),
193 mAccount.name);
194
195 // Update list of users/groups
196 updateListOfUserGroups();
197 }
198
199 private void updateListOfUserGroups() {
200 // Update list of users/groups
201 mUserGroupsAdapter = new ShareUserListAdapter(
202 getActivity(),
203 R.layout.share_user_item,
204 mShares,
205 this
206 );
207
208 // Show data
209 TextView noShares = (TextView) getView().findViewById(R.id.shareNoUsers);
210 ListView usersList = (ListView) getView().findViewById(R.id.shareUsersList);
211
212 if (mShares.size() > 0) {
213 noShares.setVisibility(View.GONE);
214 usersList.setVisibility(View.VISIBLE);
215 usersList.setAdapter(mUserGroupsAdapter);
216
217 } else {
218 noShares.setVisibility(View.VISIBLE);
219 usersList.setVisibility(View.GONE);
220 }
221 }
222
223 @Override
224 public void unshareButtonPressed(OCShare share) {
225 // Unshare
226 mListener.unshareWith(share);
227 Log_OC.d(TAG, "Unshare - " + share.getSharedWithDisplayName());
228 }
229
230
231 /**
232 * This interface must be implemented by activities that contain this
233 * fragment to allow an interaction in this fragment to be communicated
234 * to the activity and potentially other fragments contained in that
235 * activity.
236 * <p/>
237 * See the Android Training lesson <a href=
238 * "http://developer.android.com/training/basics/fragments/communicating.html"
239 * >Communicating with Other Fragments</a> for more information.
240 */
241 public interface OnShareFragmentInteractionListener {
242 void showSearchUsersAndGroups();
243 void refreshUsersOrGroupsListFromServer();
244 void unshareWith(OCShare share);
245 }
246
247 }