summaryrefslogtreecommitdiff
path: root/src/com/android/settings/development/Prefer5GNetworkListController.java
blob: ce7face9be0bc0337a2a71cd78ff4d0ca9a25ee3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
 * Copyright (c) 2020, The Linux Foundation. All rights reserved
 * Not a contribution
 *
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.development;

import static androidx.lifecycle.Lifecycle.Event.ON_DESTROY;
import static androidx.lifecycle.Lifecycle.Event.ON_PAUSE;
import static androidx.lifecycle.Lifecycle.Event.ON_RESUME;
import static androidx.lifecycle.Lifecycle.Event.ON_START;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.preference.PreferenceManager;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.util.ArrayMap;
import android.util.Log;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;

import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.R;
import com.android.settings.network.SubscriptionUtil;
import com.android.settings.network.SubscriptionsChangeListener;
import com.android.settings.network.telephony.MobileNetworkUtils;
import com.android.settingslib.core.AbstractPreferenceController;

import com.qti.extphone.Client;
import com.qti.extphone.ExtTelephonyManager;
import com.qti.extphone.ExtPhoneCallbackBase;
import com.qti.extphone.NrConfig;
import com.qti.extphone.Status;
import com.qti.extphone.ServiceCallback;
import com.qti.extphone.Token;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * This populates the entries on a page which lists all available mobile subscriptions. Each entry
 * has the name of the subscription with some subtext giving additional detail, and clicking on the
 * entry brings you to a details page for that network.
 */
