Remove share_group_indicator string
[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
38 import com.owncloud.android.R;
39 import com.owncloud.android.datamodel.FileDataStorageManager;
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.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
63 private static final String TAG = ShareFileFragment.class.getSimpleName();
64
65 // the fragment initialization parameters
66 private static final String ARG_FILE = "FILE";
67 private static final String ARG_ACCOUNT = "ACCOUNT";
68
69 // Parameters
70 private OCFile mFile;
71 private Account mAccount;
72
73 private ArrayList<OCShare> mShares;
74 private ShareUserListAdapter mUserGroupsAdapter = null;
75
76 private OnShareFragmentInteractionListener mListener;
77
78 /**
79 * Public factory method to create new ShareFileFragment instances.
80 *
81 * @param fileToShare An {@link OCFile} to show in the fragment
82 * @param account An ownCloud account
83 * @return A new instance of fragment ShareFileFragment.
84 */
85 public static ShareFileFragment newInstance(OCFile fileToShare, Account account) {
86 ShareFileFragment fragment = new ShareFileFragment();
87 Bundle args = new Bundle();
88 args.putParcelable(ARG_FILE, fileToShare);
89 args.putParcelable(ARG_ACCOUNT, account);
90 fragment.setArguments(args);
91 return fragment;
92 }
93
94 public ShareFileFragment() {
95 // Required empty public constructor
96 }
97
98 @Override
99 public void onCreate(Bundle savedInstanceState) {
100 super.onCreate(savedInstanceState);
101 if (getArguments() != null) {
102 mFile = getArguments().getParcelable(ARG_FILE);
103 mAccount = getArguments().getParcelable(ARG_ACCOUNT);
104 }
105 }
106
107 @Override
108 public View onCreateView(LayoutInflater inflater, ViewGroup container,
109 Bundle savedInstanceState) {
110 // Inflate the layout for this fragment
111 View view = inflater.inflate(R.layout.share_file_layout, container, false);
112
113 // Setup layout
114 // Image
115 ImageView icon = (ImageView) view.findViewById(R.id.shareFileIcon);
116 icon.setImageResource(MimetypeIconUtil.getFileTypeIconId(mFile.getMimetype(),
117 mFile.getFileName()));
118 if (mFile.isImage()) {
119 String remoteId = String.valueOf(mFile.getRemoteId());
120 Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(remoteId);
121 if (thumbnail != null) {
122 icon.setImageBitmap(thumbnail);
123 }
124 }
125 // Name
126 TextView filename = (TextView) view.findViewById(R.id.shareFileName);
127 filename.setText(mFile.getFileName());
128 // Size
129 TextView size = (TextView) view.findViewById(R.id.shareFileSize);
130 if (mFile.isFolder()) {
131 size.setVisibility(View.GONE);
132 } else {
133 size.setText(DisplayUtils.bytesToHumanReadable(mFile.getFileLength()));
134 }
135
136 // Add User Button
137 Button addUserGroupButton = (Button)
138 view.findViewById(R.id.addUserButton);
139 addUserGroupButton.setOnClickListener(new View.OnClickListener() {
140 @Override
141 public void onClick(View view) {
142 // Show Search Fragment
143 mListener.showSearchUsersAndGroups();
144 }
145 });
146
147 return view;
148 }
149
150 @Override
151 public void onActivityCreated(Bundle savedInstanceState) {
152 super.onActivityCreated(savedInstanceState);
153
154 refreshUsersOrGroupsList();
155 }
156
157 // TODO: Rename method, update argument and hook method into UI event
158 public void onButtonPressed(Uri uri) {
159 if (mListener != null) {
160 mListener.onShareFragmentInteraction(uri);
161 }
162 }
163
164 @Override
165 public void onAttach(Activity activity) {
166 super.onAttach(activity);
167 try {
168 mListener = (OnShareFragmentInteractionListener) activity;
169 } catch (ClassCastException e) {
170 throw new ClassCastException(activity.toString()
171 + " must implement OnShareFragmentInteractionListener");
172 }
173 }
174
175 @Override
176 public void onDetach() {
177 super.onDetach();
178 mListener = null;
179 }
180
181 // Get users and groups to fill the "share with" list
182 public void refreshUsersOrGroupsList(){
183 mShares = new ArrayList<>();
184
185 // Get Users and Groups
186 FileDataStorageManager fileDataStorageManager =
187 new FileDataStorageManager(mAccount, getActivity().getContentResolver());
188 mShares = fileDataStorageManager.getSharesWithForAFile(mFile.getRemotePath(),
189 mAccount.name);
190
191 // Update list of users/groups
192 updateListOfUserGroups();
193 }
194
195 private void updateListOfUserGroups() {
196 // Update list of users/groups
197 mUserGroupsAdapter = new ShareUserListAdapter(getActivity().getApplicationContext(),
198 R.layout.share_user_item, mShares);
199
200 // Show data
201 TextView noShares = (TextView) getView().findViewById(R.id.shareNoUsers);
202 ListView usersList = (ListView) getView().findViewById(R.id.shareUsersList);
203
204 if (mShares.size() > 0) {
205 noShares.setVisibility(View.GONE);
206 usersList.setVisibility(View.VISIBLE);
207 usersList.setAdapter(mUserGroupsAdapter);
208
209 // Add unshare options
210 registerLongClickListener(usersList);
211
212 } else {
213 noShares.setVisibility(View.VISIBLE);
214 usersList.setVisibility(View.GONE);
215 }
216 }
217
218 private void registerLongClickListener(final ListView listView) {
219 listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
220 @Override
221 public boolean onItemLongClick(AdapterView<?> parent, View view, final int position,
222 long id) {
223 // Show unshare button
224 ImageView unshareButton = (ImageView) view.findViewById(R.id.unshareButton);
225 if (unshareButton.getVisibility() == View.GONE) {
226 unshareButton.setVisibility(View.VISIBLE);
227 unshareButton.setOnClickListener(new View.OnClickListener() {
228 @Override
229 public void onClick(View v) {
230 // Unshare
231 unshareWith(mShares.get(position));
232 Log_OC.d(TAG, "Unshare - " +
233 mShares.get(position).getSharedWithDisplayName());
234 }
235 });
236
237 } else {
238 unshareButton.setVisibility(View.GONE);
239 }
240 view.setAlpha(0);
241 view.animate().alpha(1).setDuration(500).start();
242 return false;
243 }
244 });
245 }
246
247 // Call to Unshare operation
248 private void unshareWith(OCShare share){
249 OCFile file = ((FileActivity) getActivity()).getFile();
250
251 ((FileActivity) getActivity()).getFileOperationsHelper().
252 unshareFileWithUserOrGroup(
253 file, share.getShareType(), share.getShareWith()
254 );
255 }
256
257
258 // TODO: review if it is necessary
259 /**
260 * This interface must be implemented by activities that contain this
261 * fragment to allow an interaction in this fragment to be communicated
262 * to the activity and potentially other fragments contained in that
263 * activity.
264 * <p/>
265 * See the Android Training lesson <a href=
266 * "http://developer.android.com/training/basics/fragments/communicating.html"
267 * >Communicating with Other Fragments</a> for more information.
268 */
269 public interface OnShareFragmentInteractionListener {
270 void showSearchUsersAndGroups();
271
272 void onShareFragmentInteraction(Uri uri);
273 }
274
275 }