diff --git a/.gitignore b/.gitignore index 5782a95..a2b7eaf 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,6 @@ build captures local.properties gradle/wrapper/gradle-wrapper.jar +**/.project +**/.settings +**/.classpath \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 846c29c..8a7ab81 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' } diff --git a/app/src/main/java/by/yauhenl/gardine/RecentActivities.java b/app/src/main/java/by/yauhenl/gardine/DiscardingStack.java similarity index 55% rename from app/src/main/java/by/yauhenl/gardine/RecentActivities.java rename to app/src/main/java/by/yauhenl/gardine/DiscardingStack.java index 2ed7753..08a9b10 100644 --- a/app/src/main/java/by/yauhenl/gardine/RecentActivities.java +++ b/app/src/main/java/by/yauhenl/gardine/DiscardingStack.java @@ -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 + */ +public class DiscardingStack { private final int limit; - private LinkedHashSet apps; + private LinkedHashSet 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 { @@ -24,9 +30,9 @@ public void add(App app) { apps.add(app); } - public Collection getAll() { - ArrayDeque reversed = new ArrayDeque<>(); - for (App a : this.apps) { + public Collection getAll() { + ArrayDeque reversed = new ArrayDeque<>(); + for (T a : this.apps) { reversed.push(a); } return reversed; @@ -36,10 +42,10 @@ private void truncateSize(int limit) { if(apps.size() < limit) { return; } - Iterator iter = apps.iterator(); - for (int i = 0; iter.hasNext(); i++) { + Iterator iter = apps.iterator(); + for (int i = apps.size(); iter.hasNext(); i--) { iter.next(); - if(i >= limit) { + if(i > limit) { iter.remove(); } } diff --git a/app/src/main/java/by/yauhenl/gardine/GardineWidgetService.java b/app/src/main/java/by/yauhenl/gardine/GardineWidgetService.java index 5e072af..ab35adc 100644 --- a/app/src/main/java/by/yauhenl/gardine/GardineWidgetService.java +++ b/app/src/main/java/by/yauhenl/gardine/GardineWidgetService.java @@ -38,13 +38,13 @@ public class GardineWidgetService extends AccessibilityService { private WindowManager windowManager; private View gardine; private ArrayList recentApps; - private RecentActivities recentActivities; + private DiscardingStack recentActivities; private ArrayAdapter recentAppsAdapter; private Vibrator vibrator; public GardineWidgetService() { this.recentApps = new ArrayList<>(); - this.recentActivities = new RecentActivities(MAX_ITEMS); + this.recentActivities = new DiscardingStack<>(MAX_ITEMS); } @Override diff --git a/app/src/test/java/by/yauhenl/gardine/DiscardingStackTest.java b/app/src/test/java/by/yauhenl/gardine/DiscardingStackTest.java new file mode 100644 index 0000000..bcc3fa0 --- /dev/null +++ b/app/src/test/java/by/yauhenl/gardine/DiscardingStackTest.java @@ -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 ra = new DiscardingStack<>(3); + ra.add(a); + ra.add(a); + ra.add(a); + assertThat(ra.getAll()).containsOnly(a); + } + + @Test + public void twoElements_noTruncation() { + DiscardingStack 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 ra = new DiscardingStack<>(3); + ra.add(a); + ra.add(b); + ra.add(c); + ra.add(d); + assertThat(ra.getAll()).containsOnly(b, c, d); + } + + +} diff --git a/gradlew b/gradlew old mode 100644 new mode 100755