Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 89a5500d authored by Roman Birg's avatar Roman Birg
Browse files

frameworks: add persistent notification when USB OTG is connected



Tapping the notification will take the user into Storage settings, and
highlight the 'Unmount' option.

Change-Id: I650f3874a8295b2b8ffdaef78aff4b68e9cd574e
Signed-off-by: default avatarRoman Birg <roman@cyngn.com>
parent dcf2706f
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -229,4 +229,7 @@
    <string name="extmedia_format_message_cm" product="nosdcard">All data on your USB storage (under path \'%1$s\') will be erased. This action can\'t be reversed!</string>
    <!-- See EXTMEDIA_FORMAT.   This is the message. -->
    <string name="extmedia_format_message_cm" product="default">All data on your card (under path \'%1$s\') will be erased. This action can\'t be reversed!</string>

    <!-- USB OTG notification message -->
    <string name="usb_storage_notification_manage_message">Tap to manage</string>
</resources>
+3 −0
Original line number Diff line number Diff line
@@ -2034,4 +2034,7 @@
   <java-symbol type="anim" name="last_app_in" />
   <java-symbol type="anim" name="last_app_out" />

  <!-- USB OTG Notification -->
  <java-symbol type="string" name="usb_storage_notification_manage_message" />

</resources>
+28 −0
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import android.os.HandlerThread;
import android.os.UserHandle;
import android.os.storage.StorageEventListener;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.provider.Settings;
import android.util.Log;

@@ -38,6 +39,8 @@ public class StorageNotification extends SystemUI {
    private static final boolean DEBUG = false;

    private static final boolean POP_UMS_ACTIVITY_ON_CONNECT = true;
    public static final String KEY_UNMOUNT_USB = "key_unmount_usb";
    public static final String HIGHLIGHT_PREF_KEY = "pref_key";

    /**
     * The notification that is shown when a USB mass storage host
@@ -154,6 +157,7 @@ public class StorageNotification extends SystemUI {
             */
            setMediaStorageNotification(0, 0, 0, false, false, null);
            updateUsbMassStorageNotification(mUmsAvailable);
            maybeAddUsbUnmountNotification();
        } else if (newState.equals(Environment.MEDIA_UNMOUNTED)) {
            /*
             * Storage is now unmounted. We may have been unmounted
@@ -252,6 +256,30 @@ public class StorageNotification extends SystemUI {
        }
    }

    private void maybeAddUsbUnmountNotification() {
        for (StorageVolume volume : mStorageManager.getVolumeList()) {
            final String usbDiskDesc = Resources.getSystem().getString(
                    Resources.getSystem().getIdentifier("storage_usb", "string", "android"));
            final boolean isUsbStorage =  volume.getDescription(mContext).equals(usbDiskDesc);
            final boolean mounted = volume.getState().equals(Environment.MEDIA_MOUNTED)
                    || volume.getState().equals(Environment.MEDIA_MOUNTED_READ_ONLY);
            final boolean isRemovable = volume.isRemovable();

            if (isRemovable && isUsbStorage && mounted) {
                Intent intent = new Intent(Settings.ACTION_MEMORY_CARD_SETTINGS);
                intent.putExtra(HIGHLIGHT_PREF_KEY, KEY_UNMOUNT_USB);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                setUsbStorageNotification(
                        com.android.internal.R.string.usb_storage_stop_title,
                        com.android.internal.R.string.usb_storage_notification_manage_message,
                        com.android.internal.R.drawable.stat_sys_data_usb, false, true,
                        PendingIntent.getActivity(mContext, 0, intent, 0));
                return;
            }
        }
    }

    /**
     * Update the state of the USB mass storage notification
     */