Implemented equals() and toString() on OCFile. Renamed getPath() to make
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / fragment / FileDetailFragment.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18 package eu.alefzero.owncloud.ui.fragment;
19
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.graphics.Bitmap;
25 import android.graphics.BitmapFactory;
26 import android.net.Uri;
27 import android.os.Bundle;
28 import android.util.Log;
29 import android.view.LayoutInflater;
30 import android.view.View;
31 import android.view.View.OnClickListener;
32 import android.view.ViewGroup;
33 import android.widget.Button;
34 import android.widget.ImageView;
35 import android.widget.TextView;
36 import android.widget.Toast;
37
38 import com.actionbarsherlock.app.SherlockFragment;
39
40 import eu.alefzero.owncloud.DisplayUtils;
41 import eu.alefzero.owncloud.FileDownloader;
42 import eu.alefzero.owncloud.R;
43 import eu.alefzero.owncloud.datamodel.OCFile;
44
45 /**
46 * This Fragment is used to display the details about a file.
47 *
48 * @author Bartek Przybylski
49 *
50 */
51 public class FileDetailFragment extends SherlockFragment implements
52 OnClickListener {
53
54 public static final String EXTRA_FILE = "FILE";
55
56 private DownloadFinishReceiver mDownloadFinishReceiver;
57 private Intent mIntent;
58 private int mLayout;
59 private View mView;
60 private OCFile mFile;
61 private static final String TAG = "FileDetailFragment";
62
63 /**
64 * Default constructor - contains real layout
65 */
66 public FileDetailFragment(){
67 mLayout = R.layout.file_details_fragment;
68 }
69
70 /**
71 * Creates a dummy layout. For use if the user never has
72 * tapped on a file before
73 *
74 * @param useEmptyView If true, use empty layout
75 */
76 public FileDetailFragment(boolean useEmptyView){
77 if(useEmptyView){
78 mLayout = R.layout.file_details_empty;
79 } else {
80 mLayout = R.layout.file_details_fragment;
81 }
82 }
83
84 /**
85 * Use this when creating the fragment and display
86 * a file at the same time
87 *
88 * @param showDetailsIntent The Intent with the required parameters
89 * @see FileDetailFragment#updateFileDetails(Intent)
90 */
91 public FileDetailFragment(Intent showDetailsIntent) {
92 mIntent = showDetailsIntent;
93 mLayout = R.layout.file_details_fragment;
94 }
95
96 @Override
97 public void onResume() {
98 super.onResume();
99 mDownloadFinishReceiver = new DownloadFinishReceiver();
100 IntentFilter filter = new IntentFilter(
101 FileDownloader.DOWNLOAD_FINISH_MESSAGE);
102 getActivity().registerReceiver(mDownloadFinishReceiver, filter);
103 }
104
105 @Override
106 public void onPause() {
107 super.onPause();
108 getActivity().unregisterReceiver(mDownloadFinishReceiver);
109 mDownloadFinishReceiver = null;
110 }
111
112 @Override
113 public View onCreateView(LayoutInflater inflater, ViewGroup container,
114 Bundle savedInstanceState) {
115 View view = null;
116 view = inflater.inflate(mLayout, container, false);
117 mView = view;
118 if(mLayout == R.layout.file_details_fragment){
119 // Phones will launch an activity with this intent
120 if(mIntent == null){
121 mIntent = getActivity().getIntent();
122 }
123 updateFileDetails();
124 }
125
126 return view;
127 }
128
129 @Override
130 public View getView() {
131 return super.getView() == null ? mView : super.getView();
132 }
133
134 @Override
135 public void onClick(View v) {
136 Toast.makeText(getActivity(), "Downloading", Toast.LENGTH_LONG).show();
137 Intent i = new Intent(getActivity(), FileDownloader.class);
138 i.putExtra(FileDownloader.EXTRA_ACCOUNT,
139 mIntent.getParcelableExtra(FileDownloader.EXTRA_ACCOUNT));
140 i.putExtra(FileDownloader.EXTRA_FILE_PATH, mFile.getRemotePath());
141 getActivity().startService(i);
142 }
143
144 /**
145 * Can be used to get the file that is currently being displayed.
146 * @return The file on the screen.
147 */
148 public OCFile getDisplayedFile(){
149 return mFile;
150 }
151
152 /**
153 * Use this method to signal this Activity that it shall update its view.
154 *
155 * @param intent The {@link Intent} that contains extra information about
156 * this file The intent needs to have these extras:
157 * <p>
158 *
159 * {@link FileDetailFragment#EXTRA_FILE}: An {@link OCFile}
160 * {@link FileDownloader#EXTRA_ACCOUNT}: The Account that file
161 * belongs to (required for downloading)
162 */
163 public void updateFileDetails(Intent intent) {
164 mIntent = intent;
165 updateFileDetails();
166 }
167
168 /**
169 * Updates the view with all relevant details about that file.
170 */
171 private void updateFileDetails() {
172 mFile = mIntent.getParcelableExtra(EXTRA_FILE);
173 Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);
174
175 if (mFile != null) {
176 // set file details
177 setFilename(mFile.getFileName());
178 setFiletype(DisplayUtils.convertMIMEtoPrettyPrint(mFile
179 .getMimetype()));
180 setFilesize(mFile.getFileLength());
181 setTimeCreated(mFile.getCreationTimestamp());
182 setTimeModified(mFile.getModificationTimestamp());
183
184 // Update preview
185 if (mFile.getStoragePath() != null) {
186 try {
187 if (mFile.getMimetype().startsWith("image/")) {
188 ImageView preview = (ImageView) getView().findViewById(
189 R.id.fdPreview);
190 Bitmap bmp = BitmapFactory.decodeFile(mFile.getStoragePath());
191 preview.setImageBitmap(bmp);
192 }
193 } catch (OutOfMemoryError e) {
194 Log.e(TAG, "Out of memory occured for file with size " + mFile.getFileLength());
195 }
196 downloadButton.setText(R.string.filedetails_open);
197 downloadButton.setOnClickListener(new OnClickListener() {
198 @Override
199 public void onClick(View v) {
200 Intent i = new Intent(Intent.ACTION_VIEW);
201 i.setDataAndType(Uri.parse("file://"+mFile.getStoragePath()), mFile.getMimetype());
202 startActivity(i);
203 }
204 });
205 } else {
206 // Make download button effective
207 downloadButton.setOnClickListener(this);
208 }
209 }
210 }
211
212 /**
213 * Updates the filename in view
214 * @param filename to set
215 */
216 private void setFilename(String filename) {
217 TextView tv = (TextView) getView().findViewById(R.id.fdFilename);
218 if (tv != null)
219 tv.setText(filename);
220 }
221
222 /**
223 * Updates the MIME type in view
224 * @param mimetype to set
225 */
226 private void setFiletype(String mimetype) {
227 TextView tv = (TextView) getView().findViewById(R.id.fdType);
228 if (tv != null)
229 tv.setText(mimetype);
230 }
231
232 /**
233 * Updates the file size in view
234 * @param filesize in bytes to set
235 */
236 private void setFilesize(long filesize) {
237 TextView tv = (TextView) getView().findViewById(R.id.fdSize);
238 if (tv != null)
239 tv.setText(DisplayUtils.bytesToHumanReadable(filesize));
240 }
241
242 /**
243 * Updates the time that the file was created in view
244 * @param milliseconds Unix time to set
245 */
246 private void setTimeCreated(long milliseconds){
247 TextView tv = (TextView) getView().findViewById(R.id.fdCreated);
248 if(tv != null){
249 tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));
250 }
251 }
252
253 /**
254 * Updates the time that the file was last modified
255 * @param milliseconds Unix time to set
256 */
257 private void setTimeModified(long milliseconds){
258 TextView tv = (TextView) getView().findViewById(R.id.fdModified);
259 if(tv != null){
260 tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));
261 }
262 }
263
264 /**
265 * Once the file download has finished -> update view
266 * @author Bartek Przybylski
267 */
268 private class DownloadFinishReceiver extends BroadcastReceiver {
269 @Override
270 public void onReceive(Context context, Intent intent) {
271 updateFileDetails();
272 }
273
274 }
275
276 }