|
16 | 16 | package com.googlecode.androidannotations.processing; |
17 | 17 |
|
18 | 18 | import java.lang.annotation.Annotation; |
| 19 | +import java.util.List; |
19 | 20 |
|
20 | 21 | import javax.annotation.processing.ProcessingEnvironment; |
21 | 22 | import javax.lang.model.element.Element; |
| 23 | +import javax.lang.model.element.ExecutableElement; |
| 24 | +import javax.lang.model.element.Modifier; |
22 | 25 | import javax.lang.model.element.TypeElement; |
| 26 | +import javax.lang.model.type.TypeKind; |
| 27 | +import javax.lang.model.util.ElementFilter; |
23 | 28 |
|
24 | 29 | import com.googlecode.androidannotations.annotations.EActivity; |
25 | 30 | import com.googlecode.androidannotations.annotations.Id; |
| 31 | +import com.googlecode.androidannotations.api.SdkVersionHelper; |
26 | 32 | import com.googlecode.androidannotations.helper.AnnotationHelper; |
27 | 33 | import com.googlecode.androidannotations.rclass.IRClass; |
28 | 34 | import com.googlecode.androidannotations.rclass.IRClass.Res; |
|
32 | 38 | import com.sun.codemodel.JCodeModel; |
33 | 39 | import com.sun.codemodel.JExpr; |
34 | 40 | import com.sun.codemodel.JFieldRef; |
| 41 | +import com.sun.codemodel.JInvocation; |
35 | 42 | import com.sun.codemodel.JMethod; |
36 | 43 | import com.sun.codemodel.JMod; |
37 | 44 | import com.sun.codemodel.JVar; |
@@ -107,6 +114,56 @@ public void process(Element element, JCodeModel codeModel, ActivitiesHolder acti |
107 | 114 |
|
108 | 115 | onCreateBody.invoke(JExpr._super(), onCreate).arg(onCreateSavedInstanceState); |
109 | 116 |
|
| 117 | + // onBackPressed |
| 118 | + if (hasOnBackPressedMethod(typeElement)) { |
| 119 | + JMethod onKeyDownMethod = holder.activity.method(JMod.PUBLIC, codeModel.BOOLEAN, "onKeyDown"); |
| 120 | + onKeyDownMethod.annotate(Override.class); |
| 121 | + JVar keyCodeParam = onKeyDownMethod.param(codeModel.INT, "keyCode"); |
| 122 | + JClass keyEventClass = holder.refClass("android.view.KeyEvent"); |
| 123 | + JVar eventParam = onKeyDownMethod.param(keyEventClass, "event"); |
| 124 | + |
| 125 | + JClass versionHelperClass = codeModel.ref(SdkVersionHelper.class); |
| 126 | + |
| 127 | + JInvocation sdkInt = versionHelperClass.staticInvoke("getSdkInt"); |
| 128 | + |
| 129 | + JBlock onKeyDownBody = onKeyDownMethod.body(); |
| 130 | + |
| 131 | + onKeyDownBody._if( // |
| 132 | + sdkInt.lt(JExpr.lit(5)) // |
| 133 | + .cand(keyCodeParam.eq(keyEventClass.staticRef("KEYCODE_BACK"))) // |
| 134 | + .cand(eventParam.invoke("getRepeatCount").eq(JExpr.lit(0)))) // |
| 135 | + ._then() // |
| 136 | + .invoke("onBackPressed"); |
| 137 | + |
| 138 | + onKeyDownBody._return( // |
| 139 | + JExpr._super().invoke(onKeyDownMethod) // |
| 140 | + .arg(keyCodeParam) // |
| 141 | + .arg(eventParam)); |
| 142 | + |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private boolean hasOnBackPressedMethod(TypeElement activityElement) { |
| 147 | + |
| 148 | + List<? extends Element> allMembers = getElementUtils().getAllMembers(activityElement); |
| 149 | + |
| 150 | + List<ExecutableElement> activityInheritedMethods = ElementFilter.methodsIn(allMembers); |
| 151 | + |
| 152 | + for (ExecutableElement activityInheritedMethod : activityInheritedMethods) { |
| 153 | + if (isOnBackPressedMethod(activityInheritedMethod)) { |
| 154 | + return true; |
| 155 | + } |
| 156 | + } |
| 157 | + return false; |
| 158 | + } |
| 159 | + |
| 160 | + private boolean isOnBackPressedMethod(ExecutableElement method) { |
| 161 | + return method.getSimpleName().toString().equals("onBackPressed") // |
| 162 | + && method.getThrownTypes().size() == 0 // |
| 163 | + && method.getModifiers().contains(Modifier.PUBLIC) // |
| 164 | + && method.getReturnType().getKind().equals(TypeKind.VOID) // |
| 165 | + && method.getParameters().size() == 0 // |
| 166 | + ; |
110 | 167 | } |
111 | 168 |
|
112 | 169 | } |
0 commit comments