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