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