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