fix download file with noascii chars in name
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / fragment / FileDetailFragment.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18 package eu.alefzero.owncloud.ui.fragment;
19
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;
37
38 import com.actionbarsherlock.app.SherlockFragment;
39
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;
44
45 /**
46 * This Fragment is used to display the details about a file.
47 *
48 * @author Bartek Przybylski
49 *
50 */
51 public class FileDetailFragment extends SherlockFragment implements
52 OnClickListener {
53
54 public static final String FILE = "FILE";
55
56 private DownloadFinishReceiver mDownloadFinishReceiver;
57 private Intent mIntent;
58 private int mLayout;
59 private View mView;
60 private OCFile mFile;
61 private static final String TAG = "FileDetailFragment";
62
63 /**
64 * Default constructor - contains real layout
65 */
66 public FileDetailFragment(){
67 mLayout = R.layout.file_details_fragment;
68 }
69
70 /**
71 * Creates a dummy layout. For use if the user never has
72 * tapped on a file before
73 *
74 * @param useEmptyView If true, use empty layout
75 */
76 public FileDetailFragment(boolean useEmptyView){
77 if(useEmptyView){
78 mLayout = R.layout.file_details_empty;
79 } else {
80 mLayout = R.layout.file_details_fragment;
81 }
82 }
83
84 /**
85 * Use this when creating the fragment and display
86 * a file at the same time
87 *
88 * @param showDetailsIntent The Intent with the required parameters
89 * @see FileDetailFragment#updateFileDetails(Intent)
90 */
91 public FileDetailFragment(Intent showDetailsIntent) {
92 mIntent = showDetailsIntent;
93 mLayout = R.layout.file_details_fragment;
94 }
95
96 @Override
97 public void onResume() {
98 super.onResume();
99 mDownloadFinishReceiver = new DownloadFinishReceiver();
100 IntentFilter filter = new IntentFilter(
101 FileDownloader.DOWNLOAD_FINISH_MESSAGE);
102 getActivity().registerReceiver(mDownloadFinishReceiver, filter);
103 }
104
105 @Override
106 public void onPause() {
107 super.onPause();
108 getActivity().unregisterReceiver(mDownloadFinishReceiver);
109 mDownloadFinishReceiver = null;
110 }
111
112 /**
113 * Use this method to signal this Activity that it shall update its view.
114 *
115 * @param intent The {@link Intent} that contains extra information about
116 * this file The intent needs to have these extras:
117 * <p>
118 *
119 * {@link FileDetailFragment#FILE}: An {@link OCFile}
120 * {@link FileDownloader#EXTRA_ACCOUNT}: The Account that file
121 * belongs to (required for downloading)
122 */
123 public void updateFileDetails(Intent intent) {
124 mIntent = intent;
125 updateFileDetails();
126 }
127
128 private void updateFileDetails() {
129 mFile = mIntent.getParcelableExtra(FILE);
130 Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);
131
132 if (mFile != null) {
133 // set file details
134 setFilename(mFile.getFileName());
135 setFiletype(DisplayUtils.convertMIMEtoPrettyPrint(mFile
136 .getMimetype()));
137 setFilesize(mFile.getFileLength());
138 setTimeCreated(mFile.getCreationTimestamp());
139 setTimeModified(mFile.getModificationTimestamp());
140
141 // Update preview
142 if (mFile.getStoragePath() != null) {
143 try {
144 if (mFile.getMimetype().startsWith("image/")) {
145 ImageView preview = (ImageView) getView().findViewById(
146 R.id.fdPreview);
147 Bitmap bmp = BitmapFactory.decodeFile(mFile.getStoragePath());
148 preview.setImageBitmap(bmp);
149 }
150 } catch (OutOfMemoryError e) {
151 Log.e(TAG, "Out of memory occured for file with size " + mFile.getFileLength());
152 }
153 downloadButton.setText("Open file");
154 downloadButton.setOnClickListener(new OnClickListener() {
155 @Override
156 public void onClick(View v) {
157 Intent i = new Intent(Intent.ACTION_VIEW);
158 i.setDataAndType(Uri.parse("file://"+mFile.getStoragePath()), mFile.getMimetype());
159 startActivity(i);
160 }
161 });
162 } else {
163 // Make download button effective
164 downloadButton.setOnClickListener(this);
165 }
166 }
167 }
168
169 private void setFilename(String filename) {
170 TextView tv = (TextView) getView().findViewById(R.id.fdFilename);
171 if (tv != null)
172 tv.setText(filename);
173 }
174
175 private void setFiletype(String mimetype) {
176 TextView tv = (TextView) getView().findViewById(R.id.fdType);
177 if (tv != null)
178 tv.setText(mimetype);
179 }
180
181 private void setFilesize(long filesize) {
182 TextView tv = (TextView) getView().findViewById(R.id.fdSize);
183 if (tv != null)
184 tv.setText(DisplayUtils.bytesToHumanReadable(filesize));
185 }
186
187 private void setTimeCreated(long milliseconds){
188 TextView tv = (TextView) getView().findViewById(R.id.fdCreated);
189 if(tv != null){
190 tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));
191 }
192 }
193
194 private void setTimeModified(long milliseconds){
195 TextView tv = (TextView) getView().findViewById(R.id.fdModified);
196 if(tv != null){
197 tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));
198 }
199 }
200
201 @Override
202 public View onCreateView(LayoutInflater inflater, ViewGroup container,
203 Bundle savedInstanceState) {
204 View view = null;
205 view = inflater.inflate(mLayout, container, false);
206 mView = view;
207 if(mLayout == R.layout.file_details_fragment){
208 // Phones will launch an activity with this intent
209 if(mIntent == null){
210 mIntent = getActivity().getIntent();
211 }
212 updateFileDetails();
213 }
214
215 return view;
216 }
217
218
219
220 @Override
221 public View getView() {
222 return super.getView() == null ? mView : super.getView();
223 }
224
225 @Override
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);
233 }
234
235 private class DownloadFinishReceiver extends BroadcastReceiver {
236 @Override
237 public void onReceive(Context context, Intent intent) {
238 updateFileDetails();
239 }
240
241 }
242
243 }