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
.app
.FragmentTransaction
;
21 import android
.content
.BroadcastReceiver
;
22 import android
.content
.Context
;
23 import android
.content
.Intent
;
24 import android
.content
.IntentFilter
;
25 import android
.graphics
.Bitmap
;
26 import android
.graphics
.BitmapFactory
;
27 import android
.os
.Bundle
;
28 import android
.view
.LayoutInflater
;
29 import android
.view
.View
;
30 import android
.view
.View
.OnClickListener
;
31 import android
.view
.ViewGroup
;
32 import android
.widget
.ImageView
;
33 import android
.widget
.TextView
;
34 import android
.widget
.Toast
;
35 import android
.widget
.VideoView
;
37 import com
.actionbarsherlock
.app
.SherlockFragment
;
39 import eu
.alefzero
.owncloud
.DisplayUtils
;
40 import eu
.alefzero
.owncloud
.FileDownloader
;
41 import eu
.alefzero
.owncloud
.R
;
42 import eu
.alefzero
.owncloud
.datamodel
.OCFile
;
45 * This Fragment is used to display the details about a file.
47 * @author Bartek Przybylski
50 public class FileDetailFragment
extends SherlockFragment
implements
53 public static final String FILE
= "FILE";
55 private Intent mIntent
;
57 private DownloadFinishReceiver mDownloadFinishReceiver
;
61 private boolean mEmptyLayout
;
64 * Default constructor. When inflated by android -> display empty layout
66 public FileDetailFragment() {
67 mLayout
= R
.layout
.file_details_empty
;
72 * Custom construtor. Use with a {@link FragmentTransaction}. The intent has
73 * to contain {@link FileDetailFragment#FILE} with an OCFile and also
74 * {@link FileDownloader#EXTRA_ACCOUNT} with the account.
76 * @param nonEmptyFragment True, to enable file detail rendering
78 public FileDetailFragment(Intent intent
) {
79 mLayout
= R
.layout
.file_details_fragment
;
85 public void onResume() {
87 mDownloadFinishReceiver
= new DownloadFinishReceiver();
88 IntentFilter filter
= new IntentFilter(
89 FileDownloader
.DOWNLOAD_FINISH_MESSAGE
);
90 getActivity().registerReceiver(mDownloadFinishReceiver
, filter
);
94 public void onPause() {
96 getActivity().unregisterReceiver(mDownloadFinishReceiver
);
97 mDownloadFinishReceiver
= null
;
101 * Use this method to signal this Activity that it shall update its view.
103 * @param intent The {@link Intent} that contains extra information about
104 * this file The intent needs to have these extras:
107 * {@link FileDetailFragment#FILE}: An {@link OCFile}
108 * {@link FileDownloader#EXTRA_ACCOUNT}: The Account that file
109 * belongs to (required for downloading)
111 public void updateFileDetails(Intent intent
) {
116 private void updateFileDetails() {
117 mFile
= mIntent
.getParcelableExtra(FILE
);
121 setFilename(mFile
.getFileName());
122 setFiletype(DisplayUtils
.convertMIMEtoPrettyPrint(mFile
124 setFilesize(mFile
.getFileLength());
126 // set file preview if available and possible
127 VideoView videoView
= (VideoView
) mView
128 .findViewById(R
.id
.videoView1
);
129 videoView
.setVisibility(View
.INVISIBLE
);
130 if (mFile
.getStoragePath() == null
) {
131 ImageView imageView
= (ImageView
) getView().findViewById(
133 imageView
.setImageResource(R
.drawable
.download
);
134 imageView
.setOnClickListener(this);
136 if (mFile
.getMimetype().startsWith("image/")) {
137 ImageView imageView
= (ImageView
) mView
138 .findViewById(R
.id
.imageView2
);
139 Bitmap bmp
= BitmapFactory
.decodeFile(mFile
.getStoragePath());
140 imageView
.setImageBitmap(bmp
);
141 } else if (mFile
.getMimetype().startsWith("video/")) {
142 videoView
.setVisibility(View
.VISIBLE
);
143 videoView
.setVideoPath(mFile
.getStoragePath());
151 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
152 Bundle savedInstanceState
) {
155 view
= inflater
.inflate(mLayout
, container
, false
);
156 mIntent
= getActivity().getIntent();
159 // make sure we are not using the empty layout
160 if (mEmptyLayout
== false
) {
168 public View
getView() {
169 return mView
== null ?
super.getView() : mView
;
172 private void setFilename(String filename
) {
173 TextView tv
= (TextView
) getView().findViewById(R
.id
.textView1
);
175 tv
.setText(filename
);
178 private void setFiletype(String mimetype
) {
179 TextView tv
= (TextView
) getView().findViewById(R
.id
.textView2
);
181 tv
.setText(mimetype
);
184 private void setFilesize(long filesize
) {
185 TextView tv
= (TextView
) getView().findViewById(R
.id
.textView3
);
187 tv
.setText(DisplayUtils
.bitsToHumanReadable(filesize
));
191 * Use this to check if the correct layout is loaded. When android
192 * instanciates this class using the default constructor, the layout will be
195 * Once a user touches a file for the first time, you must instanciate a new
196 * Fragment with the new FileDetailFragment(true) to inflate the actual
199 * @return If the layout is empty, this method will return true, otherwise
202 public boolean isEmptyLayout() {
207 public void onClick(View v
) {
208 Toast
.makeText(getActivity(), "Downloading", Toast
.LENGTH_LONG
).show();
209 Intent i
= new Intent(getActivity(), FileDownloader
.class);
210 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
,
211 mIntent
.getParcelableExtra(FileDownloader
.EXTRA_ACCOUNT
));
212 i
.putExtra(FileDownloader
.EXTRA_FILE_PATH
,
213 mIntent
.getStringExtra(FileDownloader
.EXTRA_FILE_PATH
));
214 getActivity().startService(i
);
217 private class DownloadFinishReceiver
extends BroadcastReceiver
{
219 public void onReceive(Context context
, Intent intent
) {