Enabling storage notifications on TV

Bug: 35164265
Test: Using the Sabrent 120Gb external drive and manually going through all flows
Change-Id: If856cfd3b67bb97b60998d805f9ebb3d459cf28f
diff --git a/packages/SystemUI/src/com/android/systemui/usb/StorageNotification.java b/packages/SystemUI/src/com/android/systemui/usb/StorageNotification.java
index 43727e0..cfe16dd 100644
--- a/packages/SystemUI/src/com/android/systemui/usb/StorageNotification.java
+++ b/packages/SystemUI/src/com/android/systemui/usb/StorageNotification.java
@@ -19,6 +19,7 @@
 import android.annotation.NonNull;
 import android.app.Notification;
 import android.app.Notification.Action;
+import android.app.NotificationChannel;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.content.BroadcastReceiver;
@@ -35,6 +36,7 @@
 import android.os.storage.StorageManager;
 import android.os.storage.VolumeInfo;
 import android.os.storage.VolumeRecord;
+import android.provider.Settings;
 import android.text.TextUtils;
 import android.text.format.DateUtils;
 import android.util.Log;
@@ -181,6 +183,11 @@
     }
 
     private void updateMissingPrivateVolumes() {
+        if (isTv()) {
+            // On TV, TvSettings displays a modal full-screen activity in this case.
+            return;
+        }
+
         final List<VolumeRecord> recs = mStorageManager.getVolumeRecords();
         for (VolumeRecord rec : recs) {
             if (rec.getType() != VolumeInfo.TYPE_PRIVATE) continue;
@@ -210,7 +217,8 @@
                                 .setVisibility(Notification.VISIBILITY_PUBLIC)
                                 .setLocalOnly(true)
                                 .setCategory(Notification.CATEGORY_SYSTEM)
-                                .setDeleteIntent(buildSnoozeIntent(fsUuid));
+                                .setDeleteIntent(buildSnoozeIntent(fsUuid))
+                                .extend(new Notification.TvExtender());
                 SystemUI.overrideNotificationAppName(mContext, builder);
 
                 mNotificationManager.notifyAsUser(fsUuid, SystemMessage.NOTE_STORAGE_PRIVATE,
@@ -237,7 +245,8 @@
                             .setStyle(new Notification.BigTextStyle().bigText(text))
                             .setVisibility(Notification.VISIBILITY_PUBLIC)
                             .setLocalOnly(true)
-                            .setCategory(Notification.CATEGORY_ERROR);
+                            .setCategory(Notification.CATEGORY_ERROR)
+                            .extend(new Notification.TvExtender());
             SystemUI.overrideNotificationAppName(mContext, builder);
 
             mNotificationManager.notifyAsUser(disk.getId(), SystemMessage.NOTE_STORAGE_DISK,
@@ -378,7 +387,7 @@
                     .addAction(new Action(R.drawable.ic_eject_24dp,
                             mContext.getString(R.string.ext_media_unmount_action),
                             buildUnmountPendingIntent(vol)))
-                    .setContentIntent(browseIntent)
+                    .setContentIntent(buildUnmountPendingIntent(vol))
                     .setCategory(Notification.CATEGORY_SYSTEM);
             // Non-adoptable disks can't be snoozed.
             if (disk.isAdoptable()) {
@@ -571,15 +580,21 @@
                         .setContentText(text)
                         .setStyle(new Notification.BigTextStyle().bigText(text))
                         .setVisibility(Notification.VISIBILITY_PUBLIC)
-                        .setLocalOnly(true);
+                        .setLocalOnly(true)
+                        .extend(new Notification.TvExtender());
         overrideNotificationAppName(mContext, builder);
         return builder;
     }
 
     private PendingIntent buildInitPendingIntent(DiskInfo disk) {
         final Intent intent = new Intent();
-        intent.setClassName("com.android.settings",
-                "com.android.settings.deviceinfo.StorageWizardInit");
+        if (isTv()) {
+            intent.setPackage("com.android.tv.settings");
+            intent.setAction("com.android.tv.settings.action.NEW_STORAGE");
+        } else {
+            intent.setClassName("com.android.settings",
+                    "com.android.settings.deviceinfo.StorageWizardInit");
+        }
         intent.putExtra(DiskInfo.EXTRA_DISK_ID, disk.getId());
 
         final int requestKey = disk.getId().hashCode();
@@ -589,8 +604,13 @@
 
     private PendingIntent buildInitPendingIntent(VolumeInfo vol) {
         final Intent intent = new Intent();
-        intent.setClassName("com.android.settings",
-                "com.android.settings.deviceinfo.StorageWizardInit");
+        if (isTv()) {
+            intent.setPackage("com.android.tv.settings");
+            intent.setAction("com.android.tv.settings.action.NEW_STORAGE");
+        } else {
+            intent.setClassName("com.android.settings",
+                    "com.android.settings.deviceinfo.StorageWizardInit");
+        }
         intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, vol.getId());
 
         final int requestKey = vol.getId().hashCode();
@@ -600,13 +620,23 @@
 
     private PendingIntent buildUnmountPendingIntent(VolumeInfo vol) {
         final Intent intent = new Intent();
-        intent.setClassName("com.android.settings",
-                "com.android.settings.deviceinfo.StorageUnmountReceiver");
-        intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, vol.getId());
+        if (isTv()) {
+            intent.setPackage("com.android.tv.settings");
+            intent.setAction("com.android.tv.settings.action.UNMOUNT_STORAGE");
+            intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, vol.getId());
 
-        final int requestKey = vol.getId().hashCode();
-        return PendingIntent.getBroadcastAsUser(mContext, requestKey, intent,
-                PendingIntent.FLAG_CANCEL_CURRENT, UserHandle.CURRENT);
+            final int requestKey = vol.getId().hashCode();
+            return PendingIntent.getActivityAsUser(mContext, requestKey, intent,
+                    PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
+        } else {
+            intent.setClassName("com.android.settings",
+                    "com.android.settings.deviceinfo.StorageUnmountReceiver");
+            intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, vol.getId());
+
+            final int requestKey = vol.getId().hashCode();
+            return PendingIntent.getBroadcastAsUser(mContext, requestKey, intent,
+                    PendingIntent.FLAG_CANCEL_CURRENT, UserHandle.CURRENT);
+        }
     }
 
     private PendingIntent buildBrowsePendingIntent(VolumeInfo vol) {
@@ -619,17 +649,22 @@
 
     private PendingIntent buildVolumeSettingsPendingIntent(VolumeInfo vol) {
         final Intent intent = new Intent();
-        switch (vol.getType()) {
-            case VolumeInfo.TYPE_PRIVATE:
-                intent.setClassName("com.android.settings",
-                        "com.android.settings.Settings$PrivateVolumeSettingsActivity");
-                break;
-            case VolumeInfo.TYPE_PUBLIC:
-                intent.setClassName("com.android.settings",
-                        "com.android.settings.Settings$PublicVolumeSettingsActivity");
-                break;
-            default:
-                return null;
+        if (isTv()) {
+            intent.setPackage("com.android.tv.settings");
+            intent.setAction(Settings.ACTION_INTERNAL_STORAGE_SETTINGS);
+        } else {
+            switch (vol.getType()) {
+                case VolumeInfo.TYPE_PRIVATE:
+                    intent.setClassName("com.android.settings",
+                            "com.android.settings.Settings$PrivateVolumeSettingsActivity");
+                    break;
+                case VolumeInfo.TYPE_PUBLIC:
+                    intent.setClassName("com.android.settings",
+                            "com.android.settings.Settings$PublicVolumeSettingsActivity");
+                    break;
+                default:
+                    return null;
+            }
         }
         intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, vol.getId());
 
@@ -648,6 +683,7 @@
     }
 
     private PendingIntent buildForgetPendingIntent(VolumeRecord rec) {
+        // Not used on TV
         final Intent intent = new Intent();
         intent.setClassName("com.android.settings",
                 "com.android.settings.Settings$PrivateVolumeForgetActivity");
@@ -660,8 +696,13 @@
 
     private PendingIntent buildWizardMigratePendingIntent(MoveInfo move) {
         final Intent intent = new Intent();
-        intent.setClassName("com.android.settings",
-                "com.android.settings.deviceinfo.StorageWizardMigrateProgress");
+        if (isTv()) {
+            intent.setPackage("com.android.tv.settings");
+            intent.setAction("com.android.tv.settings.action.MIGRATE_STORAGE");
+        } else {
+            intent.setClassName("com.android.settings",
+                    "com.android.settings.deviceinfo.StorageWizardMigrateProgress");
+        }
         intent.putExtra(PackageManager.EXTRA_MOVE_ID, move.moveId);
 
         final VolumeInfo vol = mStorageManager.findVolumeByQualifiedUuid(move.volumeUuid);
@@ -674,8 +715,13 @@
 
     private PendingIntent buildWizardMovePendingIntent(MoveInfo move) {
         final Intent intent = new Intent();
-        intent.setClassName("com.android.settings",
-                "com.android.settings.deviceinfo.StorageWizardMoveProgress");
+        if (isTv()) {
+            intent.setPackage("com.android.tv.settings");
+            intent.setAction("com.android.tv.settings.action.MOVE_APP");
+        } else {
+            intent.setClassName("com.android.settings",
+                    "com.android.settings.deviceinfo.StorageWizardMoveProgress");
+        }
         intent.putExtra(PackageManager.EXTRA_MOVE_ID, move.moveId);
 
         return PendingIntent.getActivityAsUser(mContext, move.moveId, intent,
@@ -684,12 +730,22 @@
 
     private PendingIntent buildWizardReadyPendingIntent(DiskInfo disk) {
         final Intent intent = new Intent();
-        intent.setClassName("com.android.settings",
-                "com.android.settings.deviceinfo.StorageWizardReady");
+        if (isTv()) {
+            intent.setPackage("com.android.tv.settings");
+            intent.setAction(Settings.ACTION_INTERNAL_STORAGE_SETTINGS);
+        } else {
+            intent.setClassName("com.android.settings",
+                    "com.android.settings.deviceinfo.StorageWizardReady");
+        }
         intent.putExtra(DiskInfo.EXTRA_DISK_ID, disk.getId());
 
         final int requestKey = disk.getId().hashCode();
         return PendingIntent.getActivityAsUser(mContext, requestKey, intent,
                 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
     }
+
+    private boolean isTv() {
+        PackageManager packageManager = mContext.getPackageManager();
+        return packageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
+    }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/util/NotificationChannels.java b/packages/SystemUI/src/com/android/systemui/util/NotificationChannels.java
index 5911766..5df3beb 100644
--- a/packages/SystemUI/src/com/android/systemui/util/NotificationChannels.java
+++ b/packages/SystemUI/src/com/android/systemui/util/NotificationChannels.java
@@ -18,6 +18,7 @@
 import android.app.NotificationManager;
 
 import android.content.Context;
+import android.content.pm.PackageManager;
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.systemui.R;
 import com.android.systemui.SystemUI;
@@ -32,6 +33,7 @@
 
     @VisibleForTesting
     static void createAll(Context context) {
+
         final NotificationManager nm = context.getSystemService(NotificationManager.class);
         nm.createNotificationChannels(Arrays.asList(
                 new NotificationChannel(
@@ -49,7 +51,9 @@
                 new NotificationChannel(
                         STORAGE,
                         R.string.notification_channel_storage,
-                        NotificationManager.IMPORTANCE_LOW)
+                        isTv(context)
+                                ? NotificationManager.IMPORTANCE_DEFAULT
+                                : NotificationManager.IMPORTANCE_LOW)
                 ));
     }
 
@@ -57,4 +61,9 @@
     public void start() {
         createAll(mContext);
     }
+
+    private static boolean isTv(Context context) {
+        PackageManager packageManager = context.getPackageManager();
+        return packageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
+    }
 }