blob: 16cb98289ade735e480b96388d119ea2cea578aa [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18package com.android.browser;
19
20import android.content.ContentUris;
21import android.content.ContentValues;
22import android.content.Context;
23import android.content.Intent;
24import android.content.pm.PackageManager;
25import android.content.pm.ResolveInfo;
26import android.content.res.Resources;
27import android.database.Cursor;
28import android.drm.mobile1.DrmRawContent;
29import android.graphics.drawable.Drawable;
30import android.net.Uri;
31import android.provider.Downloads;
32import android.text.format.Formatter;
33import android.view.View;
34import android.widget.ImageView;
35import android.widget.ProgressBar;
36import android.widget.ResourceCursorAdapter;
37import android.widget.TextView;
38
39import java.io.File;
40import java.text.DateFormat;
41import java.util.Date;
42import java.util.List;
43
44/**
45 * This class is used to represent the data for the download list box. The only
46 * real work done by this class is to construct a custom view for the line
47 * items.
48 */
49public class BrowserDownloadAdapter extends ResourceCursorAdapter {
50
51 private int mFilenameColumnId;
52 private int mTitleColumnId;
53 private int mDescColumnId;
54 private int mStatusColumnId;
55 private int mTotalBytesColumnId;
56 private int mCurrentBytesColumnId;
57 private int mMimetypeColumnId;
58 private int mDateColumnId;
59
60 public BrowserDownloadAdapter(Context context, int layout, Cursor c) {
61 super(context, layout, c);
62 mFilenameColumnId = c.getColumnIndexOrThrow(Downloads._DATA);
Jean-Baptiste Queru3dc09b22009-03-31 16:49:44 -070063 mTitleColumnId = c.getColumnIndexOrThrow(Downloads.COLUMN_TITLE);
64 mDescColumnId = c.getColumnIndexOrThrow(Downloads.COLUMN_DESCRIPTION);
65 mStatusColumnId = c.getColumnIndexOrThrow(Downloads.COLUMN_STATUS);
66 mTotalBytesColumnId = c.getColumnIndexOrThrow(Downloads.COLUMN_TOTAL_BYTES);
The Android Open Source Project0c908882009-03-03 19:32:16 -080067 mCurrentBytesColumnId =
Jean-Baptiste Queru3dc09b22009-03-31 16:49:44 -070068 c.getColumnIndexOrThrow(Downloads.COLUMN_CURRENT_BYTES);
69 mMimetypeColumnId = c.getColumnIndexOrThrow(Downloads.COLUMN_MIME_TYPE);
70 mDateColumnId = c.getColumnIndexOrThrow(Downloads.COLUMN_LAST_MODIFICATION);
The Android Open Source Project0c908882009-03-03 19:32:16 -080071 }
72
73 @Override
74 public void bindView(View view, Context context, Cursor cursor) {
75 Resources r = context.getResources();
76
77 // Retrieve the icon for this download
78 String mimeType = cursor.getString(mMimetypeColumnId);
79 ImageView iv = (ImageView) view.findViewById(R.id.download_icon);
80 if (DrmRawContent.DRM_MIMETYPE_MESSAGE_STRING.equalsIgnoreCase(mimeType)) {
81 iv.setImageResource(R.drawable.ic_launcher_drm_file);
82 } else if (mimeType == null) {
83 iv.setVisibility(View.INVISIBLE);
84 } else {
85 Intent intent = new Intent(Intent.ACTION_VIEW);
86 intent.setDataAndType(Uri.fromParts("file", "", null), mimeType);
87 PackageManager pm = context.getPackageManager();
88 List<ResolveInfo> list = pm.queryIntentActivities(intent,
89 PackageManager.MATCH_DEFAULT_ONLY);
90 if (list.size() > 0) {
91 Drawable icon = list.get(0).activityInfo.loadIcon(pm);
92 iv.setImageDrawable(icon);
93 iv.setVisibility(View.VISIBLE);
94 } else {
95 iv.setVisibility(View.INVISIBLE);
96 }
97 }
98
99 TextView tv = (TextView) view.findViewById(R.id.download_title);
100 String title = cursor.getString(mTitleColumnId);
101 if (title == null) {
102 String fullFilename = cursor.getString(mFilenameColumnId);
103 if (fullFilename == null) {
104 title = r.getString(R.string.download_unknown_filename);
105 } else {
106 // We have a filename, so we can build a title from that
107 title = new File(fullFilename).getName();
108 ContentValues values = new ContentValues();
Jean-Baptiste Queru3dc09b22009-03-31 16:49:44 -0700109 values.put(Downloads.COLUMN_TITLE, title);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800110 // assume "_id" is the first column for the cursor
111 context.getContentResolver().update(
112 ContentUris.withAppendedId(Downloads.CONTENT_URI,
113 cursor.getLong(0)), values, null, null);
114 }
115 }
116 tv.setText(title);
117
118 tv = (TextView) view.findViewById(R.id.domain);
119 tv.setText(cursor.getString(mDescColumnId));
120
121 long totalBytes = cursor.getLong(mTotalBytesColumnId);
122
123 int status = cursor.getInt(mStatusColumnId);
124 if (Downloads.isStatusCompleted(status)) { // Download stopped
125 View v = view.findViewById(R.id.progress_text);
126 v.setVisibility(View.GONE);
127
128 v = view.findViewById(R.id.download_progress);
129 v.setVisibility(View.GONE);
130
131 tv = (TextView) view.findViewById(R.id.complete_text);
132 tv.setVisibility(View.VISIBLE);
133 if (Downloads.isStatusError(status)) {
134 tv.setText(getErrorText(status));
135 } else {
136 tv.setText(r.getString(R.string.download_success,
137 Formatter.formatFileSize(mContext, totalBytes)));
138 }
139
140 long time = cursor.getLong(mDateColumnId);
141 Date d = new Date(time);
142 DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
143 tv = (TextView) view.findViewById(R.id.complete_date);
144 tv.setVisibility(View.VISIBLE);
145 tv.setText(df.format(d));
146
147 } else { // Download is still running
148 tv = (TextView) view.findViewById(R.id.progress_text);
149 tv.setVisibility(View.VISIBLE);
150
151 View progress = view.findViewById(R.id.download_progress);
152 progress.setVisibility(View.VISIBLE);
153
154 View v = view.findViewById(R.id.complete_date);
155 v.setVisibility(View.GONE);
156
157 v = view.findViewById(R.id.complete_text);
158 v.setVisibility(View.GONE);
159
160 if (status == Downloads.STATUS_PENDING) {
161 tv.setText(r.getText(R.string.download_pending));
162 } else if (status == Downloads.STATUS_PENDING_PAUSED) {
163 tv.setText(r.getText(R.string.download_pending_network));
164 } else {
165 ProgressBar pb = (ProgressBar) progress;
166
167 StringBuilder sb = new StringBuilder();
168 if (status == Downloads.STATUS_RUNNING) {
169 sb.append(r.getText(R.string.download_running));
170 } else {
171 sb.append(r.getText(R.string.download_running_paused));
172 }
173 if (totalBytes > 0) {
174 long currentBytes = cursor.getLong(mCurrentBytesColumnId);
175 int progressAmount = (int)(currentBytes * 100 / totalBytes);
176 sb.append(' ');
177 sb.append(progressAmount);
178 sb.append("% (");
179 sb.append(Formatter.formatFileSize(mContext, currentBytes));
180 sb.append("/");
181 sb.append(Formatter.formatFileSize(mContext, totalBytes));
182 sb.append(")");
183 pb.setIndeterminate(false);
184 pb.setProgress(progressAmount);
185 } else {
186 pb.setIndeterminate(true);
187 }
188 tv.setText(sb.toString());
189 }
190 }
191
192 }
193
194 /**
195 * Provide the resource id for the error string.
196 * @param status status of the download item
197 * @return resource id for the error string.
198 */
199 public static int getErrorText(int status) {
200 switch (status) {
201 case Downloads.STATUS_NOT_ACCEPTABLE:
202 return R.string.download_not_acceptable;
203
204 case Downloads.STATUS_LENGTH_REQUIRED:
205 return R.string.download_length_required;
206
207 case Downloads.STATUS_PRECONDITION_FAILED:
208 return R.string.download_precondition_failed;
209
210 case Downloads.STATUS_CANCELED:
211 return R.string.download_canceled;
212
213 case Downloads.STATUS_FILE_ERROR:
214 return R.string.download_file_error;
215
216 case Downloads.STATUS_BAD_REQUEST:
217 case Downloads.STATUS_UNKNOWN_ERROR:
218 default:
219 return R.string.download_error;
220 }
221 }
222}