Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Copyright (C) 2010-2016 eBusiness Information, Excilys Group
* Copyright (C) 2016-2017 the AndroidAnnotations project
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
Expand All @@ -18,5 +19,5 @@
import android.view.View;

public interface HasViews {
View findViewById(int id);
<T extends View> T internalFindViewById(int id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public void viewsAreNotInjected() throws Exception {
notifier.notifyViewChanged(new HasViews() {

@Override
public View findViewById(int id) {
return mock(View.class);
public <T extends View> T internalFindViewById(int id) {
return (T) mock(View.class);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void setUp() {
@Test
public void isItemClickAvailableFromListFragment() {
startFragment(myListFragment);
ListView listView = (ListView) myListFragment.findViewById(android.R.id.list);
ListView listView = (ListView) myListFragment.internalFindViewById(android.R.id.list);
long itemId = listView.getAdapter().getItemId(TESTED_CLICKED_INDEX);
View view = listView.getChildAt(TESTED_CLICKED_INDEX);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,11 @@ private JMethod setContentViewMethod(AbstractJType[] paramTypes, String[] paramN
return method;
}

@Override
public IJExpression getFindViewByIdExpression(JVar idParam) {
return JExpr._this().invoke("findViewById").arg(idParam);
}

public JVar getInitSavedInstanceParam() {
return initSavedInstanceParam;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,14 @@ public abstract class EComponentWithViewSupportHolder extends EComponentHolder i

public EComponentWithViewSupportHolder(AndroidAnnotationsEnvironment environment, TypeElement annotatedElement) throws Exception {
super(environment, annotatedElement);
viewNotifierHelper = new ViewNotifierHelper(this);
viewNotifierHelper = new ViewNotifierHelper(this, environment);
keyEventCallbackMethodsDelegate = new KeyEventCallbackMethodsDelegate<>(this);
}

public IJExpression getFindViewByIdExpression(JVar idParam) {
return _null();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe getFindViewByIdExpression should be an abstract method?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was considering this. Then we'd have the return _null() in EBeanHolder. I decided to keep it this way, but with no solid reason. If you prefer to have this abstract I can change it.

}

public JBlock getOnViewChangedBody() {
if (onViewChangedBody == null) {
setOnViewChanged();
Expand Down Expand Up @@ -132,7 +136,7 @@ protected void setOnViewChanged() {
}

public JInvocation findViewById(JFieldRef idRef) {
JInvocation findViewById = invoke(getOnViewChangedHasViewsParam(), "findViewById");
JInvocation findViewById = invoke(getOnViewChangedHasViewsParam(), "internalFindViewById");
findViewById.arg(idRef);
return findViewById;
}
Expand All @@ -157,8 +161,6 @@ protected FoundViewHolder createFoundViewAndIfNotNullBlock(JFieldRef idRef, Abst

if (viewClass == null) {
viewClass = getClasses().VIEW;
} else if (viewClass != getClasses().VIEW) {
findViewExpression = cast(viewClass, findViewExpression);
}

IJAssignmentTarget foundView = fieldRef;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static com.helger.jcodemodel.JExpr._new;
import static com.helger.jcodemodel.JExpr._null;
import static com.helger.jcodemodel.JExpr._super;
import static com.helger.jcodemodel.JExpr.cond;
import static com.helger.jcodemodel.JExpr.invoke;
import static com.helger.jcodemodel.JExpr.ref;
import static com.helger.jcodemodel.JMod.PRIVATE;
Expand All @@ -38,12 +39,14 @@

import com.helger.jcodemodel.AbstractJClass;
import com.helger.jcodemodel.IJAssignmentTarget;
import com.helger.jcodemodel.IJExpression;
import com.helger.jcodemodel.JBlock;
import com.helger.jcodemodel.JClassAlreadyExistsException;
import com.helger.jcodemodel.JDefinedClass;
import com.helger.jcodemodel.JExpr;
import com.helger.jcodemodel.JFieldRef;
import com.helger.jcodemodel.JFieldVar;
import com.helger.jcodemodel.JInvocation;
import com.helger.jcodemodel.JMethod;
import com.helger.jcodemodel.JMod;
import com.helger.jcodemodel.JVar;
Expand Down Expand Up @@ -100,7 +103,6 @@ private void setOnCreate() {
JBlock onCreateBody = onCreate.body();

JVar previousNotifier = viewNotifierHelper.replacePreviousNotifier(onCreateBody);
setFindViewById();
onCreateBody.invoke(getInit()).arg(onCreateSavedInstanceState);
onCreateBody.invoke(_super(), onCreate).arg(onCreateSavedInstanceState);
onCreateAfterSuperBlock = onCreateBody.blockSimple();
Expand All @@ -117,20 +119,10 @@ private void setOnViewCreated() {
viewNotifierHelper.invokeViewChanged(onViewCreatedBody);
}

private void setFindViewById() {
JMethod findViewById = generatedClass.method(PUBLIC, getClasses().VIEW, "findViewById");
findViewById.annotate(Override.class);

JVar idParam = findViewById.param(getCodeModel().INT, "id");

JBlock body = findViewById.body();

public IJExpression getFindViewByIdExpression(JVar idParam) {
JFieldVar contentView = getContentView();

body._if(contentView.eq(_null())) //
._then()._return(_null());

body._return(contentView.invoke(findViewById).arg(idParam));
JInvocation invocation = contentView.invoke("findViewById").arg(idParam);
return cond(contentView.eq(_null()), _null(), invocation);
}

private void setFragmentBuilder() throws JClassAlreadyExistsException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.androidannotations.AndroidAnnotationsEnvironment;

import com.helger.jcodemodel.AbstractJClass;
import com.helger.jcodemodel.IJExpression;
import com.helger.jcodemodel.JBlock;
import com.helger.jcodemodel.JExpr;
import com.helger.jcodemodel.JFieldVar;
Expand Down Expand Up @@ -207,6 +208,11 @@ public JBlock getOnDetachBeforeSuperBlock() {
return getOnDetachedToWindowBeforeSuperBlock();
}

@Override
public IJExpression getFindViewByIdExpression(JVar idParam) {
return JExpr._this().invoke("findViewById").arg(idParam);
}

@Override
public JFieldVar getIntentFilterField(ReceiverRegistrationDelegate.IntentFilterData intentFilterData) {
return receiverRegistrationDelegate.getIntentFilterField(intentFilterData);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Copyright (C) 2010-2016 eBusiness Information, Excilys Group
* Copyright (C) 2016-2017 the AndroidAnnotations project
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
Expand All @@ -20,25 +21,34 @@
import static com.helger.jcodemodel.JExpr._this;
import static com.helger.jcodemodel.JMod.FINAL;
import static com.helger.jcodemodel.JMod.PRIVATE;
import static com.helger.jcodemodel.JMod.PUBLIC;
import static org.androidannotations.helper.ModelConstants.generationSuffix;

import org.androidannotations.AndroidAnnotationsEnvironment;
import org.androidannotations.api.view.HasViews;
import org.androidannotations.api.view.OnViewChangedNotifier;
import org.androidannotations.holder.EComponentHolder;
import org.androidannotations.holder.EComponentWithViewSupportHolder;
import org.androidannotations.holder.EViewHolder;

import com.helger.jcodemodel.AbstractJClass;
import com.helger.jcodemodel.IJExpression;
import com.helger.jcodemodel.JBlock;
import com.helger.jcodemodel.JCodeModel;
import com.helger.jcodemodel.JDirectClass;
import com.helger.jcodemodel.JExpr;
import com.helger.jcodemodel.JFieldVar;
import com.helger.jcodemodel.JMethod;
import com.helger.jcodemodel.JVar;

public class ViewNotifierHelper {

private EComponentHolder holder;
private AndroidAnnotationsEnvironment environment;
private EComponentWithViewSupportHolder holder;
private JFieldVar notifier;

public ViewNotifierHelper(EComponentHolder holder) {
public ViewNotifierHelper(EComponentWithViewSupportHolder holder, AndroidAnnotationsEnvironment environment) {
this.holder = holder;
this.environment = environment;
}

public void invokeViewChanged(JBlock block) {
Expand All @@ -49,11 +59,25 @@ public JVar replacePreviousNotifier(JBlock block) {
AbstractJClass notifierClass = holder.getEnvironment().getJClass(OnViewChangedNotifier.class);
if (notifier == null) {
notifier = holder.getGeneratedClass().field(PRIVATE | FINAL, notifierClass, "onViewChangedNotifier" + generationSuffix(), _new(notifierClass));
holder.getGeneratedClass()._implements(HasViews.class);
implementHasViewsInHolder();
}
return block.decl(notifierClass, "previousNotifier", notifierClass.staticInvoke("replaceNotifier").arg(notifier));
}

private void implementHasViewsInHolder() {
holder.getGeneratedClass()._implements(HasViews.class);
JCodeModel codeModel = environment.getCodeModel();

JDirectClass genericType = codeModel.directClass("T");
JMethod findViewById = holder.getGeneratedClass().method(PUBLIC, genericType, "internalFindViewById");
findViewById.generify("T", environment.getClasses().VIEW);
findViewById.annotate(Override.class);

JVar idParam = findViewById.param(codeModel.INT, "id");
IJExpression findViewByIdExpression = holder.getFindViewByIdExpression(idParam);
findViewById.body()._return(JExpr.cast(genericType, findViewByIdExpression));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two things:

  1. type cast is not needed on Android O .
  2. You can remove the cast for other places, since the type will be inferred with this signare

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I wanted to not have to check the Android version (or the presence of the generic method) here. Can change that if you want. Not sure if casts have a perf impact?
  2. Done.

@dodgex dodgex Jun 10, 2017

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional Note to 1: It looks like the AppCompatActivity does not yet have the generic findViewById so the cast would be needed on android O + appcompat activity.

i checked the code in android-o-preview-2 and master branches.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it would be nice to gain something from this new signature, if we doing so much work because of it. 😆 Since you already have the check code, i guess you can add it now.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, they are overriding findViewById(), great... Then leave the cast here for now.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i just found that com.android.support:appcompat-v7:26.0.0-beta2 has the new signature.

@WonderCsabo WonderCsabo Jun 10, 2017

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. Then try to remove the cast with O.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cast is now gone for Android O. But it now requires to be on appcompat 26.0.0-beta2 (maybe beta1 works too; alpha1 does not).

}

public JVar replacePreviousNotifierWithNull(JBlock block) {
AbstractJClass notifierClass = holder.getEnvironment().getJClass(OnViewChangedNotifier.class);
return block.decl(notifierClass, "previousNotifier", notifierClass.staticInvoke("replaceNotifier").arg(_null()));
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.