blob: 85539c3f651209594a239b740e9aa455108bdb73 [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;
Leon Scroggins24837452010-01-13 13:43:35 -050033import android.view.LayoutInflater;
The Android Open Source Project0c908882009-03-03 19:32:16 -080034import android.view.View;
Leon Scroggins24837452010-01-13 13:43:35 -050035import android.view.ViewGroup;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.widget.ImageView;
37import android.widget.ProgressBar;
Leon Scroggins24837452010-01-13 13:43:35 -050038import android.widget.RelativeLayout;
The Android Open Source Project0c908882009-03-03 19:32:16 -080039import android.widget.TextView;
40
41import java.io.File;
42import java.text.DateFormat;
43import java.util.Date;
44import java.util.List;
45
46/**
47 * This class is used to represent the data for the download list box. The only
48 * real work done by this class is to construct a custom view for the line
49 * items.
50 */
Leon Scroggins24837452010-01-13 13:43:35 -050051public class BrowserDownloadAdapter extends DateSortedExpandableListAdapter {
The Android Open Source Project0c908882009-03-03 19:32:16 -080052
53 private int mFilenameColumnId;
54 private int mTitleColumnId;
55 private int mDescColumnId;
56 private int mStatusColumnId;
57 private int mTotalBytesColumnId;
58 private int mCurrentBytesColumnId;
59 private int mMimetypeColumnId;
60 private int mDateColumnId;
61
Leon Scroggins24837452010-01-13 13:43:35 -050062 public BrowserDownloadAdapter(Context context, Cursor c, int index) {
63 super(context, c, index);
The Android Open Source Project0c908882009-03-03 19:32:16 -080064 mFilenameColumnId = c.getColumnIndexOrThrow(Downloads._DATA);
Jean-Baptiste Queru3dc09b22009-03-31 16:49:44 -070065 mTitleColumnId = c.getColumnIndexOrThrow(Downloads.COLUMN_TITLE);
66 mDescColumnId = c.getColumnIndexOrThrow(Downloads.COLUMN_DESCRIPTION);
67 mStatusColumnId = c.getColumnIndexOrThrow(Downloads.COLUMN_STATUS);
68 mTotalBytesColumnId = c.getColumnIndexOrThrow(Downloads.COLUMN_TOTAL_BYTES);
The Android Open Source Project0c908882009-03-03 19:32:16 -080069 mCurrentBytesColumnId =
Jean-Baptiste Queru3dc09b22009-03-31 16:49:44 -070070 c.getColumnIndexOrThrow(Downloads.COLUMN_CURRENT_BYTES);
71 mMimetypeColumnId = c.getColumnIndexOrThrow(Downloads.COLUMN_MIME_TYPE);
72 mDateColumnId = c.getColumnIndexOrThrow(Downloads.COLUMN_LAST_MODIFICATION);
The Android Open Source Project0c908882009-03-03 19:32:16 -080073 }
74
75 @Override
Leon Scroggins24837452010-01-13 13:43:35 -050076 public View getChildView(int groupPosition, int childPosition,
77 boolean isLastChild, View convertView, ViewGroup parent) {
78 Context context = getContext();
79 // The layout file uses a RelativeLayout, whereas the GroupViews use
80 // TextView.
81 if (null == convertView || !(convertView instanceof RelativeLayout)) {
82 convertView = LayoutInflater.from(context).inflate(
83 R.layout.browser_download_item, null);
84 }
85
86 // Bail early if the Cursor is closed.
87 if (!moveCursorToChildPosition(groupPosition, childPosition)) {
88 return convertView;
89 }
90
The Android Open Source Project0c908882009-03-03 19:32:16 -080091 Resources r = context.getResources();
92
93 // Retrieve the icon for this download
Leon Scroggins24837452010-01-13 13:43:35 -050094 String mimeType = getString(mMimetypeColumnId);
95 ImageView iv = (ImageView) convertView.findViewById(R.id.download_icon);
The Android Open Source Project0c908882009-03-03 19:32:16 -080096 if (DrmRawContent.DRM_MIMETYPE_MESSAGE_STRING.equalsIgnoreCase(mimeType)) {
97 iv.setImageResource(R.drawable.ic_launcher_drm_file);
98 } else if (mimeType == null) {
99 iv.setVisibility(View.INVISIBLE);
100 } else {
101 Intent intent = new Intent(Intent.ACTION_VIEW);
102 intent.setDataAndType(Uri.fromParts("file", "", null), mimeType);
103 PackageManager pm = context.getPackageManager();
104 List<ResolveInfo> list = pm.queryIntentActivities(intent,
105 PackageManager.MATCH_DEFAULT_ONLY);
106 if (list.size() > 0) {
107 Drawable icon = list.get(0).activityInfo.loadIcon(pm);
108 iv.setImageDrawable(icon);
109 iv.setVisibility(View.VISIBLE);
110 } else {
111 iv.setVisibility(View.INVISIBLE);
112 }
113 }
114
Leon Scroggins24837452010-01-13 13:43:35 -0500115 TextView tv = (TextView) convertView.findViewById(R.id.download_title);
116 String title = getString(mTitleColumnId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800117 if (title == null) {
Leon Scroggins24837452010-01-13 13:43:35 -0500118 String fullFilename = getString(mFilenameColumnId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800119 if (fullFilename == null) {
120 title = r.getString(R.string.download_unknown_filename);
121 } else {
122 // We have a filename, so we can build a title from that
123 title = new File(fullFilename).getName();
124 ContentValues values = new ContentValues();
Jean-Baptiste Queru3dc09b22009-03-31 16:49:44 -0700125 values.put(Downloads.COLUMN_TITLE, title);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800126 // assume "_id" is the first column for the cursor
127 context.getContentResolver().update(
128 ContentUris.withAppendedId(Downloads.CONTENT_URI,
Leon Scroggins24837452010-01-13 13:43:35 -0500129 getLong(0)), values, null, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800130 }
131 }
132 tv.setText(title);
133
Leon Scroggins24837452010-01-13 13:43:35 -0500134 tv = (TextView) convertView.findViewById(R.id.domain);
135 tv.setText(getString(mDescColumnId));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800136
Leon Scroggins24837452010-01-13 13:43:35 -0500137 long totalBytes = getLong(mTotalBytesColumnId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800138
Leon Scroggins24837452010-01-13 13:43:35 -0500139 int status = getInt(mStatusColumnId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800140 if (Downloads.isStatusCompleted(status)) { // Download stopped
Leon Scroggins24837452010-01-13 13:43:35 -0500141 View v = convertView.findViewById(R.id.progress_text);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800142 v.setVisibility(View.GONE);
143
Leon Scroggins24837452010-01-13 13:43:35 -0500144 v = convertView.findViewById(R.id.download_progress);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800145 v.setVisibility(View.GONE);
146
Leon Scroggins24837452010-01-13 13:43:35 -0500147 tv = (TextView) convertView.findViewById(R.id.complete_text);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800148 tv.setVisibility(View.VISIBLE);
149 if (Downloads.isStatusError(status)) {
150 tv.setText(getErrorText(status));
151 } else {
152 tv.setText(r.getString(R.string.download_success,
Leon Scroggins24837452010-01-13 13:43:35 -0500153 Formatter.formatFileSize(context, totalBytes)));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800154 }
155
Leon Scroggins24837452010-01-13 13:43:35 -0500156 long time = getLong(mDateColumnId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800157 Date d = new Date(time);
158 DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
Leon Scroggins24837452010-01-13 13:43:35 -0500159 tv = (TextView) convertView.findViewById(R.id.complete_date);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800160 tv.setVisibility(View.VISIBLE);
161 tv.setText(df.format(d));
162
163 } else { // Download is still running
Leon Scroggins24837452010-01-13 13:43:35 -0500164 tv = (TextView) convertView.findViewById(R.id.progress_text);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800165 tv.setVisibility(View.VISIBLE);
166
Leon Scroggins24837452010-01-13 13:43:35 -0500167 View progress = convertView.findViewById(R.id.download_progress);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800168 progress.setVisibility(View.VISIBLE);
169
Leon Scroggins24837452010-01-13 13:43:35 -0500170 View v = convertView.findViewById(R.id.complete_date);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800171 v.setVisibility(View.GONE);
172
Leon Scroggins24837452010-01-13 13:43:35 -0500173 v = convertView.findViewById(R.id.complete_text);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800174 v.setVisibility(View.GONE);
175
176 if (status == Downloads.STATUS_PENDING) {
177 tv.setText(r.getText(R.string.download_pending));
178 } else if (status == Downloads.STATUS_PENDING_PAUSED) {
179 tv.setText(r.getText(R.string.download_pending_network));
180 } else {
181 ProgressBar pb = (ProgressBar) progress;
182
183 StringBuilder sb = new StringBuilder();
184 if (status == Downloads.STATUS_RUNNING) {
185 sb.append(r.getText(R.string.download_running));
186 } else {
187 sb.append(r.getText(R.string.download_running_paused));
188 }
189 if (totalBytes > 0) {
Leon Scroggins24837452010-01-13 13:43:35 -0500190 long currentBytes = getLong(mCurrentBytesColumnId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800191 int progressAmount = (int)(currentBytes * 100 / totalBytes);
192 sb.append(' ');
193 sb.append(progressAmount);
194 sb.append("% (");
Leon Scroggins24837452010-01-13 13:43:35 -0500195 sb.append(Formatter.formatFileSize(context, currentBytes));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800196 sb.append("/");
Leon Scroggins24837452010-01-13 13:43:35 -0500197 sb.append(Formatter.formatFileSize(context, totalBytes));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800198 sb.append(")");
199 pb.setIndeterminate(false);
200 pb.setProgress(progressAmount);
201 } else {
202 pb.setIndeterminate(true);
203 }
204 tv.setText(sb.toString());
205 }
206 }
Leon Scroggins24837452010-01-13 13:43:35 -0500207 return convertView;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800208 }
209
210 /**
211 * Provide the resource id for the error string.
212 * @param status status of the download item
213 * @return resource id for the error string.
214 */
215 public static int getErrorText(int status) {
216 switch (status) {
217 case Downloads.STATUS_NOT_ACCEPTABLE:
218 return R.string.download_not_acceptable;
219
220 case Downloads.STATUS_LENGTH_REQUIRED:
221 return R.string.download_length_required;
222
223 case Downloads.STATUS_PRECONDITION_FAILED:
224 return R.string.download_precondition_failed;
225
226 case Downloads.STATUS_CANCELED:
227 return R.string.download_canceled;
228
229 case Downloads.STATUS_FILE_ERROR:
230 return R.string.download_file_error;
231
232 case Downloads.STATUS_BAD_REQUEST:
233 case Downloads.STATUS_UNKNOWN_ERROR:
234 default:
235 return R.string.download_error;
236 }
237 }
238}