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.settings;
18 
19 import android.content.Context;
20 import android.os.Bundle;
21 import android.os.Looper;
22 import android.telephony.SubscriptionManager;
23 
24 import androidx.annotation.VisibleForTesting;
25 
26 import com.android.settings.network.ResetNetworkOperationBuilder;
27 
28 /**
29  * A request which contains options required for resetting network.
30  */
31 public class ResetNetworkRequest {
32 
33     /* Reset option - nothing get reset */
34     public static final int RESET_NONE = 0x00;
35 
36     /* Reset option - reset ConnectivityManager */
37     public static final int RESET_CONNECTIVITY_MANAGER = 0x01;
38 
39     /* Reset option - reset VpnManager */
40     public static final int RESET_VPN_MANAGER = 0x02;
41 
42     /* Reset option - reset WiFiManager */
43     public static final int RESET_WIFI_MANAGER = 0x04;
44 
45     /* Reset option - reset WifiP2pManager */
46     public static final int RESET_WIFI_P2P_MANAGER = 0x08;
47 
48     /* Reset option - reset BluetoothManager */
49     public static final int RESET_BLUETOOTH_MANAGER = 0x10;
50 
51     /* Reset option - reset IMS stack */
52     public static final int RESET_IMS_STACK = 0x20;
53 
54     /* Reset option - reset phone process */
55     public static final int RESET_PHONE_PROCESS = 0x40;
56 
57     /* Reset option - reset RILD */
58     public static final int RESET_RILD = 0x80;
59 
60     /**
61      *  Subscription ID indicates NOT resetting any of the components below:
62      *  - TelephonyAndNetworkPolicy
63      *  - APN
64      *  - IMS
65      */
66     public static final int INVALID_SUBSCRIPTION_ID = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
67 
68     /**
69      *  Subscription ID indicates resetting components below for ALL subscriptions:
70      *  - TelephonyAndNetworkPolicy
71      *  - APN
72      *  - IMS
73      */
74     public static final int ALL_SUBSCRIPTION_ID = SubscriptionManager.DEFAULT_SUBSCRIPTION_ID;
75 
76     /* Key within Bundle. To store some connectivity options for reset */
77     @VisibleForTesting
78     protected static final String KEY_RESET_OPTIONS = "resetNetworkOptions";
79 
80     /* Key within Bundle. To store package name for resetting eSIM */
81     @VisibleForTesting
82     protected static final String KEY_ESIM_PACKAGE = "resetEsimPackage";
83 
84     /**
85      * Key within Bundle. To store subscription ID for resetting
86      * telephony manager and network and network policy manager.
87      */
88     @VisibleForTesting
89     protected static final String KEY_TELEPHONY_NET_POLICY_MANAGER_SUBID =
90             "resetTelephonyNetPolicySubId";
91 
92     /* Key within Bundle. To store subscription ID for resetting APN. */
93     @VisibleForTesting
94     protected static final String KEY_APN_SUBID = "resetApnSubId";
95 
96     /** Key within Bundle. To store subscription ID for resetting IMS. */
97     protected  static final String KEY_RESET_IMS_SUBID = "resetImsSubId";
98 
99     private int mResetOptions = RESET_NONE;
100     private String mResetEsimPackageName;
101     private int mResetTelephonyManager = INVALID_SUBSCRIPTION_ID;
102     private int mResetApn = INVALID_SUBSCRIPTION_ID;
103     private int mSubscriptionIdToResetIms = INVALID_SUBSCRIPTION_ID;
104 
105     /**
106      * Reconstruct based on keys stored within Bundle.
107      * @param optionsFromBundle is a Bundle which previously stored through #writeIntoBundle()
108      */
ResetNetworkRequest(Bundle optionsFromBundle)109     public ResetNetworkRequest(Bundle optionsFromBundle) {
110         if (optionsFromBundle == null) {
111             return;
112         }
113         mResetOptions = optionsFromBundle.getInt(KEY_RESET_OPTIONS, RESET_NONE);
114         mResetEsimPackageName = optionsFromBundle.getString(KEY_ESIM_PACKAGE);
115         mResetTelephonyManager = optionsFromBundle.getInt(
116                 KEY_TELEPHONY_NET_POLICY_MANAGER_SUBID, INVALID_SUBSCRIPTION_ID);
117         mResetApn = optionsFromBundle.getInt(KEY_APN_SUBID, INVALID_SUBSCRIPTION_ID);
118         mSubscriptionIdToResetIms = optionsFromBundle.getInt(KEY_RESET_IMS_SUBID,
119                 INVALID_SUBSCRIPTION_ID);
120     }
121 
122     /**
123      * Construct of class
124      * @param resetOptions is a binary combination(OR logic operation) of constants
125      *         comes with RESET_ prefix. Which are the reset options comes within.
126      */
ResetNetworkRequest(int resetOptions)127     public ResetNetworkRequest(int resetOptions) {
128         mResetOptions = resetOptions;
129     }
130 
131     /**
132      * Get the package name applied for resetting eSIM.
133      * @return package name. {@code null} means resetting eSIM is not part of the
134      *         option within this request.
135      */
getResetEsimPackageName()136     public String getResetEsimPackageName() {
137         return mResetEsimPackageName;
138     }
139 
140     /**
141      * Set the package name for resetting eSIM.
142      * @param packageName is the package name for resetting eSIM.
143      *        {@code null} will remove the resetting eSIM option out of this request.
144      * @return this request
145      */
setResetEsim(String packageName)146     public ResetNetworkRequest setResetEsim(String packageName) {
147         mResetEsimPackageName = packageName;
148         return this;
149     }
150 
151     /**
152      * Get the subscription ID applied for resetting Telephony and NetworkPolicy.
153      * @return subscription ID.
154      *         {@code ALL_SUBSCRIPTION_ID} for applying to all subscriptions.
155      *         {@code INVALID_SUBSCRIPTION_ID} means
156      *         resetting Telephony and NetworkPolicy is not part of the option
157      *         within this request.
158      */
getResetTelephonyAndNetworkPolicyManager()159     public int getResetTelephonyAndNetworkPolicyManager() {
160         return mResetTelephonyManager;
161     }
162 
163     /**
164      * Set the subscription ID applied for resetting Telephony and NetworkPolicy.
165      * @param subscriptionId is the subscription ID referenced fron SubscriptionManager.
166      *         {@code ALL_SUBSCRIPTION_ID} for applying to all subscriptions.
167      *         {@code INVALID_SUBSCRIPTION_ID} means resetting Telephony and NetworkPolicy
168      *         will not take place.
169      * @return this request
170      */
setResetTelephonyAndNetworkPolicyManager(int subscriptionId)171     public ResetNetworkRequest setResetTelephonyAndNetworkPolicyManager(int subscriptionId) {
172         mResetTelephonyManager = subscriptionId;
173         return this;
174     }
175 
176     /**
177      * Get the subscription ID applied for resetting APN.
178      * @return subscription ID.
179      *         {@code ALL_SUBSCRIPTION_ID} for applying to all subscriptions.
180      *         {@code INVALID_SUBSCRIPTION_ID} means resetting APN
181      *         is not part of the option within this request.
182      */
getResetApnSubId()183     public int getResetApnSubId() {
184         return mResetApn;
185     }
186 
187     /**
188      * Set the subscription ID applied for resetting APN.
189      * @param subscriptionId is the subscription ID referenced fron SubscriptionManager.
190      *         {@code ALL_SUBSCRIPTION_ID} for applying to all subscriptions.
191      *         {@code INVALID_SUBSCRIPTION_ID} means resetting APN will not take place.
192      * @return this request
193      */
setResetApn(int subscriptionId)194     public ResetNetworkRequest setResetApn(int subscriptionId) {
195         mResetApn = subscriptionId;
196         return this;
197     }
198 
199     /**
200      * Get the subscription ID applied for resetting IMS.
201      * @return subscription ID.
202      *         {@code ALL_SUBSCRIPTION_ID} for applying to all subscriptions.
203      *         {@code INVALID_SUBSCRIPTION_ID} means resetting IMS
204      *         is not part of the option within this request.
205      */
getResetImsSubId()206     public int getResetImsSubId() {
207         return mSubscriptionIdToResetIms;
208     }
209 
210     /**
211      * Set the subscription ID applied for resetting APN.
212      * @param subId is the subscription ID referenced from SubscriptionManager.
213      *         {@code ALL_SUBSCRIPTION_ID} for applying to all subscriptions.
214      *         {@code INVALID_SUBSCRIPTION_ID} means resetting IMS will not take place.
215      * @return this
216      */
setResetImsSubId(int subId)217     public ResetNetworkRequest setResetImsSubId(int subId) {
218         mSubscriptionIdToResetIms = subId;
219         return this;
220     }
221 
222     /**
223      * Store a copy of this request into Bundle given.
224      * @param writeToBundle is a Bundle for storing configurations of this request.
225      * @return this request
226      */
writeIntoBundle(Bundle writeToBundle)227     public ResetNetworkRequest writeIntoBundle(Bundle writeToBundle) {
228         writeToBundle.putInt(KEY_RESET_OPTIONS, mResetOptions);
229         writeToBundle.putString(KEY_ESIM_PACKAGE, mResetEsimPackageName);
230         writeToBundle.putInt(KEY_TELEPHONY_NET_POLICY_MANAGER_SUBID, mResetTelephonyManager);
231         writeToBundle.putInt(KEY_APN_SUBID, mResetApn);
232         writeToBundle.putInt(KEY_RESET_IMS_SUBID, mSubscriptionIdToResetIms);
233         return this;
234     }
235 
236     /**
237      * Build a ResetNetworkOperationBuilder based on configurations within this request.
238      * @param context required by ResetNetworkOperationBuilder
239      * @param looper required by ResetNetworkOperationBuilder for callback support
240      * @return a ResetNetworkOperationBuilder
241      */
toResetNetworkOperationBuilder(Context context, Looper looper)242     public ResetNetworkOperationBuilder toResetNetworkOperationBuilder(Context context,
243             Looper looper) {
244         // Follow specific order based on previous design within file ResetNetworkConfirm.java
245         ResetNetworkOperationBuilder builder = new ResetNetworkOperationBuilder(context);
246         if ((mResetOptions & RESET_CONNECTIVITY_MANAGER) != 0) {
247             builder.resetConnectivityManager();
248         }
249         if ((mResetOptions & RESET_VPN_MANAGER) != 0) {
250             builder.resetVpnManager();
251         }
252         if ((mResetOptions & RESET_WIFI_MANAGER) != 0) {
253             builder.resetWifiManager();
254         }
255         if ((mResetOptions & RESET_WIFI_P2P_MANAGER) != 0) {
256             builder.resetWifiP2pManager(looper);
257         }
258         if (mResetEsimPackageName != null) {
259             builder.resetEsim(mResetEsimPackageName);
260         }
261         if (mResetTelephonyManager != INVALID_SUBSCRIPTION_ID) {
262             builder.resetTelephonyAndNetworkPolicyManager(mResetTelephonyManager);
263         }
264         if ((mResetOptions & RESET_BLUETOOTH_MANAGER) != 0) {
265             builder.resetBluetoothManager();
266         }
267         if (mResetApn != INVALID_SUBSCRIPTION_ID) {
268             builder.resetApn(mResetApn);
269         }
270         if ((mResetOptions & RESET_IMS_STACK) != 0) {
271             builder.resetIms(mSubscriptionIdToResetIms);
272         }
273         // Reset phone process and RILD may impact above components, keep them at the end
274         if ((mResetOptions & RESET_RILD) != 0) {
275             builder.restartRild();
276         }
277         if ((mResetOptions & RESET_PHONE_PROCESS) != 0) {
278             builder.restartPhoneProcess();
279         }
280         return builder;
281     }
282 }
283