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