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