Merge pull request #1119 from owncloud/strip-index-php-from-server-url
[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.FileActivity;
45 import com.owncloud.android.ui.activity.ShareActivity;
46 import com.owncloud.android.ui.adapter.ShareUserListAdapter;
47 import com.owncloud.android.utils.DisplayUtils;
48 import com.owncloud.android.utils.MimetypeIconUtil;
49
50 import java.util.ArrayList;
51
52 /**
53 * Fragment for Sharing a file with sharees (users or groups)
54 *
55 * A simple {@link Fragment} subclass.
56 *
57 * Activities that contain this fragment must implement the
58 * {@link ShareFileFragment.OnShareFragmentInteractionListener} interface
59 * to handle interaction events.
60 *
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 ShareUserListAdapter.ShareUserAdapterListener{
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 // other members
78 private ArrayList<OCShare> mShares;
79 private ShareUserListAdapter mUserGroupsAdapter = null;
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 /**
103 * {@inheritDoc}
104 */
105 @Override
106 public void onCreate(Bundle savedInstanceState) {
107 super.onCreate(savedInstanceState);
108 if (getArguments() != null) {
109 mFile = getArguments().getParcelable(ARG_FILE);
110 mAccount = getArguments().getParcelable(ARG_ACCOUNT);
111 }
112 }
113
114 /**
115 * {@inheritDoc}
116 */
117 @Override
118 public View onCreateView(LayoutInflater inflater, ViewGroup container,
119 Bundle savedInstanceState) {
120 // Inflate the layout for this fragment
121 View view = inflater.inflate(R.layout.share_file_layout, container, false);
122
123 // Setup layout
124 // Image
125 ImageView icon = (ImageView) view.findViewById(R.id.shareFileIcon);
126 icon.setImageResource(MimetypeIconUtil.getFileTypeIconId(mFile.getMimetype(),
127 mFile.getFileName()));
128 if (mFile.isImage()) {
129 String remoteId = String.valueOf(mFile.getRemoteId());
130 Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(remoteId);
131 if (thumbnail != null) {
132 icon.setImageBitmap(thumbnail);
133 }
134 }
135 // Name
136 TextView filename = (TextView) view.findViewById(R.id.shareFileName);
137 filename.setText(mFile.getFileName());
138 // Size
139 TextView size = (TextView) view.findViewById(R.id.shareFileSize);
140 if (mFile.isFolder()) {
141 size.setVisibility(View.GONE);
142 } else {
143 size.setText(DisplayUtils.bytesToHumanReadable(mFile.getFileLength()));
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 boolean shareWithUsersEnable = AccountUtils.hasSearchUsersSupport(mAccount);
153 if (shareWithUsersEnable) {
154 // Show Search Fragment
155 mListener.showSearchUsersAndGroups();
156 } else {
157 String message = getString(R.string.share_sharee_unavailable);
158 Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
159 }
160 }
161 });
162
163 return view;
164 }
165
166 @Override
167 public void onActivityCreated(Bundle savedInstanceState) {
168 super.onActivityCreated(savedInstanceState);
169
170 // Load data into the list
171 refreshUsersOrGroupsListFromDB();
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 /**
192 * Get users and groups from the DB to fill in the "share with" list
193 *
194 * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
195 * instance ready to use. If not ready, does nothing.
196 */
197 public void refreshUsersOrGroupsListFromDB (){
198 if (((FileActivity) mListener).getStorageManager() != null) {
199 // Get Users and Groups
200 mShares = ((FileActivity) mListener).getStorageManager().getSharesWithForAFile(
201 mFile.getRemotePath(),
202 mAccount.name
203 );
204
205 // Update list of users/groups
206 updateListOfUserGroups();
207 }
208 }
209
210 private void updateListOfUserGroups() {
211 // Update list of users/groups
212 // TODO Refactoring: create a new {@link ShareUserListAdapter} instance with every call should not be needed
213 mUserGroupsAdapter = new ShareUserListAdapter(
214 getActivity(),
215 R.layout.share_user_item,
216 mShares,
217 this
218 );
219
220 // Show data
221 TextView noShares = (TextView) getView().findViewById(R.id.shareNoUsers);
222 ListView usersList = (ListView) getView().findViewById(R.id.shareUsersList);
223
224 if (mShares.size() > 0) {
225 noShares.setVisibility(View.GONE);
226 usersList.setVisibility(View.VISIBLE);
227 usersList.setAdapter(mUserGroupsAdapter);
228
229 } else {
230 noShares.setVisibility(View.VISIBLE);
231 usersList.setVisibility(View.GONE);
232 }
233 }
234
235 @Override
236 public void unshareButtonPressed(OCShare share) {
237 // Unshare
238 mListener.unshareWith(share);
239 Log_OC.d(TAG, "Unshare - " + share.getSharedWithDisplayName());
240 }
241
242
243 /**
244 * This interface must be implemented by activities that contain this
245 * fragment to allow an interaction in this fragment to be communicated
246 * to the activity and potentially other fragments contained in that
247 * activity.
248 * <p/>
249 * See the Android Training lesson <a href=
250 * "http://developer.android.com/training/basics/fragments/communicating.html"
251 * >Communicating with Other Fragments</a> for more information.
252 */
253 public interface OnShareFragmentInteractionListener {
254 void showSearchUsersAndGroups();
255 void refreshUsersOrGroupsListFromServer();
256 void unshareWith(OCShare share);
257 }
258
259 }