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
Next Next commit
Add InstanceState support to EView/EViewGroup
  • Loading branch information
simopete authored and dodgex committed Dec 10, 2016
commit b981bf41a1c5778b921973c6d801a48747f81185
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ public void enclosingElementHasEActivityOrEFragment(Element element, ElementVali
enclosingElementHasOneOfAnnotations(element, validAnnotations, valid);
}

public void enclosingElementHasEActivityOrEFragmentOrEViewOrEViewGroup(Element element, ElementValidation valid) {
List<Class<? extends Annotation>> validAnnotations = asList(EActivity.class, EFragment.class, EView.class, EViewGroup.class);
enclosingElementHasOneOfAnnotations(element, validAnnotations, valid);
}

public void enclosingElementHasEActivityOrEFragmentOrEServiceOrEIntentServiceOrEViewOrEViewGroup(Element element, ElementValidation valid) {
List<Class<? extends Annotation>> validAnnotations = asList(EActivity.class, EFragment.class, EService.class, EIntentService.class, EView.class, EViewGroup.class);
enclosingElementHasOneOfAnnotations(element, validAnnotations, valid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import com.helger.jcodemodel.JMod;
import com.helger.jcodemodel.JVar;

public class EViewHolder extends EComponentWithViewSupportHolder implements HasReceiverRegistration {
public class EViewHolder extends EComponentWithViewSupportHolder implements HasInstanceState, HasReceiverRegistration {

protected static final String ALREADY_INFLATED_COMMENT = "" // +
+ "The alreadyInflated_ hack is needed because of an Android bug\n" // +
Expand All @@ -61,6 +61,7 @@ public class EViewHolder extends EComponentWithViewSupportHolder implements HasR
private JMethod onDetachedFromWindowMethod;
private JBlock onDetachedFromWindowBeforeSuperBlock;
private ReceiverRegistrationDelegate<EViewHolder> receiverRegistrationDelegate;
private ViewInstanceStateDelegate instanceStateDelegate;
protected JBlock initBody;
protected JMethod onFinishInflate;
protected JFieldVar alreadyInflated;
Expand All @@ -70,6 +71,7 @@ public EViewHolder(AndroidAnnotationsEnvironment environment, TypeElement annota
addSuppressWarning();
createConstructorAndBuilder();
receiverRegistrationDelegate = new ReceiverRegistrationDelegate<>(this);
instanceStateDelegate = new ViewInstanceStateDelegate(this);
}

private void addSuppressWarning() {
Expand Down Expand Up @@ -248,4 +250,23 @@ private void setOnDetachedFromWindow() {
body.invoke(_super(), onAttachedToWindowMethod);
}

@Override
public JBlock getSaveStateMethodBody() {
return instanceStateDelegate.getSaveStateMethodBody();
}

@Override
public JVar getSaveStateBundleParam() {
return instanceStateDelegate.getSaveStateBundleParam();
}

@Override
public JMethod getRestoreStateMethod() {
return instanceStateDelegate.getRestoreStateMethod();
}

@Override
public JVar getRestoreStateBundleParam() {
return instanceStateDelegate.getRestoreStateBundleParam();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/**
* Copyright (C) 2010-2016 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 static com.helger.jcodemodel.JExpr._new;
import static com.helger.jcodemodel.JExpr._super;
import static com.helger.jcodemodel.JExpr.cast;
import static com.helger.jcodemodel.JExpr.lit;
import static com.helger.jcodemodel.JMod.FINAL;
import static com.helger.jcodemodel.JMod.PRIVATE;
import static com.helger.jcodemodel.JMod.PUBLIC;
import static com.helger.jcodemodel.JMod.STATIC;
import static org.androidannotations.helper.ModelConstants.generationSuffix;

import com.helger.jcodemodel.JBlock;
import com.helger.jcodemodel.JMethod;
import com.helger.jcodemodel.JVar;

public class ViewInstanceStateDelegate extends GeneratedClassHolderDelegate<EComponentHolder> implements HasInstanceState {

private JVar instanceStateKey;
private JBlock saveStateMethodBody;
private JVar saveStateBundleParam;
private JMethod restoreStateMethod;
private JVar restoreStateBundleParam;

public ViewInstanceStateDelegate(EComponentHolder holder) {
super(holder);
}

@Override
public JBlock getSaveStateMethodBody() {
if (saveStateMethodBody == null) {
setSaveStateMethod();
}
return saveStateMethodBody;
}

@Override
public JVar getSaveStateBundleParam() {
if (saveStateBundleParam == null) {
setSaveStateMethod();
}
return saveStateBundleParam;
}

private void setSaveStateMethod() {
JMethod method = getGeneratedClass().method(PUBLIC, getClasses().PARCELABLE, "onSaveInstanceState");
method.annotate(Override.class);

JMethod saveStateMethod = getGeneratedClass().method(PRIVATE, codeModel().VOID, "saveInstanceState");
saveStateBundleParam = saveStateMethod.param(getClasses().BUNDLE, "bundle");
saveStateMethodBody = saveStateMethod.body();

JBlock methodBody = method.body();
JVar onSaveSuperInstanceState = methodBody.decl(getClasses().PARCELABLE, "instanceState", _super().invoke("onSaveInstanceState"));

JVar bundleParam = methodBody.decl(getClasses().BUNDLE, "bundle" + generationSuffix(), _new(getClasses().BUNDLE));
methodBody.invoke(bundleParam, "putParcelable").arg(getInstanceStateKey()).arg(onSaveSuperInstanceState);

methodBody.invoke(saveStateMethod).arg(bundleParam);

methodBody._return(bundleParam);
}

@Override
public JMethod getRestoreStateMethod() {
if (restoreStateMethod == null) {
setRestoreStateMethod();
}
return restoreStateMethod;
}

@Override
public JVar getRestoreStateBundleParam() {
if (restoreStateBundleParam == null) {
setRestoreStateMethod();
}
return restoreStateBundleParam;
}

private void setRestoreStateMethod() {
restoreStateMethod = getGeneratedClass().method(PUBLIC, codeModel().VOID, "onRestoreInstanceState");
restoreStateMethod.annotate(Override.class);
JVar state = restoreStateMethod.param(getClasses().PARCELABLE, "state");

JBlock restoreStateMethodBody = restoreStateMethod.body();
restoreStateBundleParam = restoreStateMethodBody.decl(getClasses().BUNDLE, "bundle" + generationSuffix(), cast(getClasses().BUNDLE, state));
JVar instanceState = restoreStateMethodBody.decl(getClasses().PARCELABLE, "instanceState", restoreStateBundleParam.invoke("getParcelable").arg(getInstanceStateKey()));
restoreStateMethodBody.invoke(_super(), "onRestoreInstanceState").arg(instanceState);
}

private JVar getInstanceStateKey() {
if (instanceStateKey == null) {
instanceStateKey = getGeneratedClass().field(PUBLIC | STATIC | FINAL, getClasses().STRING, "INSTANCE_STATE_KEY", lit("instanceState"));
}
return instanceStateKey;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public InstanceStateHandler(AndroidAnnotationsEnvironment environment) {

@Override
public void validate(Element element, ElementValidation validation) {
validatorHelper.enclosingElementHasEActivityOrEFragment(element, validation);
validatorHelper.enclosingElementHasEActivityOrEFragmentOrEViewOrEViewGroup(element, validation);

validatorHelper.isNotPrivate(element, validation);

Expand Down