38 | * If you'd like to provide options dynamically at run-time, take a look at
39 | * {@link Builder}.
40 | */
41 | public final class StoreBox {
42 |
43 | /**
44 | * @param context - the context under which the
45 | * {@link android.content.SharedPreferences} should be opened
46 | * @param cls - the interface class which should be instantiated
47 | * @return new instance of class {@code cls} using {@code context}
48 | */
49 | public static
23 | * This interface should not be implemented directly, but instead one of the
24 | * following classes should be extended to provide an adapter implementation
25 | * for storing {@link F}:
26 | *
29 | * A parameter default takes precedence over this annotation.
30 | */
31 | @Retention(RetentionPolicy.RUNTIME)
32 | @Target(ElementType.METHOD)
33 | public @interface DefaultValue {
34 |
35 | int value();
36 | }
37 |
--------------------------------------------------------------------------------
/storebox-lib/src/main/java/net/orange_box/storebox/annotations/method/KeyByResource.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Martin Bella
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package net.orange_box.storebox.annotations.method;
18 |
19 | import java.lang.annotation.ElementType;
20 | import java.lang.annotation.Retention;
21 | import java.lang.annotation.RetentionPolicy;
22 | import java.lang.annotation.Target;
23 |
24 | /**
25 | * Can be used when it is more convenient for keys to be declared using an
26 | * integer resource, rather than a String. For example, if the key names are
27 | * stored in an XML resource file.
28 | *
29 | * @see KeyByString
30 | */
31 | @Retention(RetentionPolicy.RUNTIME)
32 | @Target(ElementType.METHOD)
33 | public @interface KeyByResource {
34 |
35 | int value();
36 | }
37 |
--------------------------------------------------------------------------------
/storebox-lib/src/main/java/net/orange_box/storebox/annotations/method/KeyByString.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Martin Bella
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package net.orange_box.storebox.annotations.method;
18 |
19 | import java.lang.annotation.ElementType;
20 | import java.lang.annotation.Retention;
21 | import java.lang.annotation.RetentionPolicy;
22 | import java.lang.annotation.Target;
23 |
24 | /**
25 | * Annotation which should be used on set/get methods to declare the key under
26 | * which the value should be stored/retrieved.
27 | *
28 | * @see KeyByResource
29 | */
30 | @Retention(RetentionPolicy.RUNTIME)
31 | @Target(ElementType.METHOD)
32 | public @interface KeyByString {
33 |
34 | String value();
35 | }
36 |
--------------------------------------------------------------------------------
/storebox-lib/src/main/java/net/orange_box/storebox/annotations/method/RegisterChangeListenerMethod.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Martin Bella
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package net.orange_box.storebox.annotations.method;
18 |
19 | import net.orange_box.storebox.listeners.OnPreferenceValueChangedListener;
20 |
21 | import java.lang.annotation.ElementType;
22 | import java.lang.annotation.Retention;
23 | import java.lang.annotation.RetentionPolicy;
24 | import java.lang.annotation.Target;
25 |
26 | /**
27 | * Annotation which should be used for registering a
28 | * {@link OnPreferenceValueChangedListener}.
29 | * The key for the value that should be listened to can be provided through
30 | * a {@link KeyByString} or {@link KeyByResource} annotation.
31 | *
32 | * @see UnregisterChangeListenerMethod
33 | * @see OnPreferenceValueChangedListener
34 | */
35 | @Retention(RetentionPolicy.RUNTIME)
36 | @Target(ElementType.METHOD)
37 | public @interface RegisterChangeListenerMethod {}
38 |
--------------------------------------------------------------------------------
/storebox-lib/src/main/java/net/orange_box/storebox/annotations/method/RemoveMethod.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Martin Bella
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package net.orange_box.storebox.annotations.method;
18 |
19 | import java.lang.annotation.ElementType;
20 | import java.lang.annotation.Retention;
21 | import java.lang.annotation.RetentionPolicy;
22 | import java.lang.annotation.Target;
23 |
24 | /**
25 | * Annotation which should be used for a remove method. The key for the value
26 | * which should be removed can be provided in two ways:
27 | *
31 | * Type adapters for {@link java.util.Date}, {@link Enum}, and
32 | * {@link android.net.Uri} are already supported and as such there is no need
33 | * to provide a type adapter for them.
34 | */
35 | @Retention(RetentionPolicy.RUNTIME)
36 | @Target(ElementType.METHOD)
37 | public @interface TypeAdapter {
38 |
39 | Class extends StoreBoxTypeAdapter> value();
40 | }
41 |
--------------------------------------------------------------------------------
/storebox-lib/src/main/java/net/orange_box/storebox/annotations/method/UnregisterChangeListenerMethod.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Martin Bella
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package net.orange_box.storebox.annotations.method;
18 |
19 | import net.orange_box.storebox.listeners.OnPreferenceValueChangedListener;
20 |
21 | import java.lang.annotation.ElementType;
22 | import java.lang.annotation.Retention;
23 | import java.lang.annotation.RetentionPolicy;
24 | import java.lang.annotation.Target;
25 |
26 | /**
27 | * Annotation which should be used for unregistering a
28 | * {@link OnPreferenceValueChangedListener}.
29 | * The key for the value that should stop being listened to can be provided
30 | * through a {@link KeyByString} or {@link KeyByResource} annotation.
31 | *
32 | * @see RegisterChangeListenerMethod
33 | * @see OnPreferenceValueChangedListener
34 | */
35 | @Retention(RetentionPolicy.RUNTIME)
36 | @Target(ElementType.METHOD)
37 | public @interface UnregisterChangeListenerMethod {}
38 |
--------------------------------------------------------------------------------
/storebox-lib/src/main/java/net/orange_box/storebox/annotations/option/SaveOption.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Martin Bella
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package net.orange_box.storebox.annotations.option;
18 |
19 | import net.orange_box.storebox.enums.SaveMode;
20 |
21 | import java.lang.annotation.ElementType;
22 | import java.lang.annotation.Retention;
23 | import java.lang.annotation.RetentionPolicy;
24 | import java.lang.annotation.Target;
25 |
26 | /**
27 | * Annotation which should be used to define what {@link SaveMode} will be
28 | * applied for get methods which don't specify a default value.
29 | *
30 | * Annotation can be used at interface and method-level, however any
31 | * method-level annotations will take precedence over interface-level
32 | * annotations.
33 | */
34 | @Retention(RetentionPolicy.RUNTIME)
35 | @Target({ElementType.TYPE, ElementType.METHOD})
36 | public @interface SaveOption {
37 |
38 | SaveMode value();
39 | }
40 |
--------------------------------------------------------------------------------
/storebox-lib/src/main/java/net/orange_box/storebox/annotations/type/ActivityPreferences.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Martin Bella
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package net.orange_box.storebox.annotations.type;
18 |
19 | import net.orange_box.storebox.enums.PreferencesMode;
20 |
21 | import java.lang.annotation.ElementType;
22 | import java.lang.annotation.Retention;
23 | import java.lang.annotation.RetentionPolicy;
24 | import java.lang.annotation.Target;
25 |
26 | /**
27 | * Annotation which should be used at interface-level to define that the
28 | * preferences private to the Activity should be used.
29 | *
30 | * When this annotation is used an {@link android.app.Activity} context needs
31 | * to be passed in when instantiating the interface using
32 | * {@link net.orange_box.storebox.StoreBox}.
33 | *
34 | * @see net.orange_box.storebox.enums.PreferencesType#ACTIVITY
35 | * @see android.app.Activity#getPreferences(int)
36 | */
37 | @Retention(RetentionPolicy.RUNTIME)
38 | @Target(ElementType.TYPE)
39 | public @interface ActivityPreferences {
40 |
41 | PreferencesMode mode() default PreferencesMode.MODE_PRIVATE;
42 | }
43 |
--------------------------------------------------------------------------------
/storebox-lib/src/main/java/net/orange_box/storebox/annotations/type/DefaultSharedPreferences.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Martin Bella
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package net.orange_box.storebox.annotations.type;
18 |
19 | import java.lang.annotation.ElementType;
20 | import java.lang.annotation.Retention;
21 | import java.lang.annotation.RetentionPolicy;
22 | import java.lang.annotation.Target;
23 |
24 | /**
25 | * Annotation which should be used at interface-level to define that the
26 | * default shared preferences should be used.
27 | *
28 | * This is the default type used when no annotation has been specified for an
29 | * interface.
30 | *
31 | * @see net.orange_box.storebox.enums.PreferencesType#ACTIVITY
32 | * @see android.preference.PreferenceManager#getDefaultSharedPreferences(
33 | * android.content.Context)
34 | */
35 | @Retention(RetentionPolicy.RUNTIME)
36 | @Target(ElementType.TYPE)
37 | public @interface DefaultSharedPreferences {
38 |
39 | // NOP
40 | }
41 |
--------------------------------------------------------------------------------
/storebox-lib/src/main/java/net/orange_box/storebox/annotations/type/FilePreferences.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Martin Bella
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package net.orange_box.storebox.annotations.type;
18 |
19 | import net.orange_box.storebox.enums.PreferencesMode;
20 |
21 | import java.lang.annotation.ElementType;
22 | import java.lang.annotation.Retention;
23 | import java.lang.annotation.RetentionPolicy;
24 | import java.lang.annotation.Target;
25 |
26 | /**
27 | * Annotation which should be used at interface-level to define that the
28 | * preferences should be opened from a file name.
29 | *
30 | * When this annotation is used a file name needs to be specified using
31 | * {@link #value()}.
32 | *
33 | * @see net.orange_box.storebox.enums.PreferencesType#FILE
34 | * @see android.content.Context#getSharedPreferences(String, int)
35 | */
36 | @Retention(RetentionPolicy.RUNTIME)
37 | @Target(ElementType.TYPE)
38 | public @interface FilePreferences {
39 |
40 | String value();
41 |
42 | PreferencesMode mode() default PreferencesMode.MODE_PRIVATE;
43 | }
44 |
--------------------------------------------------------------------------------
/storebox-lib/src/main/java/net/orange_box/storebox/enums/PreferencesMode.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Martin Bella
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package net.orange_box.storebox.enums;
18 |
19 | import android.content.Context;
20 |
21 | public enum PreferencesMode {
22 |
23 | /**
24 | * Default.
25 | *
26 | * @see android.content.Context#MODE_PRIVATE
27 | */
28 | MODE_PRIVATE(Context.MODE_PRIVATE),
29 |
30 | /**
31 | * @see android.content.Context#MODE_MULTI_PROCESS
32 | */
33 | MODE_MULTI_PROCESS(Context.MODE_MULTI_PROCESS),
34 |
35 | /**
36 | * @see android.content.Context#MODE_WORLD_READABLE
37 | */
38 | @Deprecated
39 | MODE_WORLD_READABLE(Context.MODE_WORLD_READABLE),
40 |
41 | /**
42 | * @see android.content.Context#MODE_WORLD_WRITEABLE
43 | */
44 | @Deprecated
45 | MODE_WORLD_WRITEABLE(Context.MODE_WORLD_WRITEABLE);
46 |
47 | private final int value;
48 |
49 | private PreferencesMode(int value) {
50 | this.value = value;
51 | }
52 |
53 | public int value() {
54 | return value;
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/storebox-lib/src/main/java/net/orange_box/storebox/enums/PreferencesType.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Martin Bella
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package net.orange_box.storebox.enums;
18 |
19 | public enum PreferencesType {
20 |
21 | /**
22 | * Default.
23 | *
24 | * @see net.orange_box.storebox.annotations.type.DefaultSharedPreferences
25 | * @see android.preference.PreferenceManager#getDefaultSharedPreferences(
26 | * android.content.Context)
27 | */
28 | DEFAULT_SHARED,
29 |
30 | /**
31 | * @see net.orange_box.storebox.annotations.type.ActivityPreferences
32 | * @see android.app.Activity#getPreferences(int)
33 | */
34 | ACTIVITY,
35 |
36 | /**
37 | * @see net.orange_box.storebox.annotations.type.FilePreferences
38 | * @see android.content.Context#getSharedPreferences(String, int)
39 | */
40 | FILE
41 | }
42 |
--------------------------------------------------------------------------------
/storebox-lib/src/main/java/net/orange_box/storebox/enums/SaveMode.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Martin Bella
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package net.orange_box.storebox.enums;
18 |
19 | public enum SaveMode {
20 |
21 | /**
22 | * Default.
23 | *
24 | * @see android.content.SharedPreferences.Editor#apply()
25 | */
26 | APPLY,
27 |
28 | /**
29 | * @see android.content.SharedPreferences.Editor#commit()
30 | */
31 | COMMIT,
32 |
33 | /**
34 | * {@link android.content.SharedPreferences.Editor#apply()} or
35 | * {@link android.content.SharedPreferences.Editor#commit()} will have
36 | * to be called explicitly.
37 | */
38 | NOME
39 | }
40 |
--------------------------------------------------------------------------------
/storebox-lib/src/main/java/net/orange_box/storebox/handlers/ChangeListenerMethodHandler.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Martin Bella
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package net.orange_box.storebox.handlers;
18 |
19 | import android.content.SharedPreferences;
20 | import android.support.annotation.Nullable;
21 |
22 | import net.jodah.typetools.TypeResolver;
23 | import net.orange_box.storebox.adapters.StoreBoxTypeAdapter;
24 | import net.orange_box.storebox.annotations.method.RegisterChangeListenerMethod;
25 | import net.orange_box.storebox.annotations.method.UnregisterChangeListenerMethod;
26 | import net.orange_box.storebox.annotations.method.TypeAdapter;
27 | import net.orange_box.storebox.listeners.OnPreferenceValueChangedListener;
28 | import net.orange_box.storebox.utils.PreferenceUtils;
29 | import net.orange_box.storebox.utils.TypeUtils;
30 |
31 | import java.lang.ref.WeakReference;
32 | import java.lang.reflect.Method;
33 | import java.util.Arrays;
34 | import java.util.HashSet;
35 | import java.util.Iterator;
36 | import java.util.Locale;
37 | import java.util.Map;
38 | import java.util.Set;
39 | import java.util.concurrent.ConcurrentHashMap;
40 |
41 | /**
42 | * TODO should use a ReferenceQueue for cleaning up
43 | */
44 | public class ChangeListenerMethodHandler implements
45 | MethodHandler,
46 | SharedPreferences.OnSharedPreferenceChangeListener {
47 |
48 | private final SharedPreferences prefs;
49 | private final Map
27 | *
40 | *
41 | * @param
28 | *
38 | *
39 | * @see android.content.SharedPreferences.Editor#remove(String)
40 | */
41 | @Retention(RetentionPolicy.RUNTIME)
42 | @Target(ElementType.METHOD)
43 | public @interface RemoveMethod {}
44 |
--------------------------------------------------------------------------------
/storebox-lib/src/main/java/net/orange_box/storebox/annotations/method/TypeAdapter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Martin Bella
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package net.orange_box.storebox.annotations.method;
18 |
19 | import net.orange_box.storebox.adapters.StoreBoxTypeAdapter;
20 |
21 | import java.lang.annotation.ElementType;
22 | import java.lang.annotation.Retention;
23 | import java.lang.annotation.RetentionPolicy;
24 | import java.lang.annotation.Target;
25 |
26 | /**
27 | * Annotation which should be used on set and get methods to declare the
28 | * {@link StoreBoxTypeAdapter} to be used for adapting the type for the
29 | * preferences.
30 | *