Skip to content

Commit fcfabce

Browse files
committed
Support for onBackPressed on pre ECLAIR Android versions. Fixes issue 46.
1 parent e6fdea7 commit fcfabce

5 files changed

Lines changed: 92 additions & 4 deletions

File tree

‎AndroidAnnotations/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<dependency>
2929
<groupId>android</groupId>
3030
<artifactId>android</artifactId>
31-
<version>1.5_r4</version>
31+
<version>1.6_r3</version>
3232
<scope>provided</scope>
3333
</dependency>
3434
</dependencies>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.googlecode.androidannotations.api;
2+
3+
import android.os.Build;
4+
import android.os.Build.VERSION;
5+
6+
public class SdkVersionHelper {
7+
8+
public static int getSdkInt() {
9+
if (Build.VERSION.RELEASE.startsWith("1.5"))
10+
return 3;
11+
12+
return HelperInternal.getSdkIntInternal();
13+
}
14+
15+
private static class HelperInternal {
16+
private static int getSdkIntInternal() {
17+
return VERSION.SDK_INT;
18+
}
19+
}
20+
21+
}

‎AndroidAnnotations/src/main/java/com/googlecode/androidannotations/processing/EActivityProcessor.java‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,19 @@
1616
package com.googlecode.androidannotations.processing;
1717

1818
import java.lang.annotation.Annotation;
19+
import java.util.List;
1920

2021
import javax.annotation.processing.ProcessingEnvironment;
2122
import javax.lang.model.element.Element;
23+
import javax.lang.model.element.ExecutableElement;
24+
import javax.lang.model.element.Modifier;
2225
import javax.lang.model.element.TypeElement;
26+
import javax.lang.model.type.TypeKind;
27+
import javax.lang.model.util.ElementFilter;
2328

2429
import com.googlecode.androidannotations.annotations.EActivity;
2530
import com.googlecode.androidannotations.annotations.Id;
31+
import com.googlecode.androidannotations.api.SdkVersionHelper;
2632
import com.googlecode.androidannotations.helper.AnnotationHelper;
2733
import com.googlecode.androidannotations.rclass.IRClass;
2834
import com.googlecode.androidannotations.rclass.IRClass.Res;
@@ -32,6 +38,7 @@
3238
import com.sun.codemodel.JCodeModel;
3339
import com.sun.codemodel.JExpr;
3440
import com.sun.codemodel.JFieldRef;
41+
import com.sun.codemodel.JInvocation;
3542
import com.sun.codemodel.JMethod;
3643
import com.sun.codemodel.JMod;
3744
import com.sun.codemodel.JVar;
@@ -107,6 +114,56 @@ public void process(Element element, JCodeModel codeModel, ActivitiesHolder acti
107114

108115
onCreateBody.invoke(JExpr._super(), onCreate).arg(onCreateSavedInstanceState);
109116

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+
;
110167
}
111168

112169
}

‎HelloWorldEclipse/src/com/googlecode/androidannotations/helloworldeclipse/ActivityWithExtra.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import android.app.Activity;
66
import android.os.Bundle;
7+
import android.view.KeyEvent;
78
import android.widget.TextView;
89

910
import com.googlecode.androidannotations.annotations.EActivity;

‎HelloWorldEclipse/src/com/googlecode/androidannotations/helloworldeclipse/MyActivity.java‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import android.view.WindowManager;
1717
import android.widget.EditText;
1818
import android.widget.TextView;
19+
import android.widget.Toast;
1920

2021
import com.googlecode.androidannotations.annotations.Background;
2122
import com.googlecode.androidannotations.annotations.BeforeViews;
@@ -52,16 +53,24 @@ public class MyActivity extends Activity {
5253

5354
@SystemService
5455
NotificationManager notificationManager;
55-
56+
5657
@SystemService
5758
WindowManager windowManager;
58-
59+
5960
@BeforeViews
6061
void doStuffWithDisplay() {
6162
// windowManager should not be null
6263
windowManager.getDefaultDisplay();
6364
}
64-
65+
66+
/**
67+
* AndroidAnnotations gracefully handles support for onBackPressed, whether
68+
* you use ECLAIR (2.0), or pre ECLAIR android version.
69+
*/
70+
public void onBackPressed() {
71+
Toast.makeText(this, "Back key pressed!", Toast.LENGTH_SHORT).show();
72+
}
73+
6574
@BeforeViews
6675
void requestIndeterminateProgress() {
6776
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

0 commit comments

Comments
 (0)