7d10ebacaf7907c33d2e94b786e0a10d53595180
[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 // Update preview
139 if (mFile.getStoragePath() != null) {
140 try {
141 if (mFile.getMimetype().startsWith("image/")) {
142 ImageView preview = (ImageView) getView().findViewById(
143 R.id.fdPreview);
144 Bitmap bmp = BitmapFactory.decodeFile(mFile.getStoragePath());
145 preview.setImageBitmap(bmp);
146 }
147 } catch (OutOfMemoryError e) {
148 Log.e(TAG, "Out of memory occured for file with size " + mFile.getFileLength());
149 }
150 downloadButton.setText("Open file");
151 downloadButton.setOnClickListener(new OnClickListener() {
152 @Override
153 public void onClick(View v) {
154 Intent i = new Intent(Intent.ACTION_VIEW);
155 i.setDataAndType(Uri.parse("file://"+mFile.getStoragePath()), mFile.getMimetype());
156 startActivity(i);
157 }
158 });
159 } else {
160 // Make download button effective
161 downloadButton.setOnClickListener(this);
162 }
163 }
164 }
165
166 private void setFilename(String filename) {
167 TextView tv = (TextView) getView().findViewById(R.id.fdFilename);
168 if (tv != null)
169 tv.setText(filename);
170 }
171
172 private void setFiletype(String mimetype) {
173 TextView tv = (TextView) getView().findViewById(R.id.fdType);
174 if (tv != null)
175 tv.setText(mimetype);
176 }
177
178 private void setFilesize(long filesize) {
179 TextView tv = (TextView) getView().findViewById(R.id.fdSize);
180 if (tv != null)
181 tv.setText(DisplayUtils.bitsToHumanReadable(filesize));
182 }
183
184 @Override
185 public View onCreateView(LayoutInflater inflater, ViewGroup container,
186 Bundle savedInstanceState) {
187 View view = null;
188 view = inflater.inflate(mLayout, container, false);
189 mView = view;
190 if(mLayout == R.layout.file_details_fragment){
191 // Phones will launch an activity with this intent
192 if(mIntent == null){
193 mIntent = getActivity().getIntent();
194 }
195 updateFileDetails();
196 }
197
198 return view;
199 }
200
201
202
203 @Override
204 public View getView() {
205 return super.getView() == null ? mView : super.getView();
206 }
207
208 @Override
209 public void onClick(View v) {
210 Toast.makeText(getActivity(), "Downloading", Toast.LENGTH_LONG).show();
211 Intent i = new Intent(getActivity(), FileDownloader.class);
212 i.putExtra(FileDownloader.EXTRA_ACCOUNT,
213 mIntent.getParcelableExtra(FileDownloader.EXTRA_ACCOUNT));
214 i.putExtra(FileDownloader.EXTRA_FILE_PATH, mFile.getPath());
215 getActivity().startService(i);
216 }
217
218 private class DownloadFinishReceiver extends BroadcastReceiver {
219 @Override
220 public void onReceive(Context context, Intent intent) {
221 updateFileDetails();
222 }
223
224 }
225
226 }