2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package com.android.browser;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.PackageManager;
23 import android.content.pm.ResolveInfo;
24 import android.content.res.Resources;
25 import android.database.Cursor;
26 import android.drm.mobile1.DrmRawContent;
27 import android.graphics.drawable.Drawable;
28 import android.net.Uri;
29 import android.provider.Downloads;
30 import android.text.format.Formatter;
31 import android.view.LayoutInflater;
32 import android.view.View;
33 import android.view.ViewGroup;
34 import android.widget.ImageView;
35 import android.widget.ProgressBar;
36 import android.widget.RelativeLayout;
37 import android.widget.TextView;
39 import java.text.DateFormat;
40 import java.util.Date;
41 import java.util.List;
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
48 public class BrowserDownloadAdapter extends DateSortedExpandableListAdapter {
50 private int mTitleColumnId;
51 private int mDescColumnId;
52 private int mStatusColumnId;
53 private int mTotalBytesColumnId;
54 private int mCurrentBytesColumnId;
55 private int mMimetypeColumnId;
56 private int mDateColumnId;
58 public BrowserDownloadAdapter(Context context, Cursor c, int index) {
59 super(context, c, index);
60 mTitleColumnId = c.getColumnIndexOrThrow(Downloads.Impl.COLUMN_TITLE);
61 mDescColumnId = c.getColumnIndexOrThrow(Downloads.Impl.COLUMN_DESCRIPTION);
62 mStatusColumnId = c.getColumnIndexOrThrow(Downloads.Impl.COLUMN_STATUS);
63 mTotalBytesColumnId = c.getColumnIndexOrThrow(Downloads.Impl.COLUMN_TOTAL_BYTES);
64 mCurrentBytesColumnId =
65 c.getColumnIndexOrThrow(Downloads.Impl.COLUMN_CURRENT_BYTES);
66 mMimetypeColumnId = c.getColumnIndexOrThrow(Downloads.Impl.COLUMN_MIME_TYPE);
67 mDateColumnId = c.getColumnIndexOrThrow(Downloads.Impl.COLUMN_LAST_MODIFICATION);
71 public View getChildView(int groupPosition, int childPosition,
72 boolean isLastChild, View convertView, ViewGroup parent) {
73 Context context = getContext();
74 // The layout file uses a RelativeLayout, whereas the GroupViews use
76 if (null == convertView || !(convertView instanceof RelativeLayout)) {
77 convertView = LayoutInflater.from(context).inflate(
78 R.layout.browser_download_item, null);
81 // Bail early if the Cursor is closed.
82 if (!moveCursorToChildPosition(groupPosition, childPosition)) {
86 Resources r = context.getResources();
88 // Retrieve the icon for this download
89 String mimeType = getString(mMimetypeColumnId);
90 ImageView iv = (ImageView) convertView.findViewById(R.id.download_icon);
91 if (DrmRawContent.DRM_MIMETYPE_MESSAGE_STRING.equalsIgnoreCase(mimeType)) {
92 iv.setImageResource(R.drawable.ic_launcher_drm_file);
93 } else if (mimeType == null) {
94 iv.setVisibility(View.INVISIBLE);
96 Intent intent = new Intent(Intent.ACTION_VIEW);
97 intent.setDataAndType(Uri.fromParts("file", "", null), mimeType);
98 PackageManager pm = context.getPackageManager();
99 List<ResolveInfo> list = pm.queryIntentActivities(intent,
100 PackageManager.MATCH_DEFAULT_ONLY);
101 if (list.size() > 0) {
102 Drawable icon = list.get(0).activityInfo.loadIcon(pm);
103 iv.setImageDrawable(icon);
104 iv.setVisibility(View.VISIBLE);
106 iv.setVisibility(View.INVISIBLE);
110 TextView tv = (TextView) convertView.findViewById(R.id.download_title);
111 String title = getString(mTitleColumnId);
113 title = r.getString(R.string.download_unknown_filename);
117 tv = (TextView) convertView.findViewById(R.id.domain);
118 tv.setText(getString(mDescColumnId));
120 long totalBytes = getLong(mTotalBytesColumnId);
122 int status = getInt(mStatusColumnId);
123 if (Downloads.Impl.isStatusCompleted(status)) { // Download stopped
124 View v = convertView.findViewById(R.id.progress_text);
125 v.setVisibility(View.GONE);
127 v = convertView.findViewById(R.id.download_progress);
128 v.setVisibility(View.GONE);
130 tv = (TextView) convertView.findViewById(R.id.complete_text);
131 tv.setVisibility(View.VISIBLE);
132 if (Downloads.Impl.isStatusError(status)) {
133 tv.setText(getErrorText(status));
135 tv.setText(r.getString(R.string.download_success,
136 Formatter.formatFileSize(context, totalBytes)));
139 long time = getLong(mDateColumnId);
140 Date d = new Date(time);
141 DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
142 tv = (TextView) convertView.findViewById(R.id.complete_date);
143 tv.setVisibility(View.VISIBLE);
144 tv.setText(df.format(d));
146 } else { // Download is still running
147 tv = (TextView) convertView.findViewById(R.id.progress_text);
148 tv.setVisibility(View.VISIBLE);
150 View progress = convertView.findViewById(R.id.download_progress);
151 progress.setVisibility(View.VISIBLE);
153 View v = convertView.findViewById(R.id.complete_date);
154 v.setVisibility(View.GONE);
156 v = convertView.findViewById(R.id.complete_text);
157 v.setVisibility(View.GONE);
159 if (status == Downloads.Impl.STATUS_PENDING) {
160 tv.setText(r.getText(R.string.download_pending));
161 } else if (status == Downloads.Impl.STATUS_PENDING_PAUSED) {
162 tv.setText(r.getText(R.string.download_pending_network));
164 ProgressBar pb = (ProgressBar) progress;
166 StringBuilder sb = new StringBuilder();
167 if (status == Downloads.Impl.STATUS_RUNNING) {
168 sb.append(r.getText(R.string.download_running));
170 sb.append(r.getText(R.string.download_running_paused));
172 if (totalBytes > 0) {
173 long currentBytes = getLong(mCurrentBytesColumnId);
174 int progressAmount = (int)(currentBytes * 100 / totalBytes);
176 sb.append(progressAmount);
178 sb.append(Formatter.formatFileSize(context, currentBytes));
180 sb.append(Formatter.formatFileSize(context, totalBytes));
182 pb.setIndeterminate(false);
183 pb.setProgress(progressAmount);
185 pb.setIndeterminate(true);
187 tv.setText(sb.toString());
194 * Provide the resource id for the error string.
195 * @param status status of the download item
196 * @return resource id for the error string.
198 public static int getErrorText(int status) {
200 case Downloads.Impl.STATUS_NOT_ACCEPTABLE:
201 return R.string.download_not_acceptable;
203 case Downloads.Impl.STATUS_LENGTH_REQUIRED:
204 return R.string.download_length_required;
206 case Downloads.Impl.STATUS_PRECONDITION_FAILED:
207 return R.string.download_precondition_failed;
209 case Downloads.Impl.STATUS_CANCELED:
210 return R.string.download_canceled;
212 case Downloads.Impl.STATUS_FILE_ERROR:
213 return R.string.download_file_error;
215 case Downloads.Impl.STATUS_BAD_REQUEST:
216 case Downloads.Impl.STATUS_UNKNOWN_ERROR:
218 return R.string.download_error;