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
@@ -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 @@ -106,6 +107,14 @@ enum Scope {
*/
Scope value() default Scope.ACTIVITY;

/**
* The name of the preferences. To be used with {@link Scope#ACTIVITY} or
* {@link Scope#UNIQUE}. If empty the interface name is used.
*
* @return the name of the preferences
*/
String name() default "";

/**
* The operating mode.
*
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 All @@ -23,6 +24,7 @@
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -143,6 +145,15 @@ public void validate(Element element, ElementValidation validation) {
}
}
}

SharedPref sharedPrefAnnotation = element.getAnnotation(SharedPref.class);
SharedPref.Scope scope = sharedPrefAnnotation.value();
String name = sharedPrefAnnotation.name();
boolean hasCustomName = !name.trim().isEmpty();
EnumSet<SharedPref.Scope> allowedScopes = EnumSet.of(SharedPref.Scope.ACTIVITY, SharedPref.Scope.UNIQUE);

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.

Nice!

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.

:)

if (hasCustomName && !allowedScopes.contains(scope)) {
validation.addError("SharedPref#name() is only supported for Scope.ACTIVITY and Scope.UNIQUE.");
}
}

@Override
Expand All @@ -154,9 +165,12 @@ public void process(Element element, SharedPrefHolder holder) {
private void generateConstructor(Element element, SharedPrefHolder holder) {
SharedPref sharedPrefAnnotation = element.getAnnotation(SharedPref.class);
SharedPref.Scope scope = sharedPrefAnnotation.value();
String name = sharedPrefAnnotation.name();
int mode = sharedPrefAnnotation.mode();

String interfaceSimpleName = element.getSimpleName().toString();
if (name.trim().isEmpty()) {
name = element.getSimpleName().toString();
}
JBlock constructorSuperBlock = holder.getConstructorSuperBlock();
JVar contextParam = holder.getConstructorContextParam();

Expand All @@ -174,14 +188,14 @@ private void generateConstructor(Element element, SharedPrefHolder holder) {
constructorSuperBlock.invoke("super") //
.arg(contextParam.invoke("getSharedPreferences") //
.arg(invoke(getLocalClassName).arg(contextParam) //
.plus(lit("_" + interfaceSimpleName))) //
.plus(lit("_" + name))) //
.arg(lit(mode)));
break;
}
case UNIQUE: {
constructorSuperBlock.invoke("super") //
.arg(contextParam.invoke("getSharedPreferences") //
.arg(lit(interfaceSimpleName)) //
.arg(lit(name)) //
.arg(lit(mode)));
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* 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.sharedprefs;

import org.androidannotations.annotations.EActivity;

import android.app.Activity;

@EActivity
public class ActivityInManifest extends Activity {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* 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.sharedprefs;

import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.sharedpreferences.SharedPref;

@EBean
public class PrefsCollection {

@SharedPref(SharedPref.Scope.ACTIVITY)
public interface ActivityPrefs {

}

@SharedPref(value = SharedPref.Scope.ACTIVITY, name = "named_pref_activity")
public interface NamedActivityPrefs {

}

@SharedPref(SharedPref.Scope.UNIQUE)
public interface UniquePrefs {

}

@SharedPref(value = SharedPref.Scope.UNIQUE, name = "named_pref_unique")
public interface NamedUniquePrefs {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* 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.sharedprefs;

import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.sharedpreferences.SharedPref;

@EBean
public class PrefsCollectionWithError {

@SharedPref(value = SharedPref.Scope.ACTIVITY_DEFAULT, name = "named_pref_activity_default")
public interface NamedActivityDefaultPrefs {

}

@SharedPref(value = SharedPref.Scope.APPLICATION_DEFAULT, name = "named_pref_application_default")
public interface NamedApplicationDefaultPrefs {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* 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.sharedprefs;

import java.io.File;
import java.io.IOException;

import org.androidannotations.internal.AndroidAnnotationProcessor;
import org.androidannotations.testutils.AAProcessorTestHelper;
import org.junit.Before;
import org.junit.Test;

public class SharedPrefNamingTest extends AAProcessorTestHelper {

@Before
public void setUp() {
addManifestProcessorParameter(ActivityInManifest.class);
addProcessor(AndroidAnnotationProcessor.class);
ensureOutputDirectoryIsEmpty();
}

@Test
public void testNamedPrefGeneration() throws IOException {
CompileResult result = compileFiles(PrefsCollection.class);
File generatedFile = toGeneratedFile(PrefsCollection.class);

assertCompilationSuccessful(result);

// CHECKSTYLE:OFF
String[] activityPref = { //
" public ActivityPrefs_(Context context) {", //
" super(context.getSharedPreferences((getLocalClassName(context)+\"_ActivityPrefs\"), 0));", //
" }" //
};
String[] namedActivityPref = { //
" public NamedActivityPrefs_(Context context) {", //
" super(context.getSharedPreferences((getLocalClassName(context)+\"_named_pref_activity\"), 0));", //
" }" //
};
String[] uniquePref = { //
" public UniquePrefs_(Context context) {", //
" super(context.getSharedPreferences(\"UniquePrefs\", 0));", //
" }" //
};
String[] namedUniquePref = { //
" public NamedUniquePrefs_(Context context) {", //
" super(context.getSharedPreferences(\"named_pref_unique\", 0));", //
" }" //
};
// CHECKSTYLE:ON
assertGeneratedClassContains(generatedFile, activityPref);
assertGeneratedClassContains(generatedFile, namedActivityPref);
assertGeneratedClassContains(generatedFile, uniquePref);
assertGeneratedClassContains(generatedFile, namedUniquePref);
}

@Test
public void testNamedPrefGenerationErrors() throws IOException {
CompileResult result = compileFiles(PrefsCollectionWithError.class);

assertCompilationError(result);

assertCompilationErrorOn(PrefsCollectionWithError.class, "@SharedPref(value = SharedPref.Scope.ACTIVITY_DEFAULT, name = \"named_pref_activity_default\")", result);
assertCompilationErrorOn(PrefsCollectionWithError.class, "@SharedPref(value = SharedPref.Scope.APPLICATION_DEFAULT, name = \"named_pref_application_default\")", result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<!--

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.

-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.androidannotations.testprocessor"
android:versionCode="1"
android:versionName="1.0" >

<application>
<activity android:name="org.androidannotations.sharedprefs.ActivityInManifest_" />
</application>

</manifest>