• 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      */
43     // TODO(b/311183933): Remove passed in Context from static method.
44     @SuppressWarnings("AvoidStaticContext")
isEuDevice(@onNull Context context)45     public static boolean isEuDevice(@NonNull Context context) {
46         Objects.requireNonNull(context);
47 
48         Flags flags = FlagsFactory.getFlags();
49         if (flags.getEnableTabletRegionFix() && flags.isEeaDeviceFeatureEnabled()) {
50             LogUtil.d("Server side region detection for tablet and phone enabled.");
51             return FlagsFactory.getFlags().isEeaDevice();
52         }
53         // We don't need following logic once the enableTabletRegionFix is fully ramp up.
54         if (context.getPackageManager().hasSystemFeature(FEATURE_TELEPHONY)) {
55             TelephonyManager telephonyManager = context.getSystemService(TelephonyManager.class);
56             // if there is no telephony manager accessible, we fall back to EU device
57             if (telephonyManager == null) return true;
58 
59             // we use PH to determine whether device is in the EEA region.
60             if (FlagsFactory.getFlags().isEeaDeviceFeatureEnabled()) {
61                 LogUtil.d("Server side region detection enabled.");
62                 return FlagsFactory.getFlags().isEeaDevice();
63             }
64 
65             // and fall back to sim card locations if the feature is not yet enabled.
66             // if there is no sim card installed, we fall back to EU device
67             String deviceCountryIso = telephonyManager.getSimCountryIso();
68             if (deviceCountryIso.isEmpty()) {
69                 return true;
70             }
71 
72             // if simCountryIso detects the user's country as one of EEA countries
73             // we treat this device as EU device, otherwise ROW device
74             if (getUiEeaCountriesSet().contains(deviceCountryIso.toUpperCase(Locale.ENGLISH))) {
75                 return true;
76             }
77             return false;
78         }
79         // if there is no telephony feature, we fall back to EU device
80         return true;
81     }
82 
getUiEeaCountriesSet()83     private static Set<String> getUiEeaCountriesSet() {
84         String uiEeaCountries = FlagsFactory.getFlags().getUiEeaCountries();
85         if (!isValidEeaCountriesString(uiEeaCountries)) {
86             LogUtil.e("Invalid EEA countries string.");
87             return Set.of();
88         } else {
89             return Set.of(uiEeaCountries.split(","));
90         }
91     }
92 
93     /** Checks whether an EEA countries string is valid. */
isValidEeaCountriesString(String str)94     public static boolean isValidEeaCountriesString(String str) {
95         if (str == null || TextUtils.isEmpty(str)) {
96             return false;
97         }
98         return (str + ",")
99                 .matches(String.format(Locale.US, "^([A-Z]{2},){%d}$", (str.length() + 1) / 3));
100     }
101 }
102