Android学习 ——SystemUIService之StorageNotification分析

来源:互联网 发布:淘宝商城女童鞋 编辑:程序博客网 时间:2024/06/03 21:24

usb 这块的内容较多,但是抽取核心部分之后就比较容易理解
文件夹描述
**查看文件目录,其实在SystemUIService 启动中相关的类只有StorageNotification.java和UsbStorageActivity.java
StorageNotification负责逻辑处理,UsbStorageActivity负责显示**

下面是StorageNotification.java 的主要函数关系图
函数关系图

先看入口函数

    @Override    public void start() {        mStorageManager = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);        final boolean connected = mStorageManager.isUsbMassStorageConnected();        if (DEBUG) Log.d(TAG, String.format( "Startup with UMS connection %s (media state %s)",                mUmsAvailable, Environment.getExternalStorageState()));        HandlerThread thr = new HandlerThread("SystemUI StorageNotification");        thr.start();        mAsyncEventHandler = new Handler(thr.getLooper());        StorageNotificationEventListener listener = new StorageNotificationEventListener();        listener.onUsbMassStorageConnectionChanged(connected);        mStorageManager.registerListener(listener);    }

先获取了STORAGE_SERVICE,然后给mStorageManager 注册了监听 StorageNotificationEventListener

    private class StorageNotificationEventListener extends StorageEventListener {        //连接改变        public void onUsbMassStorageConnectionChanged(final boolean connected) {            mAsyncEventHandler.post(new Runnable() {                @Override                public void run() {                    onUsbMassStorageConnectionChangedAsync(connected);                }            });        }        // 存储状态改变        public void onStorageStateChanged(final String path,                final String oldState, final String newState) {            mAsyncEventHandler.post(new Runnable() {                @Override                public void run() {                    onStorageStateChangedAsync(path, oldState, newState);                }            });        }    }

StorageNotificationEventListener 主要是监听两个函数

onUsbMassStorageConnectionChanged(usb连接变化)
onStorageStateChanged (存储状态变化)

在这两个函数中又主要使用了

onUsbMassStorageConnectionChangedAsync()
onStorageStateChangedAsync()

在事件监听中会返回一系列的状态值,根据不同的状态值配置显示标题,内容,使用UsbStorageActivity 显示

代码片段截取  if (newState.equals(Environment.MEDIA_CHECKING)) {            /*             * Storage is now checking. Update media notification and disable             * UMS notification.             */            setMediaStorageNotification(                    com.android.internal.R.string.ext_media_checking_notification_title,                    com.android.internal.R.string.ext_media_checking_notification_message,                    com.android.internal.R.drawable.stat_notify_sdcard_prepare, true, false, null);            updateUsbMassStorageNotification(false);            }

上述配置需要显示的内容
下面为配置到UI显示以及设置发送存储相关的通知

    void updateUsbMassStorageNotification(boolean available) {        if (available) {            Intent intent = new Intent();            intent.setClass(mContext, com.android.systemui.usb.UsbStorageActivity.class);            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);            PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0);            setUsbStorageNotification(                    com.android.internal.R.string.usb_storage_notification_title,                    com.android.internal.R.string.usb_storage_notification_message,                    com.android.internal.R.drawable.stat_sys_data_usb,                    false, true, pi);        } else {            setUsbStorageNotification(0, 0, 0, false, false, null);        }    }
 private synchronized void setUsbStorageNotification(int titleId, int messageId, int icon,            boolean sound, boolean visible, PendingIntent pi) {        // force to show UsbSettings screen to select usb mode if property is true        if (SystemProperties.getBoolean("persist.sys.ums", true)) {            titleId = 0;            messageId = 0;            icon = 0;            sound = false;            visible = false;            pi = null;        }        if (!visible && mUsbStorageNotification == null) {            return;        }        NotificationManager notificationManager = (NotificationManager) mContext                .getSystemService(Context.NOTIFICATION_SERVICE);        if (notificationManager == null) {            return;        }        if (visible) {            Resources r = Resources.getSystem();            CharSequence title = r.getText(titleId);            CharSequence message = r.getText(messageId);            if (mUsbStorageNotification == null) {                mUsbStorageNotification = new Notification();                mUsbStorageNotification.icon = icon;                mUsbStorageNotification.when = 0;            }            if (sound) {                mUsbStorageNotification.defaults |= Notification.DEFAULT_SOUND;            } else {                mUsbStorageNotification.defaults &= ~Notification.DEFAULT_SOUND;            }            mUsbStorageNotification.flags = Notification.FLAG_ONGOING_EVENT;            mUsbStorageNotification.tickerText = title;            if (pi == null) {                Intent intent = new Intent();                pi = PendingIntent.getBroadcastAsUser(mContext, 0, intent, 0,                        UserHandle.CURRENT);            }            mUsbStorageNotification.setLatestEventInfo(mContext, title, message, pi);            final boolean adbOn = 1 == Settings.Global.getInt(                mContext.getContentResolver(),                Settings.Global.ADB_ENABLED,                0);            if (POP_UMS_ACTIVITY_ON_CONNECT && !adbOn) {                // Pop up a full-screen alert to coach the user through enabling UMS. The average                // user has attached the device to USB either to charge the phone (in which case                // this is harmless) or transfer files, and in the latter case this alert saves                // several steps (as well as subtly indicates that you shouldn't mix UMS with other                // activities on the device).                //                // If ADB is enabled, however, we suppress this dialog (under the assumption that a                // developer (a) knows how to enable UMS, and (b) is probably using USB to install                // builds or use adb commands.                mUsbStorageNotification.fullScreenIntent = pi;            }        }        final int notificationId = mUsbStorageNotification.icon;        if (visible) {            notificationManager.notifyAsUser(null, notificationId, mUsbStorageNotification,                    UserHandle.ALL);        } else {            notificationManager.cancelAsUser(null, notificationId, UserHandle.ALL);        }    }
原创粉丝点击