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 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 * Use this method to signal this Activity that it shall update its view.
115 * @param intent The {@link Intent} that contains extra information about
116 * this file The intent needs to have these extras:
119 * {@link FileDetailFragment#FILE}: An {@link OCFile}
120 * {@link FileDownloader#EXTRA_ACCOUNT}: The Account that file
121 * belongs to (required for downloading)
123 public void updateFileDetails(Intent intent
) {
128 private void updateFileDetails() {
129 mFile
= mIntent
.getParcelableExtra(FILE
);
130 Button downloadButton
= (Button
) getView().findViewById(R
.id
.fdDownloadBtn
);
134 setFilename(mFile
.getFileName());
135 setFiletype(DisplayUtils
.convertMIMEtoPrettyPrint(mFile
137 setFilesize(mFile
.getFileLength());
138 setTimeCreated(mFile
.getCreationTimestamp());
139 setTimeModified(mFile
.getModificationTimestamp());
142 if (mFile
.getStoragePath() != null
) {
144 if (mFile
.getMimetype().startsWith("image/")) {
145 ImageView preview
= (ImageView
) getView().findViewById(
147 Bitmap bmp
= BitmapFactory
.decodeFile(mFile
.getStoragePath());
148 preview
.setImageBitmap(bmp
);
150 } catch (OutOfMemoryError e
) {
151 Log
.e(TAG
, "Out of memory occured for file with size " + mFile
.getFileLength());
153 downloadButton
.setText("Open file");
154 downloadButton
.setOnClickListener(new OnClickListener() {
156 public void onClick(View v
) {
157 Intent i
= new Intent(Intent
.ACTION_VIEW
);
158 i
.setDataAndType(Uri
.parse("file://"+mFile
.getStoragePath()), mFile
.getMimetype());
163 // Make download button effective
164 downloadButton
.setOnClickListener(this);
169 private void setFilename(String filename
) {
170 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdFilename
);
172 tv
.setText(filename
);
175 private void setFiletype(String mimetype
) {
176 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdType
);
178 tv
.setText(mimetype
);
181 private void setFilesize(long filesize
) {
182 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdSize
);
184 tv
.setText(DisplayUtils
.bytesToHumanReadable(filesize
));
187 private void setTimeCreated(long milliseconds
){
188 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdCreated
);
190 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
194 private void setTimeModified(long milliseconds
){
195 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdModified
);
197 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
202 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
203 Bundle savedInstanceState
) {
205 view
= inflater
.inflate(mLayout
, container
, false
);
207 if(mLayout
== R
.layout
.file_details_fragment
){
208 // Phones will launch an activity with this intent
210 mIntent
= getActivity().getIntent();
221 public View
getView() {
222 return super.getView() == null ? mView
: super.getView();
226 public void onClick(View v
) {
227 Toast
.makeText(getActivity(), "Downloading", Toast
.LENGTH_LONG
).show();
228 Intent i
= new Intent(getActivity(), FileDownloader
.class);
229 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
,
230 mIntent
.getParcelableExtra(FileDownloader
.EXTRA_ACCOUNT
));
231 i
.putExtra(FileDownloader
.EXTRA_FILE_PATH
, mFile
.getPath());
232 getActivity().startService(i
);
235 private class DownloadFinishReceiver
extends BroadcastReceiver
{
237 public void onReceive(Context context
, Intent intent
) {