1 /*
2  * Copyright 2018 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.internal.telephony;
18 
19 import android.content.ContentResolver;
20 import android.content.Context;
21 import android.os.SystemProperties;
22 import android.provider.Settings;
23 import android.telephony.TelephonyManager;
24 import android.util.TimestampedValue;
25 
26 import com.android.internal.util.IndentingPrintWriter;
27 
28 import java.io.FileDescriptor;
29 import java.io.PrintWriter;
30 
31 /**
32  * {@hide}
33  */
34 public interface NitzStateMachine {
35 
36     /**
37      * Called when the network country is set on the Phone. Although set, the network country code
38      * may be invalid.
39      *
40      * @param countryChanged true when the country code is known to have changed, false if it
41      *     probably hasn't
42      */
handleNetworkCountryCodeSet(boolean countryChanged)43     void handleNetworkCountryCodeSet(boolean countryChanged);
44 
45     /**
46      * Informs the {@link NitzStateMachine} that the network has become available.
47      */
handleNetworkAvailable()48     void handleNetworkAvailable();
49 
50     /**
51      * Informs the {@link NitzStateMachine} that the country code from network has become
52      * unavailable.
53      */
handleNetworkCountryCodeUnavailable()54     void handleNetworkCountryCodeUnavailable();
55 
56     /**
57      * Handle a new NITZ signal being received.
58      */
handleNitzReceived(TimestampedValue<NitzData> nitzSignal)59     void handleNitzReceived(TimestampedValue<NitzData> nitzSignal);
60 
61     /**
62      * Dumps the current in-memory state to the supplied PrintWriter.
63      */
dumpState(PrintWriter pw)64     void dumpState(PrintWriter pw);
65 
66     /**
67      * Dumps the time / time zone logs to the supplied IndentingPrintWriter.
68      */
dumpLogs(FileDescriptor fd, IndentingPrintWriter ipw, String[] args)69     void dumpLogs(FileDescriptor fd, IndentingPrintWriter ipw, String[] args);
70 
71     /**
72      * Returns the last NITZ data that was cached.
73      */
getCachedNitzData()74     NitzData getCachedNitzData();
75 
76     /**
77      * Returns the time zone ID from the most recent time that a time zone could be determined by
78      * this state machine.
79      */
getSavedTimeZoneId()80     String getSavedTimeZoneId();
81 
82     /**
83      * A proxy over device state that allows things like system properties, system clock
84      * to be faked for tests.
85      */
86     // Non-final to allow mocking.
87     class DeviceState {
88         private static final int NITZ_UPDATE_SPACING_DEFAULT = 1000 * 60 * 10;
89         private final int mNitzUpdateSpacing;
90 
91         private static final int NITZ_UPDATE_DIFF_DEFAULT = 2000;
92         private final int mNitzUpdateDiff;
93 
94         private final GsmCdmaPhone mPhone;
95         private final TelephonyManager mTelephonyManager;
96         private final ContentResolver mCr;
97 
DeviceState(GsmCdmaPhone phone)98         public DeviceState(GsmCdmaPhone phone) {
99             mPhone = phone;
100 
101             Context context = phone.getContext();
102             mTelephonyManager =
103                     (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
104             mCr = context.getContentResolver();
105             mNitzUpdateSpacing =
106                     SystemProperties.getInt("ro.nitz_update_spacing", NITZ_UPDATE_SPACING_DEFAULT);
107             mNitzUpdateDiff =
108                     SystemProperties.getInt("ro.nitz_update_diff", NITZ_UPDATE_DIFF_DEFAULT);
109         }
110 
111         /**
112          * If time between NITZ updates is less than {@link #getNitzUpdateSpacingMillis()} the
113          * update may be ignored.
114          */
getNitzUpdateSpacingMillis()115         public int getNitzUpdateSpacingMillis() {
116             return Settings.Global.getInt(mCr, Settings.Global.NITZ_UPDATE_SPACING,
117                     mNitzUpdateSpacing);
118         }
119 
120         /**
121          * If {@link #getNitzUpdateSpacingMillis()} hasn't been exceeded but update is >
122          * {@link #getNitzUpdateDiffMillis()} do the update
123          */
getNitzUpdateDiffMillis()124         public int getNitzUpdateDiffMillis() {
125             return Settings.Global.getInt(mCr, Settings.Global.NITZ_UPDATE_DIFF, mNitzUpdateDiff);
126         }
127 
128         /**
129          * Returns true if the {@code gsm.ignore-nitz} system property is set to "yes".
130          */
getIgnoreNitz()131         public boolean getIgnoreNitz() {
132             String ignoreNitz = SystemProperties.get("gsm.ignore-nitz");
133             return ignoreNitz != null && ignoreNitz.equals("yes");
134         }
135 
getNetworkCountryIsoForPhone()136         public String getNetworkCountryIsoForPhone() {
137             return mTelephonyManager.getNetworkCountryIsoForPhone(mPhone.getPhoneId());
138         }
139     }
140 }
141