Unify action buttons to a common widget (part 1)
- Create a preference type to host action button preferences. - Convert app battery usage page to use the new preference - Convert wifi detail page to use the new preference Change-Id: I60bc473986ab46f2897e2537eb0e013fa813a4fc Fix: 63991885 Test: robotests
This commit is contained in:
@@ -37,23 +37,22 @@ import android.os.RemoteException;
|
||||
import android.os.ServiceManager;
|
||||
import android.os.UserHandle;
|
||||
import android.os.UserManager;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.webkit.IWebViewUpdateService;
|
||||
import android.widget.Button;
|
||||
|
||||
import com.android.internal.annotations.VisibleForTesting;
|
||||
import com.android.internal.logging.nano.MetricsProto;
|
||||
import com.android.settings.DeviceAdminAdd;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.SettingsActivity;
|
||||
import com.android.settings.Utils;
|
||||
import com.android.settings.applications.LayoutPreference;
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
import com.android.settings.core.instrumentation.MetricsFeatureProvider;
|
||||
import com.android.settings.enterprise.DevicePolicyManagerWrapper;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
import com.android.settings.widget.ActionButtonPreference;
|
||||
import com.android.settingslib.RestrictedLockUtils;
|
||||
import com.android.settingslib.applications.AppUtils;
|
||||
import com.android.settingslib.applications.ApplicationsState;
|
||||
@@ -79,7 +78,7 @@ import java.util.List;
|
||||
//TODO(b/35810915): Make InstalledAppDetails use this controller
|
||||
public class AppButtonsPreferenceController extends AbstractPreferenceController implements
|
||||
PreferenceControllerMixin, LifecycleObserver, OnResume, OnPause, OnDestroy,
|
||||
View.OnClickListener, ApplicationsState.Callbacks {
|
||||
ApplicationsState.Callbacks {
|
||||
public static final String APP_CHG = "chg";
|
||||
|
||||
private static final String TAG = "AppButtonsPrefCtl";
|
||||
@@ -95,13 +94,11 @@ public class AppButtonsPreferenceController extends AbstractPreferenceController
|
||||
@VisibleForTesting
|
||||
PackageInfo mPackageInfo;
|
||||
@VisibleForTesting
|
||||
Button mForceStopButton;
|
||||
@VisibleForTesting
|
||||
Button mUninstallButton;
|
||||
@VisibleForTesting
|
||||
String mPackageName;
|
||||
@VisibleForTesting
|
||||
boolean mDisableAfterUninstall = false;
|
||||
@VisibleForTesting
|
||||
ActionButtonPreference mButtonsPref;
|
||||
|
||||
private final int mRequestUninstall;
|
||||
private final int mRequestRemoveDeviceAdmin;
|
||||
@@ -115,7 +112,6 @@ public class AppButtonsPreferenceController extends AbstractPreferenceController
|
||||
private RestrictedLockUtils.EnforcedAdmin mAppsControlDisallowedAdmin;
|
||||
private MetricsFeatureProvider mMetricsFeatureProvider;
|
||||
|
||||
private LayoutPreference mButtonsPref;
|
||||
private int mUserId;
|
||||
private boolean mUpdatedSysApp = false;
|
||||
private boolean mListeningToPackageRemove = false;
|
||||
@@ -165,14 +161,14 @@ public class AppButtonsPreferenceController extends AbstractPreferenceController
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
if (isAvailable()) {
|
||||
mButtonsPref = (LayoutPreference) screen.findPreference(KEY_ACTION_BUTTONS);
|
||||
|
||||
mUninstallButton = (Button) mButtonsPref.findViewById(R.id.left_button);
|
||||
mUninstallButton.setText(R.string.uninstall_text);
|
||||
|
||||
mForceStopButton = (Button) mButtonsPref.findViewById(R.id.right_button);
|
||||
mForceStopButton.setText(R.string.force_stop);
|
||||
mForceStopButton.setEnabled(false);
|
||||
mButtonsPref = ((ActionButtonPreference) screen.findPreference(KEY_ACTION_BUTTONS))
|
||||
.setButton1Text(R.string.uninstall_text)
|
||||
.setButton2Text(R.string.force_stop)
|
||||
.setButton1OnClickListener(new UninstallAndDisableButtonListener())
|
||||
.setButton2OnClickListener(new ForceStopButtonListener())
|
||||
.setButton1Positive(false)
|
||||
.setButton2Positive(false)
|
||||
.setButton2Enabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,11 +203,11 @@ public class AppButtonsPreferenceController extends AbstractPreferenceController
|
||||
mSession.release();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
final String packageName = mAppEntry.info.packageName;
|
||||
final int id = v.getId();
|
||||
if (id == R.id.left_button) {
|
||||
private class UninstallAndDisableButtonListener implements View.OnClickListener {
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
final String packageName = mAppEntry.info.packageName;
|
||||
// Uninstall
|
||||
if (mDpm.packageHasActiveAdmins(mPackageInfo.packageName)) {
|
||||
stopListeningToPackageRemove();
|
||||
@@ -254,7 +250,13 @@ public class AppButtonsPreferenceController extends AbstractPreferenceController
|
||||
} else {
|
||||
uninstallPkg(packageName, false, false);
|
||||
}
|
||||
} else if (id == R.id.right_button) {
|
||||
}
|
||||
}
|
||||
|
||||
private class ForceStopButtonListener implements View.OnClickListener {
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
// force stop
|
||||
if (mAppsControlDisallowedAdmin != null && !mAppsControlDisallowedBySystem) {
|
||||
RestrictedLockUtils.sendShowAdminSupportDetailsIntent(
|
||||
@@ -363,7 +365,7 @@ public class AppButtonsPreferenceController extends AbstractPreferenceController
|
||||
final boolean isBundled = (mAppEntry.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
|
||||
boolean enabled = true;
|
||||
if (isBundled) {
|
||||
enabled = handleDisableable(mUninstallButton);
|
||||
enabled = handleDisableable();
|
||||
} else {
|
||||
if ((mPackageInfo.applicationInfo.flags & ApplicationInfo.FLAG_INSTALLED) == 0
|
||||
&& mUserManager.getUsers().size() >= 2) {
|
||||
@@ -428,11 +430,7 @@ public class AppButtonsPreferenceController extends AbstractPreferenceController
|
||||
enabled = false;
|
||||
}
|
||||
|
||||
mUninstallButton.setEnabled(enabled);
|
||||
if (enabled) {
|
||||
// Register listener
|
||||
mUninstallButton.setOnClickListener(this);
|
||||
}
|
||||
mButtonsPref.setButton1Enabled(enabled);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -477,12 +475,12 @@ public class AppButtonsPreferenceController extends AbstractPreferenceController
|
||||
if (mDpm.packageHasActiveAdmins(mPackageInfo.packageName)) {
|
||||
// User can't force stop device admin.
|
||||
Log.w(TAG, "User can't force stop device admin");
|
||||
updateForceStopButtonInner(false);
|
||||
updateForceStopButtonInner(false /* enabled */);
|
||||
} else if ((mAppEntry.info.flags & ApplicationInfo.FLAG_STOPPED) == 0) {
|
||||
// If the app isn't explicitly stopped, then always show the
|
||||
// force stop button.
|
||||
Log.w(TAG, "App is not explicitly stopped");
|
||||
updateForceStopButtonInner(true);
|
||||
updateForceStopButtonInner(true /* enabled */);
|
||||
} else {
|
||||
Intent intent = new Intent(Intent.ACTION_QUERY_PACKAGE_RESTART,
|
||||
Uri.fromParts("package", mAppEntry.info.packageName, null));
|
||||
@@ -499,10 +497,9 @@ public class AppButtonsPreferenceController extends AbstractPreferenceController
|
||||
@VisibleForTesting
|
||||
void updateForceStopButtonInner(boolean enabled) {
|
||||
if (mAppsControlDisallowedBySystem) {
|
||||
mForceStopButton.setEnabled(false);
|
||||
mButtonsPref.setButton2Enabled(false);
|
||||
} else {
|
||||
mForceStopButton.setEnabled(enabled);
|
||||
mForceStopButton.setOnClickListener(this);
|
||||
mButtonsPref.setButton2Enabled(enabled);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -538,7 +535,7 @@ public class AppButtonsPreferenceController extends AbstractPreferenceController
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
boolean handleDisableable(Button button) {
|
||||
boolean handleDisableable() {
|
||||
boolean disableable = false;
|
||||
// Try to prevent the user from bricking their phone
|
||||
// by not allowing disabling of apps signed with the
|
||||
@@ -546,12 +543,16 @@ public class AppButtonsPreferenceController extends AbstractPreferenceController
|
||||
if (mHomePackages.contains(mAppEntry.info.packageName)
|
||||
|| isSystemPackage(mActivity.getResources(), mPm, mPackageInfo)) {
|
||||
// Disable button for core system applications.
|
||||
button.setText(R.string.disable_text);
|
||||
mButtonsPref.setButton1Text(R.string.disable_text)
|
||||
.setButton1Positive(false);
|
||||
|
||||
} else if (mAppEntry.info.enabled && !isDisabledUntilUsed()) {
|
||||
button.setText(R.string.disable_text);
|
||||
mButtonsPref.setButton1Text(R.string.disable_text)
|
||||
.setButton1Positive(false);
|
||||
disableable = true;
|
||||
} else {
|
||||
button.setText(R.string.enable_text);
|
||||
mButtonsPref.setButton1Text(R.string.enable_text)
|
||||
.setButton1Positive(true);
|
||||
disableable = true;
|
||||
}
|
||||
|
||||
|
||||
186
src/com/android/settings/widget/ActionButtonPreference.java
Normal file
186
src/com/android/settings/widget/ActionButtonPreference.java
Normal file
@@ -0,0 +1,186 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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.widget;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.StringRes;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.PreferenceViewHolder;
|
||||
import android.text.TextUtils;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
|
||||
import com.android.settings.R;
|
||||
|
||||
public class ActionButtonPreference extends Preference {
|
||||
|
||||
private final ButtonInfo mButton1Info = new ButtonInfo();
|
||||
private final ButtonInfo mButton2Info = new ButtonInfo();
|
||||
|
||||
public ActionButtonPreference(Context context, AttributeSet attrs,
|
||||
int defStyleAttr, int defStyleRes) {
|
||||
super(context, attrs, defStyleAttr, defStyleRes);
|
||||
init();
|
||||
}
|
||||
|
||||
public ActionButtonPreference(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
init();
|
||||
}
|
||||
|
||||
public ActionButtonPreference(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
init();
|
||||
}
|
||||
|
||||
public ActionButtonPreference(Context context) {
|
||||
super(context);
|
||||
init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
setLayoutResource(R.layout.two_action_buttons);
|
||||
setSelectable(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(PreferenceViewHolder holder) {
|
||||
super.onBindViewHolder(holder);
|
||||
holder.setDividerAllowedAbove(false);
|
||||
holder.setDividerAllowedBelow(false);
|
||||
mButton1Info.mPositiveButton = (Button) holder.findViewById(R.id.button1_positive);
|
||||
mButton1Info.mNegativeButton = (Button) holder.findViewById(R.id.button1_negative);
|
||||
mButton2Info.mPositiveButton = (Button) holder.findViewById(R.id.button2_positive);
|
||||
mButton2Info.mNegativeButton = (Button) holder.findViewById(R.id.button2_negative);
|
||||
|
||||
mButton1Info.setUpButton();
|
||||
mButton2Info.setUpButton();
|
||||
}
|
||||
|
||||
public ActionButtonPreference setButton1Text(@StringRes int textResId) {
|
||||
final String newText = getContext().getString(textResId);
|
||||
if (!TextUtils.equals(newText, mButton1Info.mText)) {
|
||||
mButton1Info.mText = newText;
|
||||
notifyChanged();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public ActionButtonPreference setButton1Enabled(boolean isEnabled) {
|
||||
if (isEnabled != mButton1Info.mIsEnabled) {
|
||||
mButton1Info.mIsEnabled = isEnabled;
|
||||
notifyChanged();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public ActionButtonPreference setButton2Text(@StringRes int textResId) {
|
||||
final String newText = getContext().getString(textResId);
|
||||
if (!TextUtils.equals(newText, mButton2Info.mText)) {
|
||||
mButton2Info.mText = newText;
|
||||
notifyChanged();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public ActionButtonPreference setButton2Enabled(boolean isEnabled) {
|
||||
if (isEnabled != mButton2Info.mIsEnabled) {
|
||||
mButton2Info.mIsEnabled = isEnabled;
|
||||
notifyChanged();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public ActionButtonPreference setButton1OnClickListener(View.OnClickListener listener) {
|
||||
if (listener != mButton1Info.mListener) {
|
||||
mButton1Info.mListener = listener;
|
||||
notifyChanged();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public ActionButtonPreference setButton2OnClickListener(View.OnClickListener listener) {
|
||||
if (listener != mButton2Info.mListener) {
|
||||
mButton2Info.mListener = listener;
|
||||
notifyChanged();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public ActionButtonPreference setButton1Positive(boolean isPositive) {
|
||||
if (isPositive != mButton1Info.mIsPositive) {
|
||||
mButton1Info.mIsPositive = isPositive;
|
||||
notifyChanged();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public ActionButtonPreference setButton2Positive(boolean isPositive) {
|
||||
if (isPositive != mButton2Info.mIsPositive) {
|
||||
mButton2Info.mIsPositive = isPositive;
|
||||
notifyChanged();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
public ActionButtonPreference setButton1Visible(boolean isPositive) {
|
||||
if (isPositive != mButton1Info.mIsVisible) {
|
||||
mButton1Info.mIsVisible = isPositive;
|
||||
notifyChanged();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public ActionButtonPreference setButton2Visible(boolean isPositive) {
|
||||
if (isPositive != mButton2Info.mIsVisible) {
|
||||
mButton2Info.mIsVisible = isPositive;
|
||||
notifyChanged();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
static class ButtonInfo {
|
||||
private Button mPositiveButton;
|
||||
private Button mNegativeButton;
|
||||
private CharSequence mText;
|
||||
private View.OnClickListener mListener;
|
||||
private boolean mIsPositive = true;
|
||||
private boolean mIsEnabled = true;
|
||||
private boolean mIsVisible = true;
|
||||
|
||||
void setUpButton() {
|
||||
setUpButton(mPositiveButton);
|
||||
setUpButton(mNegativeButton);
|
||||
if (!mIsVisible) {
|
||||
mPositiveButton.setVisibility(View.INVISIBLE);
|
||||
mNegativeButton.setVisibility(View.INVISIBLE);
|
||||
} else if (mIsPositive) {
|
||||
mPositiveButton.setVisibility(View.VISIBLE);
|
||||
mNegativeButton.setVisibility(View.INVISIBLE);
|
||||
} else {
|
||||
mPositiveButton.setVisibility(View.INVISIBLE);
|
||||
mNegativeButton.setVisibility(View.VISIBLE);
|
||||
}
|
||||
}
|
||||
|
||||
private void setUpButton(Button button) {
|
||||
button.setText(mText);
|
||||
button.setOnClickListener(mListener);
|
||||
button.setEnabled(mIsEnabled);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -58,6 +58,7 @@ import com.android.settings.applications.LayoutPreference;
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
import com.android.settings.core.instrumentation.MetricsFeatureProvider;
|
||||
import com.android.settings.vpn2.ConnectivityManagerWrapper;
|
||||
import com.android.settings.widget.ActionButtonPreference;
|
||||
import com.android.settings.widget.EntityHeaderController;
|
||||
import com.android.settings.wifi.WifiDetailPreference;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
@@ -127,10 +128,8 @@ public class WifiDetailPreferenceController extends AbstractPreferenceController
|
||||
private final MetricsFeatureProvider mMetricsFeatureProvider;
|
||||
|
||||
// UI elements - in order of appearance
|
||||
private LayoutPreference mButtonsPref;
|
||||
private ActionButtonPreference mButtonsPref;
|
||||
private EntityHeaderController mEntityHeaderController;
|
||||
private Button mForgetButton;
|
||||
private Button mSignInButton;
|
||||
private WifiDetailPreference mSignalStrengthPref;
|
||||
private WifiDetailPreference mLinkSpeedPref;
|
||||
private WifiDetailPreference mFrequencyPref;
|
||||
@@ -244,9 +243,13 @@ public class WifiDetailPreferenceController extends AbstractPreferenceController
|
||||
|
||||
setupEntityHeader(screen);
|
||||
|
||||
mButtonsPref = (LayoutPreference) screen.findPreference(KEY_BUTTONS_PREF);
|
||||
mSignInButton = mButtonsPref.findViewById(R.id.signin_button);
|
||||
mSignInButton.setOnClickListener(view -> signIntoNetwork());
|
||||
mButtonsPref = ((ActionButtonPreference) screen.findPreference(KEY_BUTTONS_PREF))
|
||||
.setButton1Text(R.string.forget)
|
||||
.setButton1Positive(false)
|
||||
.setButton2Text(R.string.support_sign_in_button_text)
|
||||
.setButton1OnClickListener(view -> forgetNetwork())
|
||||
.setButton2Positive(true)
|
||||
.setButton2OnClickListener(view -> signIntoNetwork());
|
||||
|
||||
mSignalStrengthPref =
|
||||
(WifiDetailPreference) screen.findPreference(KEY_SIGNAL_STRENGTH_PREF);
|
||||
@@ -264,8 +267,6 @@ public class WifiDetailPreferenceController extends AbstractPreferenceController
|
||||
mIpv6AddressPref = screen.findPreference(KEY_IPV6_ADDRESSES_PREF);
|
||||
|
||||
mSecurityPref.setDetailText(mAccessPoint.getSecurityString(false /* concise */));
|
||||
mForgetButton = mButtonsPref.findViewById(R.id.forget_button);
|
||||
mForgetButton.setOnClickListener(view -> forgetNetwork());
|
||||
}
|
||||
|
||||
private void setupEntityHeader(PreferenceScreen screen) {
|
||||
@@ -318,7 +319,7 @@ public class WifiDetailPreferenceController extends AbstractPreferenceController
|
||||
}
|
||||
|
||||
// Update whether the forgot button should be displayed.
|
||||
mForgetButton.setVisibility(canForgetNetwork() ? View.VISIBLE : View.INVISIBLE);
|
||||
mButtonsPref.setButton1Visible(canForgetNetwork());
|
||||
|
||||
refreshNetworkState();
|
||||
|
||||
@@ -393,9 +394,8 @@ public class WifiDetailPreferenceController extends AbstractPreferenceController
|
||||
}
|
||||
|
||||
private void updateIpLayerInfo() {
|
||||
mSignInButton.setVisibility(canSignIntoNetwork() ? View.VISIBLE : View.INVISIBLE);
|
||||
mButtonsPref.setVisible(mForgetButton.getVisibility() == View.VISIBLE
|
||||
|| mSignInButton.getVisibility() == View.VISIBLE);
|
||||
mButtonsPref.setButton2Visible(canSignIntoNetwork());
|
||||
mButtonsPref.setVisible(canSignIntoNetwork() || canForgetNetwork());
|
||||
|
||||
if (mNetwork == null || mLinkProperties == null) {
|
||||
mIpAddressPref.setVisible(false);
|
||||
|
||||
Reference in New Issue
Block a user