From f7f1be33ce121a02fd924a6eb05d589411b0ff7a Mon Sep 17 00:00:00 2001 From: Cyl18 Date: Fri, 26 Jan 2018 17:57:45 +0800 Subject: [PATCH] add quick unlock and settings UI --- app/build.gradle | 6 +- app/src/main/AndroidManifest.xml | 23 +- .../AppCompatPreferenceActivity.java | 109 ++++++++ .../cyl18/opapplocktweaker/AppLockHooker.java | 74 +++++- .../com/cyl18/opapplocktweaker/Constants.java | 9 +- .../opapplocktweaker/SettingsActivity.java | 234 ++++++++++++++++++ .../main/res/drawable/ic_info_black_24dp.xml | 9 + .../drawable/ic_notifications_black_24dp.xml | 9 + .../main/res/drawable/ic_sync_black_24dp.xml | 9 + app/src/main/res/values-zh/strings.xml | 17 ++ app/src/main/res/values/strings.xml | 14 ++ app/src/main/res/xml/pref_general.xml | 37 +++ app/src/main/res/xml/pref_headers.xml | 11 + 13 files changed, 545 insertions(+), 16 deletions(-) create mode 100644 app/src/main/java/com/cyl18/opapplocktweaker/AppCompatPreferenceActivity.java create mode 100644 app/src/main/java/com/cyl18/opapplocktweaker/SettingsActivity.java create mode 100644 app/src/main/res/drawable/ic_info_black_24dp.xml create mode 100644 app/src/main/res/drawable/ic_notifications_black_24dp.xml create mode 100644 app/src/main/res/drawable/ic_sync_black_24dp.xml create mode 100644 app/src/main/res/values-zh/strings.xml create mode 100644 app/src/main/res/xml/pref_general.xml create mode 100644 app/src/main/res/xml/pref_headers.xml diff --git a/app/build.gradle b/app/build.gradle index 99b8575..9cba6e8 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -6,8 +6,8 @@ android { applicationId "com.cyl18.opapplocktweaker" minSdkVersion 24 targetSdkVersion 26 - versionCode 6 - versionName "1.5" + versionCode 7 + versionName "1.6" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { @@ -19,9 +19,11 @@ android { dependencies { implementation 'com.android.support:appcompat-v7:26.1.0' + implementation 'com.android.support:support-v4:26.1.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' provided 'de.robv.android.xposed:api:82' compileOnly files('libs/OPFaceUnlock.jar') + compile 'org.lukhnos:nnio:0.2' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 7c197a5..232d37d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,13 +2,15 @@ + + + - @@ -19,6 +21,25 @@ android:name="xposedminversion" android:value="82" /> + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/cyl18/opapplocktweaker/AppCompatPreferenceActivity.java b/app/src/main/java/com/cyl18/opapplocktweaker/AppCompatPreferenceActivity.java new file mode 100644 index 0000000..334fedb --- /dev/null +++ b/app/src/main/java/com/cyl18/opapplocktweaker/AppCompatPreferenceActivity.java @@ -0,0 +1,109 @@ +package com.cyl18.opapplocktweaker; + +import android.content.res.Configuration; +import android.os.Bundle; +import android.preference.PreferenceActivity; +import android.support.annotation.LayoutRes; +import android.support.annotation.Nullable; +import android.support.v7.app.ActionBar; +import android.support.v7.app.AppCompatDelegate; +import android.support.v7.widget.Toolbar; +import android.view.MenuInflater; +import android.view.View; +import android.view.ViewGroup; + +/** + * A {@link android.preference.PreferenceActivity} which implements and proxies the necessary calls + * to be used with AppCompat. + */ +public abstract class AppCompatPreferenceActivity extends PreferenceActivity { + + private AppCompatDelegate mDelegate; + + @Override + protected void onCreate(Bundle savedInstanceState) { + getDelegate().installViewFactory(); + getDelegate().onCreate(savedInstanceState); + super.onCreate(savedInstanceState); + } + + @Override + protected void onPostCreate(Bundle savedInstanceState) { + super.onPostCreate(savedInstanceState); + getDelegate().onPostCreate(savedInstanceState); + } + + public ActionBar getSupportActionBar() { + return getDelegate().getSupportActionBar(); + } + + public void setSupportActionBar(@Nullable Toolbar toolbar) { + getDelegate().setSupportActionBar(toolbar); + } + + @Override + public MenuInflater getMenuInflater() { + return getDelegate().getMenuInflater(); + } + + @Override + public void setContentView(@LayoutRes int layoutResID) { + getDelegate().setContentView(layoutResID); + } + + @Override + public void setContentView(View view) { + getDelegate().setContentView(view); + } + + @Override + public void setContentView(View view, ViewGroup.LayoutParams params) { + getDelegate().setContentView(view, params); + } + + @Override + public void addContentView(View view, ViewGroup.LayoutParams params) { + getDelegate().addContentView(view, params); + } + + @Override + protected void onPostResume() { + super.onPostResume(); + getDelegate().onPostResume(); + } + + @Override + protected void onTitleChanged(CharSequence title, int color) { + super.onTitleChanged(title, color); + getDelegate().setTitle(title); + } + + @Override + public void onConfigurationChanged(Configuration newConfig) { + super.onConfigurationChanged(newConfig); + getDelegate().onConfigurationChanged(newConfig); + } + + @Override + protected void onStop() { + super.onStop(); + getDelegate().onStop(); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + getDelegate().onDestroy(); + } + + public void invalidateOptionsMenu() { + getDelegate().invalidateOptionsMenu(); + } + + private AppCompatDelegate getDelegate() { + if (mDelegate == null) { + mDelegate = AppCompatDelegate.create(this, null); + } + return mDelegate; + } +} diff --git a/app/src/main/java/com/cyl18/opapplocktweaker/AppLockHooker.java b/app/src/main/java/com/cyl18/opapplocktweaker/AppLockHooker.java index 25458b1..d274f6f 100644 --- a/app/src/main/java/com/cyl18/opapplocktweaker/AppLockHooker.java +++ b/app/src/main/java/com/cyl18/opapplocktweaker/AppLockHooker.java @@ -4,10 +4,17 @@ import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; +import android.content.SharedPreferences; +import android.os.Environment; +import android.view.KeyEvent; import android.view.View; +import android.widget.EditText; + +import java.io.File; import de.robv.android.xposed.IXposedHookLoadPackage; import de.robv.android.xposed.XC_MethodHook; +import de.robv.android.xposed.XSharedPreferences; import de.robv.android.xposed.XposedHelpers; import de.robv.android.xposed.callbacks.XC_LoadPackage; @@ -31,19 +38,61 @@ public static TrackerConnector getCurrentTracker() { @Override public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) throws Throwable { - if (!lpparam.packageName.equals(Constants.APPLOCK_PACKAGE_NAME)) return; + if (!lpparam.packageName.equals(Constants.APPLOCK_PACKAGE)) return; + - XposedHelpers.findAndHookMethod(Constants.ACTIVITY_CONFIRM_NAME, lpparam.classLoader, "onResume", new XC_MethodHook() { + XposedHelpers.findAndHookMethod(Constants.APPLOCK_ACTIVITY_CONFIRM, lpparam.classLoader, "onResume", new XC_MethodHook() { @Override protected void afterHookedMethod(MethodHookParam param) throws Throwable { - hookOnStart(param); + currentApplockerActivity = (Activity) param.thisObject; + + SharedPreferences preferences = getPreferences(); + + boolean enable_face_recognition = preferences.getBoolean("enable_face_recognition", true); + boolean enable_fast_password = preferences.getBoolean("enable_fast_password", false); + + if (enable_face_recognition) { + hookOnStart(); + } + + if (enable_fast_password) { + hookFastPassword(preferences); + } } }); - XposedHelpers.findAndHookMethod(Constants.ACTIVITY_CONFIRM_NAME, lpparam.classLoader, "onPause", new XC_MethodHook() { + XposedHelpers.findAndHookMethod(Constants.APPLOCK_ACTIVITY_CONFIRM, lpparam.classLoader, "onPause", new XC_MethodHook() { @Override protected void afterHookedMethod(MethodHookParam param) throws Throwable { - hookOnStop(); + SharedPreferences preferences = getPreferences(); + + boolean enable_face_recognition = preferences.getBoolean("enable_face_recognition", true); + + if (enable_face_recognition) + hookOnStop(); + } + }); + + } + + private SharedPreferences getPreferences() { + File dest = new File(Environment.getExternalStorageDirectory(), Constants.SHARED_SETTINGS_FILE); + return new XSharedPreferences(dest); + } + + private void hookFastPassword(SharedPreferences preferences) { + String password_length = preferences.getString("password_length", "0"); + final Integer length = Integer.parseInt(password_length); + final EditText passwordEditText = (EditText) XposedHelpers.getObjectField(currentApplockerActivity, "mPasswordEntry"); + + passwordEditText.setOnKeyListener(new View.OnKeyListener() { + @Override + public boolean onKey(View view, int i, KeyEvent keyEvent) { + if (length.equals(passwordEditText.getText().length())) { + XposedHelpers.callMethod(currentApplockerActivity, "handleNext"); + return true; + } + return false; } }); } @@ -52,18 +101,21 @@ private void hookOnStop() { currentApplockerActivity.unbindService(connection); } - private void hookOnStart(XC_MethodHook.MethodHookParam param) { - currentApplockerActivity = (Activity) param.thisObject; + private void hookOnStart() { Intent intent = new Intent(); - intent.setClassName("com.oneplus.faceunlock", "com.oneplus.faceunlock.FaceUnlockService"); + intent.setClassName(Constants.FACEUNLOCK_PACKAGE, Constants.FACEUNLOCK_SERVICE); currentApplockerActivity.bindService(intent, connection, Context.BIND_AUTO_CREATE); - currentTracker = new TrackerConnector(XposedHelpers.getObjectField(currentApplockerActivity, "mCredentialCheckResultTracker")); - currentApplockerActivity.findViewById(ONEPLUS_APPLOCK_LAYOUT_ID).setOnClickListener(new View.OnClickListener() { + currentTracker = new TrackerConnector(XposedHelpers.getObjectField(currentApplockerActivity, Constants.TRACKER)); + getLayout().setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - FaceUnlockServiceConnector.getInstance().startFaceUnlock(); + FaceUnlockServiceConnector.getInstance().stopFaceUnlock(); } }); } + + private View getLayout() { + return currentApplockerActivity.findViewById(ONEPLUS_APPLOCK_LAYOUT_ID); + } } diff --git a/app/src/main/java/com/cyl18/opapplocktweaker/Constants.java b/app/src/main/java/com/cyl18/opapplocktweaker/Constants.java index eb32634..4d0d9a5 100644 --- a/app/src/main/java/com/cyl18/opapplocktweaker/Constants.java +++ b/app/src/main/java/com/cyl18/opapplocktweaker/Constants.java @@ -2,6 +2,11 @@ public interface Constants { int USER_ID = 0; - String APPLOCK_PACKAGE_NAME = "com.oneplus.applocker"; - String ACTIVITY_CONFIRM_NAME = "com.oneplus.applocker.ApplockerConfirmActivity"; + String APPLOCK_PACKAGE = "com.oneplus.applocker"; + String APPLOCK_ACTIVITY_CONFIRM = "com.oneplus.applocker.ApplockerConfirmActivity"; + String FACEUNLOCK_PACKAGE = "com.oneplus.faceunlock"; + String FACEUNLOCK_SERVICE = "com.oneplus.faceunlock.FaceUnlockService"; + String TRACKER = "mCredentialCheckResultTracker"; + String THIS_PACKAGE = "com.cyl18.opapplocktweaker"; + String SHARED_SETTINGS_FILE = "OPApplockTweakerSettings"; } diff --git a/app/src/main/java/com/cyl18/opapplocktweaker/SettingsActivity.java b/app/src/main/java/com/cyl18/opapplocktweaker/SettingsActivity.java new file mode 100644 index 0000000..7912694 --- /dev/null +++ b/app/src/main/java/com/cyl18/opapplocktweaker/SettingsActivity.java @@ -0,0 +1,234 @@ +package com.cyl18.opapplocktweaker; + +import android.Manifest; +import android.annotation.TargetApi; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.content.res.Configuration; +import android.os.Build; +import android.os.Bundle; +import android.os.Environment; +import android.preference.ListPreference; +import android.preference.Preference; +import android.preference.PreferenceActivity; +import android.preference.PreferenceFragment; +import android.preference.PreferenceManager; +import android.support.v4.app.ActivityCompat; +import android.support.v4.content.ContextCompat; +import android.support.v7.app.ActionBar; +import android.view.MenuItem; + +import org.lukhnos.nnio.file.Files; +import org.lukhnos.nnio.file.Paths; + +import java.io.File; +import java.io.IOException; +import java.util.List; + +/** + * A {@link PreferenceActivity} that presents a set of application settings. On + * handset devices, settings are presented as a single list. On tablets, + * settings are split by category, with category headers shown to the left of + * the list of settings. + *

+ * See + * Android Design: Settings for design guidelines and the Settings + * API Guide for more information on developing a Settings UI. + */ +public class SettingsActivity extends AppCompatPreferenceActivity { + + /** + * A preference value change listener that updates the preference's summary + * to reflect its new value. + */ + private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object value) { + String stringValue = value.toString(); + + if (preference instanceof ListPreference) { + // For list preferences, look up the correct display value in + // the preference's 'entries' list. + ListPreference listPreference = (ListPreference) preference; + int index = listPreference.findIndexOfValue(stringValue); + + // Set the summary to reflect the new value. + preference.setSummary( + index >= 0 + ? listPreference.getEntries()[index] + : null); + + } else { + // For all other preferences, set the summary to the value's + // simple string representation. + preference.setSummary(stringValue); + } + return true; + } + }; + + /** + * Helper method to determine if the device has an extra-large screen. For + * example, 10" tablets are extra-large. + */ + private static boolean isXLargeTablet(Context context) { + return (context.getResources().getConfiguration().screenLayout + & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE; + } + + /** + * Binds a preference's summary to its value. More specifically, when the + * preference's value is changed, its summary (line of text below the + * preference title) is updated to reflect the value. The summary is also + * immediately updated upon calling this method. The exact display format is + * dependent on the type of preference. + * + * @see #sBindPreferenceSummaryToValueListener + */ + private static void bindPreferenceSummaryToValue(Preference preference) { + // Set the listener to watch for value changes. + preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener); + + // Trigger the listener immediately with the preference's + // current value. + sBindPreferenceSummaryToValueListener.onPreferenceChange(preference, + PreferenceManager + .getDefaultSharedPreferences(preference.getContext()) + .getString(preference.getKey(), "")); + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setupActionBar(); + if (ContextCompat.checkSelfPermission(this, + Manifest.permission.WRITE_EXTERNAL_STORAGE) + != PackageManager.PERMISSION_GRANTED) { + + // Should we show an explanation? + if (ActivityCompat.shouldShowRequestPermissionRationale(this, + Manifest.permission.WRITE_EXTERNAL_STORAGE)) { + + // Show an expanation to the user *asynchronously* -- don't block + // this thread waiting for the user's response! After the user + // sees the explanation, try again to request the permission. + + } else { + + // No explanation needed, we can request the permission. + + ActivityCompat.requestPermissions(this, + new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0x23333); + + // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an + // app-defined int constant. The callback method gets the + // result of the request. + } + } + } + + /** + * Set up the {@link android.app.ActionBar}, if the API is available. + */ + private void setupActionBar() { + ActionBar actionBar = getSupportActionBar(); + if (actionBar != null) { + // Show the Up button in the action bar. + actionBar.setDisplayHomeAsUpEnabled(true); + } + } + + /** + * {@inheritDoc} + */ + @Override + public boolean onIsMultiPane() { + return isXLargeTablet(this); + } + + /** + * {@inheritDoc} + */ + @Override + @TargetApi(Build.VERSION_CODES.HONEYCOMB) + public void onBuildHeaders(List

target) { + loadHeadersFromResource(R.xml.pref_headers, target); + } + + /** + * This method stops fragment injection in malicious applications. + * Make sure to deny any unknown fragments here. + */ + protected boolean isValidFragment(String fragmentName) { + return PreferenceFragment.class.getName().equals(fragmentName) + || GeneralPreferenceFragment.class.getName().equals(fragmentName); + } + + /** + * This fragment shows general preferences only. It is used when the + * activity is showing a two-pane settings UI. + */ + @TargetApi(Build.VERSION_CODES.HONEYCOMB) + public static class GeneralPreferenceFragment extends PreferenceFragment { + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + addPreferencesFromResource(R.xml.pref_general); + setHasOptionsMenu(true); + + bindPreferenceSummaryToValue(findPreference("password_length")); + findPreference("enable_hide_icon").setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + PackageManager packageManager = getActivity().getPackageManager(); + int state = (!(Boolean) newValue) ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED; + ComponentName aliasName = new ComponentName(getActivity(), "com.cyl18.opapplocktweaker.SettingsActivityAlias"); + packageManager.setComponentEnabledSetting(aliasName, state, PackageManager.DONT_KILL_APP); + + //TODO + return true; + } + }); + } + + @Override + public void onPause() { + super.onPause(); + // bad code here. + + File prefsDir = new File(getActivity().getApplicationInfo().dataDir, "shared_prefs"); + File source = new File(prefsDir, getPreferenceManager().getSharedPreferencesName() + ".xml"); + File dest = new File(Environment.getExternalStorageDirectory(), Constants.SHARED_SETTINGS_FILE); + + if (source.exists()) { + if (dest.exists()) dest.delete(); + try { + Files.copy(Paths.get(source.toURI()), Paths.get(dest.toURI())); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + int id = item.getItemId(); + if (id == android.R.id.home) { + startActivity(new Intent(getActivity(), SettingsActivity.class)); + return true; + } + return super.onOptionsItemSelected(item); + } + + + } + + /** + * This fragment shows notification preferences only. It is used when the + * activity is showing a two-pane settings UI. + */ + +} diff --git a/app/src/main/res/drawable/ic_info_black_24dp.xml b/app/src/main/res/drawable/ic_info_black_24dp.xml new file mode 100644 index 0000000..34b8202 --- /dev/null +++ b/app/src/main/res/drawable/ic_info_black_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_notifications_black_24dp.xml b/app/src/main/res/drawable/ic_notifications_black_24dp.xml new file mode 100644 index 0000000..e3400cf --- /dev/null +++ b/app/src/main/res/drawable/ic_notifications_black_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_sync_black_24dp.xml b/app/src/main/res/drawable/ic_sync_black_24dp.xml new file mode 100644 index 0000000..5a283aa --- /dev/null +++ b/app/src/main/res/drawable/ic_sync_black_24dp.xml @@ -0,0 +1,9 @@ + + + \ No newline at end of file diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml new file mode 100644 index 0000000..9bf1eb9 --- /dev/null +++ b/app/src/main/res/values-zh/strings.xml @@ -0,0 +1,17 @@ + + + OnePlus 应用锁修改 + OnePlus 应用锁修改设置 + + + + + 设置 + 为应用锁启用人脸识别 + 启用人脸识别 + 输完密码后不用按下确定 + 快速输入密码 + 密码长度 + 隐藏图标 + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 38726d7..0b5c751 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,3 +1,17 @@ OnePlus Applock Tweaker + OnePlus Applock Tweaker Settings + + + + + General + For fast unlock + Enable face recognition + Quick unlock needs to know the exact length of your password + Enable quick unlock + Password length + Hide icon + + diff --git a/app/src/main/res/xml/pref_general.xml b/app/src/main/res/xml/pref_general.xml new file mode 100644 index 0000000..4e46521 --- /dev/null +++ b/app/src/main/res/xml/pref_general.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/xml/pref_headers.xml b/app/src/main/res/xml/pref_headers.xml new file mode 100644 index 0000000..87660c8 --- /dev/null +++ b/app/src/main/res/xml/pref_headers.xml @@ -0,0 +1,11 @@ + + + + +
+ + +