blob: f22c9fe60c5fb0f005b5e450c5369f7a5f124712 [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;
Leon Scroggins III3a04dd32010-04-26 09:36:01 -040029import android.os.Handler;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030import android.provider.Downloads;
31import android.text.format.Formatter;
Leon Scroggins24837452010-01-13 13:43:35 -050032import android.view.LayoutInflater;
The Android Open Source Project0c908882009-03-03 19:32:16 -080033import android.view.View;
Leon Scroggins24837452010-01-13 13:43:35 -050034import android.view.ViewGroup;
The Android Open Source Project0c908882009-03-03 19:32:16 -080035import android.widget.ImageView;
36import android.widget.ProgressBar;
Leon Scroggins24837452010-01-13 13:43:35 -050037import android.widget.RelativeLayout;
The Android Open Source Project0c908882009-03-03 19:32:16 -080038import android.widget.TextView;
39
The Android Open Source Project0c908882009-03-03 19:32:16 -080040import 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 */
Leon Scroggins24837452010-01-13 13:43:35 -050049public class BrowserDownloadAdapter extends DateSortedExpandableListAdapter {
The Android Open Source Project0c908882009-03-03 19:32:16 -080050
The Android Open Source Project0c908882009-03-03 19:32:16 -080051 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 Scroggins III3a04dd32010-04-26 09:36:01 -040059 public BrowserDownloadAdapter(Context context, Cursor c, int index,
60 Handler handler) {
61 super(context, c, index, handler);
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -080062 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 Scrogginsfedc4932010-01-26 14:15:01 -0500115 title = r.getString(R.string.download_unknown_filename);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800116 }
117 tv.setText(title);
118
Leon Scroggins24837452010-01-13 13:43:35 -0500119 tv = (TextView) convertView.findViewById(R.id.domain);
120 tv.setText(getString(mDescColumnId));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800121
Leon Scroggins24837452010-01-13 13:43:35 -0500122 long totalBytes = getLong(mTotalBytesColumnId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800123
Leon Scroggins24837452010-01-13 13:43:35 -0500124 int status = getInt(mStatusColumnId);
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -0800125 if (Downloads.Impl.isStatusCompleted(status)) { // Download stopped
Leon Scroggins24837452010-01-13 13:43:35 -0500126 View v = convertView.findViewById(R.id.progress_text);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800127 v.setVisibility(View.GONE);
128
Leon Scroggins24837452010-01-13 13:43:35 -0500129 v = convertView.findViewById(R.id.download_progress);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800130 v.setVisibility(View.GONE);
131
Leon Scroggins24837452010-01-13 13:43:35 -0500132 tv = (TextView) convertView.findViewById(R.id.complete_text);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800133 tv.setVisibility(View.VISIBLE);
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -0800134 if (Downloads.Impl.isStatusError(status)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800135 tv.setText(getErrorText(status));
136 } else {
137 tv.setText(r.getString(R.string.download_success,
Leon Scroggins24837452010-01-13 13:43:35 -0500138 Formatter.formatFileSize(context, totalBytes)));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800139 }
140
Leon Scroggins24837452010-01-13 13:43:35 -0500141 long time = getLong(mDateColumnId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800142 Date d = new Date(time);
143 DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
Leon Scroggins24837452010-01-13 13:43:35 -0500144 tv = (TextView) convertView.findViewById(R.id.complete_date);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800145 tv.setVisibility(View.VISIBLE);
146 tv.setText(df.format(d));
147
148 } else { // Download is still running
Leon Scroggins24837452010-01-13 13:43:35 -0500149 tv = (TextView) convertView.findViewById(R.id.progress_text);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800150 tv.setVisibility(View.VISIBLE);
151
Leon Scroggins24837452010-01-13 13:43:35 -0500152 View progress = convertView.findViewById(R.id.download_progress);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800153 progress.setVisibility(View.VISIBLE);
154
Leon Scroggins24837452010-01-13 13:43:35 -0500155 View v = convertView.findViewById(R.id.complete_date);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800156 v.setVisibility(View.GONE);
157
Leon Scroggins24837452010-01-13 13:43:35 -0500158 v = convertView.findViewById(R.id.complete_text);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800159 v.setVisibility(View.GONE);
160
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -0800161 if (status == Downloads.Impl.STATUS_PENDING) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800162 tv.setText(r.getText(R.string.download_pending));
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -0800163 } else if (status == Downloads.Impl.STATUS_PENDING_PAUSED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800164 tv.setText(r.getText(R.string.download_pending_network));
165 } else {
166 ProgressBar pb = (ProgressBar) progress;
167
168 StringBuilder sb = new StringBuilder();
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -0800169 if (status == Downloads.Impl.STATUS_RUNNING) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800170 sb.append(r.getText(R.string.download_running));
171 } else {
172 sb.append(r.getText(R.string.download_running_paused));
173 }
174 if (totalBytes > 0) {
Leon Scroggins24837452010-01-13 13:43:35 -0500175 long currentBytes = getLong(mCurrentBytesColumnId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800176 int progressAmount = (int)(currentBytes * 100 / totalBytes);
177 sb.append(' ');
178 sb.append(progressAmount);
179 sb.append("% (");
Leon Scroggins24837452010-01-13 13:43:35 -0500180 sb.append(Formatter.formatFileSize(context, currentBytes));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800181 sb.append("/");
Leon Scroggins24837452010-01-13 13:43:35 -0500182 sb.append(Formatter.formatFileSize(context, totalBytes));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800183 sb.append(")");
184 pb.setIndeterminate(false);
185 pb.setProgress(progressAmount);
186 } else {
187 pb.setIndeterminate(true);
188 }
189 tv.setText(sb.toString());
190 }
191 }
Leon Scroggins24837452010-01-13 13:43:35 -0500192 return convertView;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800193 }
194
195 /**
196 * Provide the resource id for the error string.
197 * @param status status of the download item
198 * @return resource id for the error string.
199 */
200 public static int getErrorText(int status) {
201 switch (status) {
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -0800202 case Downloads.Impl.STATUS_NOT_ACCEPTABLE:
The Android Open Source Project0c908882009-03-03 19:32:16 -0800203 return R.string.download_not_acceptable;
204
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -0800205 case Downloads.Impl.STATUS_LENGTH_REQUIRED:
The Android Open Source Project0c908882009-03-03 19:32:16 -0800206 return R.string.download_length_required;
207
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -0800208 case Downloads.Impl.STATUS_PRECONDITION_FAILED:
The Android Open Source Project0c908882009-03-03 19:32:16 -0800209 return R.string.download_precondition_failed;
210
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -0800211 case Downloads.Impl.STATUS_CANCELED:
The Android Open Source Project0c908882009-03-03 19:32:16 -0800212 return R.string.download_canceled;
213
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -0800214 case Downloads.Impl.STATUS_FILE_ERROR:
The Android Open Source Project0c908882009-03-03 19:32:16 -0800215 return R.string.download_file_error;
216
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -0800217 case Downloads.Impl.STATUS_BAD_REQUEST:
218 case Downloads.Impl.STATUS_UNKNOWN_ERROR:
The Android Open Source Project0c908882009-03-03 19:32:16 -0800219 default:
220 return R.string.download_error;
221 }
222 }
223}