blob: 2a3b69c9308a1f942ba60a5f8debe54c1336fdda [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
The Android Open Source Project0c908882009-03-03 19:32:16 -080020import android.content.Context;
21import android.content.Intent;
22import android.content.pm.PackageManager;
23import android.content.pm.ResolveInfo;
24import android.content.res.Resources;
25import android.database.Cursor;
26import android.drm.mobile1.DrmRawContent;
27import android.graphics.drawable.Drawable;
28import android.net.Uri;
29import android.provider.Downloads;
30import android.text.format.Formatter;
Leon Scroggins24837452010-01-13 13:43:35 -050031import android.view.LayoutInflater;
The Android Open Source Project0c908882009-03-03 19:32:16 -080032import android.view.View;
Leon Scroggins24837452010-01-13 13:43:35 -050033import android.view.ViewGroup;
The Android Open Source Project0c908882009-03-03 19:32:16 -080034import android.widget.ImageView;
35import android.widget.ProgressBar;
Leon Scroggins24837452010-01-13 13:43:35 -050036import android.widget.RelativeLayout;
The Android Open Source Project0c908882009-03-03 19:32:16 -080037import android.widget.TextView;
38
The Android Open Source Project0c908882009-03-03 19:32:16 -080039import java.text.DateFormat;
40import java.util.Date;
41import java.util.List;
42
43/**
44 * This class is used to represent the data for the download list box. The only
45 * real work done by this class is to construct a custom view for the line
46 * items.
47 */
Leon Scroggins24837452010-01-13 13:43:35 -050048public class BrowserDownloadAdapter extends DateSortedExpandableListAdapter {
The Android Open Source Project0c908882009-03-03 19:32:16 -080049
50 private int mFilenameColumnId;
51 private int mTitleColumnId;
52 private int mDescColumnId;
53 private int mStatusColumnId;
54 private int mTotalBytesColumnId;
55 private int mCurrentBytesColumnId;
56 private int mMimetypeColumnId;
57 private int mDateColumnId;
58
Leon Scroggins24837452010-01-13 13:43:35 -050059 public BrowserDownloadAdapter(Context context, Cursor c, int index) {
60 super(context, c, index);
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -080061 mFilenameColumnId = c.getColumnIndexOrThrow(Downloads.Impl._DATA);
62 mTitleColumnId = c.getColumnIndexOrThrow(Downloads.Impl.COLUMN_TITLE);
63 mDescColumnId = c.getColumnIndexOrThrow(Downloads.Impl.COLUMN_DESCRIPTION);
64 mStatusColumnId = c.getColumnIndexOrThrow(Downloads.Impl.COLUMN_STATUS);
65 mTotalBytesColumnId = c.getColumnIndexOrThrow(Downloads.Impl.COLUMN_TOTAL_BYTES);
The Android Open Source Project0c908882009-03-03 19:32:16 -080066 mCurrentBytesColumnId =
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -080067 c.getColumnIndexOrThrow(Downloads.Impl.COLUMN_CURRENT_BYTES);
68 mMimetypeColumnId = c.getColumnIndexOrThrow(Downloads.Impl.COLUMN_MIME_TYPE);
69 mDateColumnId = c.getColumnIndexOrThrow(Downloads.Impl.COLUMN_LAST_MODIFICATION);
The Android Open Source Project0c908882009-03-03 19:32:16 -080070 }
71
72 @Override
Leon Scroggins24837452010-01-13 13:43:35 -050073 public View getChildView(int groupPosition, int childPosition,
74 boolean isLastChild, View convertView, ViewGroup parent) {
75 Context context = getContext();
76 // The layout file uses a RelativeLayout, whereas the GroupViews use
77 // TextView.
78 if (null == convertView || !(convertView instanceof RelativeLayout)) {
79 convertView = LayoutInflater.from(context).inflate(
80 R.layout.browser_download_item, null);
81 }
82
83 // Bail early if the Cursor is closed.
84 if (!moveCursorToChildPosition(groupPosition, childPosition)) {
85 return convertView;
86 }
87
The Android Open Source Project0c908882009-03-03 19:32:16 -080088 Resources r = context.getResources();
89
90 // Retrieve the icon for this download
Leon Scroggins24837452010-01-13 13:43:35 -050091 String mimeType = getString(mMimetypeColumnId);
92 ImageView iv = (ImageView) convertView.findViewById(R.id.download_icon);
The Android Open Source Project0c908882009-03-03 19:32:16 -080093 if (DrmRawContent.DRM_MIMETYPE_MESSAGE_STRING.equalsIgnoreCase(mimeType)) {
94 iv.setImageResource(R.drawable.ic_launcher_drm_file);
95 } else if (mimeType == null) {
96 iv.setVisibility(View.INVISIBLE);
97 } else {
98 Intent intent = new Intent(Intent.ACTION_VIEW);
99 intent.setDataAndType(Uri.fromParts("file", "", null), mimeType);
100 PackageManager pm = context.getPackageManager();
101 List<ResolveInfo> list = pm.queryIntentActivities(intent,
102 PackageManager.MATCH_DEFAULT_ONLY);
103 if (list.size() > 0) {
104 Drawable icon = list.get(0).activityInfo.loadIcon(pm);
105 iv.setImageDrawable(icon);
106 iv.setVisibility(View.VISIBLE);
107 } else {
108 iv.setVisibility(View.INVISIBLE);
109 }
110 }
111
Leon Scroggins24837452010-01-13 13:43:35 -0500112 TextView tv = (TextView) convertView.findViewById(R.id.download_title);
113 String title = getString(mTitleColumnId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800114 if (title == null) {
Leon Scroggins24837452010-01-13 13:43:35 -0500115 String fullFilename = getString(mFilenameColumnId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800116 if (fullFilename == null) {
117 title = r.getString(R.string.download_unknown_filename);
118 } else {
119 // We have a filename, so we can build a title from that
Leon Scrogginscc1a2672010-01-21 16:25:26 -0500120 title = Downloads.Impl.createTitleFromFilename(context, fullFilename,
121 getLong(0));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800122 }
123 }
124 tv.setText(title);
125
Leon Scroggins24837452010-01-13 13:43:35 -0500126 tv = (TextView) convertView.findViewById(R.id.domain);
127 tv.setText(getString(mDescColumnId));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800128
Leon Scroggins24837452010-01-13 13:43:35 -0500129 long totalBytes = getLong(mTotalBytesColumnId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800130
Leon Scroggins24837452010-01-13 13:43:35 -0500131 int status = getInt(mStatusColumnId);
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -0800132 if (Downloads.Impl.isStatusCompleted(status)) { // Download stopped
Leon Scroggins24837452010-01-13 13:43:35 -0500133 View v = convertView.findViewById(R.id.progress_text);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800134 v.setVisibility(View.GONE);
135
Leon Scroggins24837452010-01-13 13:43:35 -0500136 v = convertView.findViewById(R.id.download_progress);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800137 v.setVisibility(View.GONE);
138
Leon Scroggins24837452010-01-13 13:43:35 -0500139 tv = (TextView) convertView.findViewById(R.id.complete_text);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800140 tv.setVisibility(View.VISIBLE);
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -0800141 if (Downloads.Impl.isStatusError(status)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800142 tv.setText(getErrorText(status));
143 } else {
144 tv.setText(r.getString(R.string.download_success,
Leon Scroggins24837452010-01-13 13:43:35 -0500145 Formatter.formatFileSize(context, totalBytes)));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800146 }
147
Leon Scroggins24837452010-01-13 13:43:35 -0500148 long time = getLong(mDateColumnId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800149 Date d = new Date(time);
150 DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
Leon Scroggins24837452010-01-13 13:43:35 -0500151 tv = (TextView) convertView.findViewById(R.id.complete_date);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800152 tv.setVisibility(View.VISIBLE);
153 tv.setText(df.format(d));
154
155 } else { // Download is still running
Leon Scroggins24837452010-01-13 13:43:35 -0500156 tv = (TextView) convertView.findViewById(R.id.progress_text);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800157 tv.setVisibility(View.VISIBLE);
158
Leon Scroggins24837452010-01-13 13:43:35 -0500159 View progress = convertView.findViewById(R.id.download_progress);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800160 progress.setVisibility(View.VISIBLE);
161
Leon Scroggins24837452010-01-13 13:43:35 -0500162 View v = convertView.findViewById(R.id.complete_date);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800163 v.setVisibility(View.GONE);
164
Leon Scroggins24837452010-01-13 13:43:35 -0500165 v = convertView.findViewById(R.id.complete_text);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800166 v.setVisibility(View.GONE);
167
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -0800168 if (status == Downloads.Impl.STATUS_PENDING) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800169 tv.setText(r.getText(R.string.download_pending));
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -0800170 } else if (status == Downloads.Impl.STATUS_PENDING_PAUSED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800171 tv.setText(r.getText(R.string.download_pending_network));
172 } else {
173 ProgressBar pb = (ProgressBar) progress;
174
175 StringBuilder sb = new StringBuilder();
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -0800176 if (status == Downloads.Impl.STATUS_RUNNING) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800177 sb.append(r.getText(R.string.download_running));
178 } else {
179 sb.append(r.getText(R.string.download_running_paused));
180 }
181 if (totalBytes > 0) {
Leon Scroggins24837452010-01-13 13:43:35 -0500182 long currentBytes = getLong(mCurrentBytesColumnId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800183 int progressAmount = (int)(currentBytes * 100 / totalBytes);
184 sb.append(' ');
185 sb.append(progressAmount);
186 sb.append("% (");
Leon Scroggins24837452010-01-13 13:43:35 -0500187 sb.append(Formatter.formatFileSize(context, currentBytes));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800188 sb.append("/");
Leon Scroggins24837452010-01-13 13:43:35 -0500189 sb.append(Formatter.formatFileSize(context, totalBytes));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800190 sb.append(")");
191 pb.setIndeterminate(false);
192 pb.setProgress(progressAmount);
193 } else {
194 pb.setIndeterminate(true);
195 }
196 tv.setText(sb.toString());
197 }
198 }
Leon Scroggins24837452010-01-13 13:43:35 -0500199 return convertView;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800200 }
201
202 /**
203 * Provide the resource id for the error string.
204 * @param status status of the download item
205 * @return resource id for the error string.
206 */
207 public static int getErrorText(int status) {
208 switch (status) {
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -0800209 case Downloads.Impl.STATUS_NOT_ACCEPTABLE:
The Android Open Source Project0c908882009-03-03 19:32:16 -0800210 return R.string.download_not_acceptable;
211
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -0800212 case Downloads.Impl.STATUS_LENGTH_REQUIRED:
The Android Open Source Project0c908882009-03-03 19:32:16 -0800213 return R.string.download_length_required;
214
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -0800215 case Downloads.Impl.STATUS_PRECONDITION_FAILED:
The Android Open Source Project0c908882009-03-03 19:32:16 -0800216 return R.string.download_precondition_failed;
217
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -0800218 case Downloads.Impl.STATUS_CANCELED:
The Android Open Source Project0c908882009-03-03 19:32:16 -0800219 return R.string.download_canceled;
220
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -0800221 case Downloads.Impl.STATUS_FILE_ERROR:
The Android Open Source Project0c908882009-03-03 19:32:16 -0800222 return R.string.download_file_error;
223
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -0800224 case Downloads.Impl.STATUS_BAD_REQUEST:
225 case Downloads.Impl.STATUS_UNKNOWN_ERROR:
The Android Open Source Project0c908882009-03-03 19:32:16 -0800226 default:
227 return R.string.download_error;
228 }
229 }
230}