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