Merge "[Expressive design] Update ApnPreference." into main

This commit is contained in:
Yuchen Sun
2024-12-16 22:16:32 -08:00
committed by Android (Google) Code Review
5 changed files with 33 additions and 136 deletions

View File

@@ -1,76 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2009 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeight"
android:focusable="false"
android:gravity="center_vertical">
<RelativeLayout
android:id="@+id/text_layout"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_weight="1"
android:focusable="true"
android:clickable="true"
android:background="?android:attr/selectableItemBackground">
<TextView
android:id="@android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:labelFor="@id/apn_radio_button_frame"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceListItem" />
<TextView
android:id="@android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@android:id/title"
android:layout_alignStart="@android:id/title"
android:textAppearance="?android:attr/textAppearanceListItemSecondary"
android:textColor="?android:attr/textColorSecondary"
android:focusable="false"
android:hyphenationFrequency="normalFast"
android:lineBreakWordStyle="phrase"
android:maxLines="2" />
</RelativeLayout>
<FrameLayout
android:id="@+id/apn_radio_button_frame"
android:layout_width="@dimen/min_tap_target_size"
android:layout_height="@dimen/min_tap_target_size"
android:layout_margin="8dp">
<RadioButton
android:id="@+id/apn_radiobutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:clickable="false"
android:focusable="false" />
</FrameLayout>
</LinearLayout>

View File

@@ -50,8 +50,6 @@
<attr name="android:textAppearance" />
</declare-styleable>
<attr name="apnPreferenceStyle" format="reference" />
<attr name="slicePreferenceStyle" format="reference" />
<attr name="cardPreferenceStyle" format="reference" />

View File

@@ -20,7 +20,6 @@
<resources>
<style name="SettingsPreferenceTheme" parent="@style/PreferenceTheme.SettingsLib">
<item name="apnPreferenceStyle">@style/ApnPreference</item>
<item name="cardPreferenceStyle">@style/CardPreference</item>
<item name="slicePreferenceStyle">@style/SlicePreference</item>
<item name="seekBarPreferenceStyle">@style/SettingsSeekBarPreference</item>
@@ -32,10 +31,6 @@
<item name="preferenceFragmentCompatStyle">@style/SetupWizardPreferenceFragmentStyle</item>
</style>
<style name="ApnPreference" parent="@style/Preference.Material">
<item name="android:layout">@layout/apn_preference_layout</item>
</style>
<style name="CardPreference" parent="@style/Preference.Material">
<item name="android:layout">@layout/card_preference_layout</item>
</style>

View File

@@ -20,7 +20,6 @@
<resources>
<style name="SettingsPreferenceTheme.Expressive" parent="@style/PreferenceTheme.SettingsLib.Expressive">
<item name="apnPreferenceStyle">@style/ApnPreference</item>
<item name="cardPreferenceStyle">@style/CardPreference</item>
<item name="slicePreferenceStyle">@style/SlicePreference</item>
<item name="seekBarPreferenceStyle">@style/SettingsSeekBarPreference</item>

View File

@@ -24,83 +24,67 @@ import android.content.Intent;
import android.net.Uri;
import android.provider.Telephony;
import android.telephony.SubscriptionManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;
import com.android.settings.R;
import com.android.settings.flags.Flags;
import com.android.settings.spa.SpaActivity;
import com.android.settingslib.widget.TwoTargetPreference;
/**
* Preference of APN UI entry
*/
public class ApnPreference extends Preference
implements CompoundButton.OnCheckedChangeListener, View.OnClickListener {
public class ApnPreference extends TwoTargetPreference
implements CompoundButton.OnCheckedChangeListener, Preference.OnPreferenceClickListener {
private static final String TAG = "ApnPreference";
private boolean mIsChecked = false;
@Nullable
private RadioButton mRadioButton = null;
private RadioButton mRadioButton;
private int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
private boolean mProtectFromCheckedChange = false;
private boolean mDefaultSelectable = true;
private boolean mHideDetails = false;
/**
* Constructor of Preference
*/
public ApnPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// Primary target and radio button could be selectable, but entire preference itself is not
// selectable.
setSelectable(false);
}
/**
* Constructor of Preference
*/
public ApnPreference(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.apnPreferenceStyle);
}
/**
* Constructor of Preference
*/
public ApnPreference(Context context) {
this(context, null);
super(context);
setOnPreferenceClickListener(this);
}
@Override
public void onBindViewHolder(PreferenceViewHolder view) {
super.onBindViewHolder(view);
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
final RelativeLayout textArea = (RelativeLayout) view.findViewById(R.id.text_layout);
textArea.setOnClickListener(this);
final View radioButtonFrame = view.itemView.requireViewById(R.id.apn_radio_button_frame);
final RadioButton rb = view.itemView.requireViewById(R.id.apn_radiobutton);
mRadioButton = rb;
if (mDefaultSelectable) {
radioButtonFrame.setOnClickListener((v) -> {
rb.performClick();
});
rb.setOnCheckedChangeListener(this);
mProtectFromCheckedChange = true;
rb.setChecked(mIsChecked);
mProtectFromCheckedChange = false;
radioButtonFrame.setVisibility(View.VISIBLE);
} else {
radioButtonFrame.setVisibility(View.GONE);
final RadioButton rb = (RadioButton) holder.findViewById(android.R.id.checkbox);
final View radioButtonFrame = holder.findViewById(android.R.id.widget_frame);
if (rb == null || radioButtonFrame == null) {
throw new RuntimeException("Failed to load system layout.");
}
mRadioButton = rb;
radioButtonFrame.setOnClickListener(v -> rb.performClick());
rb.setOnCheckedChangeListener(this);
setIsChecked(mIsChecked);
rb.setVisibility(View.VISIBLE);
}
@Override
protected boolean shouldHideSecondTarget() {
return !mDefaultSelectable;
}
@Override
protected int getSecondTargetResId() {
return R.layout.preference_widget_radiobutton;
}
/**
@@ -118,6 +102,7 @@ public class ApnPreference extends Preference
/**
* Change the preference status.
*/
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.i(TAG, "ID: " + getKey() + " :" + isChecked);
if (mProtectFromCheckedChange) {
@@ -130,19 +115,14 @@ public class ApnPreference extends Preference
}
@Override
public void onClick(View layoutView) {
super.onClick();
public boolean onPreferenceClick(@NonNull Preference preference) {
final Context context = getContext();
final int pos = Integer.parseInt(getKey());
if (context == null) {
Log.w(TAG, "No context available for pos=" + pos);
return;
}
if (mHideDetails) {
Toast.makeText(context, context.getString(R.string.cannot_change_apn_toast),
Toast.LENGTH_LONG).show();
return;
return true;
}
final Uri url = ContentUris.withAppendedId(Telephony.Carriers.CONTENT_URI, pos);
@@ -156,6 +136,7 @@ public class ApnPreference extends Preference
editIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(editIntent);
}
return true;
}
public void setDefaultSelectable(boolean defaultSelectable) {