The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 18 | package com.android.browser; |
| 19 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 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; |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 31 | import android.view.LayoutInflater; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 32 | import android.view.View; |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 33 | import android.view.ViewGroup; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 34 | import android.widget.ImageView; |
| 35 | import android.widget.ProgressBar; |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 36 | import android.widget.RelativeLayout; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 37 | import android.widget.TextView; |
| 38 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 39 | import java.text.DateFormat; |
| 40 | import java.util.Date; |
| 41 | import 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 Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 48 | public class BrowserDownloadAdapter extends DateSortedExpandableListAdapter { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 49 | |
| 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 Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 59 | public BrowserDownloadAdapter(Context context, Cursor c, int index) { |
| 60 | super(context, c, index); |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 61 | 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 Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 66 | mCurrentBytesColumnId = |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 67 | 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 Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Override |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 73 | 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 Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 88 | Resources r = context.getResources(); |
| 89 | |
| 90 | // Retrieve the icon for this download |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 91 | String mimeType = getString(mMimetypeColumnId); |
| 92 | ImageView iv = (ImageView) convertView.findViewById(R.id.download_icon); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 93 | 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 Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 112 | TextView tv = (TextView) convertView.findViewById(R.id.download_title); |
| 113 | String title = getString(mTitleColumnId); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 114 | if (title == null) { |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 115 | String fullFilename = getString(mFilenameColumnId); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 116 | 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 Scroggins | cc1a267 | 2010-01-21 16:25:26 -0500 | [diff] [blame] | 120 | title = Downloads.Impl.createTitleFromFilename(context, fullFilename, |
| 121 | getLong(0)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | tv.setText(title); |
| 125 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 126 | tv = (TextView) convertView.findViewById(R.id.domain); |
| 127 | tv.setText(getString(mDescColumnId)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 128 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 129 | long totalBytes = getLong(mTotalBytesColumnId); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 130 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 131 | int status = getInt(mStatusColumnId); |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 132 | if (Downloads.Impl.isStatusCompleted(status)) { // Download stopped |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 133 | View v = convertView.findViewById(R.id.progress_text); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 134 | v.setVisibility(View.GONE); |
| 135 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 136 | v = convertView.findViewById(R.id.download_progress); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 137 | v.setVisibility(View.GONE); |
| 138 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 139 | tv = (TextView) convertView.findViewById(R.id.complete_text); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 140 | tv.setVisibility(View.VISIBLE); |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 141 | if (Downloads.Impl.isStatusError(status)) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 142 | tv.setText(getErrorText(status)); |
| 143 | } else { |
| 144 | tv.setText(r.getString(R.string.download_success, |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 145 | Formatter.formatFileSize(context, totalBytes))); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 148 | long time = getLong(mDateColumnId); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 149 | Date d = new Date(time); |
| 150 | DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT); |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 151 | tv = (TextView) convertView.findViewById(R.id.complete_date); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 152 | tv.setVisibility(View.VISIBLE); |
| 153 | tv.setText(df.format(d)); |
| 154 | |
| 155 | } else { // Download is still running |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 156 | tv = (TextView) convertView.findViewById(R.id.progress_text); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 157 | tv.setVisibility(View.VISIBLE); |
| 158 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 159 | View progress = convertView.findViewById(R.id.download_progress); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 160 | progress.setVisibility(View.VISIBLE); |
| 161 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 162 | View v = convertView.findViewById(R.id.complete_date); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 163 | v.setVisibility(View.GONE); |
| 164 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 165 | v = convertView.findViewById(R.id.complete_text); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 166 | v.setVisibility(View.GONE); |
| 167 | |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 168 | if (status == Downloads.Impl.STATUS_PENDING) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 169 | tv.setText(r.getText(R.string.download_pending)); |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 170 | } else if (status == Downloads.Impl.STATUS_PENDING_PAUSED) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 171 | tv.setText(r.getText(R.string.download_pending_network)); |
| 172 | } else { |
| 173 | ProgressBar pb = (ProgressBar) progress; |
| 174 | |
| 175 | StringBuilder sb = new StringBuilder(); |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 176 | if (status == Downloads.Impl.STATUS_RUNNING) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 177 | 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 Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 182 | long currentBytes = getLong(mCurrentBytesColumnId); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 183 | int progressAmount = (int)(currentBytes * 100 / totalBytes); |
| 184 | sb.append(' '); |
| 185 | sb.append(progressAmount); |
| 186 | sb.append("% ("); |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 187 | sb.append(Formatter.formatFileSize(context, currentBytes)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 188 | sb.append("/"); |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 189 | sb.append(Formatter.formatFileSize(context, totalBytes)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 190 | 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 Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 199 | return convertView; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 200 | } |
| 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 Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 209 | case Downloads.Impl.STATUS_NOT_ACCEPTABLE: |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 210 | return R.string.download_not_acceptable; |
| 211 | |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 212 | case Downloads.Impl.STATUS_LENGTH_REQUIRED: |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 213 | return R.string.download_length_required; |
| 214 | |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 215 | case Downloads.Impl.STATUS_PRECONDITION_FAILED: |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 216 | return R.string.download_precondition_failed; |
| 217 | |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 218 | case Downloads.Impl.STATUS_CANCELED: |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 219 | return R.string.download_canceled; |
| 220 | |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 221 | case Downloads.Impl.STATUS_FILE_ERROR: |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 222 | return R.string.download_file_error; |
| 223 | |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 224 | case Downloads.Impl.STATUS_BAD_REQUEST: |
| 225 | case Downloads.Impl.STATUS_UNKNOWN_ERROR: |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 226 | default: |
| 227 | return R.string.download_error; |
| 228 | } |
| 229 | } |
| 230 | } |