恢复出厂设置需要添加密码输入才能点击恢复出厂设置

自定义输入密码框

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.android.settings;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.widget.EditText;

import static android.graphics.Paint.ANTI_ALIAS_FLAG;

public class PasswordInputView extends EditText {
//外边框颜色
private int borderColor = 0xFFCCCCCC;
//外边框线的粗细
private float borderWidth = 5;
//外边框圆角半径
private float borderRadius = 3;
//中间分割线粗细
private float dividerWidth = 3;
//密码长度,默认6个字符
private int passwordLength = 6;
//密码文字颜色
private int passwordColor = 0xFFCCCCCC;
//密码圆点的半径
private float passwordRadius = 3;
//画笔
private Paint paint;
//整个view的宽,高
private int width, height;
public PasswordInputView(Context context, AttributeSet attrs) {
super(context, attrs);

DisplayMetrics dm = getResources().getDisplayMetrics();
borderWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, borderWidth, dm);
borderRadius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, borderRadius, dm);
passwordLength = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, passwordLength, dm);
passwordRadius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, passwordRadius, dm);
//获得自定义属性的值
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PasswordInputView, 0, 0);
borderColor = a.getColor(R.styleable.PasswordInputView_pivBorderColor, borderColor);
borderWidth = a.getDimension(R.styleable.PasswordInputView_pivBorderWidth, borderWidth);
borderRadius = a.getDimension(R.styleable.PasswordInputView_pivBorderRadius, borderRadius);
passwordLength = a.getInt(R.styleable.PasswordInputView_pivPasswordLength, passwordLength);
passwordColor = a.getColor(R.styleable.PasswordInputView_pivPasswordColor, passwordColor);
passwordRadius = a.getDimension(R.styleable.PasswordInputView_pivPasswordRadius, passwordRadius);
dividerWidth = a.getDimension(R.styleable.PasswordInputView_dividerWidth, dividerWidth);
a.recycle();

paint = new Paint(ANTI_ALIAS_FLAG);
}
@Override
protected void onDraw(Canvas canvas) {
width = getWidth();
height = getHeight();
drawBorder(canvas);
drawDivider(canvas);
drawCircle(canvas);

}
//绘制外边框
private void drawBorder(Canvas canvas) {
paint.setStyle(Paint.Style.STROKE);
paint.setColor(borderColor);
paint.setStrokeWidth(borderWidth);
RectF rectF = new RectF(0, 0, width, height);
canvas.drawRoundRect(rectF, borderRadius, borderRadius, paint);
}
//绘制分割线
private void drawDivider(Canvas canvas) {
paint.setStyle(Paint.Style.STROKE);
paint.setColor(borderColor);
paint.setStrokeWidth(dividerWidth);
for (int i = 1; i < passwordLength; i++) {
int x = i * (width / passwordLength);
canvas.drawLine(x, 0, x, height, paint);
}
}
//绘制圆点
private void drawCircle(Canvas canvas) {
paint.setStyle(Paint.Style.FILL);
paint.setColor(passwordColor);
String content = getText().toString().trim();
for (int i = 0; i < content.length(); i++) {
int cx = width / passwordLength / 2 + i * width / passwordLength;
int cy = height / 2;
canvas.drawCircle(cx, cy, width / passwordLength / 8, paint);
}
}
@Override
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
super.onTextChanged(text, start, lengthBefore, lengthAfter);
invalidate();
//密码输入完成后做的操作,例如密码验证,页面跳转等
if (text.length() == passwordLength) {
if (onCompleteListener != null) {
onCompleteListener.onComplete(text);
}
}
}
private OnCompleteListener onCompleteListener;

public void setOnCompleteListener(OnCompleteListener onCompleteListener) {
this.onCompleteListener = onCompleteListener;
}

public interface OnCompleteListener {
void onComplete(CharSequence text);
}
}

自定义数字属性

1
2
3
4
5
6
7
8
9
10
<declare-styleable name="PasswordInputView">
<attr name="pivBorderColor" format="color"/>
<attr name="pivBorderWidth" format="dimension"/>
<attr name="pivBorderRadius" format="dimension"/>
<attr name="pivPasswordColor" format="color"/>
<attr name="pivPasswordWidth" format="dimension"/>
<attr name="pivPasswordRadius" format="dimension"/>
<attr name="pivPasswordLength" format="integer"/>
<attr name="dividerWidth" format="dimension"/>
</declare-styleable>

布局文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!--add by chensy 添加恢复出厂设置 添加密码 start-->
<com.android.settings.PasswordInputView
android:id="@+id/passwordInputView"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_margin="10dp"
android:cursorVisible="false"
android:inputType="number"
app:pivBorderColor="#808080"
app:pivBorderRadius="5dp"
app:pivBorderWidth="1dp"
app:pivPasswordColor="#808080"
app:dividerWidth="0.5dp"
android:maxLength="8"
app:pivPasswordLength="8"
android:background="@null"
android:focusable="true" />
<!--add by chensy 添加恢复出厂设置 密码 end-->

添加恢复出厂设置输入密码

packages/apps/Settings/src/com/android/settings/MasterClearConfirm.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (UserManager.get(getActivity()).hasUserRestriction(
UserManager.DISALLOW_FACTORY_RESET)) {
return inflater.inflate(R.layout.master_clear_disallowed_screen, null);
}
mContentView = inflater.inflate(R.layout.master_clear_confirm, null);
/**add by chensy 添加恢复出厂设置密码*/
inputView = (PasswordInputView)mContentView.findViewById(R.id.passwordInputView);
inputView.setFocusable(true);
inputView.requestFocus();
inputView.setOnCompleteListener(new PasswordInputView.OnCompleteListener() {
@Override
public void onComplete(CharSequence text) {
Log.d("chensy", "onComplete: "+text);
passwd = text;
}
});
/** add by chensy 添加恢复出厂设置密码 */
establishFinalConfirmationState();
return mContentView;
}