Skip to content

Commit 5d1990c

Browse files
committed
PeekDisplayView: added clear all button mode
1 parent 822a94d commit 5d1990c

File tree

8 files changed

+146
-16
lines changed

8 files changed

+146
-16
lines changed

app/src/main/java/it/dhd/oxygencustomizer/ui/fragments/mods/lockscreen/LockscreenPeekNotificationsFragment.java

+50-4
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,25 @@
55
import static it.dhd.oxygencustomizer.xposed.utils.ViewHelper.dp2px;
66

77
import android.app.Notification;
8+
import android.content.res.ColorStateList;
89
import android.os.Bundle;
910
import android.os.Parcel;
1011
import android.service.notification.StatusBarNotification;
1112
import android.text.format.DateUtils;
1213
import android.view.LayoutInflater;
14+
import android.view.Menu;
15+
import android.view.MenuInflater;
16+
import android.view.MenuItem;
1317
import android.view.View;
1418
import android.view.ViewGroup;
1519

1620
import androidx.annotation.NonNull;
1721
import androidx.annotation.Nullable;
22+
import androidx.core.content.ContextCompat;
23+
import androidx.core.view.MenuHost;
24+
import androidx.core.view.MenuProvider;
1825
import androidx.fragment.app.FragmentManager;
26+
import androidx.lifecycle.Lifecycle;
1927

2028
import java.util.ArrayList;
2129
import java.util.List;
@@ -26,6 +34,7 @@
2634
import it.dhd.oxygencustomizer.ui.base.BaseFragment;
2735
import it.dhd.oxygencustomizer.ui.base.ControlledPreferenceFragmentCompat;
2836
import it.dhd.oxygencustomizer.ui.interfaces.PreferenceListener;
37+
import it.dhd.oxygencustomizer.utils.AppUtils;
2938
import it.dhd.oxygencustomizer.utils.OCPreferences;
3039
import it.dhd.oxygencustomizer.xposed.views.peek.PeekDisplayView;
3140

