Skip to content
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 1 commit
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
Prev Previous commit
Next Next commit
Creates abstract handler class to possible key events
  • Loading branch information
simopete authored and WonderCsabo committed Sep 11, 2015
commit 39e36dcb047483c8a0e7cb9da4a27a80706542b9
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public final class CanonicalNameConstants {
public static final String VIEW_GROUP = "android.view.ViewGroup";
public static final String CONTEXT = "android.content.Context";
public static final String KEY_EVENT = "android.view.KeyEvent";
public static final String KEY_EVENT_CALLBACK = "android.view.KeyEvent.Callback";
public static final String LAYOUT_INFLATER = "android.view.LayoutInflater";
public static final String FRAGMENT_ACTIVITY = "android.support.v4.app.FragmentActivity";
public static final String FRAGMENT = "android.app.Fragment";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright (C) 2010-2015 eBusiness Information, Excilys Group
*
* 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
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed To in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.androidannotations.holder;

import com.sun.codemodel.JSwitch;
import com.sun.codemodel.JVar;

public interface HasKeyEventCallbackMethods extends GeneratedClassHolder {
JSwitch getOnKeyDownSwitchBody();

JVar getOnKeyDownKeyEventParam();

JSwitch getOnKeyLongPressSwitchBody();

JVar getOnKeyLongPressKeyEventParam();

JSwitch getOnKeyMultipleSwitchBody();

JVar getOnKeyMultipleKeyEventParam();

JVar getOnKeyMultipleCountParam();

JSwitch getOnKeyUpSwitchBody();

JVar getOnKeyUpKeyEventParam();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* Copyright (C) 2010-2015 eBusiness Information, Excilys Group
*
* 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
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed To in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.androidannotations.internal.core.handler;

import static com.sun.codemodel.JExpr.TRUE;
import static com.sun.codemodel.JExpr.invoke;

import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;

import org.androidannotations.AndroidAnnotationsEnvironment;
import org.androidannotations.ElementValidation;
import org.androidannotations.helper.KeyCodeHelper;
import org.androidannotations.helper.ValidatorParameterHelper;
import org.androidannotations.holder.HasKeyEventCallbackMethods;

import com.sun.codemodel.JBlock;
import com.sun.codemodel.JInvocation;
import com.sun.codemodel.JSwitch;

public abstract class AbstractKeyEventHandler extends CoreBaseAnnotationHandler<HasKeyEventCallbackMethods> {

protected final KeyCodeHelper annotationHelper;

public AbstractKeyEventHandler(Class<?> targetClass, AndroidAnnotationsEnvironment environment) {
super(targetClass, environment);
this.annotationHelper = new KeyCodeHelper(environment, getTarget());
}

@Override
protected void validate(Element element, ElementValidation validation) {
coreValidatorHelper.enclosingElementExtendsKeyEventCallback(element, validation);

validatorHelper.isNotPrivate(element, validation);

validatorHelper.doesntThrowException(element, validation);

if (!annotationHelper.uniqueKeyCode(element, getTarget())) {
validation.addError(element, "%s " + element.getSimpleName() + " keyCode is not unique");
}

ExecutableElement executableElement = (ExecutableElement) element;

validatorHelper.returnTypeIsVoidOrBoolean(executableElement, validation);

String[] paramTypes = getParamTypes();
ValidatorParameterHelper.AnyOrderParamValidator param = validatorHelper.param.anyOrder();
if (paramTypes.length > 0) {
for (String paramType : paramTypes) {
param.type(paramType).optional();
}
param.validate(executableElement, validation);
}
}

@Override
public void process(Element element, HasKeyEventCallbackMethods holder) throws Exception {
String methodName = element.getSimpleName().toString();

ExecutableElement executableElement = (ExecutableElement) element;
TypeMirror returnType = executableElement.getReturnType();

boolean returnMethodResult = returnType.getKind() != TypeKind.VOID;

JSwitch switchBody = getSwitchBody(holder);

int[] keyCodes = annotationHelper.extractKeyCode(element);
for (int keyCode : keyCodes) {
String keyCodeFieldName = annotationHelper.getFieldNameForKeyCode(keyCode);
JBlock switchCaseBody = switchBody._case(getClasses().KEY_EVENT.staticRef(keyCodeFieldName)).body();

JInvocation methodCall = invoke(methodName);

if (returnMethodResult) {
switchCaseBody._return(methodCall);
} else {
switchCaseBody.add(methodCall);
switchCaseBody._return(TRUE);
}

passParametersToMethodCall(element, holder, methodCall);
}
}

public abstract String[] getParamTypes();

public abstract JSwitch getSwitchBody(HasKeyEventCallbackMethods holder);

public abstract void passParametersToMethodCall(Element element, HasKeyEventCallbackMethods holder, JInvocation methodCall);
}
Original file line number Diff line number Diff line change
Expand Up @@ -399,4 +399,12 @@ public void usesEnqueueIfHasId(Element element, ElementValidation valid) {
valid.addError("An id only can be used with Propagation.ENQUEUE");
}
}

public void extendsKeyEventCallback(Element element, ElementValidation validation) {
extendsType(element, CanonicalNameConstants.KEY_EVENT_CALLBACK, validation);
}

public void enclosingElementExtendsKeyEventCallback(Element element, ElementValidation validation) {
extendsKeyEventCallback(element.getEnclosingElement(), validation);
}
}