blob: a15c85952f3625120d204ef264c33f98ad136ef6 [file] [log] [blame]
Zhi Dou4655c962023-06-12 15:56:03 +00001package {package_name};
Zhi Dou22a90f42023-10-06 07:28:44 +00002// TODO(b/303773055): Remove the annotation after access issue is resolved.
3import android.compat.annotation.UnsupportedAppUsage;
Zhi Dou5aaeee32023-08-07 22:54:13 +00004{{ if not is_test_mode }}
Zhi Dou8ba6aa72023-06-26 21:03:40 +00005{{ if is_read_write- }}
Zhi Doueb744892023-05-10 04:02:33 +00006import android.provider.DeviceConfig;
Zhi Dou72c2a932023-10-31 22:57:30 +00007import android.provider.DeviceConfig.Properties;
Zhi Doueb744892023-05-10 04:02:33 +00008{{ endif }}
Mårten Kongstad65efa272023-09-11 12:17:25 +00009/** @hide */
Zhi Dou4655c962023-06-12 15:56:03 +000010public final class FeatureFlagsImpl implements FeatureFlags \{
Ted Bauerc3073782023-11-15 18:04:54 +000011{{- if is_read_write }}
12{{- for namespace_with_flags in namespace_flags }}
13 private static boolean {namespace_with_flags.namespace}_is_cached = false;
14{{- endfor- }}
15
16{{ for flag in flag_elements }}
Ted Bauer4a6af782023-11-29 15:44:24 +000017{{ if library_exported }}
18{{ if flag.exported }}
19 private static boolean {flag.method_name} = false;
20{{ endif }}
21
22{{ else }}
23
Ted Bauerc3073782023-11-15 18:04:54 +000024{{- if flag.is_read_write }}
25 private static boolean {flag.method_name} = {flag.default_value};
26{{- endif- }}
Ted Bauer4a6af782023-11-29 15:44:24 +000027{{ endif }}
Zhi Dou72c2a932023-10-31 22:57:30 +000028{{ endfor }}
Ted Bauerc3073782023-11-15 18:04:54 +000029
30{{ for namespace_with_flags in namespace_flags }}
31 private void load_overrides_{namespace_with_flags.namespace}() \{
32 try \{
33 Properties properties = DeviceConfig.getProperties("{namespace_with_flags.namespace}");
34
35 {{- for flag in namespace_with_flags.flags }}
Ted Bauer4a6af782023-11-29 15:44:24 +000036 {{ if library_exported }}
37
38 {{ if flag.exported }}
39 {flag.method_name} =
40 properties.getBoolean("{flag.device_config_flag}", false);
41 {{ endif }}
42
43 {{ else }}
44
45 {{ if flag.is_read_write }}
Ted Bauerc3073782023-11-15 18:04:54 +000046 {flag.method_name} =
47 properties.getBoolean("{flag.device_config_flag}", {flag.default_value});
Ted Bauer4a6af782023-11-29 15:44:24 +000048 {{ endif }}
49
50 {{ endif }}
Ted Bauerc3073782023-11-15 18:04:54 +000051 {{ endfor }}
52 } catch (NullPointerException e) \{
53 throw new RuntimeException(
54 "Cannot read value from namespace {namespace_with_flags.namespace} "
55 + "from DeviceConfig. It could be that the code using flag "
56 + "executed before SettingsProvider initialization. Please use "
57 + "fixed read-only flag by adding is_fixed_read_only: true in "
58 + "flag declaration.",
59 e
60 );
61 }
62 {namespace_with_flags.namespace}_is_cached = true;
63 }
64{{ endfor- }}
Zhi Dou72c2a932023-10-31 22:57:30 +000065{{ endif- }}
66
67{{ for flag in flag_elements }}
Ted Bauer4a6af782023-11-29 15:44:24 +000068{{ if library_exported }}
69
70{{ if flag.exported }}
Zhi Dou4655c962023-06-12 15:56:03 +000071 @Override
Zhi Dou22a90f42023-10-06 07:28:44 +000072 @UnsupportedAppUsage
Zhi Dou72c2a932023-10-31 22:57:30 +000073 public boolean {flag.method_name}() \{
74 {{ -if flag.is_read_write }}
Ted Bauerc3073782023-11-15 18:04:54 +000075 if (!{flag.device_config_namespace}_is_cached) \{
76 load_overrides_{flag.device_config_namespace}();
Zhi Dou72c2a932023-10-31 22:57:30 +000077 }
Ted Bauerc3073782023-11-15 18:04:54 +000078 return {flag.method_name};
Zhi Dou5aaeee32023-08-07 22:54:13 +000079 {{ else }}
Zhi Dou72c2a932023-10-31 22:57:30 +000080 return {flag.default_value};
Zhi Dou5aaeee32023-08-07 22:54:13 +000081 {{ endif- }}
Zhi Doueb744892023-05-10 04:02:33 +000082 }
Ted Bauer4a6af782023-11-29 15:44:24 +000083{{ endif }}
84
85{{ else }}
86 @Override
87 @UnsupportedAppUsage
88 public boolean {flag.method_name}() \{
89 {{ -if flag.is_read_write }}
90 if (!{flag.device_config_namespace}_is_cached) \{
91 load_overrides_{flag.device_config_namespace}();
92 }
93 return {flag.method_name};
94 {{ else }}
95 return {flag.default_value};
96 {{ endif- }}
97 }
98{{ endif }}
99
Zhi Dou06a448f2023-08-15 19:33:27 +0000100{{ endfor }}
Zhi Dou8ba6aa72023-06-26 21:03:40 +0000101}
Zhi Dou5aaeee32023-08-07 22:54:13 +0000102{{ else }}
103{#- Generate only stub if in test mode #}
Mårten Kongstad65efa272023-09-11 12:17:25 +0000104/** @hide */
Zhi Dou5aaeee32023-08-07 22:54:13 +0000105public final class FeatureFlagsImpl implements FeatureFlags \{
Zhi Dou72c2a932023-10-31 22:57:30 +0000106{{ for flag in flag_elements }}
Zhi Dou5aaeee32023-08-07 22:54:13 +0000107 @Override
Zhi Dou22a90f42023-10-06 07:28:44 +0000108 @UnsupportedAppUsage
Zhi Dou72c2a932023-10-31 22:57:30 +0000109 public boolean {flag.method_name}() \{
Zhi Dou5aaeee32023-08-07 22:54:13 +0000110 throw new UnsupportedOperationException(
111 "Method is not implemented.");
112 }
Ted Bauerc3073782023-11-15 18:04:54 +0000113{{ endfor- }}
Zhi Dou5aaeee32023-08-07 22:54:13 +0000114}
115{{ endif }}