Skip to content

Commit

Permalink
Added double tap to lock using screen timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
tanujnotes committed Dec 24, 2020
1 parent a1d42f0 commit 2fb2288
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

<uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />

<application
android:allowBackup="true"
Expand Down
81 changes: 79 additions & 2 deletions app/src/main/java/app/olauncher/light/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.LauncherActivityInfo;
import android.content.pm.LauncherApps;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.UserHandle;
Expand All @@ -21,6 +23,7 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowInsets;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
Expand All @@ -44,7 +47,7 @@ public class MainActivity extends Activity implements View.OnClickListener, View
private final int FLAG_LAUNCH_APP = 0;

private Prefs prefs;
private View appDrawer;
private View appDrawer, blackScreen;
private EditText search;
private ListView appListView;
private AppAdapter appAdapter;
Expand Down Expand Up @@ -100,16 +103,22 @@ public void afterTextChanged(Editable editable) {
@Override
protected void onResume() {
super.onResume();
blackScreen.setVisibility(View.GONE);
backToHome();
populateHomeApps();
refreshAppsList();
showNavBarAndResetScreenTimeout();
checkForDefaultLauncher();
}

@Override
public void onClick(View view) {
if (view.getId() == R.id.set_as_default_launcher) {
resetDefaultLauncher();
return;
} else if (view.getId() == R.id.black_screen) {
onResume();
return;
}
try {
int location = Integer.parseInt(view.getTag().toString());
Expand All @@ -133,6 +142,8 @@ public boolean onLongClick(View view) {
private void initClickListeners() {
setDefaultLauncher = findViewById(R.id.set_as_default_launcher);
setDefaultLauncher.setOnClickListener(this);
blackScreen = findViewById(R.id.black_screen);
blackScreen.setOnClickListener(this);

homeApp1 = findViewById(R.id.home_app_1);
homeApp2 = findViewById(R.id.home_app_2);
Expand Down Expand Up @@ -174,7 +185,6 @@ private void backToHome() {
homeAppsLayout.setVisibility(View.VISIBLE);
appAdapter.setFlag(FLAG_LAUNCH_APP);
hideKeyboard();
checkForDefaultLauncher();
appListView.setSelectionAfterHeaderView();
}

Expand Down Expand Up @@ -344,6 +354,72 @@ private void openLauncherPhoneSettings() {
}
}

private void openEditSettingsPermission() {
Toast.makeText(this, "Please grant this permission", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + BuildConfig.APPLICATION_ID));
startActivity(intent);
}

private void showLockPopup() {
String[] options = {"YES", "NO", "DON'T SHOW AGAIN"};
new AlertDialog.Builder(this)
.setTitle("Enable double tap to lock?")
.setItems(options, (dialog, which) -> {
switch (which) {
case 0:
openEditSettingsPermission();
break;
case 1:
dialog.dismiss();
break;
case 2:
prefs.setShowLockPopup(false);
break;
}
}).show();
}

private void setScreenTimeout() {
try {
int screenTimeoutInMillis = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT);
if (screenTimeoutInMillis >= 5000) prefs.setScreenTimeout(screenTimeoutInMillis);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 5000);
}

private void hideNavBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
getWindow().getInsetsController().hide(WindowInsets.Type.statusBars());
getWindow().getInsetsController().hide(WindowInsets.Type.navigationBars());
} else {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_IMMERSIVE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
}

private void showNavBarAndResetScreenTimeout() {
if (Settings.System.canWrite(this))
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, prefs.getScreenTimeout());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
getWindow().getInsetsController().show(WindowInsets.Type.navigationBars());
} else
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}

private void checkForDoubleTap() {
if (Settings.System.canWrite(this)) {
blackScreen.setVisibility(View.VISIBLE);
setScreenTimeout();
hideNavBar();
} else if (prefs.getShowLockPopup())
showLockPopup();
}

private AppClickListener getAppClickListener() {
return (appModel, flag) -> {
if (flag == FLAG_LAUNCH_APP) prepareToLaunchApp(appModel);
Expand Down Expand Up @@ -425,6 +501,7 @@ public void onLongClick() {

@Override
public void onDoubleClick() {
checkForDoubleTap();
super.onDoubleClick();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public boolean onSingleTapUp(MotionEvent motionEvent) {

@Override
public boolean onDoubleTap(MotionEvent motionEvent) {
onDoubleClick();
return super.onDoubleTap(motionEvent);
}

Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/app/olauncher/light/Prefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class Prefs {

private static final String PREF = "app.olauncher.light";
private static final String HOME_ALIGNMENT = "HOME_ALIGNMENT";
private static final String SHOW_LOCK_POPUP = "SHOW_LOCK_POPUP";
private static final String SCREEN_TIMEOUT = "SCREEN_TIMEOUT";

private static final String APP_NAME_1 = "APP_NAME_1";
private static final String APP_NAME_2 = "APP_NAME_2";
Expand Down Expand Up @@ -47,6 +49,22 @@ public void setHomeAlignment(int gravity) {
getSharedPref().edit().putInt(HOME_ALIGNMENT, gravity).apply();
}

public boolean getShowLockPopup() {
return getSharedPref().getBoolean(SHOW_LOCK_POPUP, true);
}

public void setShowLockPopup(boolean value) {
getSharedPref().edit().putBoolean(SHOW_LOCK_POPUP, value).apply();
}

public int getScreenTimeout() {
return getSharedPref().getInt(SCREEN_TIMEOUT, 30000);
}

public void setScreenTimeout(int value) {
getSharedPref().edit().putInt(SCREEN_TIMEOUT, value).apply();
}

public String getAppName(int location) {
switch (location) {
case 1:
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,11 @@

</FrameLayout>

<FrameLayout
android:id="@+id/black_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:visibility="gone" />

</FrameLayout>

0 comments on commit 2fb2288

Please sign in to comment.