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
| private void generateBroadcastResponderCode() { for (TypeElement typeElement : broadCastResponderMap.keySet()) {
MethodSpec.Builder methodBuilder = generateRegisterReceiverMethodBuilder(typeElement);
ClassName broadcastReceiverClassName = ClassName.bestGuess("android.content.BroadcastReceiver"); ClassName contextClassName = ClassName.bestGuess("android.content.Context"); ClassName intentClassName = ClassName.bestGuess("android.content.Intent"); ClassName localBroadcastManagerClassName = ClassName.bestGuess("androidx.localbroadcastmanager.content.LocalBroadcastManager");
List<Element> elements = broadCastResponderMap.get(typeElement);
HashMap<String, String> localBroadCast = new HashMap<>(); HashMap<String, String> globalBroadCast = new HashMap<>();
for (Element element : elements) { ExecutableElement executableElement = (ExecutableElement) element; BroadcastResponder broadcastResponder = executableElement.getAnnotation(BroadcastResponder.class); int type = broadcastResponder.type(); String[] actions = broadcastResponder.action(); for (String action : actions) {
if (BroadcastResponder.LOCAL_BROADCAST == type) { methodBuilder.addStatement("localBroadcastFilter.addAction(($S))", action); localBroadCast.put(action, element.getSimpleName().toString());
} else if (BroadcastResponder.GLOBAL_BROADCAST == type) { globalBroadCast.put(action, element.getSimpleName().toString()); methodBuilder.addStatement("globalBroadcastFilter.addAction(($S))", action); }
}
}
if (globalBroadCast.size() > 0) { CodeBlock.Builder caseBlockBuilder = CodeBlock.builder().beginControlFlow("switch (intent.getAction())"); for (Map.Entry<String, String> entry : globalBroadCast.entrySet()) { caseBlockBuilder.add("case $S:\n", entry.getKey()) .addStatement("target.$L(context,intent)", entry.getValue()) .addStatement("break"); }
caseBlockBuilder.endControlFlow(); MethodSpec broadcastReceiverMethod = MethodSpec.methodBuilder("onReceive") .addModifiers(Modifier.PUBLIC) .addParameter(contextClassName, "context") .addParameter(intentClassName, "intent") .addCode(caseBlockBuilder.build()) .returns(void.class) .build(); TypeSpec innerTypeSpec = TypeSpec.anonymousClassBuilder("") .addSuperinterface(broadcastReceiverClassName) .addMethod(broadcastReceiverMethod) .build(); methodBuilder.addStatement("$T globalBroadcastReceiver = $L", broadcastReceiverClassName, innerTypeSpec); methodBuilder.addStatement("target.registerReceiver(globalBroadcastReceiver,globalBroadcastFilter)"); methodBuilder.addStatement("hashMap.put($L,globalBroadcastReceiver)", BroadcastResponder.GLOBAL_BROADCAST); }
if (localBroadCast.size() > 0) { CodeBlock.Builder caseBlockBuilder = CodeBlock.builder().beginControlFlow("switch (intent.getAction())"); for (Map.Entry<String, String> entry : localBroadCast.entrySet()) { caseBlockBuilder.add("case $S:\n", entry.getKey()) .addStatement("target.$L(context,intent)", entry.getValue()) .addStatement("break"); }
caseBlockBuilder.endControlFlow();
MethodSpec broadcastReceiverMethod = MethodSpec.methodBuilder("onReceive") .addModifiers(Modifier.PUBLIC) .addParameter(contextClassName, "context") .addParameter(intentClassName, "intent") .addCode(caseBlockBuilder.build()) .returns(void.class) .build(); TypeSpec innerTypeSpec = TypeSpec.anonymousClassBuilder("") .addSuperinterface(broadcastReceiverClassName) .addMethod(broadcastReceiverMethod) .build(); methodBuilder.addStatement("$T localBroadcastReceiver = $L", broadcastReceiverClassName, innerTypeSpec); methodBuilder.addStatement("$T.getInstance(target).registerReceiver(localBroadcastReceiver,localBroadcastFilter)", localBroadcastManagerClassName); methodBuilder.addStatement("hashMap.put($L,localBroadcastReceiver)", BroadcastResponder.LOCAL_BROADCAST); }
methodBuilder.addStatement("return hashMap"); } }
|