/
SchemeItem.java
179 lines (162 loc) · 6.73 KB
/
SchemeItem.java
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Tencent is pleased to support the open source community by making QMUI_Android available.
*
* Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the MIT License (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://opensource.org/licenses/MIT
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.qmuiteam.qmui.arch.scheme;
import android.app.Activity;
import android.util.ArrayMap;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.qmuiteam.qmui.QMUILog;
import java.util.HashMap;
import java.util.Map;
public abstract class SchemeItem {
private static HashMap<Class<? extends QMUISchemeMatcher>, QMUISchemeMatcher> sSchemeMatchers;
@Nullable
private ArrayMap<String, String> mRequired;
@Nullable
private String[] mKeysForInt;
@Nullable
private String[] mKeysForBool;
@Nullable
private String[] mKeysForLong;
@Nullable
private String[] mKeysForFloat;
@Nullable
private String[] mKeysForDouble;
@Nullable
private Class<? extends QMUISchemeMatcher> mSchemeMatcherCls;
private boolean mUseRefreshIfMatchedCurrent;
public SchemeItem(@Nullable ArrayMap<String, String> required,
boolean useRefreshIfMatchedCurrent,
@Nullable String[] keysForInt,
@Nullable String[] keysForBool,
@Nullable String[] keysForLong,
@Nullable String[] keysForFloat,
@Nullable String[] keysForDouble,
@Nullable Class<? extends QMUISchemeMatcher> schemeMatcherCls) {
mRequired = required;
mUseRefreshIfMatchedCurrent = useRefreshIfMatchedCurrent;
mKeysForInt = keysForInt;
mKeysForBool = keysForBool;
mKeysForLong = keysForLong;
mKeysForFloat = keysForFloat;
mKeysForDouble = keysForDouble;
mSchemeMatcherCls = schemeMatcherCls;
}
public boolean isUseRefreshIfMatchedCurrent() {
return mUseRefreshIfMatchedCurrent;
}
@Nullable
public Map<String, SchemeValue> convertFrom(@Nullable Map<String, String> schemeParams) {
if (schemeParams == null || schemeParams.isEmpty()) {
return null;
}
Map<String, SchemeValue> queryMap = new HashMap<>();
for (Map.Entry<String, String> param : schemeParams.entrySet()) {
String name = param.getKey();
String value = param.getValue();
if (name == null || name.isEmpty()) {
continue;
}
try {
if (contains(mKeysForInt, name)) {
queryMap.put(name, new SchemeValue(value, Integer.valueOf(value), Integer.TYPE));
} else if (QMUISchemeHandler.ARG_FORCE_TO_NEW_ACTIVITY.equals(name) || contains(mKeysForBool, name)) {
boolean isFalse = "0".equals(value) || "false".equals(value.toLowerCase());
queryMap.put(name, new SchemeValue(value, !isFalse, Boolean.TYPE));
} else if (contains(mKeysForLong, name)) {
queryMap.put(name, new SchemeValue(value, Long.valueOf(value), Long.TYPE));
} else if (contains(mKeysForFloat, name)) {
queryMap.put(name, new SchemeValue(value, Float.valueOf(value), Float.TYPE));
} else if (contains(mKeysForDouble, name)) {
queryMap.put(name, new SchemeValue(value, Double.valueOf(value), Double.TYPE));
} else {
queryMap.put(name, new SchemeValue(value, value, String.class));
}
} catch (Exception e) {
QMUILog.printErrStackTrace(QMUISchemeHandler.TAG, e,
"error to parse scheme param: %s = %s", name, value);
}
}
return queryMap;
}
@Nullable
private QMUISchemeMatcher getSchemeMatcher(@NonNull QMUISchemeHandler handler) {
if (sSchemeMatchers == null) {
sSchemeMatchers = new HashMap<>();
}
Class<? extends QMUISchemeMatcher> schemeMatcherCls = mSchemeMatcherCls;
if (schemeMatcherCls == null) {
schemeMatcherCls = handler.getDefaultSchemeMatcher();
}
QMUISchemeMatcher matcher = sSchemeMatchers.get(schemeMatcherCls);
if (matcher == null) {
try {
matcher = schemeMatcherCls.newInstance();
sSchemeMatchers.put(schemeMatcherCls, matcher);
} catch (Exception e) {
QMUILog.printErrStackTrace(QMUISchemeHandler.TAG, e,
"error to instance QMUISchemeMatcher: %d", schemeMatcherCls.getSimpleName());
}
}
return matcher;
}
private static boolean contains(@Nullable String[] array, @NonNull String key) {
if (array == null || array.length == 0) {
return false;
}
for (String s : array) {
if (key.equals(s)) {
return true;
}
}
return false;
}
// used by generated code(SchemeMapImpl)
boolean match(@NonNull QMUISchemeHandler handler, @Nullable Map<String, String> params) {
QMUISchemeMatcher matcher = getSchemeMatcher(handler);
if (matcher != null) {
return matcher.match(this, params);
}
return matchRequiredParam(params);
}
public boolean matchRequiredParam(@Nullable Map<String, String> params) {
if (mRequired == null || mRequired.isEmpty()) {
return true;
}
if (params == null || params.isEmpty()) {
return false;
}
for (int i = 0; i < mRequired.size(); i++) {
String key = mRequired.keyAt(i);
if (!params.containsKey(key)) {
return false;
}
String value = mRequired.valueAt(i);
if (value == null) {
// if no value. that means scheme must provide this key.
continue;
}
String actual = params.get(key);
if (actual == null || !actual.equals(value)) {
return false;
}
}
return true;
}
public abstract boolean handle(@NonNull QMUISchemeHandler handler,
@NonNull Activity activity,
@Nullable Map<String, SchemeValue> scheme);
}