e4b33f12871549a8a1fb9cfcb0f3586b13ee6ce4
[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.Button;
33 import android.widget.ImageView;
34 import android.widget.TextView;
35 import android.widget.Toast;
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 intent Intent with an account and a file in it for rendering
77 */
78 public FileDetailFragment(Intent intent) {
79 mLayout = R.layout.file_details_fragment;
80 mIntent = intent;
81 mEmptyLayout = false;
82 }
83
84 @Override
85 public void onResume() {
86 super.onResume();
87 mDownloadFinishReceiver = new DownloadFinishReceiver();
88 IntentFilter filter = new IntentFilter(
89 FileDownloader.DOWNLOAD_FINISH_MESSAGE);
90 getActivity().registerReceiver(mDownloadFinishReceiver, filter);
91 }
92
93 @Override
94 public void onPause() {
95 super.onPause();
96 getActivity().unregisterReceiver(mDownloadFinishReceiver);
97 mDownloadFinishReceiver = null;
98 }
99
100 /**
101 * Use this method to signal this Activity that it shall update its view.
102 *
103 * @param intent The {@link Intent} that contains extra information about
104 * this file The intent needs to have these extras:
105 * <p>
106 *
107 * {@link FileDetailFragment#FILE}: An {@link OCFile}
108 * {@link FileDownloader#EXTRA_ACCOUNT}: The Account that file
109 * belongs to (required for downloading)
110 */
111 public void updateFileDetails(Intent intent) {
112 mIntent = intent;
113 updateFileDetails();
114 }
115
116 private void updateFileDetails() {
117 mFile = mIntent.getParcelableExtra(FILE);
118
119 if (mFile != null) {
120 // set file details
121 setFilename(mFile.getFileName());
122 setFiletype(DisplayUtils.convertMIMEtoPrettyPrint(mFile
123 .getMimetype()));
124 setFilesize(mFile.getFileLength());
125
126 // Update preview
127 if (mFile.getStoragePath() != null) {
128 if (mFile.getMimetype().startsWith("image/")) {
129 ImageView preview = (ImageView) getView().findViewById(
130 R.id.fdPreview);
131 Bitmap bmp = BitmapFactory.decodeFile(mFile.getStoragePath());
132 preview.setImageBitmap(bmp);
133 }
134 }
135
136 // Make download button effective
137 Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);
138 downloadButton.setOnClickListener(this);
139 }
140 }
141
142 @Override
143 public View onCreateView(LayoutInflater inflater, ViewGroup container,
144 Bundle savedInstanceState) {
145 View view = null;
146 view = inflater.inflate(mLayout, container, false);
147 return view;
148 }
149
150 @Override
151 public void onStart() {
152 super.onStart();
153
154 // Fill in required information about file displaying
155 if(mIntent == null){
156 mIntent = getActivity().getIntent();
157 }
158
159 // Fill in the details if the layout is not empty
160 if(!mEmptyLayout){
161 updateFileDetails();
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 /**
185 * Use this to check if the correct layout is loaded. When android
186 * instanciates this class using the default constructor, the layout will be
187 * empty.
188 *
189 * Once a user touches a file for the first time, you must instanciate a new
190 * Fragment with the new FileDetailFragment(true) to inflate the actual
191 * details
192 *
193 * @return If the layout is empty, this method will return true, otherwise
194 * false
195 */
196 public boolean isEmptyLayout() {
197 return mEmptyLayout;
198 }
199
200 @Override
201 public void onClick(View v) {
202 Toast.makeText(getActivity(), "Downloading", Toast.LENGTH_LONG).show();
203 Intent i = new Intent(getActivity(), FileDownloader.class);
204 i.putExtra(FileDownloader.EXTRA_ACCOUNT,
205 mIntent.getParcelableExtra(FileDownloader.EXTRA_ACCOUNT));
206 i.putExtra(FileDownloader.EXTRA_FILE_PATH, mFile.getPath());
207 getActivity().startService(i);
208 }
209
210 private class DownloadFinishReceiver extends BroadcastReceiver {
211 @Override
212 public void onReceive(Context context, Intent intent) {
213 updateFileDetails();
214 }
215
216 }
217
218 }