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