@@ -34,6 +43,8 @@ public class LockscreenPeekNotificationsFragment extends BaseFragment {
3443
private FragmentPeekNotificationsBinding binding;
3544
private PeekDisplayView mPeekView;
3645
private PreferenceListener mListener;
46+
private List<StatusBarNotification> mNotifications = new ArrayList<>();
47+
private int mLastNot = 1;
3748

3849
@Override
3950
public String getTitle() {
@@ -55,14 +66,41 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
5566

5667
@Override
5768
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
69+
70+
MenuHost menuHost = requireActivity();
71+
// Add menu items without using the Fragment Menu APIs
72+
// Note how we can tie the MenuProvider to the viewLifecycleOwner
73+
// and an optional Lifecycle.State (here, RESUMED) to indicate when
74+
// the menu should be visible
75+
menuHost.addMenuProvider(new MenuProvider() {
76+
@Override
77+
public void onCreateMenu(@NonNull Menu menu, @NonNull MenuInflater menuInflater) {
78+
// Add menu items here
79+
menu.add(1, 2, 0, R.string.add_notification)
80+
.setIcon(R.drawable.ic_add)
81+
.setIconTintList(ColorStateList.valueOf(ContextCompat.getColor(requireContext(), R.color.textColorPrimary)))
82+
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
83+
}
84+
85+
@Override
86+
public boolean onMenuItemSelected(@NonNull MenuItem menuItem) {
87+
// Handle the menu selection
88+
if (menuItem.getGroupId() == 1 && menuItem.getItemId() == 2) {
89+
addNewNotification();
90+
return true;
91+
}
92+
return false;
93+
}
94+
}, getViewLifecycleOwner(), Lifecycle.State.RESUMED);
95+
5896
mListener = this::updatePrefs;
5997
mPeekView = new PeekDisplayView(requireContext(), "TOP", true);
60-
List<StatusBarNotification> notifications = new ArrayList<>();
6198
for (int i = 1; i <= 4; i++) {
62-
notifications.add(createFakeSbn(BuildConfig.APPLICATION_ID, i, BuildConfig.APPLICATION_ID + " #" + i, "Test message " + i));
99+
mNotifications.add(createFakeSbn(BuildConfig.APPLICATION_ID, i, BuildConfig.APPLICATION_ID + " #" + i, "Test message " + i));
100+
mLastNot++;
63101
}
64102
mPeekView.updateNotificationShelf(
65-
notifications
103+
mNotifications
66104
);
67105
binding.peekContainer.addView(mPeekView);
68106
FragmentManager fragmentManager = getChildFragmentManager();
@@ -73,6 +111,12 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
73111
updatePrefs();
74112
}
75113

114+
private void addNewNotification() {
115+
mNotifications.add(createFakeSbn(BuildConfig.APPLICATION_ID, mLastNot, BuildConfig.APPLICATION_ID + " #" + mLastNot, "Test message " + mLastNot));
116+
mLastNot++;
117+
mPeekView.updateNotificationShelf(mNotifications);
118+
}
119+
76120
private StatusBarNotification createFakeSbn(String pkg, int id, String title, String text) {
77121
Parcel parcel = Parcel.obtain();
78122

@@ -130,7 +174,9 @@ private void updatePrefs() {
130174
OCPreferences.getInt(LOCKSCREEN_PEEK_CARD_BG_COLOR, mPeekView.getSurfaceColor()),
131175
radius,
132176
OCPreferences.getInt(LOCKSCREEN_PEEK_CARD_BUTTONS_COLOR, mPeekView.getPrimaryColor()),
133-
false
177+
false,
178+
Integer.parseInt(OCPreferences.getString(LOCKSCREEN_PEEK_CLEAR_ALL_MODE, "1")),
179+
OCPreferences.getInt(LOCKSCREEN_PEEK_CLEAR_ALL_COUNT, 4)
134180
);
135181
}
136182

app/src/main/java/it/dhd/oxygencustomizer/utils/Constants.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,8 @@ public static class LockscreenPeekNotifications {
652652
public static final String LOCKSCREEN_PEEK_TOP_MARGIN = "peek_top_margin";
653653
public static final String LOCKSCREEN_PEEK_USE_APP_ICON = "peek_use_app_icons";
654654
public static final String LOCKSCREEN_PEEK_IGNORE_SECURITY = "peek_ignore_security";
655+
public static final String LOCKSCREEN_PEEK_CLEAR_ALL_MODE = "peek_clear_all_mode";
656+
public static final String LOCKSCREEN_PEEK_CLEAR_ALL_COUNT = "peek_clear_all_count";
655657

656658
public static final String[] LOCKSCREEN_PEEK_PREFS = {
657659
LOCKSCREEN_PEEK_NOTIFICATIONS_ENABLED,
@@ -672,7 +674,10 @@ public static class LockscreenPeekNotifications {
672674
LOCKSCREEN_PEEK_ICON_STYLE,
673675
LOCKSCREEN_PEEK_ICON_SIZE,
674676
LOCKSCREEN_PEEK_ICON_MARGIN,
675-
LOCKSCREEN_PEEK_ICON_PADDING
677+
LOCKSCREEN_PEEK_ICON_PADDING,
678+
// Clear all mode
679+
LOCKSCREEN_PEEK_CLEAR_ALL_MODE,
680+
LOCKSCREEN_PEEK_CLEAR_ALL_COUNT
676681
};
677682

678683
}

app/src/main/java/it/dhd/oxygencustomizer/utils/PreferenceHelper.java

+5
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@
144144
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.LockscreenPeekNotifications.LOCKSCREEN_PEEK_CARD_TDX;
145145
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.LockscreenPeekNotifications.LOCKSCREEN_PEEK_CARD_TITLE_COLOR;
146146
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.LockscreenPeekNotifications.LOCKSCREEN_PEEK_CARD_TSX;
147+
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.LockscreenPeekNotifications.LOCKSCREEN_PEEK_CLEAR_ALL_COUNT;
148+
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.LockscreenPeekNotifications.LOCKSCREEN_PEEK_CLEAR_ALL_MODE;
147149
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.LockscreenPeekNotifications.LOCKSCREEN_PEEK_ICON_BG_COLOR;
148150
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.LockscreenPeekNotifications.LOCKSCREEN_PEEK_ICON_MARGIN;
149151
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.LockscreenPeekNotifications.LOCKSCREEN_PEEK_ICON_PADDING;
@@ -918,6 +920,9 @@ public static boolean isVisible(String key) {
918920
LOCKSCREEN_PEEK_ICON_PADDING -> {
919921
return instance.mPreferences.getString(LOCKSCREEN_PEEK_ICON_STYLE, "0").equals("2");
920922
}
923+
case LOCKSCREEN_PEEK_CLEAR_ALL_COUNT -> {
924+
return instance.mPreferences.getString(LOCKSCREEN_PEEK_CLEAR_ALL_MODE, "1").equals("0");
925+
}
921926

922927
// Aod Clocks
923928
case "aod_clock_custom",

app/src/main/java/it/dhd/oxygencustomizer/xposed/hooks/systemui/lockscreen/LockscreenPeekDisplay.java

+16-5
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ public class LockscreenPeekDisplay extends XposedMods {
6565
private int mPeekIconBgColor;
6666
private int mPeekIconSize, mPeekIconMargin, mPeekIconPadding;
6767

68+
// Clear all button
69+
private int mClearAllMode = 1;
70+
private int mClearAllCount = 4;
71+
6872
public LockscreenPeekDisplay(Context context) {
6973
super(context);
7074
}
@@ -97,6 +101,9 @@ public void updatePrefs(String... Key) {
97101

98102
mPeekCardButtonsColor = Xprefs.getInt(LOCKSCREEN_PEEK_CARD_BUTTONS_COLOR, PeekDisplayView.getPrimaryColor(mContext));
99103

104+
mClearAllMode = Integer.parseInt(Xprefs.getString(LOCKSCREEN_PEEK_CLEAR_ALL_MODE, "1"));
105+
mClearAllCount = Xprefs.getInt(LOCKSCREEN_PEEK_CLEAR_ALL_COUNT, 4);
106+
100107
if (Key.length > 0) {
101108
for (String peekPref : LOCKSCREEN_PEEK_PREFS) {
102109
if (peekPref.equals(Key[0])) {
@@ -152,11 +159,13 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable {
152159
placePeek();
153160
});
154161

155-
ReflectedClass NotificationKeyguardHelper = ReflectedClass.of("com.oplus.systemui.statusbar.notification.helper.NotificationKeyguardHelper");
156-
NotificationKeyguardHelper
157-
.before("shouldShowOnKeyguard")
162+
ReflectedClass NotificationLockscreenUserManagerImpl = ReflectedClass.of("com.android.systemui.statusbar.NotificationLockscreenUserManagerImpl");
163+
NotificationLockscreenUserManagerImpl
164+
.before("shouldShowLockscreenNotifications")
158165
.run(param -> {
159-
if (mPeekEnabled) param.setResult(false);
166+
if (mPeekEnabled) {
167+
param.setResult(false);
168+
}
160169
});
161170

162171
ReflectedClass NotificationPanelViewControllerExImp = ReflectedClass.of("com.oplus.systemui.shade.NotificationPanelViewControllerExImp");
@@ -260,7 +269,9 @@ private void updatePeekOptions() {
260269
mPeekCardBgColor,
261270
mPeekCardRadius,
262271
mPeekCardButtonsColor,
263-
mPeekAppIcons
272+
mPeekAppIcons,
273+
mClearAllMode,
274+
mClearAllCount
264275
);
265276

266277
}

app/src/main/java/it/dhd/oxygencustomizer/xposed/views/peek/PeekDisplayView.java

+31-6
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ public class PeekDisplayView extends LinearLayout {
114114

115115
private boolean mIsSettingsInterface = false;
116116

117+
private static final int CLEAR_ALL_MODE_NONE = 0;
118+
private static final int CLEAR_ALL_MODE_COUNT = 1;
119+
private int mClearAllMode = CLEAR_ALL_MODE_COUNT;
120+
private int mClearAllCount = 4;
121+
117122
public PeekDisplayView(Context context, String tag, boolean settingsInterface) {
118123
super(context);
119124
Log.d("PeekDisplayView", "PeekDisplayView: Constructor");
@@ -285,7 +290,8 @@ public void updatePeekStyle(
285290
int backgroundColor, int iconSize, int iconMarginEnd, int iconPadding,
286291
int titleColor, int summaryColor, int cardBackgroundColor, float[] cardBackgroundRadius,
287292
int buttonsColor,
288-
boolean useAppIcons) {
293+
boolean useAppIcons,
294+
int clearAllMode, int clearAllCount) {
289295
Log.d("PeekDisplayView", "updatePeekStyle: " + newStyle + " " + newIconStyle + "\n" +
290296
"backgroundColor: " + backgroundColor + "\n" +
291297
"iconSize: " + iconSize + "\n" +
@@ -299,7 +305,9 @@ public void updatePeekStyle(
299305
"useAppIcons: " + useAppIcons);
300306
boolean requireUpdate = checkChange(newIconStyle, iconSize, backgroundColor, iconMarginEnd, iconPadding) ||
301307
newIconStyle != mCurrentPeekStyle.getPeekIconStyle().getStyle() ||
302-
useAppIcons != mCurrentPeekStyle.useAppIcons();
308+
useAppIcons != mCurrentPeekStyle.useAppIcons() ||
309+
clearAllCount != mClearAllCount ||
310+
clearAllMode != mClearAllMode;
303311
PEEK_STYLE_CUSTOM = new PeekStyle(
304312
"custom", 2,
305313
titleColor, summaryColor, cardBackgroundColor, cardBackgroundRadius,
@@ -318,6 +326,8 @@ public void updatePeekStyle(
318326
mCurrentPeekStyle = PEEK_STYLE_CUSTOM;
319327
break;
320328
}
329+
mClearAllMode = clearAllMode;
330+
mClearAllCount = clearAllCount;
321331
if (requireUpdate) {
322332
List<StatusBarNotification> notif = notificationAdapter.getNotifications();
323333
notificationAdapter = new NotificationAdapter();
@@ -412,7 +422,7 @@ private void inflateView() {
412422
@Override
413423
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
414424
super.onScrolled(recyclerView, dx, dy);
415-
showClearAllButton();
425+
if (mClearAllMode == CLEAR_ALL_MODE_COUNT) showClearAllButton();
416426
}
417427
});
418428
addView(view);
@@ -460,7 +470,10 @@ private void clearAllNotifications() {
460470
private void removeCurrentNotification() {
461471
if (currentDisplayedNotification != null) {
462472
if (mIsSettingsInterface) {
463-
post(() -> Toast.makeText(appContext, "Remove current notification", Toast.LENGTH_SHORT).show());
473+
List<StatusBarNotification> newNotif = new ArrayList<>(lastFilteredNotifications);
474+
newNotif.remove(currentDisplayedNotification);
475+
updateNotificationShelf(newNotif);
476+
hideNotificationCard();
464477
return;
465478
}
466479
StatusBarNotification sbn = currentDisplayedNotification;
@@ -515,6 +528,9 @@ public void updateNotificationShelf(List<StatusBarNotification> notificationList
515528

516529
int iconMarginEnd = mCurrentPeekStyle.getPeekIconStyle().getIconSpacing();
517530
int newWidth = filteredNotifications.size() <= 4 ? ViewGroup.LayoutParams.WRAP_CONTENT : (4 * getIconSize()) + (4 * iconMarginEnd);
531+
if (mClearAllMode == CLEAR_ALL_MODE_NONE) {
532+
newWidth = ViewGroup.LayoutParams.MATCH_PARENT;
533+
}
518534

519535
if (newWidth != lastLayoutWidth) {
520536
ViewGroup.LayoutParams layoutParams = notificationShelf.getLayoutParams();
@@ -523,7 +539,9 @@ public void updateNotificationShelf(List<StatusBarNotification> notificationList
523539
lastLayoutWidth = newWidth;
524540
}
525541

526-
showOverflow = filteredNotifications.size() > 4;
542+
showOverflow = mClearAllMode == CLEAR_ALL_MODE_COUNT ?
543+
filteredNotifications.size() > mClearAllCount :
544+
false;
527545
overflowText.setVisibility(showOverflow ? View.VISIBLE : View.GONE);
528546
clearAllButton.setVisibility(View.GONE);
529547
}
@@ -769,7 +787,14 @@ private int getIconPadding() {
769787
public void onBindViewHolder(@NonNull NotificationViewHolder holder, @SuppressLint("RecyclerView") int position) {
770788
StatusBarNotification notification = notifications.get(position);
771789
RecyclerView.LayoutParams imageParams = (RecyclerView.LayoutParams) holder.iconView.getLayoutParams();
772-
imageParams.setMarginEnd(position != notifications.size() - 1 ? mCurrentPeekStyle.getPeekIconStyle().getIconSpacing() : 0);
790+
int marginEnd = 0;
791+
if (position != notifications.size() - 1) {
792+
marginEnd = mCurrentPeekStyle.getPeekIconStyle().getIconSpacing();
793+
}
794+
if (getItemCount() > mClearAllCount && mClearAllMode == CLEAR_ALL_MODE_COUNT) {
795+
marginEnd = mCurrentPeekStyle.getPeekIconStyle().getIconSpacing();
796+
}
797+
imageParams.setMarginEnd(marginEnd);
773798
holder.iconView.setLayoutParams(imageParams);
774799
Drawable iconDrawable =
775800
mCurrentPeekStyle.useAppIcons() ?

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

+10
Original file line numberDiff line numberDiff line change
@@ -1029,4 +1029,14 @@
10291029
<item>1</item>
10301030
</string-array>
10311031

1032+
<!-- Peek clear all mode -->
1033+
<string-array name="peek_clear_all_mode_entries" translatable="false">
1034+
<item>@string/peek_clear_all_mode_always_hidden</item>
1035+
<item>@string/peek_clear_all_mode_count</item>
1036+
</string-array>
1037+
<string-array name="peek_clear_all_mode_values" translatable="false">
1038+
<item>0</item>
1039+
<item>1</item>
1040+
</string-array>
1041+
10321042
</resources>

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

+7
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
<string name="restart_module">Restart scoped processes</string>
127127
<string name="restart_page_scope">Restart only this page scope</string>
128128
<string name="restart_scope_message">Are you sure you want to restart all scoped processes except the system framework?\nUse this if the module isn\'t working!\nMake sure all the relevant scopes are checked!</string>
129+
<string name="add_notification">Add notification</string>
129130

130131
<!-- NavBar Strings -->
131132
<string name="navbar_ui">UI</string>
@@ -1493,6 +1494,12 @@
14931494
<string name="peek_buttons_color">Buttons color</string>
14941495
<string name="peek_title_color">Title color</string>
14951496
<string name="peek_content_color">Content color</string>
1497+
<string name="peek_clear_all_cat">Clear all button</string>
1498+
<string name="peek_clear_all_mode">Clear all mode</string>
1499+
<string name="peek_clear_all_mode_always_hidden">Always hidden</string>
1500+
<string name="peek_clear_all_mode_count">Based on notification count</string>
1501+
<string name="peek_clear_all_count_title">Notification count</string>
1502+
<string name="peek_clear_all_count_summary">The clear all button will show only if notifications are more than selected number</string>
14961503

14971504
<!-- Lag Fix Strings -->
14981505
<string name="fix_lag_title">App Lag Fix</string>

app/src/main/res/xml/lockscreen_peek_notifications_prefs.xml

+21
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,27 @@
148148

149149
</it.dhd.oneplusui.preference.OplusPreferenceCategory>
150150

151+
<it.dhd.oneplusui.preference.OplusPreferenceCategory
152+
android:title="@string/peek_clear_all_cat">
153+
154+
<it.dhd.oneplusui.preference.OplusMenuPreference
155+
android:title="@string/peek_clear_all_mode"
156+
android:key="peek_clear_all_mode"
157+
android:defaultValue="1"
158+
app:useSimpleSummaryProvider="true"
159+
android:entries="@array/peek_clear_all_mode_entries"
160+
android:entryValues="@array/peek_clear_all_mode_values" />
161+
162+
<it.dhd.oneplusui.preference.OplusSectionSeekbarPreference
163+
android:title="@string/peek_clear_all_count_title"
164+
android:summary="@string/peek_clear_all_count_summary"
165+
app:defaultValue="4"
166+
android:key="peek_clear_all_count"
167+
android:min="4"
168+
android:max="8" />
169+
170+
</it.dhd.oneplusui.preference.OplusPreferenceCategory>
171+
151172
<it.dhd.oneplusui.preference.OplusPreferenceCategory>
152173

153174
<it.dhd.oneplusui.preference.OplusSwitchPreference

0 commit comments

Comments
 (0)