├── res
├── drawable
│ ├── ic_launcher.png
│ ├── edittext_normal.xml
│ ├── edittext_cursor.xml
│ ├── edittext_pressed.xml
│ ├── button_normal.xml
│ ├── button_pressed.xml
│ ├── button_background.xml
│ └── edittext_background.xml
├── anim
│ ├── cycle5.xml
│ ├── nothing.xml
│ ├── shake.xml
│ └── slide_up.xml
├── values
│ ├── colors.xml
│ ├── strings.xml
│ └── styles.xml
└── layout
│ ├── page_home.xml
│ └── page_passcode.xml
├── .settings
└── org.eclipse.core.resources.prefs
├── .gitignore
├── src
└── me
│ └── dawson
│ └── applock
│ ├── App.java
│ ├── core
│ ├── PageListener.java
│ ├── LockManager.java
│ ├── Encryptor.java
│ ├── AppLock.java
│ ├── BaseActivity.java
│ ├── AppLockImpl.java
│ └── AppLockActivity.java
│ └── HomePage.java
├── .classpath
├── project.properties
├── README.md
├── .project
├── AndroidManifest.xml
└── LICENSE
/res/drawable/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dawsonice/AppLock/HEAD/res/drawable/ic_launcher.png
--------------------------------------------------------------------------------
/.settings/org.eclipse.core.resources.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | encoding//src/me/dawson/applock/HomePage.java=UTF-8
3 |
--------------------------------------------------------------------------------
/res/anim/cycle5.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 |
3 | .git
4 |
5 | .svn
6 |
7 | lint.xml
8 |
9 | proguard-project.txt
10 |
11 | *.apk
12 | *.ap_
13 |
14 | *.dex
15 |
16 | *.class
17 |
18 | bin/
19 | gen/
20 |
21 | proguard/
22 |
--------------------------------------------------------------------------------
/res/anim/nothing.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
--------------------------------------------------------------------------------
/res/drawable/edittext_normal.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/res/anim/shake.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
--------------------------------------------------------------------------------
/res/drawable/edittext_cursor.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | #FFE5E5E5
5 | #FFCCCCCC
6 | #FF000000
7 | #FF78c8e6
8 |
9 |
--------------------------------------------------------------------------------
/res/anim/slide_up.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
--------------------------------------------------------------------------------
/res/drawable/edittext_pressed.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
10 |
11 |
--------------------------------------------------------------------------------
/res/drawable/button_normal.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/src/me/dawson/applock/App.java:
--------------------------------------------------------------------------------
1 | package me.dawson.applock;
2 |
3 | import me.dawson.applock.core.LockManager;
4 | import android.app.Application;
5 |
6 | public class App extends Application {
7 |
8 | @Override
9 | public void onCreate() {
10 | super.onCreate();
11 | LockManager.getInstance().enableAppLock(this);
12 | }
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/res/drawable/button_pressed.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/res/drawable/button_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/res/drawable/edittext_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/src/me/dawson/applock/core/PageListener.java:
--------------------------------------------------------------------------------
1 | package me.dawson.applock.core;
2 |
3 | import android.app.Activity;
4 |
5 | public interface PageListener {
6 |
7 | void onActivityCreated(Activity activity);
8 |
9 | void onActivityStarted(Activity activity);
10 |
11 | void onActivityResumed(Activity activity);
12 |
13 | void onActivityPaused(Activity activity);
14 |
15 | void onActivityStopped(Activity activity);
16 |
17 | void onActivitySaveInstanceState(Activity activity);
18 |
19 | void onActivityDestroyed(Activity activity);
20 | }
21 |
--------------------------------------------------------------------------------
/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=android-20
15 |
--------------------------------------------------------------------------------
/res/layout/page_home.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
12 |
13 |
18 |
19 |
--------------------------------------------------------------------------------
/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | AppLock
5 | Manage passcode
6 | Enter passcode
7 | Enter old passcode
8 | Re-enter passcode
9 | Change passcode
10 | Setup Passcode
11 | Wrong passcode, please try again.
12 | Disable passcode
13 | Enable passcode
14 |
15 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## Android application lock
2 |
3 | ### Feature
4 |
5 | * All android sdk supported.
6 | * Specified custom timeout.
7 | * Easy to modify style.
8 |
9 | ### Usage
10 |
11 | #### How to enable application lock?
12 |
13 | Implement application and enable it as:
14 |
15 | ```
16 | LockManager.getInstance().enableAppLock(this);
17 | ```
18 |
19 | Make all activity to extends BaseActivity:
20 |
21 | ```
22 | public class HomePage extends BaseActivity{
23 | }
24 | ```
25 |
26 | ### Demo
27 |
28 | ### demo manage screen
29 | 
30 |
31 | ### demo unlock screen
32 | 
33 |
34 | ===
35 | Any further question?
36 |
37 | [email](mailto:coder.kiss@gmail.com) me please!
38 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | AppLock
4 |
5 |
6 |
7 |
8 |
9 | com.android.ide.eclipse.adt.ResourceManagerBuilder
10 |
11 |
12 |
13 |
14 | com.android.ide.eclipse.adt.PreCompilerBuilder
15 |
16 |
17 |
18 |
19 | org.eclipse.jdt.core.javabuilder
20 |
21 |
22 |
23 |
24 | com.android.ide.eclipse.adt.ApkBuilder
25 |
26 |
27 |
28 |
29 |
30 | com.android.ide.eclipse.adt.AndroidNature
31 | org.eclipse.jdt.core.javanature
32 |
33 |
34 |
--------------------------------------------------------------------------------
/src/me/dawson/applock/core/LockManager.java:
--------------------------------------------------------------------------------
1 | package me.dawson.applock.core;
2 |
3 | import android.app.Application;
4 |
5 | public class LockManager {
6 |
7 | private volatile static LockManager instance;
8 | private AppLock curAppLocker;
9 |
10 | public static LockManager getInstance() {
11 | synchronized (LockManager.class) {
12 | if (instance == null) {
13 | instance = new LockManager();
14 | }
15 | }
16 | return instance;
17 | }
18 |
19 | public void enableAppLock(Application app) {
20 | if (curAppLocker == null) {
21 | curAppLocker = new AppLockImpl(app);
22 | }
23 | curAppLocker.enable();
24 | }
25 |
26 | public boolean isAppLockEnabled() {
27 | if (curAppLocker == null) {
28 | return false;
29 | } else {
30 | return true;
31 | }
32 | }
33 |
34 | public void setAppLock(AppLock appLocker) {
35 | if (curAppLocker != null) {
36 | curAppLocker.disable();
37 | }
38 | curAppLocker = appLocker;
39 | }
40 |
41 | public AppLock getAppLock() {
42 | return curAppLocker;
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/me/dawson/applock/core/Encryptor.java:
--------------------------------------------------------------------------------
1 | package me.dawson.applock.core;
2 |
3 | import java.security.MessageDigest;
4 | import java.util.Locale;
5 |
6 | import android.text.TextUtils;
7 |
8 | public class Encryptor {
9 |
10 | private static String bytes2Hex(byte[] bytes) {
11 | String hs = "";
12 | String stmp = "";
13 | for (int n = 0; n < bytes.length; n++) {
14 | stmp = (Integer.toHexString(bytes[n] & 0XFF));
15 | if (stmp.length() == 1) {
16 | hs += "0" + stmp;
17 | } else {
18 | hs += stmp;
19 | }
20 | }
21 | return hs.toLowerCase(Locale.ENGLISH);
22 | }
23 |
24 | public static String getSHA1(String text) {
25 | String sha1 = null;
26 | if (TextUtils.isEmpty(text)) {
27 | return sha1;
28 | }
29 | MessageDigest sha1Digest = null;
30 | try {
31 | sha1Digest = MessageDigest.getInstance("SHA-1");
32 | } catch (Exception e) {
33 | return sha1;
34 | }
35 | byte[] textBytes = text.getBytes();
36 | sha1Digest.update(textBytes, 0, text.length());
37 | byte[] sha1hash = sha1Digest.digest();
38 | return bytes2Hex(sha1hash);
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/me/dawson/applock/core/AppLock.java:
--------------------------------------------------------------------------------
1 | package me.dawson.applock.core;
2 |
3 | import java.util.HashSet;
4 |
5 | public abstract class AppLock {
6 | public static final int ENABLE_PASSLOCK = 0;
7 | public static final int DISABLE_PASSLOCK = 1;
8 | public static final int CHANGE_PASSWORD = 2;
9 | public static final int UNLOCK_PASSWORD = 3;
10 |
11 | public static final String MESSAGE = "message";
12 | public static final String TYPE = "type";
13 |
14 | public static final int DEFAULT_TIMEOUT = 0; // 2000ms
15 |
16 | protected int lockTimeOut;
17 | protected HashSet ignoredActivities;
18 |
19 | public void setTimeout(int timeout) {
20 | this.lockTimeOut = timeout;
21 | }
22 |
23 | public AppLock() {
24 | ignoredActivities = new HashSet();
25 | lockTimeOut = DEFAULT_TIMEOUT;
26 | }
27 |
28 | public void addIgnoredActivity(Class> clazz) {
29 | String clazzName = clazz.getName();
30 | this.ignoredActivities.add(clazzName);
31 | }
32 |
33 | public void removeIgnoredActivity(Class> clazz) {
34 | String clazzName = clazz.getName();
35 | this.ignoredActivities.remove(clazzName);
36 | }
37 |
38 | public abstract void enable();
39 |
40 | public abstract void disable();
41 |
42 | public abstract boolean setPasscode(String passcode);
43 |
44 | public abstract boolean checkPasscode(String passcode);
45 |
46 | public abstract boolean isPasscodeSet();
47 | }
48 |
--------------------------------------------------------------------------------
/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
20 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
17 |
18 |
27 |
28 |
--------------------------------------------------------------------------------
/src/me/dawson/applock/core/BaseActivity.java:
--------------------------------------------------------------------------------
1 | package me.dawson.applock.core;
2 |
3 | import android.app.Activity;
4 | import android.os.Bundle;
5 |
6 | public class BaseActivity extends Activity {
7 |
8 | private static PageListener pageListener;
9 |
10 | public static void setListener(PageListener listener) {
11 | pageListener = listener;
12 | }
13 |
14 | @Override
15 | protected void onCreate(Bundle savedInstanceState) {
16 | super.onCreate(savedInstanceState);
17 |
18 | if (pageListener != null) {
19 | pageListener.onActivityCreated(this);
20 | }
21 | }
22 |
23 | @Override
24 | protected void onStart() {
25 | super.onStart();
26 |
27 | if (pageListener != null) {
28 | pageListener.onActivityStarted(this);
29 | }
30 | }
31 |
32 | @Override
33 | protected void onResume() {
34 | super.onResume();
35 |
36 | if (pageListener != null) {
37 | pageListener.onActivityResumed(this);
38 | }
39 | }
40 |
41 | @Override
42 | protected void onPause() {
43 | super.onPause();
44 |
45 | if (pageListener != null) {
46 | pageListener.onActivityPaused(this);
47 | }
48 | }
49 |
50 | @Override
51 | protected void onStop() {
52 | super.onStop();
53 |
54 | if (pageListener != null) {
55 | pageListener.onActivityStopped(this);
56 | }
57 | }
58 |
59 | @Override
60 | protected void onDestroy() {
61 | super.onDestroy();
62 |
63 | if (pageListener != null) {
64 | pageListener.onActivityDestroyed(this);
65 | }
66 | }
67 |
68 | @Override
69 | protected void onSaveInstanceState(Bundle outState) {
70 | super.onSaveInstanceState(outState);
71 |
72 | if (pageListener != null) {
73 | pageListener.onActivitySaveInstanceState(this);
74 | }
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/src/me/dawson/applock/HomePage.java:
--------------------------------------------------------------------------------
1 | package me.dawson.applock;
2 |
3 | import me.dawson.applock.core.AppLock;
4 | import me.dawson.applock.core.AppLockActivity;
5 | import me.dawson.applock.core.BaseActivity;
6 | import me.dawson.applock.core.LockManager;
7 | import android.content.Intent;
8 | import android.os.Bundle;
9 | import android.view.View;
10 | import android.view.View.OnClickListener;
11 | import android.widget.Button;
12 | import android.widget.Toast;
13 |
14 | public class HomePage extends BaseActivity implements OnClickListener {
15 | public static final String TAG = "HomePage";
16 |
17 | private Button btOnOff;
18 | private Button btChange;
19 |
20 | protected void onCreate(Bundle savedInstanceState) {
21 | super.onCreate(savedInstanceState);
22 | setContentView(R.layout.page_home);
23 |
24 | btOnOff = (Button) findViewById(R.id.bt_on_off);
25 | btOnOff.setOnClickListener(this);
26 |
27 | btChange = (Button) findViewById(R.id.bt_change);
28 | btChange.setText(R.string.change_passcode);
29 | btChange.setOnClickListener(this);
30 |
31 | updateUI();
32 | }
33 |
34 | @Override
35 | public void onClick(View view) {
36 | if (view.equals(btOnOff)) {
37 | int type = LockManager.getInstance().getAppLock().isPasscodeSet() ? AppLock.DISABLE_PASSLOCK
38 | : AppLock.ENABLE_PASSLOCK;
39 | Intent intent = new Intent(this, AppLockActivity.class);
40 | intent.putExtra(AppLock.TYPE, type);
41 | startActivityForResult(intent, type);
42 | } else if (view.equals(btChange)) {
43 | Intent intent = new Intent(this, AppLockActivity.class);
44 | intent.putExtra(AppLock.TYPE, AppLock.CHANGE_PASSWORD);
45 | intent.putExtra(AppLock.MESSAGE,
46 | getString(R.string.enter_old_passcode));
47 | startActivityForResult(intent, AppLock.CHANGE_PASSWORD);
48 | }
49 | }
50 |
51 | @Override
52 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
53 | super.onActivityResult(requestCode, resultCode, data);
54 |
55 | switch (requestCode) {
56 | case AppLock.DISABLE_PASSLOCK:
57 | break;
58 | case AppLock.ENABLE_PASSLOCK:
59 | case AppLock.CHANGE_PASSWORD:
60 | if (resultCode == RESULT_OK) {
61 | Toast.makeText(this, getString(R.string.setup_passcode),
62 | Toast.LENGTH_SHORT).show();
63 | }
64 | break;
65 | default:
66 | break;
67 | }
68 | updateUI();
69 | }
70 |
71 | private void updateUI() {
72 | if (LockManager.getInstance().getAppLock().isPasscodeSet()) {
73 | btOnOff.setText(R.string.disable_passcode);
74 | btChange.setEnabled(true);
75 | } else {
76 | btOnOff.setText(R.string.enable_passcode);
77 | btChange.setEnabled(false);
78 | }
79 | }
80 |
81 | }
82 |
--------------------------------------------------------------------------------
/res/layout/page_passcode.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
17 |
18 |
28 |
29 |
34 |
35 |
38 |
39 |
40 |
41 |
42 |
45 |
46 |
47 |
50 |
51 |
52 |
55 |
56 |
57 |
58 |
65 |
66 |
67 |
68 |
72 |
73 |
77 |
78 |
82 |
83 |
84 |
85 |
86 |
90 |
91 |
95 |
96 |
100 |
101 |
102 |
103 |
104 |
108 |
109 |
113 |
114 |
118 |
119 |
120 |
121 |
122 |
129 |
130 |
134 |
135 |
139 |
140 |
141 |
142 |
143 |
--------------------------------------------------------------------------------
/src/me/dawson/applock/core/AppLockImpl.java:
--------------------------------------------------------------------------------
1 | package me.dawson.applock.core;
2 |
3 | import android.app.Activity;
4 | import android.app.Application;
5 | import android.content.Intent;
6 | import android.content.SharedPreferences;
7 | import android.preference.PreferenceManager;
8 | import android.util.Log;
9 |
10 | public class AppLockImpl extends AppLock implements PageListener {
11 | public static final String TAG = "DefaultAppLock";
12 |
13 | private static final String PASSWORD_PREFERENCE_KEY = "passcode";
14 | private static final String PASSWORD_SALT = "7xn7@c$";
15 |
16 | private SharedPreferences settings;
17 |
18 | private int liveCount;
19 | private int visibleCount;
20 |
21 | private long lastActive;
22 |
23 | public AppLockImpl(Application app) {
24 | super();
25 | SharedPreferences settings = PreferenceManager
26 | .getDefaultSharedPreferences(app);
27 | this.settings = settings;
28 | this.liveCount = 0;
29 | this.visibleCount = 0;
30 | }
31 |
32 | public void enable() {
33 | BaseActivity.setListener(this);
34 | }
35 |
36 | public void disable() {
37 | BaseActivity.setListener(null);
38 | }
39 |
40 | public boolean checkPasscode(String passcode) {
41 | passcode = PASSWORD_SALT + passcode + PASSWORD_SALT;
42 | passcode = Encryptor.getSHA1(passcode);
43 | String storedPasscode = "";
44 |
45 | if (settings.contains(PASSWORD_PREFERENCE_KEY)) {
46 | storedPasscode = settings.getString(PASSWORD_PREFERENCE_KEY, "");
47 | }
48 |
49 | if (passcode.equalsIgnoreCase(storedPasscode)) {
50 | return true;
51 | } else {
52 | return false;
53 | }
54 | }
55 |
56 | public boolean setPasscode(String passcode) {
57 | SharedPreferences.Editor editor = settings.edit();
58 |
59 | if (passcode == null) {
60 | editor.remove(PASSWORD_PREFERENCE_KEY);
61 | editor.commit();
62 | this.disable();
63 | } else {
64 | passcode = PASSWORD_SALT + passcode + PASSWORD_SALT;
65 | passcode = Encryptor.getSHA1(passcode);
66 | editor.putString(PASSWORD_PREFERENCE_KEY, passcode);
67 | editor.commit();
68 | this.enable();
69 | }
70 |
71 | return true;
72 | }
73 |
74 | // Check if we need to show the lock screen at startup
75 | public boolean isPasscodeSet() {
76 | if (settings.contains(PASSWORD_PREFERENCE_KEY)) {
77 | return true;
78 | }
79 |
80 | return false;
81 | }
82 |
83 | private boolean isIgnoredActivity(Activity activity) {
84 | String clazzName = activity.getClass().getName();
85 |
86 | // ignored activities
87 | if (ignoredActivities.contains(clazzName)) {
88 | Log.d(TAG, "ignore activity " + clazzName);
89 | return true;
90 | }
91 |
92 | return false;
93 | }
94 |
95 | private boolean shouldLockSceen(Activity activity) {
96 |
97 | // already unlock
98 | if (activity instanceof AppLockActivity) {
99 | AppLockActivity ala = (AppLockActivity) activity;
100 | if (ala.getType() == AppLock.UNLOCK_PASSWORD) {
101 | Log.d(TAG, "already unlock activity");
102 | return false;
103 | }
104 | }
105 |
106 | // no pass code set
107 | if (!isPasscodeSet()) {
108 | Log.d(TAG, "lock passcode not set.");
109 | return false;
110 | }
111 |
112 | // no enough timeout
113 | long passedTime = System.currentTimeMillis() - lastActive;
114 | if (lastActive > 0 && passedTime <= lockTimeOut) {
115 | Log.d(TAG, "no enough timeout " + passedTime + " for "
116 | + lockTimeOut);
117 | return false;
118 | }
119 |
120 | // start more than one page
121 | if (visibleCount > 1) {
122 | return false;
123 | }
124 |
125 | return true;
126 | }
127 |
128 | @Override
129 | public void onActivityPaused(Activity activity) {
130 | String clazzName = activity.getClass().getName();
131 | Log.d(TAG, "onActivityPaused " + clazzName);
132 |
133 | if (isIgnoredActivity(activity)) {
134 | return;
135 | }
136 | }
137 |
138 | @Override
139 | public void onActivityResumed(Activity activity) {
140 | String clazzName = activity.getClass().getName();
141 | Log.d(TAG, "onActivityResumed " + clazzName);
142 |
143 | if (isIgnoredActivity(activity)) {
144 | return;
145 | }
146 |
147 | if (shouldLockSceen(activity)) {
148 | Intent intent = new Intent(activity.getApplicationContext(),
149 | AppLockActivity.class);
150 | intent.putExtra(AppLock.TYPE, AppLock.UNLOCK_PASSWORD);
151 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
152 | activity.getApplication().startActivity(intent);
153 | }
154 |
155 | lastActive = 0;
156 | }
157 |
158 | @Override
159 | public void onActivityCreated(Activity activity) {
160 |
161 | if (isIgnoredActivity(activity)) {
162 | return;
163 | }
164 |
165 | liveCount++;
166 | }
167 |
168 | @Override
169 | public void onActivityDestroyed(Activity activity) {
170 | if (isIgnoredActivity(activity)) {
171 | return;
172 | }
173 |
174 | liveCount--;
175 | if (liveCount == 0) {
176 | lastActive = System.currentTimeMillis();
177 | Log.d(TAG, "set last active " + lastActive);
178 | }
179 | }
180 |
181 | @Override
182 | public void onActivitySaveInstanceState(Activity activity) {
183 | if (isIgnoredActivity(activity)) {
184 | return;
185 | }
186 | }
187 |
188 | @Override
189 | public void onActivityStarted(Activity activity) {
190 | String clazzName = activity.getClass().getName();
191 | Log.d(TAG, "onActivityStarted " + clazzName);
192 |
193 | if (isIgnoredActivity(activity)) {
194 | return;
195 | }
196 |
197 | visibleCount++;
198 | }
199 |
200 | @Override
201 | public void onActivityStopped(Activity activity) {
202 | String clazzName = activity.getClass().getName();
203 | Log.d(TAG, "onActivityStopped " + clazzName);
204 |
205 | if (isIgnoredActivity(activity)) {
206 | return;
207 | }
208 |
209 | visibleCount--;
210 | if (visibleCount == 0) {
211 | lastActive = System.currentTimeMillis();
212 | Log.d(TAG, "set last active " + lastActive);
213 | }
214 | }
215 | }
216 |
--------------------------------------------------------------------------------
/src/me/dawson/applock/core/AppLockActivity.java:
--------------------------------------------------------------------------------
1 | package me.dawson.applock.core;
2 |
3 | import me.dawson.applock.R;
4 | import android.content.Intent;
5 | import android.os.Bundle;
6 | import android.text.InputFilter;
7 | import android.text.InputType;
8 | import android.text.Spanned;
9 | import android.text.method.PasswordTransformationMethod;
10 | import android.view.Gravity;
11 | import android.view.KeyEvent;
12 | import android.view.MotionEvent;
13 | import android.view.View;
14 | import android.view.View.OnClickListener;
15 | import android.view.View.OnTouchListener;
16 | import android.view.animation.Animation;
17 | import android.view.animation.AnimationUtils;
18 | import android.widget.Button;
19 | import android.widget.EditText;
20 | import android.widget.TextView;
21 | import android.widget.Toast;
22 |
23 | public class AppLockActivity extends BaseActivity {
24 | public static final String TAG = "AppLockActivity";
25 |
26 | private int type = -1;
27 | private String oldPasscode = null;
28 |
29 | protected EditText codeField1 = null;
30 | protected EditText codeField2 = null;
31 | protected EditText codeField3 = null;
32 | protected EditText codeField4 = null;
33 | protected InputFilter[] filters = null;
34 | protected TextView tvMessage = null;
35 |
36 | @Override
37 | protected void onCreate(Bundle savedInstanceState) {
38 | super.onCreate(savedInstanceState);
39 |
40 | setContentView(R.layout.page_passcode);
41 |
42 | tvMessage = (TextView) findViewById(R.id.tv_message);
43 |
44 | Bundle extras = getIntent().getExtras();
45 | if (extras != null) {
46 | String message = extras.getString(AppLock.MESSAGE);
47 | if (message != null) {
48 | tvMessage.setText(message);
49 | }
50 |
51 | type = extras.getInt(AppLock.TYPE, -1);
52 | }
53 |
54 | filters = new InputFilter[2];
55 | filters[0] = new InputFilter.LengthFilter(1);
56 | filters[1] = numberFilter;
57 |
58 | codeField1 = (EditText) findViewById(R.id.passcode_1);
59 | setupEditText(codeField1);
60 |
61 | codeField2 = (EditText) findViewById(R.id.passcode_2);
62 | setupEditText(codeField2);
63 |
64 | codeField3 = (EditText) findViewById(R.id.passcode_3);
65 | setupEditText(codeField3);
66 |
67 | codeField4 = (EditText) findViewById(R.id.passcode_4);
68 | setupEditText(codeField4);
69 |
70 | // setup the keyboard
71 | ((Button) findViewById(R.id.button0)).setOnClickListener(btnListener);
72 | ((Button) findViewById(R.id.button1)).setOnClickListener(btnListener);
73 | ((Button) findViewById(R.id.button2)).setOnClickListener(btnListener);
74 | ((Button) findViewById(R.id.button3)).setOnClickListener(btnListener);
75 | ((Button) findViewById(R.id.button4)).setOnClickListener(btnListener);
76 | ((Button) findViewById(R.id.button5)).setOnClickListener(btnListener);
77 | ((Button) findViewById(R.id.button6)).setOnClickListener(btnListener);
78 | ((Button) findViewById(R.id.button7)).setOnClickListener(btnListener);
79 | ((Button) findViewById(R.id.button8)).setOnClickListener(btnListener);
80 | ((Button) findViewById(R.id.button9)).setOnClickListener(btnListener);
81 |
82 | ((Button) findViewById(R.id.button_clear))
83 | .setOnClickListener(new OnClickListener() {
84 | @Override
85 | public void onClick(View view) {
86 | clearFields();
87 | }
88 | });
89 |
90 | ((Button) findViewById(R.id.button_erase))
91 | .setOnClickListener(new OnClickListener() {
92 | @Override
93 | public void onClick(View view) {
94 | onDeleteKey();
95 | }
96 | });
97 |
98 | overridePendingTransition(R.anim.slide_up, R.anim.nothing);
99 |
100 | switch (type) {
101 |
102 | case AppLock.DISABLE_PASSLOCK:
103 | this.setTitle("Disable Passcode");
104 | break;
105 | case AppLock.ENABLE_PASSLOCK:
106 | this.setTitle("Enable Passcode");
107 | break;
108 | case AppLock.CHANGE_PASSWORD:
109 | this.setTitle("Change Passcode");
110 | break;
111 | case AppLock.UNLOCK_PASSWORD:
112 | this.setTitle("Unlock Passcode");
113 | break;
114 | }
115 | }
116 |
117 | public int getType() {
118 | return type;
119 | }
120 |
121 | protected void onPasscodeInputed() {
122 | String passLock = codeField1.getText().toString()
123 | + codeField2.getText().toString()
124 | + codeField3.getText().toString() + codeField4.getText();
125 |
126 | codeField1.setText("");
127 | codeField2.setText("");
128 | codeField3.setText("");
129 | codeField4.setText("");
130 | codeField1.requestFocus();
131 |
132 | switch (type) {
133 |
134 | case AppLock.DISABLE_PASSLOCK:
135 | if (LockManager.getInstance().getAppLock().checkPasscode(passLock)) {
136 | setResult(RESULT_OK);
137 | LockManager.getInstance().getAppLock().setPasscode(null);
138 | finish();
139 | } else {
140 | onPasscodeError();
141 | }
142 | break;
143 |
144 | case AppLock.ENABLE_PASSLOCK:
145 | if (oldPasscode == null) {
146 | tvMessage.setText(R.string.reenter_passcode);
147 | oldPasscode = passLock;
148 | } else {
149 | if (passLock.equals(oldPasscode)) {
150 | setResult(RESULT_OK);
151 | LockManager.getInstance().getAppLock()
152 | .setPasscode(passLock);
153 | finish();
154 | } else {
155 | oldPasscode = null;
156 | tvMessage.setText(R.string.enter_passcode);
157 | onPasscodeError();
158 | }
159 | }
160 | break;
161 |
162 | case AppLock.CHANGE_PASSWORD:
163 | if (LockManager.getInstance().getAppLock().checkPasscode(passLock)) {
164 | tvMessage.setText(R.string.enter_passcode);
165 | type = AppLock.ENABLE_PASSLOCK;
166 | } else {
167 | onPasscodeError();
168 | }
169 | break;
170 |
171 | case AppLock.UNLOCK_PASSWORD:
172 | if (LockManager.getInstance().getAppLock().checkPasscode(passLock)) {
173 | setResult(RESULT_OK);
174 | finish();
175 | } else {
176 | onPasscodeError();
177 | }
178 | break;
179 |
180 | default:
181 | break;
182 | }
183 | }
184 |
185 | @Override
186 | public void onBackPressed() {
187 | if (type == AppLock.UNLOCK_PASSWORD) {
188 | // back to home screen
189 | Intent intent = new Intent();
190 | intent.setAction(Intent.ACTION_MAIN);
191 | intent.addCategory(Intent.CATEGORY_HOME);
192 | this.startActivity(intent);
193 | finish();
194 | } else {
195 | finish();
196 | }
197 | }
198 |
199 | protected void setupEditText(EditText editText) {
200 | editText.setInputType(InputType.TYPE_NULL);
201 | editText.setFilters(filters);
202 | editText.setOnTouchListener(touchListener);
203 | editText.setTransformationMethod(PasswordTransformationMethod
204 | .getInstance());
205 | }
206 |
207 | public boolean onKeyDown(int keyCode, KeyEvent event) {
208 | if (keyCode == KeyEvent.KEYCODE_DEL) {
209 | onDeleteKey();
210 | return true;
211 | }
212 | return super.onKeyDown(keyCode, event);
213 | }
214 |
215 | private void onDeleteKey() {
216 | if (codeField1.isFocused()) {
217 |
218 | } else if (codeField2.isFocused()) {
219 | codeField1.requestFocus();
220 | codeField1.setText("");
221 | } else if (codeField3.isFocused()) {
222 | codeField2.requestFocus();
223 | codeField2.setText("");
224 | } else if (codeField4.isFocused()) {
225 | codeField3.requestFocus();
226 | codeField3.setText("");
227 | }
228 | }
229 |
230 | private OnClickListener btnListener = new OnClickListener() {
231 | @Override
232 | public void onClick(View view) {
233 | int currentValue = -1;
234 | int id = view.getId();
235 | if (id == R.id.button0) {
236 | currentValue = 0;
237 | } else if (id == R.id.button1) {
238 | currentValue = 1;
239 | } else if (id == R.id.button2) {
240 | currentValue = 2;
241 | } else if (id == R.id.button3) {
242 | currentValue = 3;
243 | } else if (id == R.id.button4) {
244 | currentValue = 4;
245 | } else if (id == R.id.button5) {
246 | currentValue = 5;
247 | } else if (id == R.id.button6) {
248 | currentValue = 6;
249 | } else if (id == R.id.button7) {
250 | currentValue = 7;
251 | } else if (id == R.id.button8) {
252 | currentValue = 8;
253 | } else if (id == R.id.button9) {
254 | currentValue = 9;
255 | } else {
256 | }
257 |
258 | // set the value and move the focus
259 | String currentValueString = String.valueOf(currentValue);
260 | if (codeField1.isFocused()) {
261 | codeField1.setText(currentValueString);
262 | codeField2.requestFocus();
263 | codeField2.setText("");
264 | } else if (codeField2.isFocused()) {
265 | codeField2.setText(currentValueString);
266 | codeField3.requestFocus();
267 | codeField3.setText("");
268 | } else if (codeField3.isFocused()) {
269 | codeField3.setText(currentValueString);
270 | codeField4.requestFocus();
271 | codeField4.setText("");
272 | } else if (codeField4.isFocused()) {
273 | codeField4.setText(currentValueString);
274 | }
275 |
276 | if (codeField4.getText().toString().length() > 0
277 | && codeField3.getText().toString().length() > 0
278 | && codeField2.getText().toString().length() > 0
279 | && codeField1.getText().toString().length() > 0) {
280 | onPasscodeInputed();
281 | }
282 | }
283 | };
284 |
285 | protected void onPasscodeError() {
286 | Toast toast = Toast.makeText(this, getString(R.string.passcode_wrong),
287 | Toast.LENGTH_SHORT);
288 | toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 30);
289 | toast.show();
290 |
291 | Thread thread = new Thread() {
292 | public void run() {
293 | Animation animation = AnimationUtils.loadAnimation(
294 | AppLockActivity.this, R.anim.shake);
295 | findViewById(R.id.ll_applock).startAnimation(animation);
296 | codeField1.setText("");
297 | codeField2.setText("");
298 | codeField3.setText("");
299 | codeField4.setText("");
300 | codeField1.requestFocus();
301 | }
302 | };
303 | runOnUiThread(thread);
304 | }
305 |
306 | private InputFilter numberFilter = new InputFilter() {
307 | @Override
308 | public CharSequence filter(CharSequence source, int start, int end,
309 | Spanned dest, int dstart, int dend) {
310 |
311 | if (source.length() > 1) {
312 | return "";
313 | }
314 |
315 | if (source.length() == 0) // erase
316 | {
317 | return null;
318 | }
319 |
320 | try {
321 | int number = Integer.parseInt(source.toString());
322 | if ((number >= 0) && (number <= 9))
323 | return String.valueOf(number);
324 | else
325 | return "";
326 | } catch (NumberFormatException e) {
327 | return "";
328 | }
329 | }
330 | };
331 |
332 | private OnTouchListener touchListener = new OnTouchListener() {
333 | @Override
334 | public boolean onTouch(View v, MotionEvent event) {
335 | v.performClick();
336 | clearFields();
337 | return false;
338 | }
339 | };
340 |
341 | private void clearFields() {
342 | codeField1.setText("");
343 | codeField2.setText("");
344 | codeField3.setText("");
345 | codeField4.setText("");
346 |
347 | codeField1.postDelayed(new Runnable() {
348 |
349 | @Override
350 | public void run() {
351 | codeField1.requestFocus();
352 | }
353 | }, 200);
354 | }
355 | }
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------