Fixed fragment not updating file infos on tablets the first time it is
[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.os.Bundle;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.view.View.OnClickListener;
30 import android.view.ViewGroup;
31 import android.widget.Button;
32 import android.widget.ImageView;
33 import android.widget.TextView;
34 import android.widget.Toast;
35
36 import com.actionbarsherlock.app.SherlockFragment;
37
38 import eu.alefzero.owncloud.DisplayUtils;
39 import eu.alefzero.owncloud.FileDownloader;
40 import eu.alefzero.owncloud.R;
41 import eu.alefzero.owncloud.datamodel.OCFile;
42
43 /**
44 * This Fragment is used to display the details about a file.
45 *
46 * @author Bartek Przybylski
47 *
48 */
49 public class FileDetailFragment extends SherlockFragment implements
50 OnClickListener {
51
52 public static final String FILE = "FILE";
53
54 private DownloadFinishReceiver mDownloadFinishReceiver;
55 private Intent mIntent;
56 private int mLayout;
57 private View mView;
58 private OCFile mFile;
59
60 /**
61 * Default constructor - contains real layout
62 */
63 public FileDetailFragment(){
64 mLayout = R.layout.file_details_fragment;
65 }
66
67 /**
68 * Creates a dummy layout. For use if the user never has
69 * tapped on a file before
70 *
71 * @param useEmptyView If true, use empty layout
72 */
73 public FileDetailFragment(boolean useEmptyView){
74 if(useEmptyView){
75 mLayout = R.layout.file_details_empty;
76 } else {
77 mLayout = R.layout.file_details_fragment;
78 }
79 }
80
81 /**
82 * Use this when creating the fragment and display
83 * a file at the same time
84 *
85 * @param showDetailsIntent The Intent with the required parameters
86 * @see FileDetailFragment#updateFileDetails(Intent)
87 */
88 public FileDetailFragment(Intent showDetailsIntent) {
89 mIntent = showDetailsIntent;
90 mLayout = R.layout.file_details_fragment;
91 }
92
93 @Override
94 public void onResume() {
95 super.onResume();
96 mDownloadFinishReceiver = new DownloadFinishReceiver();
97 IntentFilter filter = new IntentFilter(
98 FileDownloader.DOWNLOAD_FINISH_MESSAGE);
99 getActivity().registerReceiver(mDownloadFinishReceiver, filter);
100 }
101
102 @Override
103 public void onPause() {
104 super.onPause();
105 getActivity().unregisterReceiver(mDownloadFinishReceiver);
106 mDownloadFinishReceiver = null;
107 }
108
109 /**
110 * Use this method to signal this Activity that it shall update its view.
111 *
112 * @param intent The {@link Intent} that contains extra information about
113 * this file The intent needs to have these extras:
114 * <p>
115 *
116 * {@link FileDetailFragment#FILE}: An {@link OCFile}
117 * {@link FileDownloader#EXTRA_ACCOUNT}: The Account that file
118 * belongs to (required for downloading)
119 */
120 public void updateFileDetails(Intent intent) {
121 mIntent = intent;
122 updateFileDetails();
123 }
124
125 private void updateFileDetails() {
126 mFile = mIntent.getParcelableExtra(FILE);
127
128 if (mFile != null) {
129 // set file details
130 setFilename(mFile.getFileName());
131 setFiletype(DisplayUtils.convertMIMEtoPrettyPrint(mFile
132 .getMimetype()));
133 setFilesize(mFile.getFileLength());
134
135 // Update preview
136 if (mFile.getStoragePath() != null) {
137 if (mFile.getMimetype().startsWith("image/")) {
138 ImageView preview = (ImageView) getView().findViewById(
139 R.id.fdPreview);
140 Bitmap bmp = BitmapFactory.decodeFile(mFile.getStoragePath());
141 preview.setImageBitmap(bmp);
142 }
143 }
144
145 // Make download button effective
146 Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);
147 downloadButton.setOnClickListener(this);
148 }
149 }
150
151 private void setFilename(String filename) {
152 TextView tv = (TextView) getView().findViewById(R.id.fdFilename);
153 if (tv != null)
154 tv.setText(filename);
155 }
156
157 private void setFiletype(String mimetype) {
158 TextView tv = (TextView) getView().findViewById(R.id.fdType);
159 if (tv != null)
160 tv.setText(mimetype);
161 }
162
163 private void setFilesize(long filesize) {
164 TextView tv = (TextView) getView().findViewById(R.id.fdSize);
165 if (tv != null)
166 tv.setText(DisplayUtils.bitsToHumanReadable(filesize));
167 }
168
169 @Override
170 public View onCreateView(LayoutInflater inflater, ViewGroup container,
171 Bundle savedInstanceState) {
172 View view = null;
173 view = inflater.inflate(mLayout, container, false);
174 mView = view;
175 if(mLayout == R.layout.file_details_fragment){
176 // Phones will launch an activity with this intent
177 if(mIntent == null){
178 mIntent = getActivity().getIntent();
179 }
180 updateFileDetails();
181 }
182
183 return view;
184 }
185
186
187
188 @Override
189 public View getView() {
190 return super.getView() == null ? mView : super.getView();
191 }
192
193 @Override
194 public void onClick(View v) {
195 Toast.makeText(getActivity(), "Downloading", Toast.LENGTH_LONG).show();
196 Intent i = new Intent(getActivity(), FileDownloader.class);
197 i.putExtra(FileDownloader.EXTRA_ACCOUNT,
198 mIntent.getParcelableExtra(FileDownloader.EXTRA_ACCOUNT));
199 i.putExtra(FileDownloader.EXTRA_FILE_PATH, mFile.getPath());
200 getActivity().startService(i);
201 }
202
203 private class DownloadFinishReceiver extends BroadcastReceiver {
204 @Override
205 public void onReceive(Context context, Intent intent) {
206 updateFileDetails();
207 }
208
209 }
210
211 }