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.settings;
18
19import android.content.ContentUris;
20import android.content.Context;
21import android.content.Intent;
22import android.net.Uri;
23import android.provider.Telephony;
24import android.support.v7.preference.Preference;
25import android.support.v7.preference.PreferenceViewHolder;
26import android.telephony.SubscriptionManager;
27import android.util.AttributeSet;
28import android.util.Log;
29import android.view.View;
30import android.view.View.OnClickListener;
31import android.widget.CompoundButton;
32import android.widget.RadioButton;
33import android.widget.RelativeLayout;
34
35public class ApnPreference extends Preference implements
36        CompoundButton.OnCheckedChangeListener, OnClickListener {
37    final static String TAG = "ApnPreference";
38
39    private int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
40
41    public ApnPreference(Context context, AttributeSet attrs, int defStyle) {
42        super(context, attrs, defStyle);
43    }
44
45    public ApnPreference(Context context, AttributeSet attrs) {
46        this(context, attrs, R.attr.apnPreferenceStyle);
47    }
48
49    public ApnPreference(Context context) {
50        this(context, null);
51    }
52
53    private static String mSelectedKey = null;
54    private static CompoundButton mCurrentChecked = null;
55    private boolean mProtectFromCheckedChange = false;
56    private boolean mSelectable = true;
57
58    @Override
59    public void onBindViewHolder(PreferenceViewHolder view) {
60        super.onBindViewHolder(view);
61
62        View widget = view.findViewById(R.id.apn_radiobutton);
63        if ((widget != null) && widget instanceof RadioButton) {
64            RadioButton rb = (RadioButton) widget;
65            if (mSelectable) {
66                rb.setOnCheckedChangeListener(this);
67
68                boolean isChecked = getKey().equals(mSelectedKey);
69                if (isChecked) {
70                    mCurrentChecked = rb;
71                    mSelectedKey = getKey();
72                }
73
74                mProtectFromCheckedChange = true;
75                rb.setChecked(isChecked);
76                mProtectFromCheckedChange = false;
77                rb.setVisibility(View.VISIBLE);
78            } else {
79                rb.setVisibility(View.GONE);
80            }
81        }
82
83        View textLayout = view.findViewById(R.id.text_layout);
84        if ((textLayout != null) && textLayout instanceof RelativeLayout) {
85            textLayout.setOnClickListener(this);
86        }
87    }
88
89    public boolean isChecked() {
90        return getKey().equals(mSelectedKey);
91    }
92
93    public void setChecked() {
94        mSelectedKey = getKey();
95    }
96
97    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
98        Log.i(TAG, "ID: " + getKey() + " :" + isChecked);
99        if (mProtectFromCheckedChange) {
100            return;
101        }
102
103        if (isChecked) {
104            if (mCurrentChecked != null) {
105                mCurrentChecked.setChecked(false);
106            }
107            mCurrentChecked = buttonView;
108            mSelectedKey = getKey();
109            callChangeListener(mSelectedKey);
110        } else {
111            mCurrentChecked = null;
112            mSelectedKey = null;
113        }
114    }
115
116    public void onClick(android.view.View v) {
117        if ((v != null) && (R.id.text_layout == v.getId())) {
118            Context context = getContext();
119            if (context != null) {
120                int pos = Integer.parseInt(getKey());
121                Uri url = ContentUris.withAppendedId(Telephony.Carriers.CONTENT_URI, pos);
122                Intent editIntent = new Intent(Intent.ACTION_EDIT, url);
123                editIntent.putExtra(ApnSettings.SUB_ID, mSubId);
124                context.startActivity(editIntent);
125            }
126        }
127    }
128
129    public void setSelectable(boolean selectable) {
130        mSelectable = selectable;
131    }
132
133    public boolean getSelectable() {
134        return mSelectable;
135    }
136
137    public void setSubId(int subId) {
138        mSubId = subId;
139    }
140}
141