bug:3435895 listen to media_mounted broadcast intent
Vasu Nori [Wed, 9 Feb 2011 01:16:41 +0000 (17:16 -0800)]
and retry downloads that failed due to the error "sdcard media not mounted"

Change-Id: Id181b8167d331214f72679c85f18cc8b9b969e40

AndroidManifest.xml
src/com/android/providers/downloads/Constants.java
src/com/android/providers/downloads/DownloadInfo.java
src/com/android/providers/downloads/DownloadReceiver.java
src/com/android/providers/downloads/DownloadService.java
src/com/android/providers/downloads/DownloadThread.java

index 2e6a523..ad621af 100644 (file)
                 <action android:name="android.intent.action.BOOT_COMPLETED" />
                 <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
             </intent-filter>
+            <intent-filter>
+                <action android:name="android.intent.action.MEDIA_MOUNTED" />
+                <data android:scheme="file" />
+            </intent-filter>
         </receiver>
 
         <activity android:name=".SizeLimitActivity"
index ef0c6db..acf9732 100644 (file)
@@ -144,10 +144,10 @@ public class Constants {
     static final boolean LOGX = false;
 
     /** Enable verbose logging - use with "setprop log.tag.DownloadManager VERBOSE" */
-    private static final boolean LOCAL_LOGV = false;
+    private static final boolean LOCAL_LOGV = true; // STOPSHIP change this to false
     public static final boolean LOGV = LOCAL_LOGV && Log.isLoggable(TAG, Log.VERBOSE);
 
     /** Enable super-verbose logging */
-    private static final boolean LOCAL_LOGVV = false;
+    private static final boolean LOCAL_LOGVV = true; // STOPSHIP change this to false
     public static final boolean LOGVV = LOCAL_LOGVV && LOGV;
 }
index 3587dbf..2973937 100644 (file)
@@ -26,6 +26,7 @@ import android.database.Cursor;
 import android.drm.mobile1.DrmRawContent;
 import android.net.ConnectivityManager;
 import android.net.Uri;
+import android.os.Environment;
 import android.provider.Downloads;
 import android.provider.Downloads.Impl;
 import android.text.TextUtils;
@@ -300,6 +301,15 @@ public class DownloadInfo {
             case Downloads.Impl.STATUS_WAITING_TO_RETRY:
                 // download was waiting for a delayed restart
                 return restartTime(now) <= now;
+            case Downloads.Impl.STATUS_DEVICE_NOT_FOUND_ERROR:
+                // is the media mounted?
+                return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
+            case Downloads.Impl.STATUS_INSUFFICIENT_SPACE_ERROR:
+                // should check space to make sure it is worth retrying the download.
+                // but thats the first thing done by the thread when it retries to download
+                // it will fail pretty quickly if there is no space.
+                // so, it is not that bad to skip checking space availability here.
+                return true;
         }
         return false;
     }
index 3306639..7372e4a 100644 (file)
@@ -49,6 +49,16 @@ public class DownloadReceiver extends BroadcastReceiver {
 
         String action = intent.getAction();
         if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
+            if (Constants.LOGVV) {
+                Log.v(Constants.TAG, "Received broadcast intent for " +
+                        Intent.ACTION_BOOT_COMPLETED);
+            }
+            startService(context);
+        } else if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
+            if (Constants.LOGVV) {
+                Log.v(Constants.TAG, "Received broadcast intent for " +
+                        Intent.ACTION_MEDIA_MOUNTED);
+            }
             startService(context);
         } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
             NetworkInfo info = (NetworkInfo)
index d06c880..83001fd 100644 (file)
@@ -301,7 +301,10 @@ public class DownloadService extends Service {
                     DownloadInfo.Reader reader =
                             new DownloadInfo.Reader(getContentResolver(), cursor);
                     int idColumn = cursor.getColumnIndexOrThrow(Downloads.Impl._ID);
-
+                    if (Constants.LOGVV) {
+                        Log.i(Constants.TAG, "number of rows from downloads-db: " +
+                                cursor.getCount());
+                    }
                     for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                         long id = cursor.getLong(idColumn);
                         idsNoLongerInDatabase.remove(id);
@@ -425,7 +428,7 @@ public class DownloadService extends Service {
         mDownloads.put(info.mId, info);
 
         if (Constants.LOGVV) {
-            info.logVerboseInfo();
+            Log.v(Constants.TAG, "processing inserted download " + info.mId);
         }
 
         info.startIfReady(now, mStorageManager);
@@ -440,6 +443,8 @@ public class DownloadService extends Service {
         int oldStatus = info.mStatus;
 
         reader.updateFromDatabase(info);
+        Log.v(Constants.TAG, "processing updated download " + info.mId +
+                ", status: " + info.mStatus);
 
         boolean lostVisibility =
                 oldVisibility == Downloads.Impl.VISIBILITY_VISIBLE_NOTIFY_COMPLETED
index 960765d..de570ff 100644 (file)
@@ -836,6 +836,7 @@ public class DownloadThread extends Thread {
                     innerState.mContinuingDownload = true;
                     if (Constants.LOGV) {
                         Log.i(Constants.TAG, "resuming download for id: " + mInfo.mId +
+                                ", innerState.mBytesSoFar: " + innerState.mBytesSoFar +
                                 ", and setting mContinuingDownload to true: ");
                     }
                 }
@@ -861,6 +862,10 @@ public class DownloadThread extends Thread {
                 request.addHeader("If-Match", innerState.mHeaderETag);
             }
             request.addHeader("Range", "bytes=" + innerState.mBytesSoFar + "-");
+            if (Constants.LOGV) {
+                Log.i(Constants.TAG, "Adding Range header: " +
+                        "bytes=" + innerState.mBytesSoFar + "-");
+            }
         }
     }