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,54 @@
/**
* Copyright (C) 2016 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
* 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.test.eview;

import org.androidannotations.annotations.EView;
import org.androidannotations.annotations.InstanceState;
import org.androidannotations.test.instancestate.MyParcelableBean;

import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;

@EView
public class ViewWithInstanceState extends View {
Copy link
Member

Choose a reason for hiding this comment

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

I think we should have another test, which asserts we correctly retain the saved instance information from the super class. So there should be a non-AA Android custom View class, which have onSaveInstanceState() and onRestoreInstanceState(), then an enhanced class should subclass it, and add @InstanceState. We should assert that the state restoration in parent works as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

do we need an extra non aa custom view? shouldn't it be enough to have custom save/restore in this view class?

Copy link
Member

Choose a reason for hiding this comment

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

It is enough, if you can check if the state in the superclass is properly saved, however i think you cannot do that. Maybe you can extend from another View class, which saves state we can assert against, but the most clean would be writing a non-AA custom View.

Copy link
Member Author

Choose a reason for hiding this comment

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

I added this case into the current one. hope it is ok?


@InstanceState
String stateTest = "does it work?";

String stringState = "does it work?";
MyParcelableBean beanState = new MyParcelableBean(0);

public ViewWithInstanceState(Context context) {
super(context);
}

@Override
public Parcelable onSaveInstanceState() {
Bundle bundle = new Bundle();
bundle.putString("stringState", stringState);
bundle.putParcelable("beanState", beanState);
return bundle;
}

@Override
public void onRestoreInstanceState(Parcelable state) {
Bundle bundle = (Bundle) state;
stringState = bundle.getString("stringState");
beanState = bundle.getParcelable("beanState");
}
}
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 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 @@ -26,6 +27,10 @@ public MyParcelableBean(int x) {
this.x = x;
}

public int getX() {
return x;
}

protected MyParcelableBean(Parcel in) {
x = in.readInt();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Copyright (C) 2016 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
* 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.test.eview;

import static org.fest.assertions.api.Assertions.assertThat;

import org.androidannotations.test.EmptyActivityWithoutLayout_;
import org.androidannotations.test.instancestate.MyParcelableBean;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;

import android.content.Context;
import android.os.Parcelable;

@RunWith(RobolectricTestRunner.class)
public class ViewInstanceStateTest {

@Test
public void viewInstanceStateSaveTest() {
Context context = Robolectric.buildActivity(EmptyActivityWithoutLayout_.class).create().get();
ViewWithInstanceState_ savingView = (ViewWithInstanceState_) ViewWithInstanceState_.build(context);

assertThat(savingView.stateTest).isEqualTo("does it work?");
assertThat(savingView.stringState).isEqualTo("does it work?");
assertThat(savingView.beanState.getX()).isEqualTo(0);

savingView.stateTest = "it works!";
savingView.stringState = "it works!";
savingView.beanState = new MyParcelableBean(1);
Parcelable savedInstanceState = savingView.onSaveInstanceState();

ViewWithInstanceState_ restoringView = (ViewWithInstanceState_) ViewWithInstanceState_.build(context);
assertThat(restoringView.stateTest).isEqualTo("does it work?");
assertThat(restoringView.stringState).isEqualTo("does it work?");
assertThat(restoringView.beanState.getX()).isEqualTo(0);

restoringView.onRestoreInstanceState(savedInstanceState);
assertThat(restoringView.stateTest).isEqualTo("it works!");
assertThat(restoringView.stringState).isEqualTo("it works!");
assertThat(restoringView.beanState.getX()).isEqualTo(1);
}
}
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 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 Down Expand Up @@ -170,6 +171,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
@@ -1,5 +1,6 @@
/**
* Copyright (C) 2010-2016 eBusiness Information, Excilys Group
* Copyright (C) 2016 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 Down Expand Up @@ -42,7 +43,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 +62,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 +72,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 +251,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) 2016 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
* 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
@@ -1,5 +1,6 @@
/**
* Copyright (C) 2010-2016 eBusiness Information, Excilys Group
* Copyright (C) 2016 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 Down Expand Up @@ -43,7 +44,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
X Tutup