Force spaces in eclipse via project specifc settings
[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.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;
36
37 import com.actionbarsherlock.app.SherlockFragment;
38
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;
43
44 /**
45 * This Fragment is used to display the details about a file.
46 *
47 * @author Bartek Przybylski
48 *
49 */
50 public class FileDetailFragment extends SherlockFragment implements
51 OnClickListener {
52
53 public static final String FILE = "FILE";
54
55 private Intent mIntent;
56 private View mView;
57 private DownloadFinishReceiver mDownloadFinishReceiver;
58 private OCFile mFile;
59
60 private int mLayout;
61 private boolean mEmptyLayout;
62
63 /**
64 * Default constructor. When inflated by android -> display empty layout
65 */
66 public FileDetailFragment() {
67 mLayout = R.layout.file_details_empty;
68 mEmptyLayout = true;
69 }
70
71 /**
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.
75 *
76 * @param nonEmptyFragment
77 * True, to enable file detail rendering
78 */
79 public FileDetailFragment(Intent intent) {
80 mLayout = R.layout.file_details_fragment;
81 mIntent = intent;
82 mEmptyLayout = false;
83 }
84
85 @Override
86 public void onResume() {
87 super.onResume();
88 mDownloadFinishReceiver = new DownloadFinishReceiver();
89 IntentFilter filter = new IntentFilter(
90 FileDownloader.DOWNLOAD_FINISH_MESSAGE);
91 getActivity().registerReceiver(mDownloadFinishReceiver, filter);
92 }
93
94 @Override
95 public void onPause() {
96 super.onPause();
97 getActivity().unregisterReceiver(mDownloadFinishReceiver);
98 mDownloadFinishReceiver = null;
99 }
100
101 /**
102 * Use this method to signal this Activity that it shall update its view.
103 *
104 * @param intent
105 * The {@link Intent} that contains extra information about this
106 * file The intent needs to have these extras:
107 * <p>
108 *
109 * {@link FileDetailFragment#FILE}: An {@link OCFile}
110 * {@link FileDownloader#EXTRA_ACCOUNT}: The Account that file
111 * belongs to (required for downloading)
112 */
113 public void updateFileDetails(Intent intent) {
114 mIntent = intent;
115 updateFileDetails();
116 }
117
118 private void updateFileDetails() {
119 mFile = mIntent.getParcelableExtra(FILE);
120
121 if (mFile != null) {
122 // set file details
123 setFilename(mFile.getFileName());
124 setFiletype(DisplayUtils.convertMIMEtoPrettyPrint(mFile
125 .getMimetype()));
126 setFilesize(mFile.getFileLength());
127
128 // set file preview if available and possible
129 VideoView videoView = (VideoView) mView
130 .findViewById(R.id.videoView1);
131 videoView.setVisibility(View.INVISIBLE);
132 if (mFile.getPath() == null) {
133 ImageView imageView = (ImageView) getView().findViewById(
134 R.id.imageView2);
135 imageView.setImageResource(R.drawable.download);
136 imageView.setOnClickListener(this);
137 } else {
138 if (mFile.getMimetype().startsWith("image/")) {
139 ImageView imageView = (ImageView) mView
140 .findViewById(R.id.imageView2);
141 Bitmap bmp = BitmapFactory.decodeFile(mFile.getPath());
142 imageView.setImageBitmap(bmp);
143 } else if (mFile.getMimetype().startsWith("video/")) {
144 videoView.setVisibility(View.VISIBLE);
145 videoView.setVideoPath(mFile.getPath());
146 videoView.start();
147 }
148 }
149 }
150 }
151
152 @Override
153 public View onCreateView(LayoutInflater inflater, ViewGroup container,
154 Bundle savedInstanceState) {
155 View view = null;
156
157 view = inflater.inflate(mLayout, container, false);
158 mIntent = getActivity().getIntent();
159 mView = view;
160
161 // make sure we are not using the empty layout
162 if (mEmptyLayout == false) {
163 updateFileDetails();
164 }
165
166 return view;
167 }
168
169 @Override
170 public View getView() {
171 return mView == null ? super.getView() : mView;
172 };
173
174 private void setFilename(String filename) {
175 TextView tv = (TextView) getView().findViewById(R.id.textView1);
176 if (tv != null)
177 tv.setText(filename);
178 }
179
180 private void setFiletype(String mimetype) {
181 TextView tv = (TextView) getView().findViewById(R.id.textView2);
182 if (tv != null)
183 tv.setText(mimetype);
184 }
185
186 private void setFilesize(long filesize) {
187 TextView tv = (TextView) getView().findViewById(R.id.textView3);
188 if (tv != null)
189 tv.setText(DisplayUtils.bitsToHumanReadable(filesize));
190 }
191
192 /**
193 * Use this to check if the correct layout is loaded. When android
194 * instanciates this class using the default constructor, the layout will be
195 * empty.
196 *
197 * Once a user touches a file for the first time, you must instanciate a new
198 * Fragment with the new FileDetailFragment(true) to inflate the actual
199 * details
200 *
201 * @return If the layout is empty, this method will return true, otherwise
202 * false
203 */
204 public boolean isEmptyLayout() {
205 return mEmptyLayout;
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,
215 mIntent.getStringExtra(FileDownloader.EXTRA_FILE_PATH));
216 getActivity().startService(i);
217 }
218
219 private class DownloadFinishReceiver extends BroadcastReceiver {
220 @Override
221 public void onReceive(Context context, Intent intent) {
222 updateFileDetails();
223 }
224
225 }
226
227 }