Skip to content
This repository was archived by the owner on Dec 20, 2019. It is now read-only.

Commit 00edff1

Browse files
committed
add password replacement
1 parent 8e6bc19 commit 00edff1

File tree

5 files changed

+103
-51
lines changed

5 files changed

+103
-51
lines changed

app/src/main/java/com/cyl18/opapplocktweaker/AppLockHooker.java

Lines changed: 100 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
import android.content.Intent;
66
import android.content.ServiceConnection;
77
import android.content.SharedPreferences;
8+
import android.os.Bundle;
89
import android.os.Environment;
10+
import android.text.Editable;
11+
import android.text.TextWatcher;
912
import android.view.KeyEvent;
1013
import android.view.View;
1114
import android.widget.EditText;
@@ -14,7 +17,6 @@
1417

1518
import de.robv.android.xposed.IXposedHookLoadPackage;
1619
import de.robv.android.xposed.XC_MethodHook;
17-
import de.robv.android.xposed.XC_MethodReplacement;
1820
import de.robv.android.xposed.XSharedPreferences;
1921
import de.robv.android.xposed.XposedHelpers;
2022
import de.robv.android.xposed.callbacks.XC_LoadPackage;
@@ -41,8 +43,6 @@ public static TrackerConnector getCurrentTracker() {
4143
public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {
4244
if (!lpparam.packageName.equals(Constants.APPLOCK_PACKAGE)) return;
4345

44-
SharedPreferences globalPref = getPreferences();
45-
boolean enableReplacePassword = globalPref.getBoolean("enable_replace_password", true);
4646

4747
// hook face recognition
4848
XposedHelpers.findAndHookMethod(Constants.APPLOCK_ACTIVITY_CONFIRM, lpparam.classLoader, "onResume", new XC_MethodHook() {
@@ -70,21 +70,27 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable {
7070
});
7171

7272
// hook complex password
73-
XposedHelpers.findAndHookMethod(Constants.APPLOCK_ACTIVITY_CONFIRM_COMPLEX, lpparam.classLoader, "onResume", new XC_MethodHook() {
73+
XposedHelpers.findAndHookMethod(Constants.APPLOCK_ACTIVITY_CONFIRM_COMPLEX, lpparam.classLoader, "onCreate", Bundle.class, new XC_MethodHook() {
7474
@Override
7575
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
7676
currentApplockerActivity = (Activity) param.thisObject;
7777

7878
SharedPreferences preferences = getPreferences();
7979
boolean enable_fast_password = preferences.getBoolean("enable_fast_password", false);
80+
boolean enableReplacePassword = preferences.getBoolean("enable_replace_password", true);
8081

81-
if (enable_fast_password)
82+
boolean shouldReplace = shouldReplace(preferences, getUnlockPackageName());
83+
84+
if (enable_fast_password && !shouldReplace)
8285
hookFastPassword(preferences);
86+
if (shouldReplace && enableReplacePassword) {
87+
hookReplacePasswordComplex(preferences);
88+
}
8389
}
8490
});
8591

8692
// hook pin
87-
XposedHelpers.findAndHookMethod(Constants.APPLOCK_ACTIVITY_CONFIRM_PASSWORD, lpparam.classLoader, "onResume", new XC_MethodHook() {
93+
XposedHelpers.findAndHookMethod(Constants.APPLOCK_ACTIVITY_CONFIRM_PASSWORD, lpparam.classLoader, "onCreate", Bundle.class, new XC_MethodHook() {
8894
@Override
8995
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
9096
currentApplockerActivity = (Activity) param.thisObject;
@@ -98,31 +104,106 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable {
98104

99105
SharedPreferences preferences = getPreferences();
100106
boolean enable_fast_password = preferences.getBoolean("enable_fast_password", false);
107+
boolean enableReplacePassword = preferences.getBoolean("enable_replace_password", true);
101108

102109
String password_length = preferences.getString("password_length", "0");
103110
final Integer length = Integer.parseInt(password_length);
111+
boolean shouldReplace = shouldReplace(preferences, getUnlockPackageName());
104112

105-
if (enable_fast_password)
113+
if (enable_fast_password && !shouldReplace)
106114
onPINInput(length, thisObject);
115+
if (shouldReplace && enableReplacePassword) {
116+
hookReplacePasswordPin(preferences, thisObject);
117+
}
107118
}
108119
});
109120

110121
// hook custom password
111-
if (enableReplacePassword) {
112-
XposedHelpers.findAndHookMethod(Constants.APPLOCK_CHOOSE_PASSWORD, lpparam.classLoader, "launchConfirmationActivity",
113-
int.class, CharSequence.class, CharSequence.class, CharSequence.class, boolean.class, boolean.class, boolean.class, long.class, boolean.class, String.class, int.class, int.class, new XC_MethodReplacement() {
114-
@Override
115-
protected Object replaceHookedMethod(MethodHookParam param) throws Throwable {
116122

117-
return true;
118-
}
123+
XposedHelpers.findAndHookMethod(Constants.APPLOCK_ACTIVITY_CONFIRM_PASSWORD, lpparam.classLoader, "handleNext", new XC_MethodHook() {
124+
@Override
125+
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
126+
SharedPreferences pref = getPreferences();
127+
boolean replace = pref.getBoolean("enable_replace_password", true);
128+
if (replace)
129+
hookReplacePassword(param, true);
130+
}
131+
});
132+
133+
XposedHelpers.findAndHookMethod(Constants.APPLOCK_ACTIVITY_CONFIRM_COMPLEX, lpparam.classLoader, "handleNext", new XC_MethodHook() {
134+
@Override
135+
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
136+
SharedPreferences pref = getPreferences();
137+
boolean replace = pref.getBoolean("enable_replace_password", true);
138+
if (replace)
139+
hookReplacePassword(param, false);
140+
}
141+
});
142+
119143

120-
});
144+
// hook fingerprint
145+
146+
XposedHelpers.findAndHookMethod(Constants.APPLOCK_ACTIVITY_CONFIRM, lpparam.classLoader, "registerFingerprint", new XC_MethodHook() {
147+
@Override
148+
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
149+
if (getPreferences().getBoolean("disable_fingerprint", false))
150+
param.setResult(null);
151+
}
152+
});
153+
154+
}
155+
156+
private void hookReplacePasswordPin(SharedPreferences preferences, Object thisObject) {
157+
158+
if (preferences.getString("password", "").equals(((String) XposedHelpers.callMethod(thisObject, "getText")))) {
159+
XposedHelpers.callMethod(currentApplockerActivity, "onAuthenticated");
121160
}
161+
}
122162

163+
private void hookReplacePasswordComplex(final SharedPreferences preferences) {
164+
final EditText passwordEditText = (EditText) XposedHelpers.getObjectField(currentApplockerActivity, "mPasswordEntry");
123165

166+
passwordEditText.addTextChangedListener(new TextWatcher() {
167+
@Override
168+
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
169+
}
170+
171+
@Override
172+
public void onTextChanged(CharSequence s, int start, int before, int count) {
173+
}
174+
175+
@Override
176+
public void afterTextChanged(Editable s) {
177+
if (preferences.getString("password", "").equals(passwordEditText.getText().toString())) {
178+
XposedHelpers.callMethod(currentApplockerActivity, "onAuthenticated");
179+
}
180+
}
181+
});
182+
183+
}
184+
185+
private void hookReplacePassword(XC_MethodHook.MethodHookParam param, boolean isPin) {
186+
if (shouldReplace(getPreferences(), getUnlockPackageName())) {
187+
if (isPin) {
188+
Object mPasswordTextViewForPin = XposedHelpers.getObjectField(currentApplockerActivity, "mPasswordTextViewForPin");
189+
if (mPasswordTextViewForPin != null) {
190+
XposedHelpers.callMethod(mPasswordTextViewForPin, "reset", true);
191+
XposedHelpers.callMethod(mPasswordTextViewForPin, "setEnabled", true);
192+
}
193+
Object mPasswordEntry = XposedHelpers.getObjectField(currentApplockerActivity, "mPasswordEntry");
194+
if (mPasswordEntry != null)
195+
XposedHelpers.callMethod(mPasswordEntry, "setText", true);
196+
}
197+
Object mPasswordEntryInputDisabler = XposedHelpers.getObjectField(currentApplockerActivity, "mPasswordEntryInputDisabler");
198+
if (mPasswordEntryInputDisabler != null)
199+
XposedHelpers.callMethod(mPasswordEntryInputDisabler, "setInputEnabled", true);
200+
param.setResult(null);
201+
}
124202
}
125203

204+
private boolean shouldReplace(SharedPreferences preferences, String packageName) {
205+
return !preferences.getBoolean("enable_only_replace_selected", false) || preferences.getBoolean(packageName, false);
206+
}
126207

127208
private void hookFastPassword(SharedPreferences preferences) {
128209
String password_length = preferences.getString("password_length", "0");
@@ -142,7 +223,6 @@ public boolean onKey(View view, int i, KeyEvent keyEvent) {
142223
}
143224

144225
private void onPINInput(Integer length, Object thisObject) {
145-
146226
if (length.equals(((String) XposedHelpers.callMethod(thisObject, "getText")).length())) {
147227
XposedHelpers.callMethod(currentApplockerActivity, "handleNext");
148228
}
@@ -175,5 +255,9 @@ public void onClick(View view) {
175255
private View getLayout() {
176256
return currentApplockerActivity.findViewById(ONEPLUS_APPLOCK_LAYOUT_ID);
177257
}
258+
259+
public String getUnlockPackageName() {
260+
return ((String) XposedHelpers.getObjectField(currentApplockerActivity, "mPackageName"));
261+
}
178262
}
179263

app/src/main/java/com/cyl18/opapplocktweaker/SettingsActivity.java

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import android.app.Activity;
66
import android.content.ComponentName;
77
import android.content.Context;
8-
import android.content.DialogInterface;
98
import android.content.Intent;
109
import android.content.SharedPreferences;
1110
import android.content.pm.ApplicationInfo;
@@ -25,7 +24,6 @@
2524
import android.support.v4.app.ActivityCompat;
2625
import android.support.v4.content.ContextCompat;
2726
import android.support.v7.app.ActionBar;
28-
import android.support.v7.app.AlertDialog;
2927
import android.view.MenuItem;
3028

3129
import org.lukhnos.nnio.file.Files;
@@ -204,13 +202,6 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
204202
return true;
205203
}
206204
});
207-
findPreference("enable_replace_password").setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
208-
@Override
209-
public boolean onPreferenceChange(Preference preference, Object newValue) {
210-
if ((boolean) newValue) showAlert();
211-
return true;
212-
}
213-
});
214205
}
215206

