1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
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.
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.
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/>.
18 package eu
.alefzero
.owncloud
.ui
.fragment
;
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
;
38 import com
.actionbarsherlock
.app
.SherlockFragment
;
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
;
46 * This Fragment is used to display the details about a file.
48 * @author Bartek Przybylski
51 public class FileDetailFragment
extends SherlockFragment
implements
54 public static final String EXTRA_FILE
= "FILE";
56 private DownloadFinishReceiver mDownloadFinishReceiver
;
57 private Intent mIntent
;
61 private static final String TAG
= "FileDetailFragment";
64 * Default constructor - contains real layout
66 public FileDetailFragment(){
67 mLayout
= R
.layout
.file_details_fragment
;
71 * Creates a dummy layout. For use if the user never has
72 * tapped on a file before
74 * @param useEmptyView If true, use empty layout
76 public FileDetailFragment(boolean useEmptyView
){
78 mLayout
= R
.layout
.file_details_empty
;
80 mLayout
= R
.layout
.file_details_fragment
;
85 * Use this when creating the fragment and display
86 * a file at the same time
88 * @param showDetailsIntent The Intent with the required parameters
89 * @see FileDetailFragment#updateFileDetails(Intent)
91 public FileDetailFragment(Intent showDetailsIntent
) {
92 mIntent
= showDetailsIntent
;
93 mLayout
= R
.layout
.file_details_fragment
;
97 public void onResume() {
99 mDownloadFinishReceiver
= new DownloadFinishReceiver();
100 IntentFilter filter
= new IntentFilter(
101 FileDownloader
.DOWNLOAD_FINISH_MESSAGE
);
102 getActivity().registerReceiver(mDownloadFinishReceiver
, filter
);
106 public void onPause() {
108 getActivity().unregisterReceiver(mDownloadFinishReceiver
);
109 mDownloadFinishReceiver
= null
;
113 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
114 Bundle savedInstanceState
) {
116 view
= inflater
.inflate(mLayout
, container
, false
);
118 if(mLayout
== R
.layout
.file_details_fragment
){
119 // Phones will launch an activity with this intent
121 mIntent
= getActivity().getIntent();
130 public View
getView() {
131 return super.getView() == null ? mView
: super.getView();
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
);
145 * Can be used to get the file that is currently being displayed.
146 * @return The file on the screen.
148 public OCFile
getDisplayedFile(){
153 * Use this method to signal this Activity that it shall update its view.
155 * @param intent The {@link Intent} that contains extra information about
156 * this file The intent needs to have these extras:
159 * {@link FileDetailFragment#EXTRA_FILE}: An {@link OCFile}
160 * {@link FileDownloader#EXTRA_ACCOUNT}: The Account that file
161 * belongs to (required for downloading)
163 public void updateFileDetails(Intent intent
) {
169 * Updates the view with all relevant details about that file.
171 private void updateFileDetails() {
172 mFile
= mIntent
.getParcelableExtra(EXTRA_FILE
);
173 Button downloadButton
= (Button
) getView().findViewById(R
.id
.fdDownloadBtn
);
177 setFilename(mFile
.getFileName());
178 setFiletype(DisplayUtils
.convertMIMEtoPrettyPrint(mFile
180 setFilesize(mFile
.getFileLength());
181 setTimeCreated(mFile
.getCreationTimestamp());
182 setTimeModified(mFile
.getModificationTimestamp());
185 if (mFile
.getStoragePath() != null
) {
187 if (mFile
.getMimetype().startsWith("image/")) {
188 ImageView preview
= (ImageView
) getView().findViewById(
190 Bitmap bmp
= BitmapFactory
.decodeFile(mFile
.getStoragePath());
191 preview
.setImageBitmap(bmp
);
193 } catch (OutOfMemoryError e
) {
194 Log
.e(TAG
, "Out of memory occured for file with size " + mFile
.getFileLength());
196 downloadButton
.setText(R
.string
.filedetails_open
);
197 downloadButton
.setOnClickListener(new OnClickListener() {
199 public void onClick(View v
) {
200 Intent i
= new Intent(Intent
.ACTION_VIEW
);
201 i
.setDataAndType(Uri
.parse("file://"+mFile
.getStoragePath()), mFile
.getMimetype());
206 // Make download button effective
207 downloadButton
.setOnClickListener(this);
213 * Updates the filename in view
214 * @param filename to set
216 private void setFilename(String filename
) {
217 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdFilename
);
219 tv
.setText(filename
);
223 * Updates the MIME type in view
224 * @param mimetype to set
226 private void setFiletype(String mimetype
) {
227 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdType
);
229 tv
.setText(mimetype
);
233 * Updates the file size in view
234 * @param filesize in bytes to set
236 private void setFilesize(long filesize
) {
237 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdSize
);
239 tv
.setText(DisplayUtils
.bytesToHumanReadable(filesize
));
243 * Updates the time that the file was created in view
244 * @param milliseconds Unix time to set
246 private void setTimeCreated(long milliseconds
){
247 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdCreated
);
249 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
254 * Updates the time that the file was last modified
255 * @param milliseconds Unix time to set
257 private void setTimeModified(long milliseconds
){
258 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdModified
);
260 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
265 * Once the file download has finished -> update view
266 * @author Bartek Przybylski
268 private class DownloadFinishReceiver
extends BroadcastReceiver
{
270 public void onReceive(Context context
, Intent intent
) {