Merge "Fix Wi-Fi Easy Connect buttons UI defects"

This commit is contained in:
Arc Wang
2019-03-20 01:53:24 +00:00
committed by Android (Google) Code Review
9 changed files with 124 additions and 257 deletions

View File

@@ -0,0 +1,83 @@
/*
* Copyright (C) 2019 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
*/
package com.android.settings.wifi;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import androidx.annotation.DrawableRes;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;
import com.android.settings.R;
import com.android.settings.wifi.dpp.WifiDppUtils;
/**
* The Preference for users to add Wi-Fi networks in WifiSettings
*/
public class AddWifiNetworkPreference extends Preference {
private static final String TAG = "AddWifiNetworkPreference";
private boolean mInitialized;
public AddWifiNetworkPreference(Context context) {
super(context);
setLayoutResource(com.android.settingslib.R.layout.preference_access_point);
setWidgetLayoutResource(R.layout.wifi_button_preference_widget);
setIcon(R.drawable.ic_menu_add);
setTitle(R.string.wifi_add_network);
}
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
if (!mInitialized) {
mInitialized = true;
final ImageButton imageButton = (ImageButton) holder.findViewById(R.id.button_icon);
imageButton.setImageDrawable(getDrawable(R.drawable.ic_scan_24dp));
imageButton.setContentDescription(
getContext().getString(R.string.wifi_dpp_scan_qr_code));
imageButton.setOnClickListener(view -> {
getContext().startActivity(
WifiDppUtils.getEnrolleeQrCodeScannerIntent(/* ssid */ null));
});
final View divider = (View) holder.findViewById(
com.android.settingslib.R.id.two_target_divider);
divider.setVisibility(View.INVISIBLE);
}
}
private Drawable getDrawable(@DrawableRes int iconResId) {
Drawable buttonIcon = null;
try {
buttonIcon = getContext().getDrawable(iconResId);
} catch (Resources.NotFoundException exception) {
Log.e(TAG, "Resource does not exist: " + iconResId);
}
return buttonIcon;
}
}

View File

@@ -1,135 +0,0 @@
/*
* Copyright (C) 2018 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
*/
package com.android.settings.wifi;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import androidx.annotation.DrawableRes;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;
import com.android.settings.R;
/**
* This preference provides one button layout with Settings style.
* It looks like below
*
* --------------------------------------------------------------
* | icon | title | button |
* --------------------------------------------------------------
*
* User can set icon / click listener for button.
* By default, the button is invisible.
*/
public class ButtonPreference extends Preference {
private static final String TAG = "ButtonPreference";
private ImageButton mImageButton;
private Drawable mButtonIcon;
private View.OnClickListener mClickListener;
private String mContentDescription;
// Used for dummy pref.
public ButtonPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setWidgetLayoutResource(R.layout.wifi_button_preference_widget);
mImageButton = null;
mButtonIcon = null;
mClickListener = null;
mContentDescription = null;
}
public ButtonPreference(Context context) {
this(context, /* attrs */ null);
}
@Override
public void onBindViewHolder(final PreferenceViewHolder view) {
super.onBindViewHolder(view);
initButton(view);
}
@Override
public void setOrder(int order) {
super.setOrder(order);
setButtonVisibility();
}
@VisibleForTesting
protected void initButton(final PreferenceViewHolder view) {
if (mImageButton == null) {
mImageButton = (ImageButton) view.findViewById(R.id.button_icon);
}
if (mImageButton != null) {
mImageButton.setImageDrawable(mButtonIcon);
mImageButton.setOnClickListener(mClickListener);
mImageButton.setContentDescription(mContentDescription);
}
setButtonVisibility();
}
private void setButtonVisibility() {
if(mImageButton != null) {
mImageButton.setVisibility(mButtonIcon == null ? View.GONE : View.VISIBLE);
}
}
/**
* Sets the drawable to be displayed in button.
*/
public void setButtonIcon(@DrawableRes int iconResId) {
if (iconResId == 0) {
return;
}
try {
mButtonIcon = getContext().getDrawable(iconResId);
notifyChanged();
} catch (Resources.NotFoundException exception) {
Log.e(TAG, "Resource does not exist: " + iconResId);
}
}
/**
* Register a callback to be invoked when button is clicked.
*/
public void setButtonOnClickListener(View.OnClickListener listener) {
if (listener != mClickListener) {
mClickListener = listener;
notifyChanged();
}
}
/**
* A content description briefly describes the button and is primarily used for accessibility
* support to determine how a button should be presented to the user.
*/
public void setButtonContentDescription(String contentDescription) {
if (contentDescription != mContentDescription) {
mContentDescription = contentDescription;
notifyChanged();
}
}
}

View File

@@ -179,7 +179,8 @@ public class WifiSettings extends RestrictedSettingsFragment
private PreferenceCategory mConnectedAccessPointPreferenceCategory;
private PreferenceCategory mAccessPointsPreferenceCategory;
private ButtonPreference mAddPreference;
@VisibleForTesting
AddWifiNetworkPreference mAddWifiNetworkPreference;
@VisibleForTesting
Preference mConfigureWifiSettingsPreference;
@VisibleForTesting
@@ -237,20 +238,8 @@ public class WifiSettings extends RestrictedSettingsFragment
(PreferenceCategory) findPreference(PREF_KEY_ACCESS_POINTS);
mConfigureWifiSettingsPreference = findPreference(PREF_KEY_CONFIGURE_WIFI_SETTINGS);
mSavedNetworksPreference = findPreference(PREF_KEY_SAVED_NETWORKS);
Context prefContext = getPrefContext();
mAddPreference = new ButtonPreference(prefContext);
mAddPreference.setIcon(R.drawable.ic_menu_add);
mAddPreference.setTitle(R.string.wifi_add_network);
mAddPreference.setButtonIcon(R.drawable.ic_scan_24dp);
mAddPreference.setButtonOnClickListener((View v) -> {
// Launch QR code scanner to join a network.
getContext().startActivity(
WifiDppUtils.getEnrolleeQrCodeScannerIntent(/* ssid */ null));
});
mAddPreference.setButtonContentDescription(getString(R.string.wifi_dpp_scan_qr_code));
mAddWifiNetworkPreference = new AddWifiNetworkPreference(getPrefContext());
mStatusMessagePreference = (LinkablePreference) findPreference(PREF_KEY_STATUS_MESSAGE);
mUserBadgeCache = new AccessPointPreference.UserBadgeCache(getPackageManager());
}
@@ -590,7 +579,7 @@ public class WifiSettings extends RestrictedSettingsFragment
showDialog(mSelectedAccessPoint, WifiConfigUiBase.MODE_CONNECT);
break;
}
} else if (preference == mAddPreference) {
} else if (preference == mAddWifiNetworkPreference) {
onAddNetworkPressed();
} else {
return super.onPreferenceTreeClick(preference);
@@ -809,8 +798,8 @@ public class WifiSettings extends RestrictedSettingsFragment
}
}
removeCachedPrefs(mAccessPointsPreferenceCategory);
mAddPreference.setOrder(index);
mAccessPointsPreferenceCategory.addPreference(mAddPreference);
mAddWifiNetworkPreference.setOrder(index);
mAccessPointsPreferenceCategory.addPreference(mAddWifiNetworkPreference);
setAdditionalSettingsSummaries();
if (!hasAvailableAccessPoints) {