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.ContentUris;
21 import android.content.ContentValues;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.pm.PackageManager;
25 import android.content.pm.ResolveInfo;
26 import android.content.res.Resources;
27 import android.database.Cursor;
28 import android.drm.mobile1.DrmRawContent;
29 import android.graphics.drawable.Drawable;
30 import android.net.Uri;
31 import android.provider.Downloads;
32 import android.text.format.Formatter;
33 import android.view.View;
34 import android.widget.ImageView;
35 import android.widget.ProgressBar;
36 import android.widget.ResourceCursorAdapter;
37 import android.widget.TextView;
40 import java.text.DateFormat;
41 import java.util.Date;
42 import java.util.List;
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
49 public class BrowserDownloadAdapter extends ResourceCursorAdapter {
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;
60 public BrowserDownloadAdapter(Context context, int layout, Cursor c) {
61 super(context, layout, c);
62 mFilenameColumnId = c.getColumnIndexOrThrow(Downloads._DATA);
63 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);
67 mCurrentBytesColumnId =
68 c.getColumnIndexOrThrow(Downloads.COLUMN_CURRENT_BYTES);
69 mMimetypeColumnId = c.getColumnIndexOrThrow(Downloads.COLUMN_MIME_TYPE);
70 mDateColumnId = c.getColumnIndexOrThrow(Downloads.COLUMN_LAST_MODIFICATION);
74 public void bindView(View view, Context context, Cursor cursor) {
75 Resources r = context.getResources();
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);
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);
95 iv.setVisibility(View.INVISIBLE);
99 TextView tv = (TextView) view.findViewById(R.id.download_title);
100 String title = cursor.getString(mTitleColumnId);
102 String fullFilename = cursor.getString(mFilenameColumnId);
103 if (fullFilename == null) {
104 title = r.getString(R.string.download_unknown_filename);
106 // We have a filename, so we can build a title from that
107 title = new File(fullFilename).getName();
108 ContentValues values = new ContentValues();
109 values.put(Downloads.COLUMN_TITLE, title);
110 // 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);
118 tv = (TextView) view.findViewById(R.id.domain);
119 tv.setText(cursor.getString(mDescColumnId));
121 long totalBytes = cursor.getLong(mTotalBytesColumnId);
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);
128 v = view.findViewById(R.id.download_progress);
129 v.setVisibility(View.GONE);
131 tv = (TextView) view.findViewById(R.id.complete_text);
132 tv.setVisibility(View.VISIBLE);
133 if (Downloads.isStatusError(status)) {
134 tv.setText(getErrorText(status));
136 tv.setText(r.getString(R.string.download_success,
137 Formatter.formatFileSize(mContext, totalBytes)));
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));
147 } else { // Download is still running
148 tv = (TextView) view.findViewById(R.id.progress_text);
149 tv.setVisibility(View.VISIBLE);
151 View progress = view.findViewById(R.id.download_progress);
152 progress.setVisibility(View.VISIBLE);
154 View v = view.findViewById(R.id.complete_date);
155 v.setVisibility(View.GONE);
157 v = view.findViewById(R.id.complete_text);
158 v.setVisibility(View.GONE);
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));
165 ProgressBar pb = (ProgressBar) progress;
167 StringBuilder sb = new StringBuilder();
168 if (status == Downloads.STATUS_RUNNING) {
169 sb.append(r.getText(R.string.download_running));
171 sb.append(r.getText(R.string.download_running_paused));
173 if (totalBytes > 0) {
174 long currentBytes = cursor.getLong(mCurrentBytesColumnId);
175 int progressAmount = (int)(currentBytes * 100 / totalBytes);
177 sb.append(progressAmount);
179 sb.append(Formatter.formatFileSize(mContext, currentBytes));
181 sb.append(Formatter.formatFileSize(mContext, totalBytes));
183 pb.setIndeterminate(false);
184 pb.setProgress(progressAmount);
186 pb.setIndeterminate(true);
188 tv.setText(sb.toString());
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.
199 public static int getErrorText(int status) {
201 case Downloads.STATUS_NOT_ACCEPTABLE:
202 return R.string.download_not_acceptable;
204 case Downloads.STATUS_LENGTH_REQUIRED:
205 return R.string.download_length_required;
207 case Downloads.STATUS_PRECONDITION_FAILED:
208 return R.string.download_precondition_failed;
210 case Downloads.STATUS_CANCELED:
211 return R.string.download_canceled;
213 case Downloads.STATUS_FILE_ERROR:
214 return R.string.download_file_error;
216 case Downloads.STATUS_BAD_REQUEST:
217 case Downloads.STATUS_UNKNOWN_ERROR:
219 return R.string.download_error;