aboutsummaryrefslogtreecommitdiff
path: root/tools/aconfig/aconfig/templates/FeatureFlagsImpl.java.template
blob: 12b2fc1dddf4dccebd8d0d91aba29151151b6f7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package {package_name};
{{ if not library_exported- }}
// TODO(b/303773055): Remove the annotation after access issue is resolved.
import android.compat.annotation.UnsupportedAppUsage;
{{ -endif }}
{{ -if not is_test_mode }}
{{ -if runtime_lookup_required }}
import android.provider.DeviceConfig;
import android.provider.DeviceConfig.Properties;
{{ endif }}
/** @hide */
public final class FeatureFlagsImpl implements FeatureFlags \{
{{ -if runtime_lookup_required }}
{{ -for namespace_with_flags in namespace_flags }}
    private static boolean {namespace_with_flags.namespace}_is_cached = false;
{{ -endfor- }}

{{ for flag in flag_elements }}
{{- if flag.is_read_write }}
    private static boolean {flag.method_name} = {flag.default_value};
{{ -endif }}
{{ -endfor }}
{{ for namespace_with_flags in namespace_flags }}
    private void load_overrides_{namespace_with_flags.namespace}() \{
        try \{
            Properties properties = DeviceConfig.getProperties("{namespace_with_flags.namespace}");
{{ -for flag in namespace_with_flags.flags }}
{{ -if flag.is_read_write }}
            {flag.method_name} =
                properties.getBoolean("{flag.device_config_flag}", {flag.default_value});
{{ -endif }}
{{ -endfor }}
        } catch (NullPointerException e) \{
            throw new RuntimeException(
                "Cannot read value from namespace {namespace_with_flags.namespace} "
                + "from DeviceConfig. It could be that the code using flag "
                + "executed before SettingsProvider initialization. Please use "
                + "fixed read-only flag by adding is_fixed_read_only: true in "
                + "flag declaration.",
                e
            );
        }
        {namespace_with_flags.namespace}_is_cached = true;
    }
{{ endfor- }}
{{ -endif }}{#- end of runtime_lookup_required #}
{{ -for flag in flag_elements }}
    @Override
{{ -if not library_exported }}
    @UnsupportedAppUsage
{{ -endif }}
    public boolean {flag.method_name}() \{
{{ -if flag.is_read_write }}
        if (!{flag.device_config_namespace}_is_cached) \{
            load_overrides_{flag.device_config_namespace}();
        }
        return {flag.method_name};
{{ -else }}
        return {flag.default_value};
{{ -endif }}
    }
{{ endfor }}
}
{{ else }}
{#- Generate only stub if in test mode #}
/** @hide */
public final class FeatureFlagsImpl implements FeatureFlags \{
{{ for flag in flag_elements }}
    @Override
{{ -if not library_exported }}
    @UnsupportedAppUsage
{{ -endif }}
    public boolean {flag.method_name}() \{
        throw new UnsupportedOperationException(
            "Method is not implemented.");
    }
{{ endfor- }}
}
{{ endif }}