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
.os
.Bundle
;
27 import android
.view
.LayoutInflater
;
28 import android
.view
.View
;
29 import android
.view
.View
.OnClickListener
;
30 import android
.view
.ViewGroup
;
31 import android
.widget
.Button
;
32 import android
.widget
.ImageView
;
33 import android
.widget
.TextView
;
34 import android
.widget
.Toast
;
36 import com
.actionbarsherlock
.app
.SherlockFragment
;
38 import eu
.alefzero
.owncloud
.DisplayUtils
;
39 import eu
.alefzero
.owncloud
.FileDownloader
;
40 import eu
.alefzero
.owncloud
.R
;
41 import eu
.alefzero
.owncloud
.datamodel
.OCFile
;
44 * This Fragment is used to display the details about a file.
46 * @author Bartek Przybylski
49 public class FileDetailFragment
extends SherlockFragment
implements
52 public static final String FILE
= "FILE";
54 private DownloadFinishReceiver mDownloadFinishReceiver
;
55 private Intent mIntent
;
61 * Default constructor - contains real layout
63 public FileDetailFragment(){
64 mLayout
= R
.layout
.file_details_fragment
;
68 * Creates a dummy layout. For use if the user never has
69 * tapped on a file before
71 * @param useEmptyView If true, use empty layout
73 public FileDetailFragment(boolean useEmptyView
){
75 mLayout
= R
.layout
.file_details_empty
;
77 mLayout
= R
.layout
.file_details_fragment
;
82 * Use this when creating the fragment and display
83 * a file at the same time
85 * @param showDetailsIntent The Intent with the required parameters
86 * @see FileDetailFragment#updateFileDetails(Intent)
88 public FileDetailFragment(Intent showDetailsIntent
) {
89 mIntent
= showDetailsIntent
;
90 mLayout
= R
.layout
.file_details_fragment
;
94 public void onResume() {
96 mDownloadFinishReceiver
= new DownloadFinishReceiver();
97 IntentFilter filter
= new IntentFilter(
98 FileDownloader
.DOWNLOAD_FINISH_MESSAGE
);
99 getActivity().registerReceiver(mDownloadFinishReceiver
, filter
);
103 public void onPause() {
105 getActivity().unregisterReceiver(mDownloadFinishReceiver
);
106 mDownloadFinishReceiver
= null
;
110 * Use this method to signal this Activity that it shall update its view.
112 * @param intent The {@link Intent} that contains extra information about
113 * this file The intent needs to have these extras:
116 * {@link FileDetailFragment#FILE}: An {@link OCFile}
117 * {@link FileDownloader#EXTRA_ACCOUNT}: The Account that file
118 * belongs to (required for downloading)
120 public void updateFileDetails(Intent intent
) {
125 private void updateFileDetails() {
126 mFile
= mIntent
.getParcelableExtra(FILE
);
130 setFilename(mFile
.getFileName());
131 setFiletype(DisplayUtils
.convertMIMEtoPrettyPrint(mFile
133 setFilesize(mFile
.getFileLength());
136 if (mFile
.getStoragePath() != null
) {
137 if (mFile
.getMimetype().startsWith("image/")) {
138 ImageView preview
= (ImageView
) getView().findViewById(
140 Bitmap bmp
= BitmapFactory
.decodeFile(mFile
.getStoragePath());
141 preview
.setImageBitmap(bmp
);
145 // Make download button effective
146 Button downloadButton
= (Button
) getView().findViewById(R
.id
.fdDownloadBtn
);
147 downloadButton
.setOnClickListener(this);
151 private void setFilename(String filename
) {
152 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdFilename
);
154 tv
.setText(filename
);
157 private void setFiletype(String mimetype
) {
158 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdType
);
160 tv
.setText(mimetype
);
163 private void setFilesize(long filesize
) {
164 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdSize
);
166 tv
.setText(DisplayUtils
.bitsToHumanReadable(filesize
));
170 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
171 Bundle savedInstanceState
) {
173 view
= inflater
.inflate(mLayout
, container
, false
);
175 if(mLayout
== R
.layout
.file_details_fragment
){
176 // Phones will launch an activity with this intent
178 mIntent
= getActivity().getIntent();
189 public View
getView() {
190 return super.getView() == null ? mView
: super.getView();
194 public void onClick(View v
) {
195 Toast
.makeText(getActivity(), "Downloading", Toast
.LENGTH_LONG
).show();
196 Intent i
= new Intent(getActivity(), FileDownloader
.class);
197 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
,
198 mIntent
.getParcelableExtra(FileDownloader
.EXTRA_ACCOUNT
));
199 i
.putExtra(FileDownloader
.EXTRA_FILE_PATH
, mFile
.getPath());
200 getActivity().startService(i
);
203 private class DownloadFinishReceiver
extends BroadcastReceiver
{
205 public void onReceive(Context context
, Intent intent
) {