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