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 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 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; |
| 57 | |
Jeff Hamilton | 8ce956c | 2010-08-17 11:13:53 -0500 | [diff] [blame] | 58 | public BrowserDownloadAdapter(Context context, Cursor c, int index) { |
| 59 | super(context, index); |
| 60 | changeCursor(c); |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 61 | mTitleColumnId = c.getColumnIndexOrThrow(Downloads.Impl.COLUMN_TITLE); |
| 62 | mDescColumnId = c.getColumnIndexOrThrow(Downloads.Impl.COLUMN_DESCRIPTION); |
| 63 | mStatusColumnId = c.getColumnIndexOrThrow(Downloads.Impl.COLUMN_STATUS); |
| 64 | mTotalBytesColumnId = c.getColumnIndexOrThrow(Downloads.Impl.COLUMN_TOTAL_BYTES); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 65 | mCurrentBytesColumnId = |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 66 | c.getColumnIndexOrThrow(Downloads.Impl.COLUMN_CURRENT_BYTES); |
| 67 | mMimetypeColumnId = c.getColumnIndexOrThrow(Downloads.Impl.COLUMN_MIME_TYPE); |
| 68 | mDateColumnId = c.getColumnIndexOrThrow(Downloads.Impl.COLUMN_LAST_MODIFICATION); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Override |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 72 | public View getChildView(int groupPosition, int childPosition, |
| 73 | boolean isLastChild, View convertView, ViewGroup parent) { |
| 74 | Context context = getContext(); |
| 75 | // The layout file uses a RelativeLayout, whereas the GroupViews use |
| 76 | // TextView. |
| 77 | if (null == convertView || !(convertView instanceof RelativeLayout)) { |
| 78 | convertView = LayoutInflater.from(context).inflate( |
| 79 | R.layout.browser_download_item, null); |
| 80 | } |
| 81 | |
| 82 | // Bail early if the Cursor is closed. |
| 83 | if (!moveCursorToChildPosition(groupPosition, childPosition)) { |
| 84 | return convertView; |
| 85 | } |
| 86 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 87 | Resources r = context.getResources(); |
| 88 | |
| 89 | // Retrieve the icon for this download |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 90 | String mimeType = getString(mMimetypeColumnId); |
| 91 | ImageView iv = (ImageView) convertView.findViewById(R.id.download_icon); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 92 | if (DrmRawContent.DRM_MIMETYPE_MESSAGE_STRING.equalsIgnoreCase(mimeType)) { |
| 93 | iv.setImageResource(R.drawable.ic_launcher_drm_file); |
| 94 | } else if (mimeType == null) { |
| 95 | iv.setVisibility(View.INVISIBLE); |
| 96 | } else { |
| 97 | Intent intent = new Intent(Intent.ACTION_VIEW); |
| 98 | intent.setDataAndType(Uri.fromParts("file", "", null), mimeType); |
| 99 | PackageManager pm = context.getPackageManager(); |
| 100 | List<ResolveInfo> list = pm.queryIntentActivities(intent, |
| 101 | PackageManager.MATCH_DEFAULT_ONLY); |
| 102 | if (list.size() > 0) { |
| 103 | Drawable icon = list.get(0).activityInfo.loadIcon(pm); |
| 104 | iv.setImageDrawable(icon); |
| 105 | iv.setVisibility(View.VISIBLE); |
| 106 | } else { |
| 107 | iv.setVisibility(View.INVISIBLE); |
| 108 | } |
| 109 | } |
| 110 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 111 | TextView tv = (TextView) convertView.findViewById(R.id.download_title); |
| 112 | String title = getString(mTitleColumnId); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 113 | if (title == null) { |
Leon Scroggins | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 114 | title = r.getString(R.string.download_unknown_filename); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 115 | } |
| 116 | tv.setText(title); |
| 117 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 118 | tv = (TextView) convertView.findViewById(R.id.domain); |
| 119 | tv.setText(getString(mDescColumnId)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 120 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 121 | long totalBytes = getLong(mTotalBytesColumnId); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 122 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 123 | int status = getInt(mStatusColumnId); |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 124 | if (Downloads.Impl.isStatusCompleted(status)) { // Download stopped |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 125 | View v = convertView.findViewById(R.id.progress_text); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 126 | v.setVisibility(View.GONE); |
| 127 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 128 | v = convertView.findViewById(R.id.download_progress); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 129 | v.setVisibility(View.GONE); |
| 130 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 131 | tv = (TextView) convertView.findViewById(R.id.complete_text); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 132 | tv.setVisibility(View.VISIBLE); |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 133 | if (Downloads.Impl.isStatusError(status)) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 134 | tv.setText(getErrorText(status)); |
| 135 | } else { |
| 136 | tv.setText(r.getString(R.string.download_success, |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 137 | Formatter.formatFileSize(context, totalBytes))); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 140 | long time = getLong(mDateColumnId); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 141 | Date d = new Date(time); |
| 142 | DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT); |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 143 | tv = (TextView) convertView.findViewById(R.id.complete_date); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 144 | tv.setVisibility(View.VISIBLE); |
| 145 | tv.setText(df.format(d)); |
| 146 | |
| 147 | } else { // Download is still running |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 148 | tv = (TextView) convertView.findViewById(R.id.progress_text); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 149 | tv.setVisibility(View.VISIBLE); |
| 150 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 151 | View progress = convertView.findViewById(R.id.download_progress); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 152 | progress.setVisibility(View.VISIBLE); |
| 153 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 154 | View v = convertView.findViewById(R.id.complete_date); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 155 | v.setVisibility(View.GONE); |
| 156 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 157 | v = convertView.findViewById(R.id.complete_text); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 158 | v.setVisibility(View.GONE); |
| 159 | |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 160 | if (status == Downloads.Impl.STATUS_PENDING) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 161 | tv.setText(r.getText(R.string.download_pending)); |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 162 | } else if (status == Downloads.Impl.STATUS_PENDING_PAUSED) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 163 | tv.setText(r.getText(R.string.download_pending_network)); |
| 164 | } else { |
| 165 | ProgressBar pb = (ProgressBar) progress; |
| 166 | |
| 167 | StringBuilder sb = new StringBuilder(); |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 168 | if (status == Downloads.Impl.STATUS_RUNNING) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 169 | sb.append(r.getText(R.string.download_running)); |
| 170 | } else { |
| 171 | sb.append(r.getText(R.string.download_running_paused)); |
| 172 | } |
| 173 | if (totalBytes > 0) { |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 174 | long currentBytes = getLong(mCurrentBytesColumnId); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 175 | int progressAmount = (int)(currentBytes * 100 / totalBytes); |
| 176 | sb.append(' '); |
| 177 | sb.append(progressAmount); |
| 178 | sb.append("% ("); |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 179 | sb.append(Formatter.formatFileSize(context, currentBytes)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 180 | sb.append("/"); |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 181 | sb.append(Formatter.formatFileSize(context, totalBytes)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 182 | sb.append(")"); |
| 183 | pb.setIndeterminate(false); |
| 184 | pb.setProgress(progressAmount); |
| 185 | } else { |
| 186 | pb.setIndeterminate(true); |
| 187 | } |
| 188 | tv.setText(sb.toString()); |
| 189 | } |
| 190 | } |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 191 | return convertView; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /** |
| 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. |
| 198 | */ |
| 199 | public static int getErrorText(int status) { |
| 200 | switch (status) { |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 201 | case Downloads.Impl.STATUS_NOT_ACCEPTABLE: |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 202 | return R.string.download_not_acceptable; |
| 203 | |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 204 | case Downloads.Impl.STATUS_LENGTH_REQUIRED: |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 205 | return R.string.download_length_required; |
| 206 | |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 207 | case Downloads.Impl.STATUS_PRECONDITION_FAILED: |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 208 | return R.string.download_precondition_failed; |
| 209 | |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 210 | case Downloads.Impl.STATUS_CANCELED: |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 211 | return R.string.download_canceled; |
| 212 | |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 213 | case Downloads.Impl.STATUS_FILE_ERROR: |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 214 | return R.string.download_file_error; |
| 215 | |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 216 | case Downloads.Impl.STATUS_BAD_REQUEST: |
| 217 | case Downloads.Impl.STATUS_UNKNOWN_ERROR: |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 218 | default: |
| 219 | return R.string.download_error; |
| 220 | } |
| 221 | } |
| 222 | } |