summaryrefslogtreecommitdiffstats
path: root/src/com/android/contacts/common/dialog/SelectSIMDialogFragment.java
blob: e15aa20336db9f2a9b07c0d2a7adbcc763d46d71 (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
package com.android.contacts.common.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.android.contacts.common.R;

/**
 * Dialog that allows the user to switch between default SIM cards
 */
public class SelectSIMDialogFragment extends DialogFragment {
    private int mInitialSelectedItem;
    private int mCurrentSelectedItem;
    private boolean mHasDefaultSim;
    private boolean mIsDefaultSet;
    private OnClickOkListener mOkListener;

    /* Preferred way to show this dialog */
    public static void show(FragmentManager fragmentManager,
            int currentSimCard) {
        SelectSIMDialogFragment fragment = new SelectSIMDialogFragment();
        fragment.mInitialSelectedItem = currentSimCard;
        fragment.mCurrentSelectedItem = currentSimCard;
        fragment.show(fragmentManager, "selectSIM");
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // TODO: Get the sim names programmatically
        CharSequence[] sims = {"SIM1-ATT", "SIM2-TMobile"};

        // TODO: pull default sim from database
        mHasDefaultSim = false;

        final DialogInterface.OnClickListener selectionListener =
                new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mCurrentSelectedItem = which;
            }
        };
        final DialogInterface.OnClickListener okListener =
                new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                boolean isChanged = mInitialSelectedItem != mCurrentSelectedItem;

                if (isChanged) {
                    mOkListener.passSimUpdate(mCurrentSelectedItem);
                }
                if (mIsDefaultSet && (!mHasDefaultSim || isChanged)) {
                    // if setting a new default, save to database
                    // TODO: save to database
                }
                else if (!mIsDefaultSet && mHasDefaultSim) {
                    // if unchecking the checkbox, remove the default
                    // TODO: remove from database
                }
            }
        };

        final CompoundButton.OnCheckedChangeListener checkListener =
                new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton check, boolean isChecked) {
                mIsDefaultSet = isChecked;
            }
        };

        // Generate custom checkbox view
        LinearLayout checkboxLayout = (LinearLayout) getActivity()
                .getLayoutInflater()
                .inflate(R.layout.default_sim_checkbox, null);

        CheckBox cb = (CheckBox) checkboxLayout.findViewById(R.id.default_sim_checkbox_view);
        cb.setOnCheckedChangeListener(checkListener);
        cb.setChecked(mHasDefaultSim);

        AlertDialog dialog = new AlertDialog.Builder(getActivity())
                .setTitle(R.string.menu_select_sim)
                .setSingleChoiceItems(sims, mInitialSelectedItem, selectionListener)
                .setPositiveButton(android.R.string.ok, okListener)
                .setNegativeButton(android.R.string.cancel, null)
                .create();

        dialog.getListView().addFooterView(checkboxLayout);

        return dialog;
    }

    /*
     * Interface to help pass updated SIM information to the main dialer
     */
    public interface OnClickOkListener {
        public void passSimUpdate(int simId);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        try {
            mOkListener = (OnClickOkListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnClickOkListener");
        }
    }
}