The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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 android.widget; |
| 18 | |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 19 | import android.annotation.IdRes; |
Felipe Leme | 92736c1 | 2018-11-13 12:00:59 -0800 | [diff] [blame] | 20 | import android.annotation.NonNull; |
sallyyuen | 69a146c | 2019-11-25 17:05:47 -0800 | [diff] [blame] | 21 | import android.annotation.Nullable; |
Artur Satayev | ed5a6ae | 2019-12-10 17:47:54 +0000 | [diff] [blame] | 22 | import android.compat.annotation.UnsupportedAppUsage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.content.res.TypedArray; |
| 25 | import android.util.AttributeSet; |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 26 | import android.util.Log; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.view.View; |
| 28 | import android.view.ViewGroup; |
Felipe Leme | c01a873 | 2017-02-22 17:26:06 -0800 | [diff] [blame] | 29 | import android.view.ViewStructure; |
sallyyuen | 69a146c | 2019-11-25 17:05:47 -0800 | [diff] [blame] | 30 | import android.view.accessibility.AccessibilityNodeInfo; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 31 | import android.view.autofill.AutofillManager; |
| 32 | import android.view.autofill.AutofillValue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 34 | import com.android.internal.R; |
| 35 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * <p>This class is used to create a multiple-exclusion scope for a set of radio |
| 39 | * buttons. Checking one radio button that belongs to a radio group unchecks |
| 40 | * any previously checked radio button within the same group.</p> |
| 41 | * |
| 42 | * <p>Intially, all of the radio buttons are unchecked. While it is not possible |
| 43 | * to uncheck a particular radio button, the radio group can be cleared to |
| 44 | * remove the checked state.</p> |
| 45 | * |
| 46 | * <p>The selection is identified by the unique id of the radio button as defined |
| 47 | * in the XML layout file.</p> |
| 48 | * |
| 49 | * <p><strong>XML Attributes</strong></p> |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 50 | * <p>See {@link android.R.styleable#RadioGroup RadioGroup Attributes}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | * {@link android.R.styleable#LinearLayout LinearLayout Attributes}, |
| 52 | * {@link android.R.styleable#ViewGroup ViewGroup Attributes}, |
| 53 | * {@link android.R.styleable#View View Attributes}</p> |
| 54 | * <p>Also see |
| 55 | * {@link android.widget.LinearLayout.LayoutParams LinearLayout.LayoutParams} |
| 56 | * for layout attributes.</p> |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 57 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | * @see RadioButton |
| 59 | * |
| 60 | */ |
| 61 | public class RadioGroup extends LinearLayout { |
Philip P. Moltmann | 9668903 | 2017-03-09 13:19:55 -0800 | [diff] [blame] | 62 | private static final String LOG_TAG = RadioGroup.class.getSimpleName(); |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 63 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | // holds the checked id; the selection is empty by default |
| 65 | private int mCheckedId = -1; |
| 66 | // tracks children radio buttons checked state |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 67 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | private CompoundButton.OnCheckedChangeListener mChildOnCheckedChangeListener; |
| 69 | // when true, mOnCheckedChangeListener discards events |
| 70 | private boolean mProtectFromCheckedChange = false; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 71 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | private OnCheckedChangeListener mOnCheckedChangeListener; |
| 73 | private PassThroughHierarchyChangeListener mPassThroughListener; |
| 74 | |
Felipe Leme | c01a873 | 2017-02-22 17:26:06 -0800 | [diff] [blame] | 75 | // Indicates whether the child was set from resources or dynamically, so it can be used |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 76 | // to sanitize autofill requests. |
Felipe Leme | c01a873 | 2017-02-22 17:26:06 -0800 | [diff] [blame] | 77 | private int mInitialCheckedId = View.NO_ID; |
| 78 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | /** |
| 80 | * {@inheritDoc} |
| 81 | */ |
| 82 | public RadioGroup(Context context) { |
| 83 | super(context); |
| 84 | setOrientation(VERTICAL); |
| 85 | init(); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * {@inheritDoc} |
| 90 | */ |
| 91 | public RadioGroup(Context context, AttributeSet attrs) { |
| 92 | super(context, attrs); |
| 93 | |
Felipe Leme | d04a697 | 2017-03-02 12:56:18 -0800 | [diff] [blame] | 94 | // RadioGroup is important by default, unless app developer overrode attribute. |
| 95 | if (getImportantForAutofill() == IMPORTANT_FOR_AUTOFILL_AUTO) { |
| 96 | setImportantForAutofill(IMPORTANT_FOR_AUTOFILL_YES); |
| 97 | } |
sallyyuen | 69a146c | 2019-11-25 17:05:47 -0800 | [diff] [blame] | 98 | setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES); |
Felipe Leme | d04a697 | 2017-03-02 12:56:18 -0800 | [diff] [blame] | 99 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | // retrieve selected radio button as requested by the user in the |
| 101 | // XML layout file |
| 102 | TypedArray attributes = context.obtainStyledAttributes( |
| 103 | attrs, com.android.internal.R.styleable.RadioGroup, com.android.internal.R.attr.radioButtonStyle, 0); |
Aurimas Liutikas | ab324cf | 2019-02-07 16:46:38 -0800 | [diff] [blame] | 104 | saveAttributeDataForStyleable(context, com.android.internal.R.styleable.RadioGroup, |
| 105 | attrs, attributes, com.android.internal.R.attr.radioButtonStyle, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | |
| 107 | int value = attributes.getResourceId(R.styleable.RadioGroup_checkedButton, View.NO_ID); |
| 108 | if (value != View.NO_ID) { |
| 109 | mCheckedId = value; |
Felipe Leme | c01a873 | 2017-02-22 17:26:06 -0800 | [diff] [blame] | 110 | mInitialCheckedId = value; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | final int index = attributes.getInt(com.android.internal.R.styleable.RadioGroup_orientation, VERTICAL); |
| 113 | setOrientation(index); |
| 114 | |
| 115 | attributes.recycle(); |
| 116 | init(); |
| 117 | } |
| 118 | |
| 119 | private void init() { |
| 120 | mChildOnCheckedChangeListener = new CheckedStateTracker(); |
| 121 | mPassThroughListener = new PassThroughHierarchyChangeListener(); |
| 122 | super.setOnHierarchyChangeListener(mPassThroughListener); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * {@inheritDoc} |
| 127 | */ |
| 128 | @Override |
| 129 | public void setOnHierarchyChangeListener(OnHierarchyChangeListener listener) { |
| 130 | // the user listener is delegated to our pass-through listener |
| 131 | mPassThroughListener.mOnHierarchyChangeListener = listener; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * {@inheritDoc} |
| 136 | */ |
| 137 | @Override |
| 138 | protected void onFinishInflate() { |
| 139 | super.onFinishInflate(); |
| 140 | |
| 141 | // checks the appropriate radio button as requested in the XML file |
| 142 | if (mCheckedId != -1) { |
| 143 | mProtectFromCheckedChange = true; |
| 144 | setCheckedStateForView(mCheckedId, true); |
| 145 | mProtectFromCheckedChange = false; |
| 146 | setCheckedId(mCheckedId); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public void addView(View child, int index, ViewGroup.LayoutParams params) { |
| 152 | if (child instanceof RadioButton) { |
| 153 | final RadioButton button = (RadioButton) child; |
| 154 | if (button.isChecked()) { |
| 155 | mProtectFromCheckedChange = true; |
| 156 | if (mCheckedId != -1) { |
| 157 | setCheckedStateForView(mCheckedId, false); |
| 158 | } |
| 159 | mProtectFromCheckedChange = false; |
| 160 | setCheckedId(button.getId()); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | super.addView(child, index, params); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * <p>Sets the selection to the radio button whose identifier is passed in |
| 169 | * parameter. Using -1 as the selection identifier clears the selection; |
| 170 | * such an operation is equivalent to invoking {@link #clearCheck()}.</p> |
| 171 | * |
| 172 | * @param id the unique id of the radio button to select in this group |
| 173 | * |
| 174 | * @see #getCheckedRadioButtonId() |
| 175 | * @see #clearCheck() |
| 176 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 177 | public void check(@IdRes int id) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | // don't even bother |
| 179 | if (id != -1 && (id == mCheckedId)) { |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | if (mCheckedId != -1) { |
| 184 | setCheckedStateForView(mCheckedId, false); |
| 185 | } |
| 186 | |
| 187 | if (id != -1) { |
| 188 | setCheckedStateForView(id, true); |
| 189 | } |
| 190 | |
| 191 | setCheckedId(id); |
| 192 | } |
| 193 | |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 194 | private void setCheckedId(@IdRes int id) { |
Felipe Leme | 27d0446 | 2018-02-14 15:08:58 -0800 | [diff] [blame] | 195 | boolean changed = id != mCheckedId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | mCheckedId = id; |
Felipe Leme | 27d0446 | 2018-02-14 15:08:58 -0800 | [diff] [blame] | 197 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | if (mOnCheckedChangeListener != null) { |
| 199 | mOnCheckedChangeListener.onCheckedChanged(this, mCheckedId); |
| 200 | } |
Felipe Leme | 27d0446 | 2018-02-14 15:08:58 -0800 | [diff] [blame] | 201 | if (changed) { |
| 202 | final AutofillManager afm = mContext.getSystemService(AutofillManager.class); |
| 203 | if (afm != null) { |
| 204 | afm.notifyValueChanged(this); |
| 205 | } |
Felipe Leme | 5882c4f | 2017-02-16 21:46:46 -0800 | [diff] [blame] | 206 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | private void setCheckedStateForView(int viewId, boolean checked) { |
| 210 | View checkedView = findViewById(viewId); |
| 211 | if (checkedView != null && checkedView instanceof RadioButton) { |
| 212 | ((RadioButton) checkedView).setChecked(checked); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * <p>Returns the identifier of the selected radio button in this group. |
| 218 | * Upon empty selection, the returned value is -1.</p> |
| 219 | * |
| 220 | * @return the unique id of the selected radio button in this group |
| 221 | * |
| 222 | * @see #check(int) |
| 223 | * @see #clearCheck() |
Philip Milne | aac722a | 2012-03-26 13:30:26 -0700 | [diff] [blame] | 224 | * |
| 225 | * @attr ref android.R.styleable#RadioGroup_checkedButton |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 227 | @IdRes |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | public int getCheckedRadioButtonId() { |
| 229 | return mCheckedId; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * <p>Clears the selection. When the selection is cleared, no radio button |
| 234 | * in this group is selected and {@link #getCheckedRadioButtonId()} returns |
| 235 | * null.</p> |
| 236 | * |
| 237 | * @see #check(int) |
| 238 | * @see #getCheckedRadioButtonId() |
| 239 | */ |
| 240 | public void clearCheck() { |
| 241 | check(-1); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * <p>Register a callback to be invoked when the checked radio button |
| 246 | * changes in this group.</p> |
| 247 | * |
| 248 | * @param listener the callback to call on checked state change |
| 249 | */ |
| 250 | public void setOnCheckedChangeListener(OnCheckedChangeListener listener) { |
| 251 | mOnCheckedChangeListener = listener; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * {@inheritDoc} |
| 256 | */ |
| 257 | @Override |
| 258 | public LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 259 | return new RadioGroup.LayoutParams(getContext(), attrs); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * {@inheritDoc} |
| 264 | */ |
| 265 | @Override |
| 266 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 267 | return p instanceof RadioGroup.LayoutParams; |
| 268 | } |
| 269 | |
| 270 | @Override |
| 271 | protected LinearLayout.LayoutParams generateDefaultLayoutParams() { |
| 272 | return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); |
| 273 | } |
| 274 | |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 275 | @Override |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 276 | public CharSequence getAccessibilityClassName() { |
| 277 | return RadioGroup.class.getName(); |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 278 | } |
| 279 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 | /** |
| 281 | * <p>This set of layout parameters defaults the width and the height of |
| 282 | * the children to {@link #WRAP_CONTENT} when they are not specified in the |
| 283 | * XML file. Otherwise, this class ussed the value read from the XML file.</p> |
| 284 | * |
| 285 | * <p>See |
| 286 | * {@link android.R.styleable#LinearLayout_Layout LinearLayout Attributes} |
| 287 | * for a list of all child view attributes that this class supports.</p> |
| 288 | * |
| 289 | */ |
| 290 | public static class LayoutParams extends LinearLayout.LayoutParams { |
| 291 | /** |
| 292 | * {@inheritDoc} |
| 293 | */ |
| 294 | public LayoutParams(Context c, AttributeSet attrs) { |
| 295 | super(c, attrs); |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * {@inheritDoc} |
| 300 | */ |
| 301 | public LayoutParams(int w, int h) { |
| 302 | super(w, h); |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * {@inheritDoc} |
| 307 | */ |
| 308 | public LayoutParams(int w, int h, float initWeight) { |
| 309 | super(w, h, initWeight); |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * {@inheritDoc} |
| 314 | */ |
| 315 | public LayoutParams(ViewGroup.LayoutParams p) { |
| 316 | super(p); |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * {@inheritDoc} |
| 321 | */ |
| 322 | public LayoutParams(MarginLayoutParams source) { |
| 323 | super(source); |
| 324 | } |
Dave Burke | 579e140 | 2012-10-18 20:41:55 -0700 | [diff] [blame] | 325 | |
| 326 | /** |
| 327 | * <p>Fixes the child's width to |
| 328 | * {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT} and the child's |
| 329 | * height to {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT} |
| 330 | * when not specified in the XML file.</p> |
| 331 | * |
| 332 | * @param a the styled attributes set |
| 333 | * @param widthAttr the width attribute to fetch |
| 334 | * @param heightAttr the height attribute to fetch |
| 335 | */ |
| 336 | @Override |
| 337 | protected void setBaseAttributes(TypedArray a, |
| 338 | int widthAttr, int heightAttr) { |
| 339 | |
| 340 | if (a.hasValue(widthAttr)) { |
| 341 | width = a.getLayoutDimension(widthAttr, "layout_width"); |
| 342 | } else { |
| 343 | width = WRAP_CONTENT; |
| 344 | } |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 345 | |
Dave Burke | 579e140 | 2012-10-18 20:41:55 -0700 | [diff] [blame] | 346 | if (a.hasValue(heightAttr)) { |
| 347 | height = a.getLayoutDimension(heightAttr, "layout_height"); |
| 348 | } else { |
| 349 | height = WRAP_CONTENT; |
| 350 | } |
| 351 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /** |
| 355 | * <p>Interface definition for a callback to be invoked when the checked |
| 356 | * radio button changed in this group.</p> |
| 357 | */ |
| 358 | public interface OnCheckedChangeListener { |
| 359 | /** |
| 360 | * <p>Called when the checked radio button has changed. When the |
| 361 | * selection is cleared, checkedId is -1.</p> |
| 362 | * |
| 363 | * @param group the group in which the checked radio button has changed |
| 364 | * @param checkedId the unique identifier of the newly checked radio button |
| 365 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 366 | public void onCheckedChanged(RadioGroup group, @IdRes int checkedId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | private class CheckedStateTracker implements CompoundButton.OnCheckedChangeListener { |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 370 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 371 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
| 372 | // prevents from infinite recursion |
| 373 | if (mProtectFromCheckedChange) { |
| 374 | return; |
| 375 | } |
| 376 | |
| 377 | mProtectFromCheckedChange = true; |
| 378 | if (mCheckedId != -1) { |
| 379 | setCheckedStateForView(mCheckedId, false); |
| 380 | } |
| 381 | mProtectFromCheckedChange = false; |
| 382 | |
| 383 | int id = buttonView.getId(); |
| 384 | setCheckedId(id); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * <p>A pass-through listener acts upon the events and dispatches them |
| 390 | * to another listener. This allows the table layout to set its own internal |
| 391 | * hierarchy change listener without preventing the user to setup his.</p> |
| 392 | */ |
| 393 | private class PassThroughHierarchyChangeListener implements |
| 394 | ViewGroup.OnHierarchyChangeListener { |
| 395 | private ViewGroup.OnHierarchyChangeListener mOnHierarchyChangeListener; |
| 396 | |
| 397 | /** |
| 398 | * {@inheritDoc} |
| 399 | */ |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 400 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | public void onChildViewAdded(View parent, View child) { |
| 402 | if (parent == RadioGroup.this && child instanceof RadioButton) { |
| 403 | int id = child.getId(); |
| 404 | // generates an id if it's missing |
| 405 | if (id == View.NO_ID) { |
Adam Powell | a9108a2 | 2012-07-18 11:18:09 -0700 | [diff] [blame] | 406 | id = View.generateViewId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 407 | child.setId(id); |
| 408 | } |
| 409 | ((RadioButton) child).setOnCheckedChangeWidgetListener( |
| 410 | mChildOnCheckedChangeListener); |
| 411 | } |
| 412 | |
| 413 | if (mOnHierarchyChangeListener != null) { |
| 414 | mOnHierarchyChangeListener.onChildViewAdded(parent, child); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * {@inheritDoc} |
| 420 | */ |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 421 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | public void onChildViewRemoved(View parent, View child) { |
| 423 | if (parent == RadioGroup.this && child instanceof RadioButton) { |
| 424 | ((RadioButton) child).setOnCheckedChangeWidgetListener(null); |
| 425 | } |
| 426 | |
| 427 | if (mOnHierarchyChangeListener != null) { |
| 428 | mOnHierarchyChangeListener.onChildViewRemoved(parent, child); |
| 429 | } |
| 430 | } |
| 431 | } |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 432 | |
Felipe Leme | 92736c1 | 2018-11-13 12:00:59 -0800 | [diff] [blame] | 433 | /** @hide */ |
Felipe Leme | 0200d9e | 2017-01-24 15:10:26 -0800 | [diff] [blame] | 434 | @Override |
Felipe Leme | 92736c1 | 2018-11-13 12:00:59 -0800 | [diff] [blame] | 435 | protected void onProvideStructure(@NonNull ViewStructure structure, |
| 436 | @ViewStructureType int viewFor, int flags) { |
| 437 | super.onProvideStructure(structure, viewFor, flags); |
| 438 | |
| 439 | if (viewFor == VIEW_STRUCTURE_FOR_AUTOFILL) { |
| 440 | structure.setDataIsSensitive(mCheckedId != mInitialCheckedId); |
| 441 | } |
Felipe Leme | c01a873 | 2017-02-22 17:26:06 -0800 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | @Override |
Felipe Leme | 955e252 | 2017-03-29 17:47:58 -0700 | [diff] [blame] | 445 | public void autofill(AutofillValue value) { |
| 446 | if (!isEnabled()) return; |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 447 | |
Felipe Leme | 955e252 | 2017-03-29 17:47:58 -0700 | [diff] [blame] | 448 | if (!value.isList()) { |
Philip P. Moltmann | 9668903 | 2017-03-09 13:19:55 -0800 | [diff] [blame] | 449 | Log.w(LOG_TAG, value + " could not be autofilled into " + this); |
Felipe Leme | 955e252 | 2017-03-29 17:47:58 -0700 | [diff] [blame] | 450 | return; |
Philip P. Moltmann | 9668903 | 2017-03-09 13:19:55 -0800 | [diff] [blame] | 451 | } |
| 452 | |
Felipe Leme | 955e252 | 2017-03-29 17:47:58 -0700 | [diff] [blame] | 453 | final int index = value.getListValue(); |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 454 | final View child = getChildAt(index); |
| 455 | if (child == null) { |
| 456 | Log.w(VIEW_LOG_TAG, "RadioGroup.autoFill(): no child with index " + index); |
Felipe Leme | 955e252 | 2017-03-29 17:47:58 -0700 | [diff] [blame] | 457 | return; |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 458 | } |
Felipe Leme | 955e252 | 2017-03-29 17:47:58 -0700 | [diff] [blame] | 459 | |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 460 | check(child.getId()); |
| 461 | } |
| 462 | |
| 463 | @Override |
Felipe Leme | 8931e30 | 2017-03-06 13:44:35 -0800 | [diff] [blame] | 464 | public @AutofillType int getAutofillType() { |
| 465 | return isEnabled() ? AUTOFILL_TYPE_LIST : AUTOFILL_TYPE_NONE; |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 466 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 467 | |
| 468 | @Override |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 469 | public AutofillValue getAutofillValue() { |
Felipe Leme | d09ccb8 | 2017-02-22 15:02:03 -0800 | [diff] [blame] | 470 | if (!isEnabled()) return null; |
| 471 | |
| 472 | final int count = getChildCount(); |
| 473 | for (int i = 0; i < count; i++) { |
| 474 | final View child = getChildAt(i); |
| 475 | if (child.getId() == mCheckedId) { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 476 | return AutofillValue.forList(i); |
Felipe Leme | d09ccb8 | 2017-02-22 15:02:03 -0800 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | return null; |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 480 | } |
sallyyuen | 69a146c | 2019-11-25 17:05:47 -0800 | [diff] [blame] | 481 | |
| 482 | @Override |
| 483 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 484 | super.onInitializeAccessibilityNodeInfo(info); |
| 485 | if (this.getOrientation() == HORIZONTAL) { |
| 486 | info.setCollectionInfo(AccessibilityNodeInfo.CollectionInfo.obtain(1, |
| 487 | getVisibleChildCount(), false, |
| 488 | AccessibilityNodeInfo.CollectionInfo.SELECTION_MODE_SINGLE)); |
| 489 | } else { |
| 490 | info.setCollectionInfo( |
| 491 | AccessibilityNodeInfo.CollectionInfo.obtain(getVisibleChildCount(), |
| 492 | 1, false, |
| 493 | AccessibilityNodeInfo.CollectionInfo.SELECTION_MODE_SINGLE)); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | private int getVisibleChildCount() { |
| 498 | int count = 0; |
| 499 | for (int i = 0; i < getChildCount(); i++) { |
| 500 | if (this.getChildAt(i) instanceof RadioButton) { |
| 501 | if (((RadioButton) this.getChildAt(i)).getVisibility() == VISIBLE) { |
| 502 | count++; |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | return count; |
| 507 | } |
| 508 | |
| 509 | int getIndexWithinVisibleButtons(@Nullable View child) { |
| 510 | if (!(child instanceof RadioButton)) { |
| 511 | return -1; |
| 512 | } |
| 513 | int index = 0; |
| 514 | for (int i = 0; i < getChildCount(); i++) { |
| 515 | if (this.getChildAt(i) instanceof RadioButton) { |
| 516 | RadioButton radioButton = (RadioButton) this.getChildAt(i); |
| 517 | if (radioButton == child) { |
| 518 | return index; |
| 519 | } |
| 520 | if (radioButton.getVisibility() == VISIBLE) { |
| 521 | index++; |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | return -1; |
| 526 | } |
| 527 | } |