1/*
2 * Copyright (C) 2009 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
17package com.android.phone;
18
19import android.content.Context;
20import android.os.AsyncResult;
21import android.os.Bundle;
22import android.os.Handler;
23import android.os.Message;
24import android.preference.ListPreference;
25import android.provider.Settings;
26import android.provider.Settings.Secure;
27import android.util.AttributeSet;
28import android.util.Log;
29
30import com.android.internal.telephony.Phone;
31import com.android.internal.telephony.PhoneFactory;
32
33public class CdmaSubscriptionListPreference extends ListPreference {
34
35    private static final String LOG_TAG = "CdmaSubscriptionListPreference";
36
37    // Used for CDMA subscription mode
38    private static final int CDMA_SUBSCRIPTION_RUIM_SIM = 0;
39    private static final int CDMA_SUBSCRIPTION_NV = 1;
40
41    //preferredSubscriptionMode  0 - RUIM/SIM, preferred
42    //                           1 - NV
43    static final int preferredSubscriptionMode = CDMA_SUBSCRIPTION_NV;
44
45    private Phone mPhone;
46    private CdmaSubscriptionButtonHandler mHandler;
47
48    public CdmaSubscriptionListPreference(Context context, AttributeSet attrs) {
49        super(context, attrs);
50
51        mPhone = PhoneFactory.getDefaultPhone();
52        mHandler = new CdmaSubscriptionButtonHandler();
53        setCurrentCdmaSubscriptionModeValue();
54    }
55
56    private void setCurrentCdmaSubscriptionModeValue() {
57        int cdmaSubscriptionMode = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
58                Settings.Global.CDMA_SUBSCRIPTION_MODE, preferredSubscriptionMode);
59        setValue(Integer.toString(cdmaSubscriptionMode));
60    }
61
62    public CdmaSubscriptionListPreference(Context context) {
63        this(context, null);
64    }
65
66    @Override
67    protected void showDialog(Bundle state) {
68        setCurrentCdmaSubscriptionModeValue();
69
70        super.showDialog(state);
71    }
72
73    @Override
74    protected void onDialogClosed(boolean positiveResult) {
75        super.onDialogClosed(positiveResult);
76
77        if (!positiveResult) {
78            //The button was dismissed - no need to set new value
79            return;
80        }
81
82        int buttonCdmaSubscriptionMode = Integer.valueOf(getValue()).intValue();
83        Log.d(LOG_TAG, "Setting new value " + buttonCdmaSubscriptionMode);
84        int statusCdmaSubscriptionMode;
85        switch(buttonCdmaSubscriptionMode) {
86            case CDMA_SUBSCRIPTION_NV:
87                statusCdmaSubscriptionMode = Phone.CDMA_SUBSCRIPTION_NV;
88                break;
89            case CDMA_SUBSCRIPTION_RUIM_SIM:
90                statusCdmaSubscriptionMode = Phone.CDMA_SUBSCRIPTION_RUIM_SIM;
91                break;
92            default:
93                statusCdmaSubscriptionMode = Phone.PREFERRED_CDMA_SUBSCRIPTION;
94        }
95
96        // Set the CDMA subscription mode, when mode has been successfully changed
97        // handleSetCdmaSubscriptionMode will be invoked and the value saved.
98        mPhone.setCdmaSubscription(statusCdmaSubscriptionMode, mHandler
99                .obtainMessage(CdmaSubscriptionButtonHandler.MESSAGE_SET_CDMA_SUBSCRIPTION,
100                        getValue()));
101
102    }
103
104    private class CdmaSubscriptionButtonHandler extends Handler {
105
106        static final int MESSAGE_SET_CDMA_SUBSCRIPTION = 0;
107
108        @Override
109        public void handleMessage(Message msg) {
110            switch (msg.what) {
111                case MESSAGE_SET_CDMA_SUBSCRIPTION:
112                    handleSetCdmaSubscriptionMode(msg);
113                    break;
114            }
115        }
116
117        private void handleSetCdmaSubscriptionMode(Message msg) {
118            mPhone = PhoneFactory.getDefaultPhone();
119            AsyncResult ar = (AsyncResult) msg.obj;
120
121            if (ar.exception == null) {
122                // Get the original string entered by the user
123                int cdmaSubscriptionMode = Integer.valueOf((String) ar.userObj).intValue();
124                Settings.Global.putInt(mPhone.getContext().getContentResolver(),
125                        Settings.Global.CDMA_SUBSCRIPTION_MODE,
126                        cdmaSubscriptionMode );
127            } else {
128                Log.e(LOG_TAG, "Setting Cdma subscription source failed");
129            }
130        }
131    }
132}
133