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