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 
30 import com.android.messaging.R;
31 import com.android.messaging.datamodel.data.ParticipantData;
32 import com.android.messaging.ui.UIIntents;
33 
34 /**
35  * ApnPreference implements a pref, typically used as a list item, that has a title/summary on
36  * the left and a radio button on the right.
37  *
38  */
39 public class ApnPreference extends Preference implements
40         CompoundButton.OnCheckedChangeListener, OnClickListener {
41     static final String TAG = "ApnPreference";
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     private int mSubId = ParticipantData.DEFAULT_SELF_SUB_ID;
60 
61     @Override
getView(View convertView, ViewGroup parent)62     public View getView(View convertView, ViewGroup parent) {
63         View view = super.getView(convertView, parent);
64 
65         View widget = view.findViewById(R.id.apn_radiobutton);
66         if ((widget != null) && widget instanceof RadioButton) {
67             RadioButton rb = (RadioButton) widget;
68             if (mSelectable) {
69                 rb.setOnCheckedChangeListener(this);
70 
71                 boolean isChecked = getKey().equals(mSelectedKey);
72                 if (isChecked) {
73                     mCurrentChecked = rb;
74                     mSelectedKey = getKey();
75                 }
76 
77                 mProtectFromCheckedChange = true;
78                 rb.setChecked(isChecked);
79                 mProtectFromCheckedChange = false;
80             } else {
81                 rb.setVisibility(View.GONE);
82             }
83             rb.setContentDescription(getTitle());
84         }
85 
86         View textLayout = view.findViewById(R.id.text_layout);
87         if ((textLayout != null) && textLayout instanceof RelativeLayout) {
88             textLayout.setOnClickListener(this);
89         }
90 
91         return view;
92     }
93 
isChecked()94     public boolean isChecked() {
95         return getKey().equals(mSelectedKey);
96     }
97 
setChecked()98     public void setChecked() {
99         mSelectedKey = getKey();
100     }
101 
setSubId(final int subId)102     public void setSubId(final int subId) {
103         mSubId = subId;
104     }
105 
onCheckedChanged(CompoundButton buttonView, boolean isChecked)106     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
107         Log.i(TAG, "ID: " + getKey() + " :" + isChecked);
108         if (mProtectFromCheckedChange) {
109             return;
110         }
111 
112         if (isChecked) {
113             if (mCurrentChecked != null) {
114                 mCurrentChecked.setChecked(false);
115             }
116             mCurrentChecked = buttonView;
117             mSelectedKey = getKey();
118             callChangeListener(mSelectedKey);
119         } else {
120             mCurrentChecked = null;
121             mSelectedKey = null;
122         }
123         buttonView.setContentDescription(getTitle());
124     }
125 
onClick(android.view.View v)126     public void onClick(android.view.View v) {
127         if ((v != null) && (R.id.text_layout == v.getId())) {
128             Context context = getContext();
129             if (context != null) {
130                 context.startActivity(
131                         UIIntents.get().getApnEditorIntent(context, getKey(), mSubId));
132             }
133         }
134     }
135 
setSelectable(boolean selectable)136     public void setSelectable(boolean selectable) {
137         mSelectable = selectable;
138     }
139 
getSelectable()140     public boolean getSelectable() {
141         return mSelectable;
142     }
143 }
144