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 | package com.android.browser; |
| 18 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 19 | import android.app.AlertDialog; |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 20 | import android.app.ExpandableListActivity; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 21 | import android.content.ActivityNotFoundException; |
| 22 | import android.content.ContentValues; |
| 23 | import android.content.DialogInterface; |
| 24 | import android.content.Intent; |
| 25 | import android.content.ContentUris; |
| 26 | import android.content.pm.PackageManager; |
| 27 | import android.content.pm.ResolveInfo; |
| 28 | import android.database.Cursor; |
| 29 | import android.net.Uri; |
| 30 | import android.os.Bundle; |
| 31 | import android.provider.Downloads; |
| 32 | import android.view.ContextMenu; |
| 33 | import android.view.ContextMenu.ContextMenuInfo; |
| 34 | import android.view.LayoutInflater; |
| 35 | import android.view.Menu; |
| 36 | import android.view.MenuItem; |
| 37 | import android.view.MenuInflater; |
| 38 | import android.view.View; |
| 39 | import android.view.ViewGroup.LayoutParams; |
| 40 | import android.widget.AdapterView; |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 41 | import android.widget.ExpandableListView; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 42 | |
| 43 | import java.io.File; |
| 44 | import java.util.List; |
| 45 | |
| 46 | /** |
| 47 | * View showing the user's current browser downloads |
| 48 | */ |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 49 | public class BrowserDownloadPage extends ExpandableListActivity { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 50 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 51 | private ExpandableListView mListView; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 52 | private Cursor mDownloadCursor; |
| 53 | private BrowserDownloadAdapter mDownloadAdapter; |
| 54 | private int mStatusColumnId; |
| 55 | private int mIdColumnId; |
| 56 | private int mTitleColumnId; |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 57 | private long mContextMenuPosition; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 58 | |
| 59 | @Override |
| 60 | public void onCreate(Bundle icicle) { |
| 61 | super.onCreate(icicle); |
| 62 | setContentView(R.layout.browser_downloads_page); |
| 63 | |
| 64 | setTitle(getText(R.string.download_title)); |
| 65 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 66 | mListView = (ExpandableListView) findViewById(android.R.id.list); |
Mihai Preda | 83817df | 2009-04-28 14:24:47 +0200 | [diff] [blame] | 67 | mListView.setEmptyView(findViewById(R.id.empty)); |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 68 | mDownloadCursor = managedQuery(Downloads.Impl.CONTENT_URI, |
Leon Scroggins | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 69 | new String [] {Downloads.Impl._ID, Downloads.Impl.COLUMN_TITLE, |
| 70 | Downloads.Impl.COLUMN_STATUS, Downloads.Impl.COLUMN_TOTAL_BYTES, |
| 71 | Downloads.Impl.COLUMN_CURRENT_BYTES, |
| 72 | Downloads.Impl.COLUMN_DESCRIPTION, |
| 73 | Downloads.Impl.COLUMN_NOTIFICATION_PACKAGE, |
| 74 | Downloads.Impl.COLUMN_LAST_MODIFICATION, |
| 75 | Downloads.Impl.COLUMN_VISIBILITY, |
| 76 | Downloads.Impl.COLUMN_MIME_TYPE}, |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 77 | null, Downloads.Impl.COLUMN_LAST_MODIFICATION + " DESC"); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 78 | |
| 79 | // only attach everything to the listbox if we can access |
| 80 | // the download database. Otherwise, just show it empty |
| 81 | if (mDownloadCursor != null) { |
| 82 | mStatusColumnId = |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 83 | mDownloadCursor.getColumnIndexOrThrow(Downloads.Impl.COLUMN_STATUS); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 84 | mIdColumnId = |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 85 | mDownloadCursor.getColumnIndexOrThrow(Downloads.Impl._ID); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 86 | mTitleColumnId = |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 87 | mDownloadCursor.getColumnIndexOrThrow(Downloads.Impl.COLUMN_TITLE); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 88 | |
| 89 | // Create a list "controller" for the data |
| 90 | mDownloadAdapter = new BrowserDownloadAdapter(this, |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 91 | mDownloadCursor, mDownloadCursor.getColumnIndexOrThrow( |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 92 | Downloads.Impl.COLUMN_LAST_MODIFICATION)); |
Mihai Preda | 83817df | 2009-04-28 14:24:47 +0200 | [diff] [blame] | 93 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 94 | setListAdapter(mDownloadAdapter); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 95 | mListView.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET); |
| 96 | mListView.setOnCreateContextMenuListener(this); |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 97 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 98 | Intent intent = getIntent(); |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 99 | final int groupToShow = intent == null || intent.getData() == null |
| 100 | ? 0 : checkStatus(ContentUris.parseId(intent.getData())); |
| 101 | if (mDownloadAdapter.getGroupCount() > groupToShow) { |
| 102 | mListView.post(new Runnable() { |
| 103 | public void run() { |
| 104 | if (mDownloadAdapter.getGroupCount() > groupToShow) { |
| 105 | mListView.expandGroup(groupToShow); |
| 106 | } |
| 107 | } |
| 108 | }); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | } |
Leon Scroggins | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 112 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 113 | @Override |
| 114 | public boolean onCreateOptionsMenu(Menu menu) { |
| 115 | if (mDownloadCursor != null) { |
| 116 | MenuInflater inflater = getMenuInflater(); |
| 117 | inflater.inflate(R.menu.downloadhistory, menu); |
| 118 | } |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public boolean onPrepareOptionsMenu(Menu menu) { |
| 124 | boolean showCancel = getCancelableCount() > 0; |
| 125 | menu.findItem(R.id.download_menu_cancel_all).setEnabled(showCancel); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 126 | return super.onPrepareOptionsMenu(menu); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public boolean onOptionsItemSelected(MenuItem item) { |
| 131 | switch (item.getItemId()) { |
| 132 | case R.id.download_menu_cancel_all: |
| 133 | promptCancelAll(); |
| 134 | return true; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 135 | } |
| 136 | return false; |
| 137 | } |
| 138 | |
Leon Scroggins | 9be1733 | 2010-01-14 15:44:06 -0500 | [diff] [blame] | 139 | /** |
| 140 | * Remove the file from the list of downloads. |
| 141 | * @param id Unique ID of the download to remove. |
| 142 | */ |
| 143 | private void clearFromDownloads(long id) { |
| 144 | getContentResolver().delete(ContentUris.withAppendedId( |
| 145 | Downloads.Impl.CONTENT_URI, id), null, null); |
| 146 | } |
| 147 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 148 | @Override |
| 149 | public boolean onContextItemSelected(MenuItem item) { |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 150 | if (!mDownloadAdapter.moveCursorToPackedChildPosition( |
| 151 | mContextMenuPosition)) { |
| 152 | return false; |
| 153 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 154 | switch (item.getItemId()) { |
| 155 | case R.id.download_menu_open: |
| 156 | hideCompletedDownload(); |
Leon Scroggins | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 157 | openOrDeleteCurrentDownload(false); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 158 | return true; |
Leon Scroggins | 9be1733 | 2010-01-14 15:44:06 -0500 | [diff] [blame] | 159 | |
| 160 | case R.id.download_menu_delete: |
Leon Scroggins | 9be1733 | 2010-01-14 15:44:06 -0500 | [diff] [blame] | 161 | new AlertDialog.Builder(this) |
| 162 | .setTitle(R.string.download_delete_file) |
| 163 | .setIcon(android.R.drawable.ic_dialog_alert) |
Leon Scroggins | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 164 | .setMessage(mDownloadCursor.getString(mTitleColumnId)) |
Leon Scroggins | 9be1733 | 2010-01-14 15:44:06 -0500 | [diff] [blame] | 165 | .setNegativeButton(R.string.cancel, null) |
| 166 | .setPositiveButton(R.string.ok, |
| 167 | new DialogInterface.OnClickListener() { |
| 168 | public void onClick(DialogInterface dialog, |
| 169 | int whichButton) { |
Leon Scroggins | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 170 | openOrDeleteCurrentDownload(true); |
Leon Scroggins | 9be1733 | 2010-01-14 15:44:06 -0500 | [diff] [blame] | 171 | } |
| 172 | }) |
| 173 | .show(); |
| 174 | break; |
| 175 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 176 | case R.id.download_menu_clear: |
| 177 | case R.id.download_menu_cancel: |
Leon Scroggins | 9be1733 | 2010-01-14 15:44:06 -0500 | [diff] [blame] | 178 | clearFromDownloads(mDownloadCursor.getLong(mIdColumnId)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 179 | return true; |
| 180 | } |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public void onCreateContextMenu(ContextMenu menu, View v, |
| 186 | ContextMenuInfo menuInfo) { |
| 187 | if (mDownloadCursor != null) { |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 188 | ExpandableListView.ExpandableListContextMenuInfo info |
| 189 | = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo; |
| 190 | long packedPosition = info.packedPosition; |
| 191 | // Only show a context menu for the child views |
| 192 | if (!mDownloadAdapter.moveCursorToPackedChildPosition( |
| 193 | packedPosition)) { |
| 194 | return; |
| 195 | } |
| 196 | mContextMenuPosition = packedPosition; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 197 | menu.setHeaderTitle(mDownloadCursor.getString(mTitleColumnId)); |
| 198 | |
| 199 | MenuInflater inflater = getMenuInflater(); |
| 200 | int status = mDownloadCursor.getInt(mStatusColumnId); |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 201 | if (Downloads.Impl.isStatusSuccess(status)) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 202 | inflater.inflate(R.menu.downloadhistorycontextfinished, menu); |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 203 | } else if (Downloads.Impl.isStatusError(status)) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 204 | inflater.inflate(R.menu.downloadhistorycontextfailed, menu); |
| 205 | } else { |
| 206 | inflater.inflate(R.menu.downloadhistorycontextrunning, menu); |
| 207 | } |
| 208 | } |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 209 | super.onCreateContextMenu(menu, v, menuInfo); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /** |
| 213 | * This function is called to check the status of the download and if it |
| 214 | * has an error show an error dialog. |
| 215 | * @param id Row id of the download to check |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 216 | * @return Group which contains the download |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 217 | */ |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 218 | private int checkStatus(final long id) { |
| 219 | int groupToShow = mDownloadAdapter.groupFromChildId(id); |
| 220 | if (-1 == groupToShow) return 0; |
| 221 | int status = mDownloadCursor.getInt(mStatusColumnId); |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 222 | if (!Downloads.Impl.isStatusError(status)) { |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 223 | return groupToShow; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 224 | } |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 225 | if (status == Downloads.Impl.STATUS_FILE_ERROR) { |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 226 | String title = mDownloadCursor.getString(mTitleColumnId); |
| 227 | if (title == null || title.length() == 0) { |
| 228 | title = getString(R.string.download_unknown_filename); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 229 | } |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 230 | String msg = getString(R.string.download_file_error_dlg_msg, title); |
| 231 | new AlertDialog.Builder(this) |
| 232 | .setTitle(R.string.download_file_error_dlg_title) |
| 233 | .setIcon(android.R.drawable.ic_popup_disk_full) |
| 234 | .setMessage(msg) |
| 235 | .setPositiveButton(R.string.ok, null) |
| 236 | .setNegativeButton(R.string.retry, |
| 237 | new DialogInterface.OnClickListener() { |
| 238 | public void onClick(DialogInterface dialog, |
| 239 | int whichButton) { |
| 240 | resumeDownload(id); |
| 241 | } |
| 242 | }) |
| 243 | .show(); |
| 244 | } else { |
| 245 | new AlertDialog.Builder(this) |
| 246 | .setTitle(R.string.download_failed_generic_dlg_title) |
| 247 | .setIcon(R.drawable.ssl_icon) |
| 248 | .setMessage(BrowserDownloadAdapter.getErrorText(status)) |
| 249 | .setPositiveButton(R.string.ok, null) |
| 250 | .show(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 251 | } |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 252 | return groupToShow; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Resume a given download |
| 257 | * @param id Row id of the download to resume |
| 258 | */ |
| 259 | private void resumeDownload(final long id) { |
| 260 | // the relevant functionality doesn't exist in the download manager |
| 261 | } |
| 262 | |
| 263 | /** |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 264 | * Return the number of items in the list that can be canceled. |
| 265 | * @return count |
| 266 | */ |
| 267 | private int getCancelableCount() { |
| 268 | // Count the number of items that will be canceled. |
| 269 | int count = 0; |
| 270 | if (mDownloadCursor != null) { |
| 271 | for (mDownloadCursor.moveToFirst(); !mDownloadCursor.isAfterLast(); |
| 272 | mDownloadCursor.moveToNext()) { |
| 273 | int status = mDownloadCursor.getInt(mStatusColumnId); |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 274 | if (!Downloads.Impl.isStatusCompleted(status)) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 275 | count++; |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | return count; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Prompt the user if they would like to clear the download history |
| 285 | */ |
| 286 | private void promptCancelAll() { |
| 287 | int count = getCancelableCount(); |
| 288 | |
| 289 | // If there is nothing to do, just return |
| 290 | if (count == 0) { |
| 291 | return; |
| 292 | } |
| 293 | |
| 294 | // Don't show the dialog if there is only one download |
| 295 | if (count == 1) { |
| 296 | cancelAllDownloads(); |
| 297 | return; |
| 298 | } |
| 299 | String msg = |
| 300 | getString(R.string.download_cancel_dlg_msg, count); |
| 301 | new AlertDialog.Builder(this) |
| 302 | .setTitle(R.string.download_cancel_dlg_title) |
| 303 | .setIcon(R.drawable.ssl_icon) |
| 304 | .setMessage(msg) |
| 305 | .setPositiveButton(R.string.ok, |
| 306 | new DialogInterface.OnClickListener() { |
| 307 | public void onClick(DialogInterface dialog, |
| 308 | int whichButton) { |
| 309 | cancelAllDownloads(); |
| 310 | } |
| 311 | }) |
| 312 | .setNegativeButton(R.string.cancel, null) |
| 313 | .show(); |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Cancel all downloads. As canceled downloads are not |
| 318 | * listed, we removed them from the db. Removing a download |
| 319 | * record, cancels the download. |
| 320 | */ |
| 321 | private void cancelAllDownloads() { |
| 322 | if (mDownloadCursor.moveToFirst()) { |
| 323 | StringBuilder where = new StringBuilder(); |
| 324 | boolean firstTime = true; |
| 325 | while (!mDownloadCursor.isAfterLast()) { |
| 326 | int status = mDownloadCursor.getInt(mStatusColumnId); |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 327 | if (!Downloads.Impl.isStatusCompleted(status)) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 328 | if (firstTime) { |
| 329 | firstTime = false; |
| 330 | } else { |
| 331 | where.append(" OR "); |
| 332 | } |
| 333 | where.append("( "); |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 334 | where.append(Downloads.Impl._ID); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 335 | where.append(" = '"); |
| 336 | where.append(mDownloadCursor.getLong(mIdColumnId)); |
| 337 | where.append("' )"); |
| 338 | } |
| 339 | mDownloadCursor.moveToNext(); |
| 340 | } |
| 341 | if (!firstTime) { |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 342 | getContentResolver().delete(Downloads.Impl.CONTENT_URI, |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 343 | where.toString(), null); |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | private int getClearableCount() { |
| 349 | int count = 0; |
| 350 | if (mDownloadCursor.moveToFirst()) { |
| 351 | while (!mDownloadCursor.isAfterLast()) { |
| 352 | int status = mDownloadCursor.getInt(mStatusColumnId); |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 353 | if (Downloads.Impl.isStatusCompleted(status)) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 354 | count++; |
| 355 | } |
| 356 | mDownloadCursor.moveToNext(); |
| 357 | } |
| 358 | } |
| 359 | return count; |
| 360 | } |
Leon Scroggins | 4cf7de0 | 2010-01-21 10:05:59 -0500 | [diff] [blame] | 361 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 362 | /** |
Leon Scroggins | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 363 | * Open or delete content where the download db cursor currently is. Sends |
| 364 | * an Intent to perform the action. |
| 365 | * @param delete If true, delete the content. Otherwise open it. |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 366 | */ |
Leon Scroggins | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 367 | private void openOrDeleteCurrentDownload(boolean delete) { |
| 368 | int packageColumnId = mDownloadCursor.getColumnIndexOrThrow( |
| 369 | Downloads.Impl.COLUMN_NOTIFICATION_PACKAGE); |
| 370 | String packageName = mDownloadCursor.getString(packageColumnId); |
| 371 | Intent intent = new Intent(delete ? Intent.ACTION_DELETE |
| 372 | : Downloads.Impl.ACTION_NOTIFICATION_CLICKED); |
| 373 | Uri contentUri = ContentUris.withAppendedId( |
| 374 | Downloads.Impl.CONTENT_URI, |
| 375 | mDownloadCursor.getLong(mIdColumnId)); |
| 376 | intent.setData(contentUri); |
| 377 | intent.setPackage(packageName); |
| 378 | sendBroadcast(intent); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 379 | } |
| 380 | |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 381 | @Override |
| 382 | public boolean onChildClick(ExpandableListView parent, View v, |
| 383 | int groupPosition, int childPosition, long id) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 384 | // Open the selected item |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 385 | mDownloadAdapter.moveCursorToChildPosition(groupPosition, |
| 386 | childPosition); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 387 | |
| 388 | hideCompletedDownload(); |
| 389 | |
| 390 | int status = mDownloadCursor.getInt(mStatusColumnId); |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 391 | if (Downloads.Impl.isStatusSuccess(status)) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 392 | // Open it if it downloaded successfully |
Leon Scroggins | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 393 | openOrDeleteCurrentDownload(false); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 394 | } else { |
| 395 | // Check to see if there is an error. |
| 396 | checkStatus(id); |
| 397 | } |
Leon Scroggins | 2483745 | 2010-01-13 13:43:35 -0500 | [diff] [blame] | 398 | return true; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | /** |
| 402 | * hides the notification for the download pointed by mDownloadCursor |
| 403 | * if the download has completed. |
| 404 | */ |
| 405 | private void hideCompletedDownload() { |
| 406 | int status = mDownloadCursor.getInt(mStatusColumnId); |
| 407 | |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 408 | int visibilityColumn = mDownloadCursor.getColumnIndexOrThrow( |
| 409 | Downloads.Impl.COLUMN_VISIBILITY); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 410 | int visibility = mDownloadCursor.getInt(visibilityColumn); |
| 411 | |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 412 | if (Downloads.Impl.isStatusCompleted(status) && |
| 413 | visibility == Downloads.Impl.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 414 | ContentValues values = new ContentValues(); |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 415 | values.put(Downloads.Impl.COLUMN_VISIBILITY, Downloads.Impl.VISIBILITY_VISIBLE); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 416 | getContentResolver().update( |
Jean-Baptiste Queru | 1e5bad9 | 2010-01-14 16:09:03 -0800 | [diff] [blame] | 417 | ContentUris.withAppendedId(Downloads.Impl.CONTENT_URI, |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 418 | mDownloadCursor.getLong(mIdColumnId)), values, null, null); |
| 419 | } |
| 420 | } |
| 421 | } |