Fix incorrect updates of BUCKET_ID/BUCKET_DISPLAY_NAME.

Whenever any image/video gets updated, MediaProvider is incorrectly
setting BUCKET_ID/BUCKET_DISPLAY_NAME values to null. They shouldn't
be changed unless the filepath gets updated.

Bug: 122788554
Test: manual
Test: atest cts/tests/tests/provider/src/android/provider/cts/MediaStore*
Change-Id: I5254a5f7da5ec760046d54c26b35b8288d1da0dc
(cherry picked from commit 7cb3f9ec5cb425a7f6c1b4307bf3e8372c32852f)
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index b9cccb0..014d863 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -2,7 +2,7 @@
         package="com.android.providers.media"
         android:sharedUserId="android.media"
         android:sharedUserLabel="@string/uid_label"
-        android:versionCode="1011">
+        android:versionCode="1012">
 
     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
     <uses-permission android:name="android.permission.RECEIVE_DEVICE_CUSTOMIZATION_READY" />
diff --git a/src/com/android/providers/media/MediaProvider.java b/src/com/android/providers/media/MediaProvider.java
index 84565ad..e9c7746 100644
--- a/src/com/android/providers/media/MediaProvider.java
+++ b/src/com/android/providers/media/MediaProvider.java
@@ -948,6 +948,27 @@
         db.execSQL("ALTER TABLE files ADD COLUMN is_trashed INTEGER DEFAULT 0;");
     }
 
+    private static void updateFixBucketIds(SQLiteDatabase db, boolean internal) {
+        final String selection = "bucket_id IS NULL AND _data IS NOT NULL AND "
+                + "(media_type=? OR media_type=?)";
+        final String[] selectionArgs = new String[] {
+                String.valueOf(FileColumns.MEDIA_TYPE_IMAGE),
+                String.valueOf(FileColumns.MEDIA_TYPE_VIDEO)
+        };
+        final ContentValues values = new ContentValues();
+        try (Cursor cursor = db.query("files", new String[] { FileColumns._ID, FileColumns.DATA },
+                selection, selectionArgs, null, null, null)) {
+            while (cursor.moveToNext()) {
+                values.clear();
+                final long id = cursor.getLong(0);
+                final String data = cursor.getString(1);
+                values.put(FileColumns.DATA, data);
+                computeBucketValues(values);
+                db.update("files", values, "_id=" + id, null);
+            }
+        }
+    }
+
     static final int VERSION_J = 509;
     static final int VERSION_K = 700;
     static final int VERSION_L = 700;
@@ -955,7 +976,7 @@
     static final int VERSION_N = 800;
     static final int VERSION_O = 800;
     static final int VERSION_P = 900;
-    static final int VERSION_Q = 1011;
+    static final int VERSION_Q = 1012;
 
     /**
      * This method takes care of updating all the tables in the database to the
@@ -1006,6 +1027,9 @@
             if (fromVersion < 1010) {
                 updateAddExpiresAndTrashed(db, internal);
             }
+            if (fromVersion < 1012) {
+                updateFixBucketIds(db, internal);
+            }
         }
 
         // Always recreate latest views during upgrade; they're cheap and it's
@@ -1075,9 +1099,9 @@
 
     static void computeBucketValues(ContentValues values) {
         // Worst case we have to assume no bucket details
-        values.putNull(ImageColumns.BUCKET_ID);
-        values.putNull(ImageColumns.BUCKET_DISPLAY_NAME);
-        values.putNull(ImageColumns.SECONDARY_BUCKET_ID);
+        values.remove(ImageColumns.BUCKET_ID);
+        values.remove(ImageColumns.BUCKET_DISPLAY_NAME);
+        values.remove(ImageColumns.SECONDARY_BUCKET_ID);
 
         final String data = values.getAsString(MediaColumns.DATA);
         if (!TextUtils.isEmpty(data)) {