Skip to content

Commit

Permalink
fix: discard old items in recent activities list
Browse files Browse the repository at this point in the history
  • Loading branch information
yauhen-l committed Jun 7, 2020
1 parent 4c36cd3 commit 993e27e
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ build
captures
local.properties
gradle/wrapper/gradle-wrapper.jar
**/.project
**/.settings
**/.classpath
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ dependencies {
})
compile 'com.android.support:appcompat-v7:25.2.0'
//compile 'com.android.support.constraint:constraint-layout:1.1.3'
//testCompile 'org.testng:testng:7.1.0'
testCompile 'junit:junit:4.12'
testCompile 'org.assertj:assertj-core:3.16.1'
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@
import java.util.Iterator;
import java.util.LinkedHashSet;

public class RecentActivities {
/**
* Stack (FILO) of unique elements.
* All elements over limit are dropped.
* Adding the same element several times moves it on top of the stack.
* @param <T>
*/
public class DiscardingStack<T> {

private final int limit;
private LinkedHashSet<App> apps;
private LinkedHashSet<T> apps;

public RecentActivities(int limit) {
public DiscardingStack(int limit) {
this.limit = limit;
this.apps = new LinkedHashSet<>();
}

public void add(App app) {
public void add(T app) {
if(apps.contains(app)) {
apps.remove(app);
} else {
Expand All @@ -24,9 +30,9 @@ public void add(App app) {
apps.add(app);
}

public Collection<App> getAll() {
ArrayDeque<App> reversed = new ArrayDeque<>();
for (App a : this.apps) {
public Collection<T> getAll() {
ArrayDeque<T> reversed = new ArrayDeque<>();
for (T a : this.apps) {
reversed.push(a);
}
return reversed;
Expand All @@ -36,10 +42,10 @@ private void truncateSize(int limit) {
if(apps.size() < limit) {
return;
}
Iterator<App> iter = apps.iterator();
for (int i = 0; iter.hasNext(); i++) {
Iterator<T> iter = apps.iterator();
for (int i = apps.size(); iter.hasNext(); i--) {
iter.next();
if(i >= limit) {
if(i > limit) {
iter.remove();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ public class GardineWidgetService extends AccessibilityService {
private WindowManager windowManager;
private View gardine;
private ArrayList<App> recentApps;
private RecentActivities recentActivities;
private DiscardingStack<App> recentActivities;
private ArrayAdapter<App> recentAppsAdapter;
private Vibrator vibrator;

public GardineWidgetService() {
this.recentApps = new ArrayList<>();
this.recentActivities = new RecentActivities(MAX_ITEMS);
this.recentActivities = new DiscardingStack<>(MAX_ITEMS);
}

@Override
Expand Down
39 changes: 39 additions & 0 deletions app/src/test/java/by/yauhenl/gardine/DiscardingStackTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package by.yauhenl.gardine;

import org.junit.Test;
import static org.assertj.core.api.Java6Assertions.*;

public class RecentActivitiesTest {

private static final String a = "a", b = "b", c = "c", d = "d";

@Test
public void singleElement_noTruncation() {
DiscardingStack<String> ra = new DiscardingStack<>(3);
ra.add(a);
ra.add(a);
ra.add(a);
assertThat(ra.getAll()).containsOnly(a);
}

@Test
public void twoElements_noTruncation() {
DiscardingStack<String> ra = new DiscardingStack<>(3);
ra.add(a);
ra.add(b);
ra.add(a);
assertThat(ra.getAll()).containsOnly(a, b);
}

@Test
public void threeElements_withTruncation() {
DiscardingStack<String> ra = new DiscardingStack<>(3);
ra.add(a);
ra.add(b);
ra.add(c);
ra.add(d);
assertThat(ra.getAll()).containsOnly(b, c, d);
}


}
Empty file modified gradlew
100644 → 100755
Empty file.

0 comments on commit 993e27e

Please sign in to comment.