e88787628f0becd9715581c1fb1f9d2b0b74db88
[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 @Override
82 public void onResume() {
83 super.onResume();
84 mDownloadFinishReceiver = new DownloadFinishReceiver();
85 IntentFilter filter = new IntentFilter(
86 FileDownloader.DOWNLOAD_FINISH_MESSAGE);
87 getActivity().registerReceiver(mDownloadFinishReceiver, filter);
88 }
89
90 @Override
91 public void onPause() {
92 super.onPause();
93 getActivity().unregisterReceiver(mDownloadFinishReceiver);
94 mDownloadFinishReceiver = null;
95 }
96
97 /**
98 * Use this method to signal this Activity that it shall update its view.
99 *
100 * @param intent The {@link Intent} that contains extra information about
101 * this file The intent needs to have these extras:
102 * <p>
103 *
104 * {@link FileDetailFragment#FILE}: An {@link OCFile}
105 * {@link FileDownloader#EXTRA_ACCOUNT}: The Account that file
106 * belongs to (required for downloading)
107 */
108 public void updateFileDetails(Intent intent) {
109 mIntent = intent;
110 updateFileDetails();
111 }
112
113 private void updateFileDetails() {
114 mFile = mIntent.getParcelableExtra(FILE);
115
116 if (mFile != null) {
117 // set file details
118 setFilename(mFile.getFileName());
119 setFiletype(DisplayUtils.convertMIMEtoPrettyPrint(mFile
120 .getMimetype()));
121 setFilesize(mFile.getFileLength());
122
123 // Update preview
124 if (mFile.getStoragePath() != null) {
125 if (mFile.getMimetype().startsWith("image/")) {
126 ImageView preview = (ImageView) getView().findViewById(
127 R.id.fdPreview);
128 Bitmap bmp = BitmapFactory.decodeFile(mFile.getStoragePath());
129 preview.setImageBitmap(bmp);
130 }
131 }
132
133 // Make download button effective
134 Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);
135 downloadButton.setOnClickListener(this);
136 }
137 }
138
139 private void setFilename(String filename) {
140 TextView tv = (TextView) getView().findViewById(R.id.fdFilename);
141 if (tv != null)
142 tv.setText(filename);
143 }
144
145 private void setFiletype(String mimetype) {
146 TextView tv = (TextView) getView().findViewById(R.id.fdType);
147 if (tv != null)
148 tv.setText(mimetype);
149 }
150
151 private void setFilesize(long filesize) {
152 TextView tv = (TextView) getView().findViewById(R.id.fdSize);
153 if (tv != null)
154 tv.setText(DisplayUtils.bitsToHumanReadable(filesize));
155 }
156
157 @Override
158 public View onCreateView(LayoutInflater inflater, ViewGroup container,
159 Bundle savedInstanceState) {
160 View view = null;
161 view = inflater.inflate(mLayout, container, false);
162 mView = view;
163 if(mLayout == R.layout.file_details_fragment){
164 mIntent = getActivity().getIntent();
165 updateFileDetails();
166 }
167
168 return view;
169 }
170
171
172
173 @Override
174 public View getView() {
175 return super.getView() == null ? mView : super.getView();
176 }
177
178 @Override
179 public void onClick(View v) {
180 Toast.makeText(getActivity(), "Downloading", Toast.LENGTH_LONG).show();
181 Intent i = new Intent(getActivity(), FileDownloader.class);
182 i.putExtra(FileDownloader.EXTRA_ACCOUNT,
183 mIntent.getParcelableExtra(FileDownloader.EXTRA_ACCOUNT));
184 i.putExtra(FileDownloader.EXTRA_FILE_PATH, mFile.getPath());
185 getActivity().startService(i);
186 }
187
188 private class DownloadFinishReceiver extends BroadcastReceiver {
189 @Override
190 public void onReceive(Context context, Intent intent) {
191 updateFileDetails();
192 }
193
194 }
195
196 }