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; |
Leon Scroggins III | 3a04dd3 | 2010-04-26 09:36:01 -0400 | [diff] [blame] | 29 | import android.os.Handler; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 30 | import android.provider.Downloads; |
| 31 | import android.text.format.Formatter; |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 32 | import android.view.LayoutInflater; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 33 | import android.view.View; |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 34 | import android.view.ViewGroup; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 35 | import android.widget.ImageView; |
| 36 | import android.widget.ProgressBar; |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 37 | import android.widget.RelativeLayout; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 38 | import android.widget.TextView; |
| 39 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 40 | import java.text.DateFormat; |
| 41 | import java.util.Date; |
| 42 | import 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 Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 49 | public class BrowserDownloadAdapter extends DateSortedExpandableListAdapter { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 50 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 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 III | 3a04dd3 | 2010-04-26 09:36:01 -0400 | [diff] [blame] | 59 | public BrowserDownloadAdapter(Context context, Cursor c, int index, |
| 60 | Handler handler) { |
| 61 | super(context, c, index, handler); |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 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 | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 115 | title = r.getString(R.string.download_unknown_filename); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 116 | } |
| 117 | tv.setText(title); |
| 118 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 119 | tv = (TextView) convertView.findViewById(R.id.domain); |
| 120 | tv.setText(getString(mDescColumnId)); |
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 | long totalBytes = getLong(mTotalBytesColumnId); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 123 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 124 | int status = getInt(mStatusColumnId); |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 125 | if (Downloads.Impl.isStatusCompleted(status)) { // Download stopped |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 126 | View v = convertView.findViewById(R.id.progress_text); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 127 | v.setVisibility(View.GONE); |
| 128 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 129 | v = convertView.findViewById(R.id.download_progress); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 130 | v.setVisibility(View.GONE); |
| 131 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 132 | tv = (TextView) convertView.findViewById(R.id.complete_text); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 133 | tv.setVisibility(View.VISIBLE); |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 134 | if (Downloads.Impl.isStatusError(status)) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 135 | tv.setText(getErrorText(status)); |
| 136 | } else { |
| 137 | tv.setText(r.getString(R.string.download_success, |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 138 | Formatter.formatFileSize(context, totalBytes))); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 141 | long time = getLong(mDateColumnId); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 142 | Date d = new Date(time); |
| 143 | DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT); |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 144 | tv = (TextView) convertView.findViewById(R.id.complete_date); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 145 | tv.setVisibility(View.VISIBLE); |
| 146 | tv.setText(df.format(d)); |
| 147 | |
| 148 | } else { // Download is still running |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 149 | tv = (TextView) convertView.findViewById(R.id.progress_text); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 150 | tv.setVisibility(View.VISIBLE); |
| 151 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 152 | View progress = convertView.findViewById(R.id.download_progress); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 153 | progress.setVisibility(View.VISIBLE); |
| 154 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 155 | View v = convertView.findViewById(R.id.complete_date); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 156 | v.setVisibility(View.GONE); |
| 157 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 158 | v = convertView.findViewById(R.id.complete_text); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 159 | v.setVisibility(View.GONE); |
| 160 | |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 161 | if (status == Downloads.Impl.STATUS_PENDING) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 162 | tv.setText(r.getText(R.string.download_pending)); |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 163 | } else if (status == Downloads.Impl.STATUS_PENDING_PAUSED) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 164 | tv.setText(r.getText(R.string.download_pending_network)); |
| 165 | } else { |
| 166 | ProgressBar pb = (ProgressBar) progress; |
| 167 | |
| 168 | StringBuilder sb = new StringBuilder(); |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 169 | if (status == Downloads.Impl.STATUS_RUNNING) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 170 | 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 Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 175 | long currentBytes = getLong(mCurrentBytesColumnId); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 176 | int progressAmount = (int)(currentBytes * 100 / totalBytes); |
| 177 | sb.append(' '); |
| 178 | sb.append(progressAmount); |
| 179 | sb.append("% ("); |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 180 | sb.append(Formatter.formatFileSize(context, currentBytes)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 181 | sb.append("/"); |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 182 | sb.append(Formatter.formatFileSize(context, totalBytes)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 183 | 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 Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 192 | return convertView; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 193 | } |
| 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 Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 202 | case Downloads.Impl.STATUS_NOT_ACCEPTABLE: |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 203 | return R.string.download_not_acceptable; |
| 204 | |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 205 | case Downloads.Impl.STATUS_LENGTH_REQUIRED: |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 206 | return R.string.download_length_required; |
| 207 | |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 208 | case Downloads.Impl.STATUS_PRECONDITION_FAILED: |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 209 | return R.string.download_precondition_failed; |
| 210 | |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 211 | case Downloads.Impl.STATUS_CANCELED: |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 212 | return R.string.download_canceled; |
| 213 | |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 214 | case Downloads.Impl.STATUS_FILE_ERROR: |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 215 | return R.string.download_file_error; |
| 216 | |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 217 | case Downloads.Impl.STATUS_BAD_REQUEST: |
| 218 | case Downloads.Impl.STATUS_UNKNOWN_ERROR: |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 219 | default: |
| 220 | return R.string.download_error; |
| 221 | } |
| 222 | } |
| 223 | } |