b780e78256df5a9e9a184740b66d8738ce0175e6
[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.design.widget.FloatingActionButton;
29 import android.support.v4.app.Fragment;
30 import android.view.LayoutInflater;
31 import android.view.View;
32 import android.view.ViewGroup;
33 import android.widget.ImageView;
34 import android.widget.TextView;
35
36 import com.owncloud.android.R;
37 import com.owncloud.android.datamodel.OCFile;
38 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
39 import com.owncloud.android.utils.DisplayUtils;
40 import com.owncloud.android.utils.MimetypeIconUtil;
41
42 /**
43 * Fragment for Sharing a file with users
44 *
45 * A simple {@link Fragment} subclass.
46 * Activities that contain this fragment must implement the
47 * {@link ShareFileFragment.OnShareFragmentInteractionListener} interface
48 * to handle interaction events.
49 * Use the {@link ShareFileFragment#newInstance} factory method to
50 * create an instance of this fragment.
51 */
52 public class ShareFileFragment extends Fragment {
53 private static final String TAG = ShareFileFragment.class.getSimpleName();
54
55 // the fragment initialization parameters
56 private static final String ARG_FILE = "FILE";
57 private static final String ARG_ACCOUNT = "ACCOUNT";
58
59 // Parameters
60 private OCFile mFile;
61 private Account mAccount;
62
63 private OnShareFragmentInteractionListener mListener;
64
65 /**
66 * Public factory method to create new ShareFileFragment instances.
67 *
68 * @param fileToShare An {@link OCFile} to show in the fragment
69 * @param account An ownCloud account
70 * @return A new instance of fragment ShareFileFragment.
71 */
72 public static ShareFileFragment newInstance(OCFile fileToShare, Account account) {
73 ShareFileFragment fragment = new ShareFileFragment();
74 Bundle args = new Bundle();
75 args.putParcelable(ARG_FILE, fileToShare);
76 args.putParcelable(ARG_ACCOUNT, account);
77 fragment.setArguments(args);
78 return fragment;
79 }
80
81 public ShareFileFragment() {
82 // Required empty public constructor
83 }
84
85 @Override
86 public void onCreate(Bundle savedInstanceState) {
87 super.onCreate(savedInstanceState);
88 if (getArguments() != null) {
89 mFile = getArguments().getParcelable(ARG_FILE);
90 mAccount = getArguments().getParcelable(ARG_ACCOUNT);
91 }
92 }
93
94 @Override
95 public View onCreateView(LayoutInflater inflater, ViewGroup container,
96 Bundle savedInstanceState) {
97 // Inflate the layout for this fragment
98 View view = inflater.inflate(R.layout.share_file_layout, container, false);
99
100 // Setup layout
101 // Image
102 ImageView icon = (ImageView) view.findViewById(R.id.shareFileIcon);
103 icon.setImageResource(MimetypeIconUtil.getFileTypeIconId(mFile.getMimetype(),
104 mFile.getFileName()));
105 if (mFile.isImage()) {
106 String remoteId = String.valueOf(mFile.getRemoteId());
107 Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(remoteId);
108 if (thumbnail != null){
109 icon.setImageBitmap(thumbnail);
110 }
111 }
112 // Name
113 TextView filename = (TextView) view.findViewById(R.id.shareFileName);
114 filename.setText(mFile.getFileName());
115 // Size
116 TextView size = (TextView) view.findViewById(R.id.shareFileSize);
117 if (mFile.isFolder()){
118 size.setVisibility(View.GONE);
119 } else {
120 size.setText(DisplayUtils.bytesToHumanReadable(mFile.getFileLength()));
121 }
122
123 // Add User Button
124 FloatingActionButton addUserGroupButton = (FloatingActionButton)
125 view.findViewById(R.id.addUserButton);
126 addUserGroupButton.setOnClickListener(new View.OnClickListener() {
127 @Override
128 public void onClick(View view) {
129 // Show Search Fragment
130 mListener.showSearchUsersAndGroups();
131 }
132 });
133
134 return view;
135 }
136
137 // TODO: Rename method, update argument and hook method into UI event
138 public void onButtonPressed(Uri uri) {
139 if (mListener != null) {
140 mListener.onShareFragmentInteraction(uri);
141 }
142 }
143
144 @Override
145 public void onAttach(Activity activity) {
146 super.onAttach(activity);
147 try {
148 mListener = (OnShareFragmentInteractionListener) activity;
149 } catch (ClassCastException e) {
150 throw new ClassCastException(activity.toString()
151 + " must implement OnShareFragmentInteractionListener");
152 }
153 }
154
155 @Override
156 public void onDetach() {
157 super.onDetach();
158 mListener = null;
159 }
160
161 // TODO: review if it is necessary
162 /**
163 * This interface must be implemented by activities that contain this
164 * fragment to allow an interaction in this fragment to be communicated
165 * to the activity and potentially other fragments contained in that
166 * activity.
167 * <p/>
168 * See the Android Training lesson <a href=
169 * "http://developer.android.com/training/basics/fragments/communicating.html"
170 * >Communicating with Other Fragments</a> for more information.
171 */
172 public interface OnShareFragmentInteractionListener {
173 public void showSearchUsersAndGroups();
174
175 public void onShareFragmentInteraction(Uri uri);
176 }
177
178 }