public class Prefer5GNetworkListController extends AbstractPreferenceController implements
        LifecycleObserver, ListPreference.OnPreferenceChangeListener,
        SubscriptionsChangeListener.SubscriptionsChangeListenerClient {
    private static final String TAG = "Prefer5gNetworkListCtlr";

    @VisibleForTesting
    static final String KEY_ADD_MORE = "prefer_5g_add_more";
    private static final int NR_MODE_NSA_SA = 0;
    private static final int NR_MODE_NSA = 1;
    private static final int NR_MODE_SA = 2;
    private static final int EVENT_SET_NR_CONFIG_STATUS = 101;
    private static final int EVENT_GET_NR_CONFIG_STATUS = 102;

    private Client mClient;
    private Context mContext;
    private String mPackageName;
    private SharedPreferences mSharedPreferences;
    private SubscriptionManager mSubscriptionManager;
    private SubscriptionsChangeListener mChangeListener;
    private PreferenceScreen mPreferenceScreen;
    private TelephonyManager mTelephonyManager;
    private Map<Integer, ListPreference> mPreferences;
    private List<ListPreference> mPreferenceList;

    private int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
    private int mPhoneId = SubscriptionManager.INVALID_PHONE_INDEX;
    private int userPrefNrConfig;
    private ExtTelephonyManager mExtTelephonyManager;
    private boolean mServiceConnected;
    private boolean mRetry;

    private ExtPhoneCallbackBase mCallback = new ExtPhoneCallbackBase() {
        @Override
        public void onSetNrConfig(int slotId, Token token, Status status) throws
                RemoteException {
            Log.d(TAG, "onSetNrConfig: slotId = " + slotId + " token = " + token + " status = " +
                    status);
            if (status.get() == Status.SUCCESS) {
                updateSharedPreference(slotId, userPrefNrConfig);
            }
            mMainThreadHandler.sendMessage(mMainThreadHandler
                    .obtainMessage(EVENT_SET_NR_CONFIG_STATUS, slotId, -1));
        }

        @Override
        public void onNrConfigStatus(int slotId, Token token, Status status, NrConfig nrConfig)
                throws RemoteException {
            Log.d(TAG, "onNrConfigStatus: slotId = " + slotId + " token = " + token + " status = " +
                    status + " nrConfig = " + nrConfig);
            if (status.get() == Status.SUCCESS) {
                updateSharedPreference(slotId, nrConfig.get());
                mMainThreadHandler.sendMessage(mMainThreadHandler
                        .obtainMessage(EVENT_GET_NR_CONFIG_STATUS, slotId, -1));
            }
        }
    };

    private Handler mMainThreadHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            Log.d(TAG, "handleMessage msg.what = " + msg.what);
            switch (msg.what) {
                case EVENT_SET_NR_CONFIG_STATUS:
                case EVENT_GET_NR_CONFIG_STATUS: {
                    int slotId = msg.arg1;
                    if (mPreferenceScreen != null) {
                        updatePreferenceForSlot(slotId);
                    }
                    break;
                }
            }
        }
    };

    private void updateSharedPreference(int slotId, int nrConfig) {
        if (mSharedPreferences != null) {
            mSharedPreferences.edit().putInt("nr_mode_" + slotId, nrConfig).apply();
        }
    }


    public Prefer5GNetworkListController(Context context, Lifecycle lifecycle) {
        super(context);
        Log.i(TAG, "constructor");
        mContext = context;
        mPackageName = mContext.getPackageName();
        mSharedPreferences = mContext.getSharedPreferences(mContext.getPackageName(),
                mContext.MODE_PRIVATE);
        mSubscriptionManager = context.getSystemService(SubscriptionManager.class);
        mTelephonyManager = context.getSystemService(TelephonyManager.class);
        mChangeListener = new SubscriptionsChangeListener(context, this);
        mPreferences = new ArrayMap<>();
        if (mExtTelephonyManager == null) {
            mExtTelephonyManager = ExtTelephonyManager.getInstance(
                    mContext.getApplicationContext());
        }
        Log.d(TAG, "Connect to ExtTelephony bound service...");
        mExtTelephonyManager.connectService(mServiceCallback);

        lifecycle.addObserver(this);
    }

    private ServiceCallback mServiceCallback = new ServiceCallback() {
        @Override
        public void onConnected() {
            Log.d(TAG, "ExtTelephony Service connected");
            mServiceConnected = true;
            mClient = mExtTelephonyManager.registerCallback(mPackageName, mCallback);
            Log.d(TAG, "Client = " + mClient);
            if (mRetry) {
                for (int slotId = 0; slotId < mTelephonyManager.getPhoneCount();
                        slotId++) {
                    mRetry = false;
                    Token token = mExtTelephonyManager.queryNrConfig(slotId, mClient);
                    Log.d(TAG, "queryNrConfig: " + token);
                }
            }
        }

        @Override
        public void onDisconnected() {
            Log.d(TAG, "ExtTelephony Service disconnected...");
            if (mServiceConnected) {
                mServiceConnected = false;
                mClient = null;
            }
        }
    };

    @OnLifecycleEvent(ON_RESUME)
    public void onResume() {
        Log.i(TAG, "onResume");
        mChangeListener.start();
        update();
    }

    @OnLifecycleEvent(ON_PAUSE)
    public void onPause() {
        mChangeListener.stop();
    }

    @OnLifecycleEvent(ON_DESTROY)
    protected void onDestroy() {
        Log.d(TAG, "onDestroy");
        mExtTelephonyManager.unRegisterCallback(mCallback);
        mExtTelephonyManager.disconnectService();
    }

    @Override
    public void displayPreference(PreferenceScreen screen) {
        super.displayPreference(screen);
        Log.i(TAG, "displayPreference");
        mPreferenceScreen = screen;
        mPreferenceScreen.findPreference(KEY_ADD_MORE).setVisible(
                MobileNetworkUtils.showEuiccSettings(mContext));
    }

    private void update() {
        if (mPreferenceScreen == null) {
            return;
        }

        // Since we may already have created some preferences previously, we first grab the list of
        // those, then go through the current available subscriptions making sure they are all
        // present in the screen, and finally remove any now-outdated ones.
        final Map<Integer, ListPreference> existingPreferences = mPreferences;
        mPreferences = new ArrayMap<>();
        mPreferenceList = new ArrayList<>();

        for (int slotId = 0; slotId < mTelephonyManager.getPhoneCount();
                slotId++) {
            SubscriptionInfo info = mSubscriptionManager.
                getActiveSubscriptionInfoForSimSlotIndex(slotId);
            if (info != null) {
                final int subId = info.getSubscriptionId();
                ListPreference pref = existingPreferences.remove(subId);
                if (pref == null) {
                    pref = new ListPreference(mPreferenceScreen.getContext());
                    mPreferenceScreen.addPreference(pref);
                }
                pref.setTitle(info.getDisplayName());
                pref.setOrder(slotId);
                pref.setDialogTitle("Select NR Mode For Slot " + slotId);

                pref.setOnPreferenceChangeListener(this);
                pref.setEntries(R.array.preferred_5g_network_mode_choices);
                pref.setEntryValues(R.array.preferred_5g_network_mode_values);
                if (mServiceConnected && mClient != null) {
                    mRetry = false;
                    Token token = mExtTelephonyManager.queryNrConfig(slotId, mClient);
                    Log.d(TAG, "queryNrConfig: " + token);
                } else {
                    mRetry = true;
                }

                mPreferences.put(subId, pref);
                mPreferenceList.add(pref);
            } else {
                Log.d(TAG, "sub info is null, add null preference for slot: " + slotId);
                mPreferenceList.add(slotId, null);
            }
        }
        for (Preference pref : existingPreferences.values()) {
            mPreferenceScreen.removePreference(pref);
        }
    }

    private void updatePreferenceForSlot(int slotId) {
        int nrConfig = mSharedPreferences.getInt("nr_mode_" + slotId,
                NrConfig.NR_CONFIG_COMBINED_SA_NSA);
        String text = mContext.getString(getSummaryResId(nrConfig));
        Log.d(TAG, "updatePreferenceForSlot for " + slotId + " ,nr mode is " + nrConfig +
                " , set summary to " + text);
        ListPreference pref = mPreferenceList.get(slotId);
        if (pref != null) {
            pref.setSummary(text);
            pref.setValue(Integer.toString(nrConfig));
        }
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object object) {
        final int newNrMode = Integer.parseInt((String) object);
        final int slotId = mPreferenceList.indexOf(preference);
        Log.i(TAG, "onPreferenceChange for slot: " + slotId + ", setNrConfig: " + newNrMode);
        userPrefNrConfig = newNrMode;
        if (mServiceConnected && mClient != null) {
            Token token = mExtTelephonyManager.setNrConfig(
                    slotId, new NrConfig(newNrMode), mClient);
            Log.d(TAG, "setNrConfig: " + token);
        }
        final ListPreference listPreference = (ListPreference) preference;
        String summary = mContext.getString(getSummaryResId(newNrMode));
        listPreference.setSummary(summary);
        return true;
    }

    private int getSummaryResId(int nrMode) {
        if (nrMode == NrConfig.NR_CONFIG_COMBINED_SA_NSA) {
            return R.string.nr_nsa_sa;
        } else if (nrMode == NrConfig.NR_CONFIG_NSA) {
            return R.string.nr_nsa;
        } else if (nrMode == NrConfig.NR_CONFIG_SA) {
            return R.string.nr_sa;
        } else {
            return R.string.nr_nsa_sa;
        }
    }

    @Override
    public boolean isAvailable() {
        return true;
    }

    @Override
    public String getPreferenceKey() {
        return null;
    }

    @Override
    public void onAirplaneModeChanged(boolean airplaneModeEnabled) {
    }

    @Override
    public void onSubscriptionsChanged() {
        boolean isSubChanged = false;
        final List<SubscriptionInfo> subs = SubscriptionUtil.getAvailableSubscriptions(
                mContext);
        for (SubscriptionInfo info : subs) {
            if (info == null) return;

            final int subId = info.getSubscriptionId();
            if (mPreferences.get(subId) == null) {
                isSubChanged = true;
            }
        }

        if (isSubChanged) {
            Log.d(TAG, "sub changed, will update preference");
            update();
        }
    }
}