216207
private void addAppsToReplace() {
@@ -248,20 +239,6 @@ private void setHide(Boolean newValue) {
248239
packageManager.setComponentEnabledSetting(aliasName, state, PackageManager.DONT_KILL_APP);
249240
}
250241

251-
private void showAlert() {
252-
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
253-
builder.setTitle(R.string.alert_title);
254-
builder.setMessage(R.string.alert_summary);
255-
builder.setCancelable(true);
256-
builder.setPositiveButton(R.string.alert_button, new DialogInterface.OnClickListener() {
257-
@Override
258-
public void onClick(DialogInterface dialog, int which) {
259-
// do nothing
260-
}
261-
});
262-
builder.create().show();
263-
}
264-
265242
private List<ApplicationInfo> getAllPackages() {
266243
return this.getActivity().getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);
267244
}

app/src/main/res/values-zh/strings.xml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,11 @@
1414
<string name="password_length">密码长度</string>
1515
<string name="hide_icon">隐藏图标</string>
1616
<string name="replace_password">替换应用锁密码</string>
17-
<string name="replace_password_summary">使用下面的密码而不是系统密码</string>
1817
<string name="password">密码</string>
1918
<string name="apps_to_replace">要替换的应用</string>
2019
<string name="only_replace_selected_apps">只替换选择的应用</string>
21-
<string name="alert_title">警告</string>
22-
<string name="alert_summary">不要把快速解锁和密码替换一起使用, 快速解锁在密码替换里是可用的</string>
23-
<string name="alert_button">好的</string>
2420
<string name="disable_fingerprint">关闭指纹解锁</string>
25-
<string name="enable_replace_password_summary">需求系统重启, 请使用和系统相同的密码</string>
21+
<string name="enable_replace_password_summary">请使用和系统相同类型的密码</string>
2622

