• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.dialer.assisteddialing;
18 
19 import android.annotation.TargetApi;
20 import android.os.Build.VERSION_CODES;
21 import android.support.annotation.NonNull;
22 import android.support.annotation.Nullable;
23 import android.telephony.TelephonyManager;
24 import android.text.TextUtils;
25 import com.android.dialer.common.LogUtil;
26 import java.util.Locale;
27 import java.util.Optional;
28 
29 // TODO(erfanian): Improve definition of roaming and home country in finalized API.
30 /**
31  * LocationDetector is responsible for determining the Roaming location of the User, in addition to
32  * User's home country.
33  */
34 final class LocationDetector {
35 
36   private final TelephonyManager telephonyManager;
37   private final String userProvidedHomeCountry;
38 
LocationDetector( @onNull TelephonyManager telephonyManager, @Nullable String userProvidedHomeCountry)39   public LocationDetector(
40       @NonNull TelephonyManager telephonyManager, @Nullable String userProvidedHomeCountry) {
41     if (telephonyManager == null) {
42       throw new NullPointerException("Provided TelephonyManager was null");
43     }
44 
45     this.telephonyManager = telephonyManager;
46     this.userProvidedHomeCountry = userProvidedHomeCountry;
47   }
48 
49   // TODO(erfanian):  confirm this is based on ISO 3166-1 alpha-2. libphonenumber expects Unicode's
50   // CLDR
51   // TODO(erfanian):  confirm these are still valid in a multi-sim environment.
52   /**
53    * Returns what we believe to be the User's home country. This should resolve to
54    * PROPERTY_ICC_OPERATOR_ISO_COUNTRY
55    */
56   @SuppressWarnings("AndroidApiChecker") // Use of optional
57   @TargetApi(VERSION_CODES.N)
getUpperCaseUserHomeCountry()58   public Optional<String> getUpperCaseUserHomeCountry() {
59 
60     if (!TextUtils.isEmpty(userProvidedHomeCountry)) {
61       LogUtil.i(
62           "LocationDetector.getUpperCaseUserRoamingCountry", "user provided home country code");
63       return Optional.of(userProvidedHomeCountry.toUpperCase(Locale.US));
64     }
65 
66     String simCountryIso = telephonyManager.getSimCountryIso();
67     if (simCountryIso != null) {
68       LogUtil.i("LocationDetector.getUpperCaseUserRoamingCountry", "using sim country iso");
69       return Optional.of(telephonyManager.getSimCountryIso().toUpperCase(Locale.US));
70     }
71     LogUtil.i("LocationDetector.getUpperCaseUserHomeCountry", "user home country was null");
72     return Optional.empty();
73   }
74 
75   /** Returns what we believe to be the User's current (roaming) country */
76   @SuppressWarnings("AndroidApiChecker") // Use of optional
77   @TargetApi(VERSION_CODES.N)
getUpperCaseUserRoamingCountry()78   public Optional<String> getUpperCaseUserRoamingCountry() {
79     // TODO Increase coverage of location resolution??
80     String networkCountryIso = telephonyManager.getNetworkCountryIso();
81     if (networkCountryIso != null) {
82       return Optional.of(telephonyManager.getNetworkCountryIso().toUpperCase(Locale.US));
83     }
84     LogUtil.i("LocationDetector.getUpperCaseUserRoamingCountry", "user roaming country was null");
85     return Optional.empty();
86   }
87 }
88