| Zhi Dou | 4655c96 | 2023-06-12 15:56:03 +0000 | [diff] [blame] | 1 | package {package_name}; |
| Zhi Dou | 22a90f4 | 2023-10-06 07:28:44 +0000 | [diff] [blame] | 2 | // TODO(b/303773055): Remove the annotation after access issue is resolved. |
| 3 | import android.compat.annotation.UnsupportedAppUsage; |
| Zhi Dou | 5aaeee3 | 2023-08-07 22:54:13 +0000 | [diff] [blame] | 4 | {{ if not is_test_mode }} |
| Zhi Dou | 8ba6aa7 | 2023-06-26 21:03:40 +0000 | [diff] [blame] | 5 | {{ if is_read_write- }} |
| Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 6 | import android.provider.DeviceConfig; |
| Zhi Dou | 72c2a93 | 2023-10-31 22:57:30 +0000 | [diff] [blame] | 7 | import android.provider.DeviceConfig.Properties; |
| Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 8 | {{ endif }} |
| Mårten Kongstad | 65efa27 | 2023-09-11 12:17:25 +0000 | [diff] [blame] | 9 | /** @hide */ |
| Zhi Dou | 4655c96 | 2023-06-12 15:56:03 +0000 | [diff] [blame] | 10 | public final class FeatureFlagsImpl implements FeatureFlags \{ |
| Ted Bauer | c307378 | 2023-11-15 18:04:54 +0000 | [diff] [blame] | 11 | {{- 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 Bauer | 4a6af78 | 2023-11-29 15:44:24 +0000 | [diff] [blame^] | 17 | {{ if library_exported }} |
| 18 | {{ if flag.exported }} |
| 19 | private static boolean {flag.method_name} = false; |
| 20 | {{ endif }} |
| 21 | |
| 22 | {{ else }} |
| 23 | |
| Ted Bauer | c307378 | 2023-11-15 18:04:54 +0000 | [diff] [blame] | 24 | {{- if flag.is_read_write }} |
| 25 | private static boolean {flag.method_name} = {flag.default_value}; |
| 26 | {{- endif- }} |
| Ted Bauer | 4a6af78 | 2023-11-29 15:44:24 +0000 | [diff] [blame^] | 27 | {{ endif }} |
| Zhi Dou | 72c2a93 | 2023-10-31 22:57:30 +0000 | [diff] [blame] | 28 | {{ endfor }} |
| Ted Bauer | c307378 | 2023-11-15 18:04:54 +0000 | [diff] [blame] | 29 | |
| 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 Bauer | 4a6af78 | 2023-11-29 15:44:24 +0000 | [diff] [blame^] | 36 | {{ 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 Bauer | c307378 | 2023-11-15 18:04:54 +0000 | [diff] [blame] | 46 | {flag.method_name} = |
| 47 | properties.getBoolean("{flag.device_config_flag}", {flag.default_value}); |
| Ted Bauer | 4a6af78 | 2023-11-29 15:44:24 +0000 | [diff] [blame^] | 48 | {{ endif }} |
| 49 | |
| 50 | {{ endif }} |
| Ted Bauer | c307378 | 2023-11-15 18:04:54 +0000 | [diff] [blame] | 51 | {{ 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 Dou | 72c2a93 | 2023-10-31 22:57:30 +0000 | [diff] [blame] | 65 | {{ endif- }} |
| 66 | |
| 67 | {{ for flag in flag_elements }} |
| Ted Bauer | 4a6af78 | 2023-11-29 15:44:24 +0000 | [diff] [blame^] | 68 | {{ if library_exported }} |
| 69 | |
| 70 | {{ if flag.exported }} |
| Zhi Dou | 4655c96 | 2023-06-12 15:56:03 +0000 | [diff] [blame] | 71 | @Override |
| Zhi Dou | 22a90f4 | 2023-10-06 07:28:44 +0000 | [diff] [blame] | 72 | @UnsupportedAppUsage |
| Zhi Dou | 72c2a93 | 2023-10-31 22:57:30 +0000 | [diff] [blame] | 73 | public boolean {flag.method_name}() \{ |
| 74 | {{ -if flag.is_read_write }} |
| Ted Bauer | c307378 | 2023-11-15 18:04:54 +0000 | [diff] [blame] | 75 | if (!{flag.device_config_namespace}_is_cached) \{ |
| 76 | load_overrides_{flag.device_config_namespace}(); |
| Zhi Dou | 72c2a93 | 2023-10-31 22:57:30 +0000 | [diff] [blame] | 77 | } |
| Ted Bauer | c307378 | 2023-11-15 18:04:54 +0000 | [diff] [blame] | 78 | return {flag.method_name}; |
| Zhi Dou | 5aaeee3 | 2023-08-07 22:54:13 +0000 | [diff] [blame] | 79 | {{ else }} |
| Zhi Dou | 72c2a93 | 2023-10-31 22:57:30 +0000 | [diff] [blame] | 80 | return {flag.default_value}; |
| Zhi Dou | 5aaeee3 | 2023-08-07 22:54:13 +0000 | [diff] [blame] | 81 | {{ endif- }} |
| Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 82 | } |
| Ted Bauer | 4a6af78 | 2023-11-29 15:44:24 +0000 | [diff] [blame^] | 83 | {{ 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 Dou | 06a448f | 2023-08-15 19:33:27 +0000 | [diff] [blame] | 100 | {{ endfor }} |
| Zhi Dou | 8ba6aa7 | 2023-06-26 21:03:40 +0000 | [diff] [blame] | 101 | } |
| Zhi Dou | 5aaeee3 | 2023-08-07 22:54:13 +0000 | [diff] [blame] | 102 | {{ else }} |
| 103 | {#- Generate only stub if in test mode #} |
| Mårten Kongstad | 65efa27 | 2023-09-11 12:17:25 +0000 | [diff] [blame] | 104 | /** @hide */ |
| Zhi Dou | 5aaeee3 | 2023-08-07 22:54:13 +0000 | [diff] [blame] | 105 | public final class FeatureFlagsImpl implements FeatureFlags \{ |
| Zhi Dou | 72c2a93 | 2023-10-31 22:57:30 +0000 | [diff] [blame] | 106 | {{ for flag in flag_elements }} |
| Zhi Dou | 5aaeee3 | 2023-08-07 22:54:13 +0000 | [diff] [blame] | 107 | @Override |
| Zhi Dou | 22a90f4 | 2023-10-06 07:28:44 +0000 | [diff] [blame] | 108 | @UnsupportedAppUsage |
| Zhi Dou | 72c2a93 | 2023-10-31 22:57:30 +0000 | [diff] [blame] | 109 | public boolean {flag.method_name}() \{ |
| Zhi Dou | 5aaeee3 | 2023-08-07 22:54:13 +0000 | [diff] [blame] | 110 | throw new UnsupportedOperationException( |
| 111 | "Method is not implemented."); |
| 112 | } |
| Ted Bauer | c307378 | 2023-11-15 18:04:54 +0000 | [diff] [blame] | 113 | {{ endfor- }} |
| Zhi Dou | 5aaeee3 | 2023-08-07 22:54:13 +0000 | [diff] [blame] | 114 | } |
| 115 | {{ endif }} |