[Galaxy S III Themes and Apps] mad_ady:
This app will make the external (not the internal!) microSD card of your Samsung Galaxy S3 (International, US versions, International LTE) and Note 2 available as USB mass storage device.

The tasker profiles unmount your SD card, switch the USB mode and allow direct access to your card from your operating system of choice. The commands are based on the work of [MENTION=2460591]Kopfgeldjaeger[/MENTION], [MENTION=3064829]d4fseeker[/MENTION], [MENTION=2529010]Sakaschi[/MENTION] and others and their SGS3 Easy UMS app (http://xdaforums.com/showthread.php?t=1711009). You can refer to that thread for details/compatibility and troubleshooting commands.

Prerequisites:

- Tasker (v4.0 or later (I am using 4.2u3m right now))

- A kernel with usb-gadget support

- Root

- Busybox (path to busybox is configurable)

- USB Debugging

- SuperSu v1.93 or later (for the namespace mount fix)

Usage:

Import the profiles in tasker. When you plug into a PC via USB, the UMS Switcher profile should fire up and execute the Enable UMS task. This task detects if you have usb-gadget support, if it finds an external SD card and then proceeds to unmount it. If it fails, it will show a notification with a list of open files on the SD card and a button to retry the mount (after you close those files).

Remember, once the phone is in UMS mode, your PC may automatically mount the SD card. Make sure you unmount it cleanly before removing the USB cable!

Once you remove the USB cable, the profile MTP Switcher kicks in and switches USB mode back to MTP and tries to remount your USB card.

Disclaimer: I am not responsible of any data loss due to the card not being completely unmounted at either end. I haven't experienced data loss, but there's always a risk things can go wrong.

Code:

The actual profiles/Tasks can be downloaded at the end of the post and imported into tasker, or you can "type" them in yourself. To import the profiles into tasker by unzipping the file into your internalSDCard/Tasker/profiles folder. Then you can import it in Tasker by tapping the profiles tab. Long tap the Profiles tab and select import.

Profile: UMS Switcher (65)
State: Power [ Source:USB ]
Enter: Enable UMS (59)
A1: Flash [ Text:Switching to USB Mass Storage mode Long:eek:ff ]
<This variable toggles between using the possibly dangerous mode of lazy umounting and the non-dangerous mode. With lazy umounting your sd card will remain mounted on the phone while the phone is connected to the PC and there is a greater chance of file corruption if you opperate on the same file simultaneously. The benefit of this mode is that you will be able to use the UMS mode multiple times without rebooting. Use with care and report back any corruption issues.>
A2: Variable Set [ Name:%dangerous_mode To:0 Do Maths:eek:ff Append:eek:ff ]
<set lazy switch to blank>
A3: Variable Set [ Name:%lazy_switch To: Do Maths:eek:ff Append:eek:ff ]
<Enable lazy umounting if requested>
A4: Variable Set [ Name:%lazy_switch To:-l Do Maths:eek:ff Append:eek:ff ] If [ %dangerous_mode = 1 ]
<Path to your busybox (change it if you have a custom busybox)>
A5: Variable Set [ Name:%busybox To:/system/xbin/busybox Do Maths:eek:ff Append:eek:ff ]
<remember if UMS is enabled>
A6: Variable Set [ Name:%UMSStatus To:0 Do Maths:eek:ff Append:eek:ff ]
<A place to keep errors>
A7: Variable Set [ Name:%error To:0 Do Maths:eek:ff Append:eek:ff ]
<A variable for the current device>
A8: Variable Set [ Name:%sdcard To:0 Do Maths:eek:ff Append:eek:ff ]
<Variable to keep the mountpoint name>
A9: Variable Set [ Name:%mountPoint To:0 Do Maths:eek:ff Append:eek:ff ]
<Variable to hold the path to the relevant /sys file>
A10: Variable Set [ Name:%sysFile To:0 Do Maths:eek:ff Append:eek:ff ]
<Find the correct /sys file>
A11: Run Shell [ Command:ls /sys/devices/platform/s3c-usbgadget/gadget/lun0/file; ls /sys/devices/virtual/android_usb/android0/f_mass_storage/lun_ex/file; ls /sys/devices/virtual/android_usb/android0/f_mass_storage/lun/file; Timeout (Seconds):0 Use Root:eek:ff Store Output In:%sysFile Store Errors In: Store Result In: Continue Task After Error:eek:n ]
<No sys file found>
A12: If [ %sysFile ! Set ]
A13: Notify [ Title:UMSSwitcher Error 10: Unable to find a suitable file in /sys. Text:Maybe you are not running a kernel with usb-gadget support. Icon:hd_aaa_ext_halt Number:0 Permanent:eek:ff Priority:3 ]
</sys file found>
A14: Else
<Find current extSD device number>
A15: Run Shell [ Command:mount | sed 's/ on / /g' | egrep "extSdCard|external_sd|sdcard1" | grep /dev/block | cut -f 1 -d " " Timeout (Seconds):0 Use Root:eek:ff Store Output In:%sdcard Store Errors In: Store Result In: Continue Task After Error:eek:n ]
<get the mountpoint>
A16: Run Shell [ Command:mount | sed 's/ on / /g' | egrep "extSdCard|external_sd|sdcard1" | grep /dev/block | cut -f 2 -d " " Timeout (Seconds):0 Use Root:eek:ff Store Output In:%mountPoint Store Errors In: Store Result In: Continue Task After Error:eek:n ]
<Found sdcard?>
A17: If [ %sdcard !~ /dev ]
A18: Flash [ Text:About to unmount %mountPoint Long:eek:ff ]
<See if there are open files after umount (1 file)>
A19: Run Shell [ Command:lsof -Fn | grep %mountPoint | awk '{ s = ""; for (i = 9; i <= NF; i++) s = s $i " "; print s }' Timeout (Seconds):0 Use Root:eek:n Store Output In:%error Store Errors In: Store Result In: Continue Task After Error:eek:n ]
<no open files>
A20: If [ %error ! Set ]
<Try to unmount>
A21: Run Shell [ Command:sync; su --mount-master -c "%busybox umount %lazy_switch '%mountPoint' " Timeout (Seconds):0 Use Root:eek:n Store Output In: Store Errors In:%error Store Result In: Continue Task After Error:eek:n ]
<We couldn't unmount>
A22: If [ %error Is Set ]
A23: Notify [ Title:UMSSwitcher Error 11: Error unmounting SD card Text:%error Icon:hd_aaa_ext_halt Number:0 Permanent:eek:ff Priority:3 Actions:(1) ]
<We managed to unmount.>
A24: Else
<Switch to ums mode>
A25: Run Shell [ Command:setprop persist.sys.usb.config mass_storage,adb; echo %sdcard > %sysFile Timeout (Seconds):0 Use Root:eek:n Store Output In: Store Errors In:%error Store Result In: Continue Task After Error:eek:n ]
<There's an error>
A26: If [ %error Is Set ]
A27: Notify [ Title:UMSSwitcher Error 12: There was an error when enabling UMS. The SD card is unmounted Text:%error Icon:hd_aaa_ext_halt Number:0 Permanent:eek:ff Priority:3 ]
A28: Else
A29: Variable Set [ Name:%UMSStatus To:1 Do Maths:eek:ff Append:eek:ff ]
A30: Flash [ Text:UMS mode enabled :) Long:eek:ff ]
A31: End If
A32: End If
<still open files>
A33: Else
A34: Notify [ Title:UMSSwitcher Error 13: Open files on SD Text:%error Icon:hd_aaa_ext_halt Number:0 Permanent:eek:ff Priority:3 Actions:(1) ]
A35: End If
<no sdcard found>
A36: Else
A37: Notify [ Title:UMSSwitcher Error 14: No external SD Card found Text:Make sure it's mounted first. Icon:hd_aaa_ext_halt Number:0 Permanent:eek:ff Priority:3 Actions:(2) ]
A38: End If
A39: End If

Profile: MTP Switcher (64)
State: Not Power [ Source:USB ]
State: Variable Value [ Name:%UMSStatus Op:Maths: Equals Value:1 ]
Enter: Enable MTP + Mount (61)
A1: Variable Set [ Name:%error To:0 Do Maths:eek:ff Append:eek:ff ]
A2: Wait [ MS:0 Seconds:2 Minutes:0 Hours:0 Days:0 ]
<Remount card and switch to MTP>
A3: Run Shell [ Command:echo '' > %sysFile; sleep 2; vold; sleep 2; setprop persist.sys.usb.config mtp,adb Timeout (Seconds):0 Use Root:eek:n Store Output In: Store Errors In:%error Store Result In: Continue Task After Error:eek:n ]
<All ok>
A4: If [ %error ! Set ]
<Check card mount point>
A5: Run Shell [ Command:mount | sed 's/ on / /g' | egrep "extSdCard|external_sd|sdcard1" | grep /dev/block | cut -f 2 -d " " Timeout (Seconds):0 Use Root:eek:ff Store Output In:%mountPoint Store Errors In: Store Result In: Continue Task After Error:eek:n ]
<not mounted>
A6: If [ %mountPoint ! Set ]
A7: Flash [ Text:Unable to mount card automatically Long:eek:ff ]
A8: Notify [ Title:MTPSwitcher Error 10 Text:%error. Unable to remount card automatically Icon:hd_aaa_ext_halt Number:0 Permanent:eek:ff Priority:3 ]
<Card was mounted ok>
A9: Else
A10: Variable Set [ Name:%UMSStatus To:0 Do Maths:eek:ff Append:eek:ff ]
A11: Flash [ Text:SD card mounted on %mountPoint Long:eek:ff ]
A12: End If
A13: Else
A14: Flash [ Text:Error: %error Long:eek:ff ]
A15: Notify [ Title:MTPSwitcher Error 11 Text:%error Icon:hd_aaa_ext_halt Number:0 Permanent:eek:ff Priority:3 ]
A16: End If

Tested on:

- NeatRom 4.8 (based on samsung 4.1.2), with Adam Kernel

- NeatRom 6.2 (based on samsung 4.3), with Boeffla-Kernel (5.1-beta10) (or stock kernel)

- NeatRom 2.8 (based on SlimKat 2.8 (KitKat 4.4.2)), with Boeffla-Kernel (omni)

- NeatRom 3.1 (based on SlimKat 2.0 (KitKat 4.4.2)), with Boeffla-Kernel (omni)

Changelog:

v0.6 - Added support for S4

v0.5 - Fixes to allow lazy unmounting so that you can mount the SD card multiple times without rebooting the phone (off by default).

v0.4 - Added support for KitKat ROMs. Also added support for lazy unmounting (dangerous, off by default)

v0.3 - Added some more checks

v0.2 - Added support for Android 4.3 (look for mount lines with /dev in the name)

v0.1 - initial release

Error codes:

Because Tasker has a limitation on the number of characters shown in a notification you won't be able to get very comprehensive error messages. So, I've added numeric codes that you can lookup here:

MTPSwitcher Error 10: Unable to remount card automatically

MTPSwitcher Error 11: Error while disabling UMS mode

UMSSwitcher Error 10: Unable to find a suitable file in /sys. Maybe you are not running a kernel with usb-gadget support

UMSSwitcher Error 11: Error unmounting SD card

UMSSwitcher Error 12: There was an error while enabling UMS. The SD card is unmounted

UMSSwitcher Error 13: Open files on SD

UMSSwitcher Error 14: No external SD Card found. Make sure it's mounted first

Bugs:

On android 4.3 you will likely get an Error 11 when trying to unmount the card the second time. It can be fixed by a rebooting the phone. Still working on a fix.

On android 4.4 you will likely get an Error 11 when trying to unmount the card the second time. To overcome this I added the dangerous_mode switch variable (can be enabled by editing the Enable UMS task). If you set this switch to 1 the system will try to do a lazy umount of your SD card, leaving it still accessible by your phone while it is still mounted on the PC. It has the risk to do data corruption if you use the card simultaneously on your PC and phone, but it is the method used in the Boeffla Kernel app to toggle UMS mode. According to him he hasn't seen any problems so far: http://xdaforums.com/showpost.php?p=50229253&postcount=2355, http://xdaforums.com/showpost.php?p=50257161&postcount=2366, http://xdaforums.com/showpost.php?p=50257593&postcount=2368

Let me know if there are problems or if it works for you too. Also, if your mountpoint is different, let me know to add it to the app.