StorageStateManager.java
4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package com.sw.laryngoscope.manager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.util.Log;
public class StorageStateManager {
public final String TAG = "StorageStateManager";
private static StorageStateManager ourInstance = null;
private BroadcastReceiver mReceiver = null;
private boolean mIsUDiskMount = false;
private String UdiskPath = "/mnt/usbhost/Storage01/";
private Context mContext;
public static StorageStateManager getInstance() {
if ( null == ourInstance ) {
synchronized (StorageStateManager.class) {
if (null == ourInstance ) {
ourInstance = new StorageStateManager();
}
}
}
return ourInstance;
}
private StorageStateManager() {}
/**
* @explain:register and unregister需要成对出现
*/
public void registerReceiver(Context context) {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_MEDIA_EJECT);
intentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
intentFilter.addAction(Intent.ACTION_MEDIA_NOFS);
intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTABLE);
intentFilter.addAction(Intent.ACTION_MEDIA_CHECKING);
intentFilter.addAction(Intent.ACTION_MEDIA_REMOVED);
intentFilter.addAction(Intent.ACTION_MEDIA_SHARED);
intentFilter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
intentFilter.addDataScheme("file");
mReceiver = new StorageBroadcastReceiver();
context.registerReceiver(mReceiver, intentFilter);
mContext = context;
}
/**
* @explain:register and unregister需要成对出现
*/
public void unregisterReceiver(Context context) {
if (mReceiver != null) {
mContext.unregisterReceiver(mReceiver);
mReceiver = null;
}
}
/*public boolean getUdiskState() {
if(Storage.getUDiskTotalSpace(mContext) > 0)
mIsUDiskMount = true;
else
mIsUDiskMount = false;
return mIsUDiskMount;
}*/
public void setUdiskState(boolean state) {
mIsUDiskMount = state;
if( mMountCallBack != null ) {
mMountCallBack.MountEventProc(mIsUDiskMount);
}
if( mCallBack != null ) {
mCallBack.MountEventProc(mIsUDiskMount);
}
}
public String getUdiskPath() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
//return Storage.getStoragePath(mContext, "usb");
return Storage.getUPath(mContext);
} else {
return UdiskPath;
}
}
public MountCallBack mMountCallBack = null;
public void setMountCallBackFun(MountCallBack cb) {
mMountCallBack = cb;
}
public interface MountCallBack {
public void MountEventProc(boolean mount);
}
public CallBack mCallBack = null;
public void setCallBackFun(CallBack cb) {
mCallBack = cb;
}
public interface CallBack {
public void MountEventProc(boolean mount);
}
/**
* @explain:监听系统插拔SD卡,扫描SD卡广播消息
*/
private class StorageBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d(TAG, "action = " + action);
final Uri uri = intent.getData();
String path = uri.getPath();
if (action.equals(Intent.ACTION_MEDIA_EJECT)) {
Log.d(TAG, "Intent.ACTION_MEDIA_EJECT path = " + path);
if( !path.contains("emulated") ) {
setUdiskState(false);
}
} else if (action.equals(Intent.ACTION_MEDIA_UNMOUNTED)) {
Log.d(TAG, "Intent.ACTION_MEDIA_UNMOUNTED path = " + path);
} else if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
Log.d(TAG, "Intent.ACTION_MEDIA_MOUNTED = " + path);
if( !path.contains("emulated") ) {
UdiskPath = path;
setUdiskState(true);
}
}
}
}
}