X Tutup
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 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
@@ -0,0 +1,65 @@
/**
* Copyright (C) 2010-2014 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.annotations;

import android.content.Intent;
import android.os.Parcelable;

import java.io.Serializable;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* <p>
* Use on any native, {@link Parcelable} or {@link Serializable} parameter of an
* {@link OnActivityResult} annotated method to bind it with the value from the Intent.
* </p>
* <p>
* The annotation value is the key used for the result data. If not set, the field name
* will be used as the key.
* </p>
*
* <blockquote>
*
* Some usage examples of &#064;Result annotation:
*
* <pre>
* &#064;OnActivityResult(REQUEST_CODE)
* void onResult(int resultCode, Intent data, <b>@Result String value</b>) {
* }
*
* &#064;OnActivityResult(REQUEST_CODE)
* void onResult(int resultCode, <b>@Result(value = "key") String value</b>) {
* }
*
* &#064;OnActivityResult(REQUEST_CODE)
* void onResult(<b>@Result String strVal</b>, <b>@Result int intVal</b>) {
* }
* </pre>
*
* </blockquote>
*
* @see android.app.Activity#onActivityResult(int, int, Intent)
* @see OnActivityResult
*/

@Retention(RetentionPolicy.CLASS)
@Target(ElementType.PARAMETER)
public @interface Result {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please, add some javadoc on the annotation. See existing annotations to have some examples.

String value() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.androidannotations.handler;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.processing.ProcessingEnvironment;
Expand All @@ -24,19 +25,28 @@
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;

import android.content.Intent;
import org.androidannotations.annotations.OnActivityResult;
import org.androidannotations.annotations.Result;
import org.androidannotations.helper.APTCodeModelHelper;
import org.androidannotations.helper.AnnotationHelper;
import org.androidannotations.helper.BundleHelper;
import org.androidannotations.helper.CanonicalNameConstants;
import org.androidannotations.holder.HasOnActivityResult;
import org.androidannotations.model.AnnotationElements;
import org.androidannotations.process.IsValid;

import com.sun.codemodel.JBlock;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JExpression;
import com.sun.codemodel.JInvocation;
import com.sun.codemodel.JVar;

public class OnActivityResultHandler extends BaseAnnotationHandler<HasOnActivityResult> {

private final APTCodeModelHelper codeModelHelper = new APTCodeModelHelper();

public OnActivityResultHandler(ProcessingEnvironment processingEnvironment) {
super(OnActivityResult.class, processingEnvironment);
}
Expand Down Expand Up @@ -65,35 +75,32 @@ public void process(Element element, HasOnActivityResult holder) throws Exceptio
ExecutableElement executableElement = (ExecutableElement) element;
List<? extends VariableElement> parameters = executableElement.getParameters();

int intentParameterPosition = -1;
int resultCodeParameterPosition = -1;
int requestCode = executableElement.getAnnotation(OnActivityResult.class).value();
JBlock onResultBlock = holder.getOnActivityResultCaseBlock(requestCode).block();

for (int i = 0; i < parameters.size(); i++) {
VariableElement parameter = parameters.get(i);
List<JExpression> onResultArgs = new ArrayList<JExpression>();
for (VariableElement parameter : parameters) {
TypeMirror parameterType = parameter.asType();

if (CanonicalNameConstants.INTENT.equals(parameterType.toString())) {
intentParameterPosition = i;
Result resultAnnotation = parameter.getAnnotation(Result.class);
if (resultAnnotation != null) {
JExpression extraParameter = ResultHandler.getExtraValue(holder, onResultBlock, parameter);
onResultArgs.add(extraParameter);
} else if (CanonicalNameConstants.INTENT.equals(parameterType.toString())) {
JVar intentParameter = holder.getOnActivityResultDataParam();
onResultArgs.add(intentParameter);
} else if (parameterType.getKind().equals(TypeKind.INT) //
|| CanonicalNameConstants.INTEGER.equals(parameterType.toString())) {
resultCodeParameterPosition = i;
|| CanonicalNameConstants.INTEGER.equals(parameterType.toString())) {
JVar resultCodeParameter = holder.getOnActivityResultResultCodeParam();
onResultArgs.add(resultCodeParameter);
}
}

int requestCode = executableElement.getAnnotation(OnActivityResult.class).value();
JBlock onActivityResultCase = holder.getOnActivityResultCaseBlock(requestCode);

JExpression activityRef = holder.getGeneratedClass().staticRef("this");
JInvocation onResultInvocation = onActivityResultCase.invoke(activityRef, methodName);

for (int i = 0; i < parameters.size(); i++) {
if (i == intentParameterPosition) {
JVar intentParameter = holder.getOnActivityResultDataParam();
onResultInvocation.arg(intentParameter);
} else if (i == resultCodeParameterPosition) {
JVar resultCodeParameter = holder.getOnActivityResultResultCodeParam();
onResultInvocation.arg(resultCodeParameter);
}
JInvocation onResultInvocation = onResultBlock.invoke(activityRef, methodName);
for (JExpression onResultArg : onResultArgs) {
onResultInvocation.arg(onResultArg);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright (C) 2010-2014 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.handler;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.Name;
import javax.lang.model.element.VariableElement;

import org.androidannotations.annotations.Result;
import org.androidannotations.helper.APTCodeModelHelper;
import org.androidannotations.helper.AnnotationHelper;
import org.androidannotations.helper.BundleHelper;
import org.androidannotations.helper.IdValidatorHelper;
import org.androidannotations.helper.TargetAnnotationHelper;
import org.androidannotations.helper.ValidatorHelper;
import org.androidannotations.holder.HasOnActivityResult;
import org.androidannotations.model.AnnotationElements;

import com.sun.codemodel.JBlock;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JExpression;
import com.sun.codemodel.JVar;
import org.androidannotations.process.IsValid;

public class ResultHandler {

private final static APTCodeModelHelper codeModelHelper = new APTCodeModelHelper();

public static JExpression getExtraValue(HasOnActivityResult holder, JBlock block,
VariableElement parameter) {
Result annotation = parameter.getAnnotation(Result.class);
String parameterName = parameter.getSimpleName().toString();
String extraKey = annotation.value();
if (extraKey.isEmpty()) {
extraKey = parameterName;
}

JVar extras = holder.getOnActivityResultExtras();
BundleHelper bundleHelper = new BundleHelper(new AnnotationHelper(holder.processingEnvironment()), parameter);
JExpression restoreMethodCall = JExpr.invoke(extras, bundleHelper.getMethodNameToRestore()).arg(extraKey);
JClass parameterClass = codeModelHelper.typeMirrorToJClass(parameter.asType(), holder);
if (bundleHelper.restoreCallNeedCastStatement()) {
restoreMethodCall = JExpr.cast(parameterClass, restoreMethodCall);
}
return block.decl(parameterClass, parameterName + "_", restoreMethodCall);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import org.androidannotations.annotations.EIntentService;
import org.androidannotations.annotations.EService;
import org.androidannotations.annotations.Receiver;
import org.androidannotations.annotations.Result;
import org.androidannotations.annotations.Trace;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.rest.Delete;
Expand Down Expand Up @@ -1366,6 +1367,9 @@ public void hasOnResultMethodParameters(ExecutableElement executableElement, IsV
boolean intentParameterFound = false;
for (VariableElement parameter : parameters) {
TypeMirror parameterType = parameter.asType();
if (parameter.getAnnotation(Result.class) != null) {
continue;
}
if (parameterType.toString().equals(CanonicalNameConstants.INTEGER) //
|| parameterType.getKind().equals(TypeKind.INT)) {
if (resultCodeParameterFound) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,50 @@
*/
package org.androidannotations.holder;

import com.sun.codemodel.*;
import org.androidannotations.api.SdkVersionHelper;
import org.androidannotations.helper.*;
import org.androidannotations.process.ProcessHolder;
import static com.sun.codemodel.JExpr.FALSE;
import static com.sun.codemodel.JExpr.TRUE;
import static com.sun.codemodel.JExpr._new;
import static com.sun.codemodel.JExpr._null;
import static com.sun.codemodel.JExpr._super;
import static com.sun.codemodel.JExpr._this;
import static com.sun.codemodel.JExpr.cast;
import static com.sun.codemodel.JExpr.invoke;
import static com.sun.codemodel.JMod.PRIVATE;
import static com.sun.codemodel.JMod.PUBLIC;
import static org.androidannotations.helper.ModelConstants.GENERATION_SUFFIX;

import java.util.ArrayList;
import java.util.List;

import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.util.ElementFilter;
import java.util.ArrayList;
import java.util.List;

import static com.sun.codemodel.JExpr.*;
import static com.sun.codemodel.JMod.PRIVATE;
import static com.sun.codemodel.JMod.PUBLIC;
import org.androidannotations.api.SdkVersionHelper;
import org.androidannotations.helper.ActionBarSherlockHelper;
import org.androidannotations.helper.ActivityIntentBuilder;
import org.androidannotations.helper.AndroidManifest;
import org.androidannotations.helper.AnnotationHelper;
import org.androidannotations.helper.CanonicalNameConstants;
import org.androidannotations.helper.GreenDroidHelper;
import org.androidannotations.helper.IntentBuilder;
import org.androidannotations.process.ProcessHolder;

import com.sun.codemodel.JBlock;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JClassAlreadyExistsException;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JExpression;
import com.sun.codemodel.JFieldVar;
import com.sun.codemodel.JInvocation;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;
import com.sun.codemodel.JType;
import com.sun.codemodel.JVar;

public class EActivityHolder extends EComponentWithViewSupportHolder implements HasIntentBuilder, HasExtras, HasInstanceState, HasOptionsMenu, HasOnActivityResult, HasReceiverRegistration {

Expand Down Expand Up @@ -660,6 +686,11 @@ public JVar getOnActivityResultRequestCodeParam() {
return onActivityResultHolder.getRequestCodeParam();
}

@Override
public JVar getOnActivityResultExtras() {
return onActivityResultHolder.getResultExtras();
}

public JBlock getOnDestroyAfterSuperBlock() {
if (onDestroyAfterSuperBlock == null) {
setOnDestroy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,34 @@
*/
package org.androidannotations.holder;

import com.sun.codemodel.*;
import static com.sun.codemodel.JExpr.FALSE;
import static com.sun.codemodel.JExpr.TRUE;
import static com.sun.codemodel.JExpr._new;
import static com.sun.codemodel.JExpr._null;
import static com.sun.codemodel.JExpr._super;
import static com.sun.codemodel.JExpr.invoke;
import static com.sun.codemodel.JExpr.ref;
import static com.sun.codemodel.JMod.PRIVATE;
import static com.sun.codemodel.JMod.PUBLIC;
import static com.sun.codemodel.JMod.STATIC;

import javax.lang.model.element.TypeElement;

import org.androidannotations.helper.ActionBarSherlockHelper;
import org.androidannotations.helper.AnnotationHelper;
import org.androidannotations.helper.HoloEverywhereHelper;
import org.androidannotations.process.ProcessHolder;

import javax.lang.model.element.TypeElement;

import static com.sun.codemodel.JExpr.*;
import static com.sun.codemodel.JMod.*;
import com.sun.codemodel.JBlock;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JClassAlreadyExistsException;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JFieldRef;
import com.sun.codemodel.JFieldVar;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;
import com.sun.codemodel.JVar;

public class EFragmentHolder extends EComponentWithViewSupportHolder implements HasInstanceState, HasOptionsMenu, HasOnActivityResult, HasReceiverRegistration {

Expand Down Expand Up @@ -416,6 +434,11 @@ public JVar getOnActivityResultResultCodeParam() {
return onActivityResultHolder.getResultCodeParam();
}

@Override
public JVar getOnActivityResultExtras() {
return onActivityResultHolder.getResultExtras();
}

@Override
public JFieldVar getIntentFilterField(String[] actions) {
return receiverRegistrationHolder.getIntentFilterField(actions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ public interface HasOnActivityResult extends GeneratedClassHolder {
JVar getOnActivityResultDataParam();

JVar getOnActivityResultResultCodeParam();

JVar getOnActivityResultExtras();
}
Loading
X Tutup