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 java
.util
.List
;
22 import android
.accounts
.Account
;
23 import android
.accounts
.AccountManager
;
24 import android
.content
.BroadcastReceiver
;
25 import android
.content
.Context
;
26 import android
.content
.Intent
;
27 import android
.content
.IntentFilter
;
28 import android
.content
.pm
.PackageManager
;
29 import android
.graphics
.Bitmap
;
30 import android
.graphics
.BitmapFactory
;
31 import android
.net
.Uri
;
32 import android
.os
.Bundle
;
33 import android
.util
.Log
;
34 import android
.view
.LayoutInflater
;
35 import android
.view
.View
;
36 import android
.view
.View
.OnClickListener
;
37 import android
.view
.ViewGroup
;
38 import android
.widget
.Button
;
39 import android
.widget
.ImageView
;
40 import android
.widget
.TextView
;
41 import android
.widget
.Toast
;
43 import com
.actionbarsherlock
.app
.SherlockFragment
;
45 import eu
.alefzero
.owncloud
.DisplayUtils
;
46 import eu
.alefzero
.owncloud
.R
;
47 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
48 import eu
.alefzero
.owncloud
.datamodel
.OCFile
;
49 import eu
.alefzero
.owncloud
.files
.services
.FileDownloader
;
50 import eu
.alefzero
.owncloud
.utils
.OwnCloudVersion
;
53 * This Fragment is used to display the details about a file.
55 * @author Bartek Przybylski
58 public class FileDetailFragment
extends SherlockFragment
implements
61 public static final String EXTRA_FILE
= "FILE";
63 private DownloadFinishReceiver mDownloadFinishReceiver
;
64 private Intent mIntent
;
68 private static final String TAG
= "FileDetailFragment";
71 * Default constructor - contains real layout
73 public FileDetailFragment(){
74 mLayout
= R
.layout
.file_details_fragment
;
78 * Creates a dummy layout. For use if the user never has
79 * tapped on a file before
81 * @param useEmptyView If true, use empty layout
83 public FileDetailFragment(boolean useEmptyView
){
85 mLayout
= R
.layout
.file_details_empty
;
87 mLayout
= R
.layout
.file_details_fragment
;
92 * Use this when creating the fragment and display
93 * a file at the same time
95 * @param showDetailsIntent The Intent with the required parameters
96 * @see FileDetailFragment#updateFileDetails(Intent)
98 public FileDetailFragment(Intent showDetailsIntent
) {
99 mIntent
= showDetailsIntent
;
100 mLayout
= R
.layout
.file_details_fragment
;
104 public void onResume() {
106 mDownloadFinishReceiver
= new DownloadFinishReceiver();
107 IntentFilter filter
= new IntentFilter(
108 FileDownloader
.DOWNLOAD_FINISH_MESSAGE
);
109 getActivity().registerReceiver(mDownloadFinishReceiver
, filter
);
113 public void onPause() {
115 getActivity().unregisterReceiver(mDownloadFinishReceiver
);
116 mDownloadFinishReceiver
= null
;
120 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
121 Bundle savedInstanceState
) {
123 view
= inflater
.inflate(mLayout
, container
, false
);
125 if(mLayout
== R
.layout
.file_details_fragment
){
126 // Phones will launch an activity with this intent
128 mIntent
= getActivity().getIntent();
137 public View
getView() {
138 return super.getView() == null ? mView
: super.getView();
142 public void onClick(View v
) {
143 Toast
.makeText(getActivity(), "Downloading", Toast
.LENGTH_LONG
).show();
144 Intent i
= new Intent(getActivity(), FileDownloader
.class);
145 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
,
146 mIntent
.getParcelableExtra(FileDownloader
.EXTRA_ACCOUNT
));
147 i
.putExtra(FileDownloader
.EXTRA_FILE_PATH
, mFile
.getRemotePath());
148 i
.putExtra(FileDownloader
.EXTRA_FILE_SIZE
, mFile
.getFileLength());
149 getActivity().startService(i
);
153 * Can be used to get the file that is currently being displayed.
154 * @return The file on the screen.
156 public OCFile
getDisplayedFile(){
161 * Use this method to signal this Activity that it shall update its view.
163 * @param intent The {@link Intent} that contains extra information about
164 * this file The intent needs to have these extras:
167 * {@link FileDetailFragment#EXTRA_FILE}: An {@link OCFile}
168 * {@link FileDownloader#EXTRA_ACCOUNT}: The Account that file
169 * belongs to (required for downloading)
171 public void updateFileDetails(Intent intent
) {
177 * Updates the view with all relevant details about that file.
179 private void updateFileDetails() {
180 mFile
= mIntent
.getParcelableExtra(EXTRA_FILE
);
181 Button downloadButton
= (Button
) getView().findViewById(R
.id
.fdDownloadBtn
);
185 setFilename(mFile
.getFileName());
186 setFiletype(DisplayUtils
.convertMIMEtoPrettyPrint(mFile
188 setFilesize(mFile
.getFileLength());
189 if(ocVersionSupportsTimeCreated()){
190 setTimeCreated(mFile
.getCreationTimestamp());
193 setTimeModified(mFile
.getModificationTimestamp());
196 if (mFile
.getStoragePath() != null
) {
198 if (mFile
.getMimetype().startsWith("image/")) {
199 ImageView preview
= (ImageView
) getView().findViewById(
201 Bitmap bmp
= BitmapFactory
.decodeFile(mFile
.getStoragePath());
202 preview
.setImageBitmap(bmp
);
204 } catch (OutOfMemoryError e
) {
205 Log
.e(TAG
, "Out of memory occured for file with size " + mFile
.getFileLength());
207 downloadButton
.setText(R
.string
.filedetails_open
);
208 downloadButton
.setOnClickListener(new OnClickListener() {
210 public void onClick(View v
) {
211 Intent i
= new Intent(Intent
.ACTION_VIEW
);
212 i
.setDataAndType(Uri
.parse("file://"+mFile
.getStoragePath()), mFile
.getMimetype());
213 List list
= getActivity().getPackageManager().queryIntentActivities(i
, PackageManager
.MATCH_DEFAULT_ONLY
);
214 if (list
.size() > 0) {
217 Toast
.makeText(getActivity(), "There is no application to handle file " + mFile
.getFileName(), Toast
.LENGTH_SHORT
).show();
222 // Make download button effective
223 downloadButton
.setOnClickListener(this);
229 * Updates the filename in view
230 * @param filename to set
232 private void setFilename(String filename
) {
233 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdFilename
);
235 tv
.setText(filename
);
239 * Updates the MIME type in view
240 * @param mimetype to set
242 private void setFiletype(String mimetype
) {
243 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdType
);
245 tv
.setText(mimetype
);
249 * Updates the file size in view
250 * @param filesize in bytes to set
252 private void setFilesize(long filesize
) {
253 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdSize
);
255 tv
.setText(DisplayUtils
.bytesToHumanReadable(filesize
));
259 * Updates the time that the file was created in view
260 * @param milliseconds Unix time to set
262 private void setTimeCreated(long milliseconds
){
263 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdCreated
);
264 TextView tvLabel
= (TextView
) getView().findViewById(R
.id
.fdCreatedLabel
);
266 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
267 tv
.setVisibility(View
.VISIBLE
);
268 tvLabel
.setVisibility(View
.VISIBLE
);
273 * Updates the time that the file was last modified
274 * @param milliseconds Unix time to set
276 private void setTimeModified(long milliseconds
){
277 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdModified
);
279 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
284 * In ownCloud 3.0.3 and 4.0.0 there is a bug that SabreDAV does not return
285 * the time that the file was created. There is a chance that this will
286 * be fixed in future versions. Use this method to check if this version of
287 * ownCloud has this fix.
288 * @return True, if ownCloud the ownCloud version is > 3.0.4 and 4.0.1
290 private boolean ocVersionSupportsTimeCreated(){
292 Account ocAccount
= mIntent
.getParcelableExtra(FileDownloader
.EXTRA_ACCOUNT
);
293 if(ocAccount
!= null
){
294 AccountManager accManager
= (AccountManager
) getActivity().getSystemService(Context
.ACCOUNT_SERVICE
);
295 OwnCloudVersion ocVersion
= new OwnCloudVersion(accManager
296 .getUserData(ocAccount
, AccountAuthenticator
.KEY_OC_VERSION
));
297 if(ocVersion
.compareTo(new OwnCloudVersion(0x030004)) >= 0 || ocVersion
.compareTo(new OwnCloudVersion(0x040001)) >= 0){
306 * Once the file download has finished -> update view
307 * @author Bartek Przybylski
309 private class DownloadFinishReceiver
extends BroadcastReceiver
{
311 public void onReceive(Context context
, Intent intent
) {
312 ((OCFile
)mIntent
.getParcelableExtra(EXTRA_FILE
)).setStoragePath(intent
.getStringExtra(FileDownloader
.EXTRA_FILE_PATH
));
313 updateFileDetails(mIntent
);