Skip to content

Commit

Permalink
Hide appgrid without back button (scroll up when on top of the list).…
Browse files Browse the repository at this point in the history
… Status and navbar handling on appgrid.
  • Loading branch information
krasanen committed Jan 28, 2024
1 parent ba6a489 commit 02aed70
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
61 changes: 57 additions & 4 deletions app/src/main/java/fi/zmengames/zen/AppGridActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import androidx.fragment.app.FragmentActivity;

Expand All @@ -18,13 +20,14 @@

public class AppGridActivity extends FragmentActivity {
private static final String TAG = AppGridActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Setting the theme needs to be done before setContentView()
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String theme = prefs.getString("theme", "light");

applySystemUi(isPreferenceHideNavBar(prefs), false);

switch (theme) {
case "dark":
Expand Down Expand Up @@ -64,14 +67,27 @@ private int getColorBasedOnTheme(Context context, int attr) {
return typedValue.data;
}

float startY;
float touchSlop = 400.0f; // amount scroll needed to close app grid
public static boolean onTop = false;
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (BuildConfig.DEBUG) Log.i(TAG, "dispatchTouchEvent: " + ev.getAction());
super.dispatchTouchEvent(ev);
if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
MainActivity.dismissPopup();
return true;

switch (ev.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
startY = ev.getY(); // Save the initial Y coordinate
MainActivity.dismissPopup();
break;
case MotionEvent.ACTION_MOVE:
float deltaY = ev.getY() - startY;
if (onTop && deltaY > touchSlop) {
onBackPressed();
}
break;
}

return true;
}
@Override
Expand All @@ -89,4 +105,41 @@ public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_settings, menu);
return true;
}

private void applySystemUi(boolean hideNavBar, boolean hideStatusBar) {
int visibility = 0;
if (hideNavBar) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
visibility = visibility
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION; // hide nav bar
} else {
visibility = visibility
| View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION; // hide nav bar
}
}
if (hideStatusBar) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
visibility = visibility
| View.SYSTEM_UI_FLAG_FULLSCREEN; // hide status bar
}
}
if (hideNavBar || hideStatusBar) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
visibility = visibility
| View.SYSTEM_UI_FLAG_IMMERSIVE;
}
}

View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(visibility);

}
private boolean isPreferenceHideNavBar(SharedPreferences prefs) {
return prefs.getBoolean("pref-hide-navbar", false);
}

private boolean isPreferenceHideStatusBar(SharedPreferences prefs) {
return prefs.getBoolean("pref-hide-statusbar", false);
}
}
18 changes: 18 additions & 0 deletions app/src/main/java/fi/zmengames/zen/AppsGridFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.GridView;
import android.widget.Toast;

Expand All @@ -29,6 +30,7 @@
public class AppsGridFragment extends GridFragment {

AppListAdapter mAdapter;
private GridView gridView;
private static final String TAG = AppsGridFragment.class.getSimpleName();
@Override
public void onActivityCreated(Bundle savedInstanceState) {
Expand All @@ -40,6 +42,22 @@ public void onActivityCreated(Bundle savedInstanceState) {
mAdapter = new AppListAdapter(getActivity());
setGridAdapter(mAdapter);

gridView = getGridView(); // Assuming you have a method to get the GridView
gridView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {

}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (firstVisibleItem == 0 && gridView.getChildAt(0) != null && gridView.getChildAt(0).getTop() == 0) {
AppGridActivity.onTop = true;
} else {
AppGridActivity.onTop = false;
}
}
});

List<Pojo> apps = KissApplication.getApplication(getContext()).getDataHandler().getApplications();
try {
Collections.sort(apps, new PojoComparator());
Expand Down

0 comments on commit 02aed70

Please sign in to comment.