NotificationParser.java
1.41 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
/*
* Copyright (C) 2015 The Telink Bluetooth Light Project
*
*/
package com.telink.bluetooth.light;
import android.util.SparseArray;
/**
* Notification解析器接口
* <p>继承NotificationParser编写自定义的解析器,通过{@link NotificationParser#register(NotificationParser)}来注册.
*
* @param <E>
*/
public abstract class NotificationParser<E> {
private static final SparseArray<NotificationParser> PARSER_ARRAY = new SparseArray<>();
/**
* 注册解析器
*
* @param parser
*/
public static void register(NotificationParser parser) {
synchronized (NotificationParser.class) {
PARSER_ARRAY.put(parser.opcode() & 0xFF, parser);
}
}
/**
* 获取解析器
*
* @param opcode 操作码
* @return
*/
public static NotificationParser get(int opcode) {
synchronized (NotificationParser.class) {
return PARSER_ARRAY.get(opcode & 0xFF);
}
}
public static NotificationParser get(Opcode opcode) {
return get(opcode.getValue());
}
/**
* 操作码
*
* @return
*/
abstract public byte opcode();
/**
* 将{@link NotificationInfo#params}转换成自定义的数据格式
*
* @param notifyInfo
* @return
*/
abstract public E parse(NotificationInfo notifyInfo);
}