2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.network;
19 import android.content.ContentUris;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.net.Uri;
23 import android.provider.Telephony;
24 import android.telephony.SubscriptionManager;
25 import android.util.AttributeSet;
26 import android.util.Log;
27 import android.view.View;
28 import android.widget.CompoundButton;
29 import android.widget.RadioButton;
30 import android.widget.Toast;
32 import androidx.preference.Preference;
33 import androidx.preference.PreferenceViewHolder;
35 import com.android.settings.R;
37 public class ApnPreference extends Preference implements CompoundButton.OnCheckedChangeListener {
38 final static String TAG = "ApnPreference";
40 private int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
42 public ApnPreference(Context context, AttributeSet attrs, int defStyle) {
43 super(context, attrs, defStyle);
46 public ApnPreference(Context context, AttributeSet attrs) {
47 this(context, attrs, R.attr.apnPreferenceStyle);
50 public ApnPreference(Context context) {
54 private static String mSelectedKey = null;
55 private static CompoundButton mCurrentChecked = null;
56 private boolean mProtectFromCheckedChange = false;
57 private boolean mSelectable = true;
58 private boolean mHideDetails = false;
61 public void onBindViewHolder(PreferenceViewHolder view) {
62 super.onBindViewHolder(view);
64 View widget = view.findViewById(R.id.apn_radiobutton);
65 if ((widget != null) && widget instanceof RadioButton) {
66 RadioButton rb = (RadioButton) widget;
68 rb.setOnCheckedChangeListener(this);
70 boolean isChecked = getKey().equals(mSelectedKey);
73 mSelectedKey = getKey();
76 mProtectFromCheckedChange = true;
77 rb.setChecked(isChecked);
78 mProtectFromCheckedChange = false;
79 rb.setVisibility(View.VISIBLE);
81 rb.setVisibility(View.GONE);
86 public boolean isChecked() {
87 return getKey().equals(mSelectedKey);
90 public void setChecked() {
91 mSelectedKey = getKey();
94 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
95 Log.i(TAG, "ID: " + getKey() + " :" + isChecked);
96 if (mProtectFromCheckedChange) {
101 if (mCurrentChecked != null) {
102 mCurrentChecked.setChecked(false);
104 mCurrentChecked = buttonView;
105 mSelectedKey = getKey();
106 callChangeListener(mSelectedKey);
108 mCurrentChecked = null;
114 protected void onClick() {
116 Context context = getContext();
117 if (context != null) {
119 Toast.makeText(context, context.getString(
120 R.string.cannot_change_apn_toast), Toast.LENGTH_LONG).show();
123 int pos = Integer.parseInt(getKey());
124 Uri url = ContentUris.withAppendedId(Telephony.Carriers.CONTENT_URI, pos);
125 Intent editIntent = new Intent(Intent.ACTION_EDIT, url);
126 editIntent.putExtra(ApnSettings.SUB_ID, mSubId);
127 context.startActivity(editIntent);
131 public void setSelectable(boolean selectable) {
132 mSelectable = selectable;
135 public boolean getSelectable() {
139 public void setSubId(int subId) {
143 public void setHideDetails() {