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 | |
| 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; |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 33 | import android.view.LayoutInflater; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 34 | import android.view.View; |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 35 | import android.view.ViewGroup; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 36 | import android.widget.ImageView; |
| 37 | import android.widget.ProgressBar; |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 38 | import android.widget.RelativeLayout; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 39 | import android.widget.TextView; |
| 40 | |
| 41 | import java.io.File; |
| 42 | import java.text.DateFormat; |
| 43 | import java.util.Date; |
| 44 | import java.util.List; |
| 45 | |
| 46 | /** |
| 47 | * This class is used to represent the data for the download list box. The only |
| 48 | * real work done by this class is to construct a custom view for the line |
| 49 | * items. |
| 50 | */ |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 51 | public class BrowserDownloadAdapter extends DateSortedExpandableListAdapter { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 52 | |
| 53 | private int mFilenameColumnId; |
| 54 | private int mTitleColumnId; |
| 55 | private int mDescColumnId; |
| 56 | private int mStatusColumnId; |
| 57 | private int mTotalBytesColumnId; |
| 58 | private int mCurrentBytesColumnId; |
| 59 | private int mMimetypeColumnId; |
| 60 | private int mDateColumnId; |
| 61 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 62 | public BrowserDownloadAdapter(Context context, Cursor c, int index) { |
| 63 | super(context, c, index); |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 64 | mFilenameColumnId = c.getColumnIndexOrThrow(Downloads.Impl._DATA); |
| 65 | mTitleColumnId = c.getColumnIndexOrThrow(Downloads.Impl.COLUMN_TITLE); |
| 66 | mDescColumnId = c.getColumnIndexOrThrow(Downloads.Impl.COLUMN_DESCRIPTION); |
| 67 | mStatusColumnId = c.getColumnIndexOrThrow(Downloads.Impl.COLUMN_STATUS); |
| 68 | mTotalBytesColumnId = c.getColumnIndexOrThrow(Downloads.Impl.COLUMN_TOTAL_BYTES); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 69 | mCurrentBytesColumnId = |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 70 | c.getColumnIndexOrThrow(Downloads.Impl.COLUMN_CURRENT_BYTES); |
| 71 | mMimetypeColumnId = c.getColumnIndexOrThrow(Downloads.Impl.COLUMN_MIME_TYPE); |
| 72 | mDateColumnId = c.getColumnIndexOrThrow(Downloads.Impl.COLUMN_LAST_MODIFICATION); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Override |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 76 | public View getChildView(int groupPosition, int childPosition, |
| 77 | boolean isLastChild, View convertView, ViewGroup parent) { |
| 78 | Context context = getContext(); |
| 79 | // The layout file uses a RelativeLayout, whereas the GroupViews use |
| 80 | // TextView. |
| 81 | if (null == convertView || !(convertView instanceof RelativeLayout)) { |
| 82 | convertView = LayoutInflater.from(context).inflate( |
| 83 | R.layout.browser_download_item, null); |
| 84 | } |
| 85 | |
| 86 | // Bail early if the Cursor is closed. |
| 87 | if (!moveCursorToChildPosition(groupPosition, childPosition)) { |
| 88 | return convertView; |
| 89 | } |
| 90 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 91 | Resources r = context.getResources(); |
| 92 | |
| 93 | // Retrieve the icon for this download |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 94 | String mimeType = getString(mMimetypeColumnId); |
| 95 | ImageView iv = (ImageView) convertView.findViewById(R.id.download_icon); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 96 | if (DrmRawContent.DRM_MIMETYPE_MESSAGE_STRING.equalsIgnoreCase(mimeType)) { |
| 97 | iv.setImageResource(R.drawable.ic_launcher_drm_file); |
| 98 | } else if (mimeType == null) { |
| 99 | iv.setVisibility(View.INVISIBLE); |
| 100 | } else { |
| 101 | Intent intent = new Intent(Intent.ACTION_VIEW); |
| 102 | intent.setDataAndType(Uri.fromParts("file", "", null), mimeType); |
| 103 | PackageManager pm = context.getPackageManager(); |
| 104 | List<ResolveInfo> list = pm.queryIntentActivities(intent, |
| 105 | PackageManager.MATCH_DEFAULT_ONLY); |
| 106 | if (list.size() > 0) { |
| 107 | Drawable icon = list.get(0).activityInfo.loadIcon(pm); |
| 108 | iv.setImageDrawable(icon); |
| 109 | iv.setVisibility(View.VISIBLE); |
| 110 | } else { |
| 111 | iv.setVisibility(View.INVISIBLE); |
| 112 | } |
| 113 | } |
| 114 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 115 | TextView tv = (TextView) convertView.findViewById(R.id.download_title); |
| 116 | String title = getString(mTitleColumnId); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 117 | if (title == null) { |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 118 | String fullFilename = getString(mFilenameColumnId); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 119 | if (fullFilename == null) { |
| 120 | title = r.getString(R.string.download_unknown_filename); |
| 121 | } else { |
| 122 | // We have a filename, so we can build a title from that |
| 123 | title = new File(fullFilename).getName(); |
| 124 | ContentValues values = new ContentValues(); |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 125 | values.put(Downloads.Impl.COLUMN_TITLE, title); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 126 | // assume "_id" is the first column for the cursor |
| 127 | context.getContentResolver().update( |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 128 | ContentUris.withAppendedId(Downloads.Impl.CONTENT_URI, |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 129 | getLong(0)), values, null, null); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | tv.setText(title); |
| 133 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 134 | tv = (TextView) convertView.findViewById(R.id.domain); |
| 135 | tv.setText(getString(mDescColumnId)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 136 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 137 | long totalBytes = getLong(mTotalBytesColumnId); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 138 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 139 | int status = getInt(mStatusColumnId); |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 140 | if (Downloads.Impl.isStatusCompleted(status)) { // Download stopped |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 141 | View v = convertView.findViewById(R.id.progress_text); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 142 | v.setVisibility(View.GONE); |
| 143 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 144 | v = convertView.findViewById(R.id.download_progress); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 145 | v.setVisibility(View.GONE); |
| 146 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 147 | tv = (TextView) convertView.findViewById(R.id.complete_text); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 148 | tv.setVisibility(View.VISIBLE); |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 149 | if (Downloads.Impl.isStatusError(status)) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 150 | tv.setText(getErrorText(status)); |
| 151 | } else { |
| 152 | tv.setText(r.getString(R.string.download_success, |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 153 | Formatter.formatFileSize(context, totalBytes))); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 156 | long time = getLong(mDateColumnId); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 157 | Date d = new Date(time); |
| 158 | DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT); |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 159 | tv = (TextView) convertView.findViewById(R.id.complete_date); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 160 | tv.setVisibility(View.VISIBLE); |
| 161 | tv.setText(df.format(d)); |
| 162 | |
| 163 | } else { // Download is still running |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 164 | tv = (TextView) convertView.findViewById(R.id.progress_text); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 165 | tv.setVisibility(View.VISIBLE); |
| 166 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 167 | View progress = convertView.findViewById(R.id.download_progress); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 168 | progress.setVisibility(View.VISIBLE); |
| 169 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 170 | View v = convertView.findViewById(R.id.complete_date); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 171 | v.setVisibility(View.GONE); |
| 172 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 173 | v = convertView.findViewById(R.id.complete_text); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 174 | v.setVisibility(View.GONE); |
| 175 | |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 176 | if (status == Downloads.Impl.STATUS_PENDING) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 177 | tv.setText(r.getText(R.string.download_pending)); |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 178 | } else if (status == Downloads.Impl.STATUS_PENDING_PAUSED) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 179 | tv.setText(r.getText(R.string.download_pending_network)); |
| 180 | } else { |
| 181 | ProgressBar pb = (ProgressBar) progress; |
| 182 | |
| 183 | StringBuilder sb = new StringBuilder(); |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 184 | if (status == Downloads.Impl.STATUS_RUNNING) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 185 | sb.append(r.getText(R.string.download_running)); |
| 186 | } else { |
| 187 | sb.append(r.getText(R.string.download_running_paused)); |
| 188 | } |
| 189 | if (totalBytes > 0) { |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 190 | long currentBytes = getLong(mCurrentBytesColumnId); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 191 | int progressAmount = (int)(currentBytes * 100 / totalBytes); |
| 192 | sb.append(' '); |
| 193 | sb.append(progressAmount); |
| 194 | sb.append("% ("); |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 195 | sb.append(Formatter.formatFileSize(context, currentBytes)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 196 | sb.append("/"); |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 197 | sb.append(Formatter.formatFileSize(context, totalBytes)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 198 | sb.append(")"); |
| 199 | pb.setIndeterminate(false); |
| 200 | pb.setProgress(progressAmount); |
| 201 | } else { |
| 202 | pb.setIndeterminate(true); |
| 203 | } |
| 204 | tv.setText(sb.toString()); |
| 205 | } |
| 206 | } |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 207 | return convertView; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Provide the resource id for the error string. |
| 212 | * @param status status of the download item |
| 213 | * @return resource id for the error string. |
| 214 | */ |
| 215 | public static int getErrorText(int status) { |
| 216 | switch (status) { |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 217 | case Downloads.Impl.STATUS_NOT_ACCEPTABLE: |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 218 | return R.string.download_not_acceptable; |
| 219 | |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 220 | case Downloads.Impl.STATUS_LENGTH_REQUIRED: |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 221 | return R.string.download_length_required; |
| 222 | |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 223 | case Downloads.Impl.STATUS_PRECONDITION_FAILED: |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 224 | return R.string.download_precondition_failed; |
| 225 | |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 226 | case Downloads.Impl.STATUS_CANCELED: |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 227 | return R.string.download_canceled; |
| 228 | |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 229 | case Downloads.Impl.STATUS_FILE_ERROR: |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 230 | return R.string.download_file_error; |
| 231 | |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 232 | case Downloads.Impl.STATUS_BAD_REQUEST: |
| 233 | case Downloads.Impl.STATUS_UNKNOWN_ERROR: |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 234 | default: |
| 235 | return R.string.download_error; |
| 236 | } |
| 237 | } |
| 238 | } |