summaryrefslogtreecommitdiffstats
path: root/src/com/cyanogenmod/setupwizard/setup/ChooseDataSimPage.java
blob: de8c8e356425cee37114c9ba9a2d4a49062471b1 (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
/*
 * Copyright (C) 2014 The CyanogenMod 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.cyanogenmod.setupwizard.setup;

import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.telephony.SignalStrength;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.cyanogenmod.setupwizard.R;
import com.cyanogenmod.setupwizard.SetupWizardApp;
import com.cyanogenmod.setupwizard.cmstats.SetupStats;
import com.cyanogenmod.setupwizard.ui.SetupPageFragment;
import com.cyanogenmod.setupwizard.util.SetupWizardUtils;

import java.util.List;

public class ChooseDataSimPage extends SetupPage {

    public static final String TAG = "ChooseDataSimPage";

    public ChooseDataSimPage(Context context, SetupDataCallbacks callbacks) {
        super(context, callbacks);
    }

    @Override
    public Fragment getFragment(FragmentManager fragmentManager, int action) {
        Fragment fragment = fragmentManager.findFragmentByTag(getKey());
        if (fragment == null) {
            Bundle args = new Bundle();
            args.putString(Page.KEY_PAGE_ARGUMENT, getKey());
            args.putInt(Page.KEY_PAGE_ACTION, action);
            fragment = new ChooseDataSimFragment();
            fragment.setArguments(args);
        }
        return fragment;
    }

    @Override
    public String getKey() {
        return TAG;
    }

    @Override
    public int getTitleResId() {
        return R.string.setup_choose_data_sim;
    }


    public static class ChooseDataSimFragment extends SetupPageFragment {

        private ViewGroup mPageView;
        private ProgressBar mProgressBar;
        private SparseArray<TextView> mNameViews;
        private SparseArray<ImageView> mSignalViews;
        private SparseArray<CheckBox> mCheckBoxes;

        private TelephonyManager mPhone;
        private SparseArray<SubscriptionInfo> mSubInfoRecords;
        private SparseArray<SignalStrength> mSignalStrengths;
        private SparseArray<ServiceState> mServiceStates;
        private SparseArray<PhoneStateListener> mPhoneStateListeners;

        private boolean mIsAttached = false;

        private Context mContext;
        private SubscriptionManager mSubscriptionManager;

        private final Handler mHandler = new Handler();

        private final Runnable mRadioReadyRunnable = new Runnable() {
            @Override
            public void run() {
                hideWaitForRadio();
            }
        };

        private View.OnClickListener mSetDataSimClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SubscriptionInfo subInfoRecord = (SubscriptionInfo)view.getTag();
                if (subInfoRecord != null) {
                    mSubscriptionManager.setDefaultDataSubId(subInfoRecord.getSubscriptionId());
                    setDataSubChecked(subInfoRecord);
                }
            }
        };

        @Override
        protected void initializePage() {
            mPageView = (ViewGroup)mRootView.findViewById(R.id.page_view);
            mProgressBar = (ProgressBar) mRootView.findViewById(R.id.progress);
            List<SubscriptionInfo> subInfoRecords = mSubscriptionManager.getActiveSubscriptionInfoList();
            int simCount = subInfoRecords.size();
            mSubInfoRecords = new SparseArray<SubscriptionInfo>(simCount);
            for (SubscriptionInfo subInfoRecord : subInfoRecords) {
                mSubInfoRecords.put(subInfoRecord.getSimSlotIndex(), subInfoRecord);
            }
            mNameViews = new SparseArray<TextView>(simCount);
            mSignalViews = new SparseArray<ImageView>(simCount);
            mCheckBoxes = new SparseArray<CheckBox>(simCount);
            mServiceStates = new SparseArray<ServiceState>(simCount);
            mSignalStrengths = new SparseArray<SignalStrength>(simCount);
            mPhoneStateListeners = new SparseArray<PhoneStateListener>(simCount);
            LayoutInflater inflater = LayoutInflater.from(getActivity());
            for (int i = 0; i < simCount; i++) {
                View simRow = inflater.inflate(R.layout.data_sim_row, null);
                mPageView.addView(simRow);
                SubscriptionInfo subInfoRecord = mSubInfoRecords.valueAt(i);
                simRow.setTag(subInfoRecord);
                simRow.setOnClickListener(mSetDataSimClickListener);
                int slot = subInfoRecord.getSimSlotIndex();
                mNameViews.put(slot, (TextView) simRow.findViewById(R.id.sim_title));
                mSignalViews.put(slot, (ImageView) simRow.findViewById(R.id.signal));
                mCheckBoxes.put(slot, (CheckBox) simRow.findViewById(R.id.enable_check));
                mPhoneStateListeners.put(slot, createPhoneStateListener(subInfoRecord));
                mPageView.addView(inflater.inflate(R.layout.divider, null));
            }
            updateSignalStrengths();
            updateCurrentDataSub();
        }

        @Override
        protected int getLayoutResource() {
            return R.layout.choose_data_sim_page;
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mContext = getActivity().getApplicationContext();
            mSubscriptionManager = SubscriptionManager.from(mContext);
        }

        @Override
        public void onResume() {
            super.onResume();
            mIsAttached = true;
            mPhone = (TelephonyManager)getActivity().getSystemService(Context.TELEPHONY_SERVICE);
            for (int i = 0; i < mPhoneStateListeners.size(); i++) {
                mPhone.listen(mPhoneStateListeners.valueAt(i),
                        PhoneStateListener.LISTEN_SERVICE_STATE
                                | PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
            }
            updateSignalStrengths();
            updateCurrentDataSub();
            if (SetupWizardUtils.isRadioReady(mContext, null)) {
                hideWaitForRadio();
            } else if (mTitleView != null) {
                mTitleView.setText(R.string.loading);
                mHandler.postDelayed(mRadioReadyRunnable, SetupWizardApp.RADIO_READY_TIMEOUT);
            }
        }

        @Override
        public void onPause() {
            super.onPause();
            mIsAttached = false;
            for (int i = 0; i < mPhoneStateListeners.size(); i++) {
                mPhone.listen(mPhoneStateListeners.valueAt(i), PhoneStateListener.LISTEN_NONE);
            }
        }

        private PhoneStateListener createPhoneStateListener(final SubscriptionInfo subInfoRecord) {
            return new PhoneStateListener(subInfoRecord.getSubscriptionId()) {

                @Override
                public void onSignalStrengthsChanged(SignalStrength signalStrength) {
                    mSignalStrengths.put(subInfoRecord.getSimSlotIndex(), signalStrength);
                    updateSignalStrength(subInfoRecord);
                }

                @Override
                public void onServiceStateChanged(ServiceState state) {
                    if (SetupWizardUtils.isRadioReady(mContext, state)) {
                        hideWaitForRadio();
                    }
                    mServiceStates.put(subInfoRecord.getSimSlotIndex(), state);
                    updateSignalStrength(subInfoRecord);
                }
            };
        }

        private void hideWaitForRadio() {
            if (getUserVisibleHint() && mProgressBar.isShown()) {
                mHandler.removeCallbacks(mRadioReadyRunnable);
                if (mTitleView != null) {
                    mTitleView.setText(mPage.getTitleResId());
                }
                mProgressBar.setVisibility(View.GONE);
                mPageView.setVisibility(View.VISIBLE);
                mPageView.startAnimation(
                        AnimationUtils.loadAnimation(getActivity(), R.anim.translucent_enter));
            }
        }

        private void updateSignalStrengths() {
            if (mIsAttached) {
                for (int i = 0; i < mSubInfoRecords.size(); i++) {
                    updateSignalStrength(mSubInfoRecords.valueAt(i));
                }
            }
        }

        private void setDataSubChecked(SubscriptionInfo subInfoRecord) {
            if (mIsAttached) {
                for (int i = 0; i < mCheckBoxes.size(); i++) {
                    if (subInfoRecord.getSimSlotIndex() == i) {
                        mCheckBoxes.get(i).setChecked(true);
                        SetupStats.addEvent(SetupStats.Categories.SETTING_CHANGED,
                                SetupStats.Action.PREFERRED_DATA_SIM,
                                SetupStats.Label.SLOT, String.valueOf(i + 1));
                    } else {
                        mCheckBoxes.get(i).setChecked(false);
                    }

                }
            }
        }

        private void updateCurrentDataSub() {
            if (mIsAttached) {
                for (int i = 0; i < mSubInfoRecords.size(); i++) {
                    SubscriptionInfo subInfoRecord = mSubInfoRecords.valueAt(i);
                    mCheckBoxes.get(i).setChecked(mSubscriptionManager.getDefaultDataPhoneId()
                            == subInfoRecord.getSimSlotIndex());
                }
            }
        }

        private void updateCarrierText(SubscriptionInfo subInfoRecord) {
            if (mIsAttached) {
                String name = mPhone.getNetworkOperatorName(subInfoRecord.getSimSlotIndex());
                ServiceState serviceState = mServiceStates.get(subInfoRecord.getSimSlotIndex());
                if (TextUtils.isEmpty(name)) {
                    if (serviceState != null && serviceState.isEmergencyOnly()) {
                        name = getString(R.string.setup_mobile_data_emergency_only);
                    } else {
                        name = getString(R.string.setup_mobile_data_no_service);
                    }
                }
                String formattedName =
                        getString(R.string.data_sim_name,
                                  subInfoRecord.getSimSlotIndex() + 1, name);
                mNameViews.get(subInfoRecord.getSimSlotIndex()).setText(formattedName);
            }
        }

        private void updateSignalStrength(SubscriptionInfo subInfoRecord) {
            if (mIsAttached) {
                ImageView signalView = mSignalViews.get(subInfoRecord.getSimSlotIndex());
                SignalStrength signalStrength = mSignalStrengths.get(subInfoRecord.getSimSlotIndex());
                if (!hasService(subInfoRecord)) {
                    signalView.setImageResource(R.drawable.ic_signal_no_signal);
                } else {
                    if (signalStrength != null) {
                        int resId;
                        switch (signalStrength.getLevel()) {
                            case 4:
                                resId = R.drawable.ic_signal_4;
                                break;
                            case 3:
                                resId = R.drawable.ic_signal_3;
                                break;
                            case 2:
                                resId = R.drawable.ic_signal_2;
                                break;
                            case 1:
                                resId = R.drawable.ic_signal_1;
                                break;
                            default:
                                resId = R.drawable.ic_signal_0;
                                break;
                        }
                        signalView.setImageResource(resId);
                    }
                }
                updateCarrierText(subInfoRecord);
            }
        }

        private boolean hasService(SubscriptionInfo subInfoRecord) {
            boolean retVal;
            ServiceState serviceState = mServiceStates.get(subInfoRecord.getSimSlotIndex());
            if (serviceState != null) {
                // Consider the device to be in service if either voice or data service is available.
                // Some SIM cards are marketed as data-only and do not support voice service, and on
                // these SIM cards, we want to show signal bars for data service as well as the "no
                // service" or "emergency calls only" text that indicates that voice is not available.
                switch(serviceState.getVoiceRegState()) {
                    case ServiceState.STATE_POWER_OFF:
                        retVal = false;
                        break;
                    case ServiceState.STATE_OUT_OF_SERVICE:
                    case ServiceState.STATE_EMERGENCY_ONLY:
                        retVal = serviceState.getDataRegState() == ServiceState.STATE_IN_SERVICE;
                        break;
                    default:
                        retVal = true;
                }
            } else {
                retVal = false;
            }
            return retVal;
        }
    }

}