• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
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 com.android.adservices.service.consent;
18 
19 import android.annotation.NonNull;
20 import android.content.Context;
21 import android.telephony.TelephonyManager;
22 import android.text.TextUtils;
23 
24 import com.android.adservices.LogUtil;
25 import com.android.adservices.service.Flags;
26 import com.android.adservices.service.FlagsFactory;
27 
28 import java.util.Locale;
29 import java.util.Objects;
30 import java.util.Set;
31 
32 /**
33  * Provides information about the region of the device which is used. Currently it's used only to
34  * determine whether the device should be treated as EU or non-EU.
35  */
36 public class DeviceRegionProvider {
37     private static final String FEATURE_TELEPHONY = "android.hardware.telephony";
38 
39     /**
40      * @return true if the device should be treated as EU device, otherwise false.
41      * @param context {@link Context} of the caller.
42      */
isEuDevice(@onNull Context context)43     public static boolean isEuDevice(@NonNull Context context) {
44         Objects.requireNonNull(context);
45         if (context.getPackageManager().hasSystemFeature(FEATURE_TELEPHONY)) {
46             TelephonyManager telephonyManager = context.getSystemService(TelephonyManager.class);
47             // if there is no telephony manager accessible, we fall back to EU device
48             if (telephonyManager == null) return true;
49 
50             // we use PH to determine whether device is in the EEA region.
51             if (FlagsFactory.getFlags().isEeaDeviceFeatureEnabled()) {
52                 return FlagsFactory.getFlags().isEeaDevice();
53             }
54 
55             // and fall back to sim card locations if the feature is not yet enabled.
56             // if there is no sim card installed, we fall back to EU device
57             String deviceCountryIso = telephonyManager.getSimCountryIso();
58             if (deviceCountryIso.isEmpty()) {
59                 return true;
60             }
61 
62             // if simCountryIso detects the user's country as one of EEA countries
63             // we treat this device as EU device, otherwise ROW device
64             if (getUiEeaCountriesSet().contains(deviceCountryIso.toUpperCase(Locale.ENGLISH))) {
65                 return true;
66             }
67 
68             return false;
69         }
70         // if there is no telephony feature, we fall back to EU device
71         return true;
72     }
73 
74     /**
75      * @return true if the device should be treated as EU device, otherwise false.
76      * @param context {@link Context} of the caller.
77      */
isEuDevice(@onNull Context context, @NonNull Flags flags)78     public static boolean isEuDevice(@NonNull Context context, @NonNull Flags flags) {
79         Objects.requireNonNull(context);
80         if (context.getPackageManager().hasSystemFeature(FEATURE_TELEPHONY)) {
81             TelephonyManager telephonyManager = context.getSystemService(TelephonyManager.class);
82             // if there is no telephony manager accessible, we fall back to EU device
83             if (telephonyManager == null) return true;
84 
85             // we use PH to determine whether device is in the EEA region.
86             if (flags.isEeaDeviceFeatureEnabled()) {
87                 return flags.isEeaDevice();
88             }
89 
90             // and fall back to sim card locations if the feature is not yet enabled.
91             // if there is no sim card installed, we fall back to EU device
92             String deviceCountryIso = telephonyManager.getSimCountryIso();
93             if (deviceCountryIso.isEmpty()) {
94                 return true;
95             }
96 
97             // if simCountryIso detects the user's country as one of EEA countries
98             // we treat this device as EU device, otherwise ROW device
99             if (getUiEeaCountriesSet(flags)
100                     .contains(deviceCountryIso.toUpperCase(Locale.ENGLISH))) {
101                 return true;
102             }
103 
104             return false;
105         }
106         // if there is no telephony feature, we fall back to EU device
107         return true;
108     }
109 
getUiEeaCountriesSet(Flags flags)110     private static Set<String> getUiEeaCountriesSet(Flags flags) {
111         String uiEeaCountries = flags.getUiEeaCountries();
112         if (!isValidEeaCountriesString(uiEeaCountries)) {
113             LogUtil.e("Invalid EEA countries string.");
114             return Set.of();
115         } else {
116             return Set.of(uiEeaCountries.split(","));
117         }
118     }
119 
getUiEeaCountriesSet()120     private static Set<String> getUiEeaCountriesSet() {
121         String uiEeaCountries = FlagsFactory.getFlags().getUiEeaCountries();
122         if (!isValidEeaCountriesString(uiEeaCountries)) {
123             LogUtil.e("Invalid EEA countries string.");
124             return Set.of();
125         } else {
126             return Set.of(uiEeaCountries.split(","));
127         }
128     }
129 
130     /** Checks whether an EEA countries string is valid. */
isValidEeaCountriesString(String str)131     public static boolean isValidEeaCountriesString(String str) {
132         if (str == null || TextUtils.isEmpty(str)) {
133             return false;
134         }
135         return (str + ",")
136                 .matches(String.format(Locale.US, "^([A-Z]{2},){%d}$", (str.length() + 1) / 3));
137     }
138 }
139