sync option in filelist menu
[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.accounts.Account;
21 import android.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.IntentFilter;
25 import android.database.Cursor;
26 import android.graphics.Bitmap;
27 import android.graphics.BitmapFactory;
28 import android.net.Uri;
29 import android.os.Bundle;
30 import android.view.LayoutInflater;
31 import android.view.View;
32 import android.view.View.OnClickListener;
33 import android.view.ViewGroup;
34 import android.widget.ImageView;
35 import android.widget.TextView;
36 import android.widget.Toast;
37 import android.widget.VideoView;
38
39 import com.actionbarsherlock.app.SherlockFragment;
40
41 import eu.alefzero.owncloud.DisplayUtils;
42 import eu.alefzero.owncloud.FileDownloader;
43 import eu.alefzero.owncloud.R;
44 import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
45
46 /**
47 * This Fragment is used to display the details about a file.
48 * @author Bartek Przybylski
49 *
50 */
51 public class FileDetailFragment extends SherlockFragment implements OnClickListener {
52
53 private Intent mIntent;
54 private View mView;
55 private DownloadFinishReceiver dfr;
56
57 @Override
58 public void onResume() {
59 super.onResume();
60 dfr = new DownloadFinishReceiver();
61 IntentFilter filter = new IntentFilter(FileDownloader.DOWNLOAD_FINISH_MESSAGE);
62 getActivity().registerReceiver(dfr, filter);
63 }
64
65 @Override
66 public void onPause() {
67 super.onPause();
68 getActivity().unregisterReceiver(dfr);
69 dfr = null;
70 }
71
72 public void setStuff(Intent intent) {
73 mIntent = intent;
74 setStuff(getView());
75 }
76
77 private void setStuff(View view) {
78 mView = view;
79 String id = mIntent.getStringExtra("FILE_ID");
80 Account account = mIntent.getParcelableExtra("ACCOUNT");
81 String account_name = account.name;
82 Cursor c = getActivity().managedQuery(
83 Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, id),
84 null,
85 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
86 new String[]{account_name},
87 null);
88 c.moveToFirst();
89
90 // Retrieve details from DB
91 String filename = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_NAME));
92 String mimetype = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE));
93 String path = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH));
94 long filesize = c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH));
95
96 // set file details
97 setFilename(filename);
98 setFiletype(DisplayUtils.convertMIMEtoPrettyPrint(mimetype));
99 setFilesize(filesize);
100
101 // set file preview if available and possible
102 View w = view.findViewById(R.id.videoView1);
103 w.setVisibility(View.INVISIBLE);
104 if (path == null) {
105 ImageView v = (ImageView) getView().findViewById(R.id.imageView2);
106 v.setImageResource(R.drawable.download);
107 v.setOnClickListener(this);
108 } else {
109 if (mimetype.startsWith("image/")) {
110 ImageView v = (ImageView) view.findViewById(R.id.imageView2);
111 Bitmap bmp = BitmapFactory.decodeFile(path);
112 v.setImageBitmap(bmp);
113 } else if (mimetype.startsWith("video/")) {
114 VideoView v = (VideoView) view.findViewById(R.id.videoView1);
115 v.setVisibility(View.VISIBLE);
116 v.setVideoPath(path);
117 v.start();
118 }
119 }
120 }
121
122 @Override
123 public View onCreateView(LayoutInflater inflater, ViewGroup container,
124 Bundle savedInstanceState) {
125 View v = null;
126
127 if (getActivity().getIntent() != null && getActivity().getIntent().getStringExtra("FILE_ID") != null) {
128 v = inflater.inflate(R.layout.file_details_fragment, container, false);
129 mIntent = getActivity().getIntent();
130 setStuff(v);
131 } else {
132 v = inflater.inflate(R.layout.file_details_empty, container, false);
133 }
134 return v;
135 }
136
137 @Override
138 public View getView() {
139 return mView == null ? super.getView() : mView;
140 };
141
142 public void setFilename(String filename) {
143 TextView tv = (TextView) getView().findViewById(R.id.textView1);
144 if (tv != null) tv.setText(filename);
145 }
146
147 public void setFiletype(String mimetype) {
148 TextView tv = (TextView) getView().findViewById(R.id.textView2);
149 if (tv != null) tv.setText(mimetype);
150 }
151
152 public void setFilesize(long filesize) {
153 TextView tv = (TextView) getView().findViewById(R.id.textView3);
154 if (tv != null) tv.setText(DisplayUtils.bitsToHumanReadable(filesize));
155 }
156
157 @Override
158 public void onClick(View v) {
159 Toast.makeText(getActivity(), "Downloading", Toast.LENGTH_LONG).show();
160 Intent i = new Intent(getActivity(), FileDownloader.class);
161 i.putExtra(FileDownloader.EXTRA_ACCOUNT, mIntent.getParcelableExtra("ACCOUNT"));
162 i.putExtra(FileDownloader.EXTRA_FILE_PATH, mIntent.getStringExtra("FULL_PATH"));
163 getActivity().startService(i);
164 }
165
166 private class DownloadFinishReceiver extends BroadcastReceiver {
167 @Override
168 public void onReceive(Context context, Intent intent) {
169 setStuff(getView());
170 }
171
172 }
173
174 }