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.apn;
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.RelativeLayout;
31 import android.widget.Toast;
32 
33 import androidx.preference.Preference;
34 import androidx.preference.PreferenceViewHolder;
35 
36 import com.android.settings.R;
37 
38 /**
39  * Preference of APN UI entry
40  */
41 public class ApnPreference extends Preference implements CompoundButton.OnCheckedChangeListener,
42         View.OnClickListener {
43     private static final  String TAG = "ApnPreference";
44 
45     private int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
46 
47     /**
48      * Constructor of Preference
49      */
ApnPreference(Context context, AttributeSet attrs, int defStyle)50     public ApnPreference(Context context, AttributeSet attrs, int defStyle) {
51         super(context, attrs, defStyle);
52     }
53 
54     /**
55      * Constructor of Preference
56      */
ApnPreference(Context context, AttributeSet attrs)57     public ApnPreference(Context context, AttributeSet attrs) {
58         this(context, attrs, R.attr.apnPreferenceStyle);
59     }
60 
61     /**
62      * Constructor of Preference
63      */
ApnPreference(Context context)64     public ApnPreference(Context context) {
65         this(context, null);
66     }
67 
68     private static String sSelectedKey = null;
69     private static CompoundButton sCurrentChecked = null;
70     private boolean mProtectFromCheckedChange = false;
71     private boolean mSelectable = true;
72     private boolean mHideDetails = false;
73 
74     @Override
onBindViewHolder(PreferenceViewHolder view)75     public void onBindViewHolder(PreferenceViewHolder view) {
76         super.onBindViewHolder(view);
77 
78         final RelativeLayout textArea = (RelativeLayout) view.findViewById(R.id.text_layout);
79         textArea.setOnClickListener(this);
80 
81         final View widget = view.findViewById(R.id.apn_radiobutton);
82         if ((widget != null) && widget instanceof RadioButton) {
83             final RadioButton rb = (RadioButton) widget;
84             if (mSelectable) {
85                 rb.setOnCheckedChangeListener(this);
86 
87                 final boolean isChecked = getKey().equals(sSelectedKey);
88                 if (isChecked) {
89                     sCurrentChecked = rb;
90                     sSelectedKey = getKey();
91                 }
92 
93                 mProtectFromCheckedChange = true;
94                 rb.setChecked(isChecked);
95                 mProtectFromCheckedChange = false;
96                 rb.setVisibility(View.VISIBLE);
97             } else {
98                 rb.setVisibility(View.GONE);
99             }
100         }
101     }
102 
103     /**
104      * Return the preference is checked or not.
105      */
isChecked()106     public boolean isChecked() {
107         return getKey().equals(sSelectedKey);
108     }
109 
110     /**
111      * Set preference checked.
112      */
setChecked()113     public void setChecked() {
114         sSelectedKey = getKey();
115     }
116 
117     /**
118      * Change the preference status.
119      */
onCheckedChanged(CompoundButton buttonView, boolean isChecked)120     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
121         Log.i(TAG, "ID: " + getKey() + " :" + isChecked);
122         if (mProtectFromCheckedChange) {
123             return;
124         }
125 
126         if (isChecked) {
127             if (sCurrentChecked != null) {
128                 sCurrentChecked.setChecked(false);
129             }
130             sCurrentChecked = buttonView;
131             sSelectedKey = getKey();
132             callChangeListener(sSelectedKey);
133         } else {
134             sCurrentChecked = null;
135             sSelectedKey = null;
136         }
137     }
138 
139     @Override
onClick(View layoutView)140     public void onClick(View layoutView) {
141         super.onClick();
142         final Context context = getContext();
143         final int pos = Integer.parseInt(getKey());
144         if (context == null) {
145             Log.w(TAG, "No context available for pos=" + pos);
146             return;
147         }
148 
149         if (mHideDetails) {
150             Toast.makeText(context, context.getString(
151                     R.string.cannot_change_apn_toast), Toast.LENGTH_LONG).show();
152             return;
153         }
154         final Uri url = ContentUris.withAppendedId(Telephony.Carriers.CONTENT_URI, pos);
155         final Intent editIntent = new Intent(Intent.ACTION_EDIT, url);
156         editIntent.putExtra(ApnSettings.SUB_ID, mSubId);
157         editIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
158         context.startActivity(editIntent);
159     }
160 
setSelectable(boolean selectable)161     public void setSelectable(boolean selectable) {
162         mSelectable = selectable;
163     }
164 
getSelectable()165     public boolean getSelectable() {
166         return mSelectable;
167     }
168 
setSubId(int subId)169     public void setSubId(int subId) {
170         mSubId = subId;
171     }
172 
173     /**
174      * Hide details
175      */
setHideDetails()176     public void setHideDetails() {
177         mHideDetails = true;
178     }
179 }
180