Xamarin.Macでリムーバブルメディアの着脱を検知する

提供: MonoBook
2016年11月17日 (木) 05:38時点におけるimported>Administratorによる版 (Administrator がページ「MonoMac/リムーバブルメディアの着脱を検知する」を「Xamarin.Mac/リムーバブルメディアの着脱を検知する」に移動しました)
ナビゲーションに移動 検索に移動

MonoMacでCD-ROMDVD-ROMUSBメモリの挿入などを検出できると何かと嬉しい。

実装例

NSWorkspace.SharedWorkspace.NotificationCenterを使うと検出できるようだ。この通知で情報として飛んでくるのはデバイスのパスのみなので、そのパスをNSWorkspace.SharedWorkspace.GetFileSystemInfoを使いCD-ROMなのかUSBメモリなのかなどを判別する。なお、CD-ROMやDVD-ROMの場合はディスクフォーマットごとに返ってくる値が違うようだ。どんな種類があるのか全部はまだ把握していない。

通知には種類があるようで、この例では以下を実装している。

  • NSWorkspaceDidMountNotification - マウント直後
  • NSWorkspaceWillUnmountNotification - アンマウント直前

NSWorkspaceDidUnmountNotification(WillではなくDid)も実装すればアンマウント直前のみならずアンマウント直後も検出できる。ただしアンマウント後なのでデバイスパスしか取得できない。アンマウント失敗時を考慮するとこちらの方が確実かもしれないが詳しくは知らない。

なお、いわゆる自動実行(autorun.inf)などとは違うので起動していないアプリが起動するわけではない。あくまでアプリが立ち上がっている状態でのみ機能する。

    using System;
    using MonoMac.Foundation;
    using MonoMac.AppKit;

    public class RemovableMediaNotificationEventArgs : EventArgs
    {
        public string DevicePath { get; set; }
        public bool IsRemovable { get; set; }
        public bool IsWritable { get; set; }
        public bool IsUnmountable { get; set; }
        public string Description { get; set; }
        public string FileSystemType { get; set; }
    }

    public delegate void RemovableMediaNotificationEventHandler(object sender, RemovableMediaNotificationEventArgs args);

    // 着脱を検知するクラス
    public class RemovableMediaNotification : IDisposable
    {
        private NSObject _mountObserver;
        private NSObject _unmountObserver;

        public event RemovableMediaNotificationEventHandler OnMount;
        public event RemovableMediaNotificationEventHandler OnUnmount;

        public RemovableMediaNotification()
        {
        }

        public void Dispose()
        {
            Stop();
        }

        public void Start()
        {
            var workspace = NSWorkspace.SharedWorkspace;
            var notification = workspace.NotificationCenter;

            // マウント直後
            if (_mountObserver == null)
            {
                _mountObserver = notification.AddObserver("NSWorkspaceDidMountNotification", DidMount);
            }

            // マウント直前
            if (_unmountObserver == null)
            {
                _unmountObserver = notification.AddObserver("NSWorkspaceWillUnmountNotification", WiilUnmount);
            }
        }

        public void Stop()
        {
            var workspace = NSWorkspace.SharedWorkspace;
            var notification = workspace.NotificationCenter;

            if (_mountObserver != null)
            {
                notification.RemoveObserver(_mountObserver);
                _mountObserver.Dispose();
                _mountObserver = null;
            }

            if (_unmountObserver != null)
            {
                notification.RemoveObserver(_unmountObserver);
                _unmountObserver.Dispose();
                _unmountObserver = null;
            }
        }

        private void DidMount(NSNotification notification)
        {
            if (OnUnmount != null)
            {
                var path = notification.UserInfo["NSDevicePath"].ToString();
                var args = CreateEventArg(path);
                OnMount(this, args);
            }
        }

        private void WiilUnmount(NSNotification notification)
        {
            if (OnUnmount != null)
            {
                var path = notification.UserInfo["NSDevicePath"].ToString();
                var args = CreateEventArg(path);
                OnUnmount(this, args);
            }
        }

        private RemovableMediaNotificationEventArgs CreateEventArg(string devicePath)
        {
            bool isRemovable;
            bool isWritable;
            bool isUnmountable;
            string description;
            string fileSystemType;

            var workspace = NSWorkspace.SharedWorkspace;
            workspace.GetFileSystemInfo(devicePath, out isRemovable, out isWritable,out isUnmountable, out description,  out fileSystemType);

            var args = new RemovableMediaNotificationEventArgs {
                DevicePath = devicePath,
                IsRemovable = isRemovable,
                IsWritable = isWritable,
                IsUnmountable = isUnmountable,
                Description = description,
                FileSystemType = fileSystemType,
            };
            return args;
        }
    }

関連項目

参考文献