2723

2824
</resources>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,11 @@
1313
<string name="password_length">Password length</string>
1414
<string name="hide_icon">Hide icon</string>
1515
<string name="replace_password">Replace applock password</string>
16-
<string name="replace_password_summary">Use password below instead of system password</string>
1716
<string name="password">Password</string>
1817
<string name="apps_to_replace">Apps to replace</string>
1918
<string name="only_replace_selected_apps">Only replaces selected apps</string>
20-
<string name="alert_title">Warning</string>
21-
<string name="alert_summary">DO NOT enable fast unlock and password replacement together, as fast lock is usable by default in password unlock</string>
22-
<string name="alert_button">Okay</string>
2319
<string name="disable_fingerprint">Disable fingerprint</string>
24-
<string name="enable_replace_password_summary">Require restart system, please use the same password type as the system\'s</string>
20+
<string name="enable_replace_password_summary">Use password below instead of system password, please use the same password type as the system\'s</string>
2521

2622

2723
</resources>

app/src/main/res/xml/pref_general.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
android:defaultValue="false"
2929
android:key="enable_replace_password"
3030
android:title="@string/replace_password"
31-
android:summary="@string/replace_password_summary" />
31+
android:summary="@string/enable_replace_password_summary" />
3232

3333
<EditTextPreference
3434
android:dependency="enable_replace_password"
@@ -37,7 +37,6 @@
3737
android:selectAllOnFocus="true"
3838
android:singleLine="true"
3939
android:title="@string/password"
40-
android:summary="@string/enable_replace_password_summary"
4140
android:key="password" />
4241

4342
<SwitchPreference

0 commit comments

Comments
 (0)