am cc167f03: (-s ours) am bb611587: (-s ours) Import translations. DO NOT MERGE
[android/platform/packages/providers/DownloadProvider.git] / src / com / android / providers / downloads / DownloadReceiver.java
index cbc963c..28e2a67 100644 (file)
@@ -18,10 +18,11 @@ package com.android.providers.downloads;
 
 import static android.app.DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED;
 import static android.app.DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION;
+import static com.android.providers.downloads.Constants.TAG;
 
 import android.app.DownloadManager;
-import android.content.ActivityNotFoundException;
 import android.content.BroadcastReceiver;
+import android.content.ContentResolver;
 import android.content.ContentUris;
 import android.content.ContentValues;
 import android.content.Context;
@@ -35,6 +36,7 @@ import android.os.HandlerThread;
 import android.provider.Downloads;
 import android.text.TextUtils;
 import android.util.Log;
+import android.util.Slog;
 import android.widget.Toast;
 
 import com.google.common.annotations.VisibleForTesting;
@@ -43,12 +45,10 @@ import com.google.common.annotations.VisibleForTesting;
  * Receives system broadcasts (boot, network connectivity)
  */
 public class DownloadReceiver extends BroadcastReceiver {
-    private static final String TAG = "DownloadReceiver";
-
     private static Handler sAsyncHandler;
 
     static {
-        final HandlerThread thread = new HandlerThread(TAG);
+        final HandlerThread thread = new HandlerThread("DownloadReceiver");
         thread.start();
         sAsyncHandler = new Handler(thread.getLooper());
     }
@@ -62,31 +62,37 @@ public class DownloadReceiver extends BroadcastReceiver {
             mSystemFacade = new RealSystemFacade(context);
         }
 
-        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);
-            }
+        final String action = intent.getAction();
+        if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
             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);
-            }
+
+        } else if (Intent.ACTION_MEDIA_MOUNTED.equals(action)) {
             startService(context);
-        } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
+
+        } else if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) {
             final ConnectivityManager connManager = (ConnectivityManager) context
                     .getSystemService(Context.CONNECTIVITY_SERVICE);
             final NetworkInfo info = connManager.getActiveNetworkInfo();
             if (info != null && info.isConnected()) {
                 startService(context);
             }
-        } else if (action.equals(Constants.ACTION_RETRY)) {
+
+        } else if (Intent.ACTION_UID_REMOVED.equals(action)) {
+            final PendingResult result = goAsync();
+            sAsyncHandler.post(new Runnable() {
+                @Override
+                public void run() {
+                    handleUidRemoved(context, intent);
+                    result.finish();
+                }
+            });
+
+        } else if (Constants.ACTION_RETRY.equals(action)) {
             startService(context);
-        } else if (action.equals(Constants.ACTION_OPEN)
-                || action.equals(Constants.ACTION_LIST)
-                || action.equals(Constants.ACTION_HIDE)) {
+
+        } else if (Constants.ACTION_OPEN.equals(action)
+                || Constants.ACTION_LIST.equals(action)
+                || Constants.ACTION_HIDE.equals(action)) {
 
             final PendingResult result = goAsync();
             if (result == null) {
@@ -94,7 +100,7 @@ public class DownloadReceiver extends BroadcastReceiver {
                 handleNotificationBroadcast(context, intent);
             } else {
                 sAsyncHandler.post(new Runnable() {
-                        @Override
+                    @Override
                     public void run() {
                         handleNotificationBroadcast(context, intent);
                         result.finish();
@@ -104,6 +110,18 @@ public class DownloadReceiver extends BroadcastReceiver {
         }
     }
 
+    private void handleUidRemoved(Context context, Intent intent) {
+        final ContentResolver resolver = context.getContentResolver();
+
+        final int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
+        final int count = resolver.delete(
+                Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI, Constants.UID + "=" + uid, null);
+
+        if (count > 0) {
+            Slog.d(TAG, "Deleted " + count + " downloads owned by UID " + uid);
+        }
+    }
+
     /**
      * Handle any broadcast related to a system notification.
      */
@@ -162,13 +180,8 @@ public class DownloadReceiver extends BroadcastReceiver {
      * {@link DownloadManager#COLUMN_ID}.
      */
     private void openDownload(Context context, long id) {
-        final Intent intent = OpenHelper.buildViewIntent(context, id);
-        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-        try {
-            context.startActivity(intent);
-        } catch (ActivityNotFoundException ex) {
-            Log.d(Constants.TAG, "no activity for " + intent, ex);
-            Toast.makeText(context, R.string.download_no_application_title, Toast.LENGTH_LONG)
+        if (!OpenHelper.startViewIntent(context, id, Intent.FLAG_ACTIVITY_NEW_TASK)) {
+            Toast.makeText(context, R.string.download_no_application_title, Toast.LENGTH_SHORT)
                     .show();
         }
     }