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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
| package com.alibaba.android.arouter.core
import android.content.Context import android.net.Uri import com.alibaba.android.arouter.exception.HandlerException import com.alibaba.android.arouter.exception.NoRouteFoundException import com.alibaba.android.arouter.facade.Postcard import com.alibaba.android.arouter.facade.enums.TypeKind import com.alibaba.android.arouter.facade.model.RouteMeta import com.alibaba.android.arouter.facade.template.IInterceptorGroup import com.alibaba.android.arouter.facade.template.IProvider import com.alibaba.android.arouter.facade.template.IProviderGroup import com.alibaba.android.arouter.facade.template.IRouteGroup import com.alibaba.android.arouter.facade.template.IRouteRoot import com.alibaba.android.arouter.launcher.ARouter import com.alibaba.android.arouter.launcher.ARouter.logger import com.alibaba.android.arouter.utils.ClassUtils import com.alibaba.android.arouter.utils.Consts import com.alibaba.android.arouter.utils.Consts.AROUTER_SP_CACHE_KEY import com.alibaba.android.arouter.utils.Consts.AROUTER_SP_KEY_MAP import com.alibaba.android.arouter.utils.Consts.DOT import com.alibaba.android.arouter.utils.Consts.ROUTE_ROOT_PAKCAGE import com.alibaba.android.arouter.utils.Consts.SDK_NAME import com.alibaba.android.arouter.utils.Consts.SEPARATOR import com.alibaba.android.arouter.utils.Consts.SUFFIX_INTERCEPTORS import com.alibaba.android.arouter.utils.Consts.SUFFIX_PROVIDERS import com.alibaba.android.arouter.utils.Consts.SUFFIX_ROOT import com.alibaba.android.arouter.utils.Consts.TAG import com.alibaba.android.arouter.utils.MapUtils import com.alibaba.android.arouter.utils.PackageUtils import com.alibaba.android.arouter.utils.TextUtils import java.lang.Boolean import java.lang.reflect.InvocationTargetException import java.util.* import java.util.concurrent.ThreadPoolExecutor import kotlin.Exception import kotlin.Int import kotlin.String import kotlin.Throwable import kotlin.Throws import kotlin.arrayOf
object LogisticsCenter { private var mContext: Context? = null var executor: ThreadPoolExecutor? = null private var registerByPlugin = false
private fun loadRouterMap() { registerByPlugin = false }
private fun register(className: String) { if (!TextUtils.isEmpty(className)) { try { val clazz = Class.forName(className) val obj = clazz.getConstructor().newInstance() if (obj is IRouteRoot) { registerRouteRoot(obj as IRouteRoot) } else if (obj is IProviderGroup) { registerProvider(obj as IProviderGroup) } else if (obj is IInterceptorGroup) { registerInterceptor(obj as IInterceptorGroup) } else { logger.info( TAG, "注册失败,类名:" + className + " 应该实现其中一个接口 IRouteRoot/IProviderGroup/IInterceptorGroup。" ) } } catch (e: Exception) { logger.error(TAG, "注册类错误:$className", e) } } }
private fun registerRouteRoot(routeRoot: IRouteRoot?) { markRegisteredByPlugin() if (routeRoot != null) { routeRoot.loadInto(Warehouse.groupsIndex) } }
private fun registerInterceptor(interceptorGroup: IInterceptorGroup?) { markRegisteredByPlugin() if (interceptorGroup != null) { interceptorGroup.loadInto(Warehouse.interceptorsIndex) } }
private fun registerProvider(providerGroup: IProviderGroup?) { markRegisteredByPlugin() if (providerGroup != null) { providerGroup.loadInto(Warehouse.providersIndex) } }
private fun markRegisteredByPlugin() { if (!registerByPlugin) { registerByPlugin = true } }
@Synchronized @Throws(HandlerException::class) fun init(context: Context, tpe: ThreadPoolExecutor?) { mContext = context executor = tpe try { var startInit = System.currentTimeMillis() loadRouterMap() if (registerByPlugin) { logger.info(TAG, "通过 arouter-auto-register 插件加载路由映射信息。") } else { val routerMap: Set<String>
if (ARouter.debuggable() || PackageUtils.isNewVersion(context)) { logger.info(TAG, "在调试模式下或安装新版本,重新构建路由映射。") routerMap = ClassUtils.getFileNameByPackageName(mContext, ROUTE_ROOT_PAKCAGE) if (!routerMap.isEmpty()) { context.getSharedPreferences(AROUTER_SP_CACHE_KEY, Context.MODE_PRIVATE) .edit().putStringSet(AROUTER_SP_KEY_MAP, routerMap).apply() } PackageUtils.updateVersion(context) } else { logger.info(TAG, "从缓存中加载路由映射信息。") routerMap = HashSet( context.getSharedPreferences( AROUTER_SP_CACHE_KEY, Context.MODE_PRIVATE ).getStringSet(AROUTER_SP_KEY_MAP, HashSet()) ) } logger.info( TAG, "找到路由映射信息,映射大小 = " + routerMap.size + ",耗时 " + (System.currentTimeMillis() - startInit) + " 毫秒。" ) startInit = System.currentTimeMillis() for (className in routerMap) { if (className.startsWith(ROUTE_ROOT_PAKCAGE + DOT + SDK_NAME + SEPARATOR + SUFFIX_ROOT)) { (Class.forName(className).getConstructor() .newInstance() as IRouteRoot).loadInto(Warehouse.groupsIndex) } else if (className.startsWith(ROUTE_ROOT_PAKCAGE + DOT + SDK_NAME + SEPARATOR + SUFFIX_INTERCEPTORS)) { (Class.forName(className).getConstructor() .newInstance() as IInterceptorGroup).loadInto(Warehouse.interceptorsIndex) } else if (className.startsWith(ROUTE_ROOT_PAKCAGE + DOT + SDK_NAME + SEPARATOR + SUFFIX_PROVIDERS)) { (Class.forName(className).getConstructor() .newInstance() as IProviderGroup).loadInto(Warehouse.providersIndex) } } } logger.info( TAG, "加载根元素完成,耗时 " + (System.currentTimeMillis() - startInit) + " 毫秒。" ) if (Warehouse.groupsIndex.size() === 0) { logger.error(TAG, "未找到映射文件,请检查您的配置!") } if (ARouter.debuggable()) { logger.debug( TAG, java.lang.String.format( Locale.getDefault(), "LogisticsCenter 已经加载,GroupIndex[%d], InterceptorIndex[%d], ProviderIndex[%d]", Warehouse.groupsIndex.size(), Warehouse.interceptorsIndex.size(), Warehouse.providersIndex.size() ) ) } } catch (e: Exception) { throw HandlerException(TAG.toString() + "ARouter 初始化物流中心异常! [" + e.message + "]") } }
fun buildProvider(serviceName: String?): Postcard? { val meta: RouteMeta = Warehouse.providersIndex.get(serviceName) return if (null == meta) { null } else { Postcard(meta.getPath(), meta.getGroup()) } }
@Synchronized fun completion(postcard: Postcard?) { if (null == postcard) { throw NoRouteFoundException(TAG.toString() + "没有 Postcard!") } val routeMeta: RouteMeta = Warehouse.routes.get(postcard.getPath()) if (null == routeMeta) { if (!Warehouse.groupsIndex.containsKey(postcard.getGroup())) { throw NoRouteFoundException(TAG.toString() + "没有匹配路径 [" + postcard.getPath() + "],在组 [" + postcard.getGroup() + "] 中。") } else { try { if (ARouter.debuggable()) { logger.debug( TAG, java.lang.String.format( Locale.getDefault(), "开始加载组 [%s],由 [%s] 触发。", postcard.getGroup(), postcard.getPath() ) ) } addRouteGroupDynamic(postcard.getGroup(), null) if (ARouter.debuggable()) { logger.debug( TAG, java.lang.String.format( Locale.getDefault(), "组 [%s] 已经加载,由 [%s] 触发。", postcard.getGroup(), postcard.getPath() ) ) } } catch (e: Exception) { throw HandlerException(TAG.toString() + "加载组元信息时发生致命异常。 [" + e.message + "]") } completion(postcard) } } else { postcard.setDestination(routeMeta.getDestination()) postcard.setType(routeMeta.getType()) postcard.setPriority(routeMeta.getPriority()) postcard.setExtra(routeMeta.getExtra()) val rawUri: Uri = postcard.getUri() if (null != rawUri) { val resultMap: Map<String, String> = TextUtils.splitQueryParameters(rawUri) val paramsType: Map<String, Int?> = routeMeta.getParamsType() if (MapUtils.isNotEmpty(paramsType)) { for ((key, value): Map.Entry<String, Int?> in paramsType) { setValue( postcard, value, key, resultMap[key] ) }
postcard.getExtras().putStringArray( ARouter.AUTO_INJECT, paramsType.keys.toArray(arrayOf<String>()) ) }
postcard.withString(ARouter.RAW_URI, rawUri.toString()) } when (routeMeta.getType()) { PROVIDER -> { val providerMeta: Class<out IProvider?> = routeMeta.getDestination() var instance: IProvider? = Warehouse.providers.get(providerMeta) if (null == instance) { val provider: IProvider try { provider = providerMeta.getConstructor().newInstance() provider.init(mContext) Warehouse.providers.put(providerMeta, provider) instance = provider } catch (e: Exception) { logger.error(TAG, "初始化提供者失败!", e) throw HandlerException("初始化提供者失败!") } } postcard.setProvider(instance) postcard.greenChannel() } FRAGMENT -> postcard.greenChannel() else -> {} } } }
private fun setValue(postcard: Postcard, typeDef: Int?, key: String, value: String?) { if (TextUtils.isEmpty(key) || TextUtils.isEmpty(value)) { return } try { if (null != typeDef) { if (typeDef === TypeKind.BOOLEAN.ordinal()) { postcard.withBoolean(key, Boolean.parseBoolean(value)) } else if (typeDef === TypeKind.BYTE.ordinal()) { postcard.withByte(key, value!!.toByte()) } else if (typeDef === TypeKind.SHORT.ordinal()) { postcard.withShort(key, value!!.toShort()) } else if (typeDef === TypeKind.INT.ordinal()) { postcard.withInt(key, value!!.toInt()) } else if (typeDef === TypeKind.LONG.ordinal()) { postcard.withLong(key, value!!.toLong()) } else if (typeDef === TypeKind.FLOAT.ordinal()) { postcard.withFloat(key, value!!.toFloat()) } else if (typeDef === TypeKind.DOUBLE.ordinal()) { postcard.withDouble(key, value!!.toDouble()) } else if (typeDef === TypeKind.STRING.ordinal()) { postcard.withString(key, value) } else if (typeDef === TypeKind.PARCELABLE.ordinal()) { } else if (typeDef === TypeKind.OBJECT.ordinal()) { postcard.withString(key, value) } else { postcard.withString(key, value) } } else { postcard.withString(key, value) } } catch (ex: Throwable) { logger.warning(Consts.TAG, "LogisticsCenter setValue 失败!" + ex.message) } }
fun suspend() { Warehouse.clear() }
@Synchronized @Throws( NoSuchMethodException::class, IllegalAccessException::class, InvocationTargetException::class, InstantiationException::class ) fun addRouteGroupDynamic(groupName: String?, group: IRouteGroup?) { if (Warehouse.groupsIndex.containsKey(groupName)) { Warehouse.groupsIndex.get(groupName).getConstructor().newInstance() .loadInto(Warehouse.routes) Warehouse.groupsIndex.remove(groupName) }
if (null != group) { group.loadInto(Warehouse.routes) } } }
|