1 /*
2  * Copyright (C) 2015 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.messaging.ui.appsettings;
18 
19 import android.content.Context;
20 import android.preference.Preference;
21 import android.util.AttributeSet;
22 import android.util.Log;
23 import android.view.View;
24 import android.view.View.OnClickListener;
25 import android.view.ViewGroup;
26 import android.widget.CompoundButton;
27 import android.widget.RadioButton;
28 import android.widget.RelativeLayout;
29 import android.widget.TextView;
30 
31 import com.android.messaging.R;
32 import com.android.messaging.datamodel.data.ParticipantData;
33 import com.android.messaging.ui.UIIntents;
34 
35 /**
36  * ApnPreference implements a pref, typically used as a list item, that has a title/summary on
37  * the left and a radio button on the right.
38  *
39  */
40 public class ApnPreference extends Preference implements
41         CompoundButton.OnCheckedChangeListener, OnClickListener {
42     static final String TAG = "ApnPreference";
43 
ApnPreference(Context context, AttributeSet attrs, int defStyle)44     public ApnPreference(Context context, AttributeSet attrs, int defStyle) {
45         super(context, attrs, defStyle);
46     }
47 
ApnPreference(Context context, AttributeSet attrs)48     public ApnPreference(Context context, AttributeSet attrs) {
49         this(context, attrs, R.attr.apnPreferenceStyle);
50     }
51 
ApnPreference(Context context)52     public ApnPreference(Context context) {
53         this(context, null);
54     }
55 
56     private static String mSelectedKey = null;
57     private static CompoundButton mCurrentChecked = null;
58     private boolean mProtectFromCheckedChange = false;
59     private boolean mSelectable = true;
60     private int mSubId = ParticipantData.DEFAULT_SELF_SUB_ID;
61 
62     @Override
getView(View convertView, ViewGroup parent)63     public View getView(View convertView, ViewGroup parent) {
64         View view = super.getView(convertView, parent);
65 
66         View widget = view.findViewById(R.id.apn_radiobutton);
67         if ((widget != null) && widget instanceof RadioButton) {
68             RadioButton rb = (RadioButton) widget;
69             if (mSelectable) {
70                 rb.setOnCheckedChangeListener(this);
71 
72                 boolean isChecked = getKey().equals(mSelectedKey);
73                 if (isChecked) {
74                     mCurrentChecked = rb;
75                     mSelectedKey = getKey();
76                 }
77 
78                 mProtectFromCheckedChange = true;
79                 rb.setChecked(isChecked);
80                 mProtectFromCheckedChange = false;
81             } else {
82                 rb.setVisibility(View.GONE);
83             }
84             setApnRadioButtonContentDescription(rb);
85         }
86 
87         View textLayout = view.findViewById(R.id.text_layout);
88         if ((textLayout != null) && textLayout instanceof RelativeLayout) {
89             textLayout.setOnClickListener(this);
90         }
91 
92         return view;
93     }
94 
setApnRadioButtonContentDescription(final CompoundButton buttonView)95     public void setApnRadioButtonContentDescription(final CompoundButton buttonView) {
96         final View widget = (View) buttonView.getParent();
97         final TextView tv = (TextView) widget.findViewById(R.id.title);
98         final String apnTitle = tv.getText().toString();
99         buttonView.setContentDescription(apnTitle);
100     }
101 
isChecked()102     public boolean isChecked() {
103         return getKey().equals(mSelectedKey);
104     }
105 
setChecked()106     public void setChecked() {
107         mSelectedKey = getKey();
108     }
109 
setSubId(final int subId)110     public void setSubId(final int subId) {
111         mSubId = subId;
112     }
113 
onCheckedChanged(CompoundButton buttonView, boolean isChecked)114     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
115         Log.i(TAG, "ID: " + getKey() + " :" + isChecked);
116         if (mProtectFromCheckedChange) {
117             return;
118         }
119 
120         if (isChecked) {
121             if (mCurrentChecked != null) {
122                 mCurrentChecked.setChecked(false);
123             }
124             mCurrentChecked = buttonView;
125             mSelectedKey = getKey();
126             callChangeListener(mSelectedKey);
127         } else {
128             mCurrentChecked = null;
129             mSelectedKey = null;
130         }
131         setApnRadioButtonContentDescription(buttonView);
132     }
133 
onClick(android.view.View v)134     public void onClick(android.view.View v) {
135         if ((v != null) && (R.id.text_layout == v.getId())) {
136             Context context = getContext();
137             if (context != null) {
138                 context.startActivity(
139                         UIIntents.get().getApnEditorIntent(context, getKey(), mSubId));
140             }
141         }
142     }
143 
setSelectable(boolean selectable)144     public void setSelectable(boolean selectable) {
145         mSelectable = selectable;
146     }
147 
getSelectable()148     public boolean getSelectable() {
149         return mSelectable;
150     }
151 }
152