├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── drawable-xxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── styles.xml │ │ │ │ └── dimens.xml │ │ │ └── layout │ │ │ │ └── activity_apps.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── fr │ │ │ └── xebia │ │ │ └── jmartinez │ │ │ └── installed │ │ │ ├── AppsProvider.java │ │ │ ├── AppsPresenter.java │ │ │ ├── App.java │ │ │ └── AppsActivity.java │ ├── androidTest │ │ └── java │ │ │ └── fr │ │ │ └── xebia │ │ │ └── jmartinez │ │ │ └── installed │ │ │ ├── internal │ │ │ ├── SpoonRule.java │ │ │ ├── ActivityRule.java │ │ │ └── JUnitTestCase.java │ │ │ └── AppsActivityTest.java │ └── test │ │ └── java │ │ └── fr │ │ └── xebia │ │ └── jmartinez │ │ └── installed │ │ ├── AppsPresenterTest.java │ │ └── AppTest.java ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── .gitignore ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradle.properties ├── gradlew.bat └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /local.properties 3 | /.idea/workspace.xml 4 | /.idea/libraries 5 | .DS_Store 6 | /build 7 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeremiemartinez/installed_apps/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeremiemartinez/installed_apps/HEAD/app/src/main/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Installed Apps 3 | Enter text… 4 | My installed applications 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Apr 10 15:27:10 PDT 2013 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip 7 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /usr/local/opt/android-sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_apps.xml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 13 | 14 | 15 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 10 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/fr/xebia/jmartinez/installed/internal/SpoonRule.java: -------------------------------------------------------------------------------- 1 | package fr.xebia.jmartinez.installed.internal; 2 | 3 | import android.app.Activity; 4 | 5 | import com.squareup.spoon.Spoon; 6 | 7 | import org.junit.rules.TestWatcher; 8 | import org.junit.runner.Description; 9 | 10 | public class SpoonRule extends TestWatcher { 11 | 12 | private String className; 13 | private String methodName; 14 | 15 | @Override 16 | protected void starting(Description description) { 17 | super.starting(description); 18 | className = description.getClassName(); 19 | methodName = description.getMethodName(); 20 | } 21 | 22 | public void takeScreenshot(Activity activity, String tag) { 23 | Spoon.screenshot(activity, tag, className, methodName); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /app/src/main/java/fr/xebia/jmartinez/installed/AppsProvider.java: -------------------------------------------------------------------------------- 1 | package fr.xebia.jmartinez.installed; 2 | 3 | import android.content.Context; 4 | import android.content.pm.ApplicationInfo; 5 | import android.content.pm.PackageManager; 6 | 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | 10 | public class AppsProvider { 11 | 12 | private final Context context; 13 | 14 | public AppsProvider(Context context) { 15 | this.context = context; 16 | } 17 | 18 | public List findInstalledApps() { 19 | PackageManager packageManager = context.getPackageManager(); 20 | List installedApps = packageManager.getInstalledApplications(PackageManager.GET_META_DATA); 21 | List result = new ArrayList<>(installedApps.size()); 22 | for (ApplicationInfo installedApp : installedApps) { 23 | result.add(new App(installedApp.loadLabel(packageManager).toString())); 24 | } 25 | return result; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/java/fr/xebia/jmartinez/installed/AppsPresenter.java: -------------------------------------------------------------------------------- 1 | package fr.xebia.jmartinez.installed; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | import java.util.List; 6 | 7 | public class AppsPresenter { 8 | private final AppsActivity appsActivity; 9 | private final AppsProvider appsProvider; 10 | 11 | public AppsPresenter(AppsActivity appsActivity, AppsProvider appsProvider) { 12 | this.appsActivity = appsActivity; 13 | this.appsProvider = appsProvider; 14 | } 15 | 16 | public void showAllApps() { 17 | appsActivity.showApps(sort(appsProvider.findInstalledApps())); 18 | } 19 | 20 | public void filterApps(String text) { 21 | appsActivity.showApps(sort(filter(text))); 22 | } 23 | 24 | protected List sort(List apps) { 25 | Collections.sort(apps); 26 | return apps; 27 | } 28 | 29 | protected List filter(String filter) { 30 | List installedApps = appsProvider.findInstalledApps(); 31 | List result = new ArrayList<>(installedApps); 32 | for (App app : installedApps) { 33 | if (!app.matches(filter)) { 34 | result.remove(app); 35 | } 36 | } 37 | return result; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /app/src/androidTest/java/fr/xebia/jmartinez/installed/AppsActivityTest.java: -------------------------------------------------------------------------------- 1 | package fr.xebia.jmartinez.installed; 2 | 3 | import android.support.test.runner.AndroidJUnit4; 4 | 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | 8 | import fr.xebia.jmartinez.installed.internal.JUnitTestCase; 9 | 10 | import static android.support.test.espresso.Espresso.onView; 11 | import static android.support.test.espresso.action.ViewActions.typeText; 12 | import static android.support.test.espresso.assertion.ViewAssertions.matches; 13 | import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; 14 | import static android.support.test.espresso.matcher.ViewMatchers.withId; 15 | import static android.support.test.espresso.matcher.ViewMatchers.withText; 16 | 17 | @RunWith(AndroidJUnit4.class) 18 | public class AppsActivityTest extends JUnitTestCase { 19 | 20 | public AppsActivityTest() { 21 | super(AppsActivity.class); 22 | } 23 | 24 | @Test 25 | public void testFilterInstalledApp() { 26 | onView(withId(R.id.filter)).check(matches(isDisplayed())); 27 | takeScreenshot("Display_All_Installed_Apps"); 28 | onView(withId(R.id.filter)).perform(typeText("Installed")); 29 | onView(withText(R.string.app_name)).check(matches(isDisplayed())); 30 | takeScreenshot("Display_Filtered_Installed_Apps"); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /app/src/main/java/fr/xebia/jmartinez/installed/App.java: -------------------------------------------------------------------------------- 1 | package fr.xebia.jmartinez.installed; 2 | 3 | import android.os.Parcel; 4 | import android.os.Parcelable; 5 | 6 | public class App implements Parcelable, Comparable { 7 | 8 | public final String title; 9 | 10 | public App(String title) { 11 | this.title = title; 12 | } 13 | 14 | public boolean matches(String filter) { 15 | return filter != null && title.contains(filter); 16 | } 17 | 18 | @Override 19 | public int compareTo(App app) { 20 | return title.compareTo(app.title); 21 | } 22 | 23 | @Override 24 | public boolean equals(Object o) { 25 | if (this == o) return true; 26 | if (o == null || getClass() != o.getClass()) return false; 27 | 28 | App app = (App) o; 29 | 30 | if (title != null ? !title.equals(app.title) : app.title != null) return false; 31 | 32 | return true; 33 | } 34 | 35 | @Override 36 | public int hashCode() { 37 | return title != null ? title.hashCode() : 0; 38 | } 39 | 40 | 41 | @Override 42 | public int describeContents() { 43 | return 0; 44 | } 45 | 46 | @Override 47 | public void writeToParcel(Parcel dest, int flags) { 48 | dest.writeString(this.title); 49 | } 50 | 51 | private App(Parcel in) { 52 | this.title = in.readString(); 53 | } 54 | 55 | public static final Creator CREATOR = new Creator() { 56 | public App createFromParcel(Parcel source) { 57 | return new App(source); 58 | } 59 | 60 | public App[] newArray(int size) { 61 | return new App[size]; 62 | } 63 | }; 64 | 65 | } 66 | -------------------------------------------------------------------------------- /app/src/test/java/fr/xebia/jmartinez/installed/AppsPresenterTest.java: -------------------------------------------------------------------------------- 1 | package fr.xebia.jmartinez.installed; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.mockito.InjectMocks; 6 | import org.mockito.Mock; 7 | import org.mockito.runners.MockitoJUnitRunner; 8 | 9 | import java.util.Arrays; 10 | import java.util.List; 11 | 12 | import static org.assertj.core.api.Assertions.assertThat; 13 | import static org.mockito.Mockito.verify; 14 | import static org.mockito.Mockito.when; 15 | 16 | @RunWith(MockitoJUnitRunner.class) 17 | public class AppsPresenterTest { 18 | 19 | @Mock AppsActivity activity; 20 | @Mock AppsProvider appsProvider; 21 | @InjectMocks AppsPresenter appsPresenter; 22 | 23 | @Test 24 | public void should_filter() throws Exception { 25 | // Given 26 | when(appsProvider.findInstalledApps()).thenReturn(Arrays.asList(new App("AAA"), new App("BBB"))); 27 | 28 | // When 29 | List result = appsPresenter.filter("A"); 30 | 31 | // Then 32 | assertThat(result).hasSize(1); 33 | assertThat(result.get(0).title).isEqualTo("AAA"); 34 | } 35 | 36 | @Test 37 | public void should_sort() throws Exception { 38 | // Given 39 | List apps = Arrays.asList(new App("BBB"), new App("AAA")); 40 | 41 | // When 42 | List result = appsPresenter.sort(apps); 43 | 44 | // Then 45 | assertThat(result).hasSize(2); 46 | assertThat(result.get(0).title).isEqualTo("AAA"); 47 | assertThat(result.get(1).title).isEqualTo("BBB"); 48 | } 49 | 50 | @Test 51 | public void should_filter_apps() throws Exception { 52 | // Given 53 | when(appsProvider.findInstalledApps()).thenReturn(Arrays.asList(new App("BBB"), new App("AAA"), new App("ABB"))); 54 | 55 | // When 56 | appsPresenter.filterApps("A"); 57 | 58 | // Then 59 | verify(activity).showApps(Arrays.asList(new App("AAA"), new App("ABB"))); 60 | } 61 | 62 | 63 | @Test 64 | public void should_show_all_apps() throws Exception { 65 | // Given 66 | when(appsProvider.findInstalledApps()).thenReturn(Arrays.asList(new App("BBB"), new App("AAA"))); 67 | 68 | // When 69 | appsPresenter.showAllApps(); 70 | 71 | // Then 72 | verify(activity).showApps(Arrays.asList(new App("AAA"), new App("BBB"))); 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | jcenter() 4 | } 5 | dependencies { 6 | classpath 'com.stanfy.spoon:spoon-gradle-plugin:1.0.1' 7 | } 8 | } 9 | 10 | apply plugin: 'com.android.application' 11 | apply plugin: 'spoon' 12 | 13 | android { 14 | compileSdkVersion 22 15 | buildToolsVersion "22.0.0" 16 | 17 | packagingOptions { 18 | exclude 'META-INF/LICENSE.txt' 19 | exclude 'LICENSE.txt' 20 | exclude 'META-INF/LICENSE' 21 | exclude 'META-INF/NOTICE' 22 | } 23 | 24 | defaultConfig { 25 | applicationId "fr.xebia.jmartinez.installed" 26 | minSdkVersion 15 27 | targetSdkVersion 22 28 | versionCode 1 29 | versionName "1.0" 30 | 31 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 32 | } 33 | 34 | compileOptions { 35 | sourceCompatibility JavaVersion.VERSION_1_7 36 | targetCompatibility JavaVersion.VERSION_1_7 37 | } 38 | 39 | buildTypes { 40 | debug { 41 | minifyEnabled false 42 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 43 | } 44 | } 45 | } 46 | 47 | tasks.withType(JavaCompile) { 48 | options.encoding = "UTF-8" 49 | } 50 | 51 | 52 | 53 | dependencies { 54 | // BUILD 55 | compile 'com.android.support:appcompat-v7:22.0.0' 56 | 57 | configurations { 58 | androidTestCompile.exclude module: 'support-v4' 59 | androidTestCompile.exclude module: 'support-v7' 60 | androidTestCompile.exclude module: 'android' 61 | } 62 | 63 | // UNIT TESTING 64 | testCompile( 65 | 'junit:junit:4.11', 66 | 'com.android.support:support-annotations:22.0.0', 67 | 'com.squareup.assertj:assertj-android:1.0.0', 68 | 'org.mockito:mockito-core:1.9.5', 69 | 'org.assertj:assertj-core:1.7.0' 70 | ) 71 | testCompile('org.robolectric:robolectric:2.4') { 72 | exclude group: 'commons-logging' 73 | exclude group: 'org.apache.httpcomponents' 74 | } 75 | 76 | // INTEGRATION TESTING 77 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.0', 78 | 'com.android.support.test.espresso:espresso-idling-resource:2.0', 79 | 'com.android.support.test:testing-support-lib:0.1', 80 | 'com.squareup.spoon:spoon-client:1.1.8') 81 | androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.0') { 82 | exclude group: 'com.android.support', module: 'support-annotations' 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /app/src/test/java/fr/xebia/jmartinez/installed/AppTest.java: -------------------------------------------------------------------------------- 1 | package fr.xebia.jmartinez.installed; 2 | 3 | import android.os.Parcel; 4 | 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.robolectric.RobolectricTestRunner; 8 | 9 | import static org.assertj.core.api.Assertions.assertThat; 10 | 11 | @RunWith(RobolectricTestRunner.class) 12 | public class AppTest { 13 | 14 | @Test 15 | public void should_restore_from_parcelable() { 16 | // Given 17 | App app = new App("MyTitle"); 18 | 19 | // When 20 | Parcel parcel = Parcel.obtain(); 21 | app.writeToParcel(parcel, 0); 22 | parcel.setDataPosition(0); 23 | 24 | // Then 25 | App fromParcel = App.CREATOR.createFromParcel(parcel); 26 | assertThat(fromParcel.title).isEqualTo("MyTitle"); 27 | } 28 | 29 | @Test 30 | public void should_match() { 31 | // Given 32 | App app = new App("MyTitle"); 33 | 34 | // When 35 | boolean result = app.matches("My"); 36 | 37 | // Then 38 | assertThat(result).isTrue(); 39 | } 40 | 41 | @Test 42 | public void should_not_match() { 43 | // Given 44 | App app = new App("MyTitle"); 45 | 46 | // When 47 | boolean result = app.matches("My"); 48 | 49 | // Then 50 | assertThat(result).isTrue(); 51 | } 52 | 53 | @Test 54 | public void should_not_match_null() { 55 | // Given 56 | App app = new App("MyTitle"); 57 | 58 | // When 59 | boolean result = app.matches(null); 60 | 61 | // Then 62 | assertThat(result).isFalse(); 63 | } 64 | 65 | @Test 66 | public void should_not_match_null_empty() { 67 | // Given 68 | App app = new App("MyTitle"); 69 | 70 | // When 71 | boolean result = app.matches(""); 72 | 73 | // Then 74 | assertThat(result).isTrue(); 75 | } 76 | 77 | @Test 78 | public void should_compare_less() { 79 | // Given 80 | App app = new App("AAA"); 81 | 82 | // When 83 | int result = app.compareTo(new App("BBB")); 84 | 85 | // Then 86 | assertThat(result).isEqualTo(-1); 87 | } 88 | 89 | @Test 90 | public void should_compare_greater() { 91 | // Given 92 | App app = new App("BBB"); 93 | 94 | // When 95 | int result = app.compareTo(new App("AAA")); 96 | 97 | // Then 98 | assertThat(result).isEqualTo(1); 99 | } 100 | 101 | @Test 102 | public void should_compare_same() { 103 | // Given 104 | App app = new App("AAA"); 105 | 106 | // When 107 | int result = app.compareTo(new App("AAA")); 108 | 109 | // Then 110 | assertThat(result).isEqualTo(0); 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /app/src/main/java/fr/xebia/jmartinez/installed/AppsActivity.java: -------------------------------------------------------------------------------- 1 | package fr.xebia.jmartinez.installed; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.ActionBarActivity; 5 | import android.text.Editable; 6 | import android.text.TextWatcher; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.widget.BaseAdapter; 10 | import android.widget.EditText; 11 | import android.widget.ListView; 12 | import android.widget.TextView; 13 | 14 | import java.util.ArrayList; 15 | import java.util.List; 16 | 17 | public class AppsActivity extends ActionBarActivity implements TextWatcher { 18 | 19 | private AppsAdapter adapter; 20 | private AppsPresenter appsPresenter; 21 | 22 | @Override 23 | protected void onCreate(Bundle savedInstanceState) { 24 | super.onCreate(savedInstanceState); 25 | setContentView(R.layout.activity_apps); 26 | appsPresenter = new AppsPresenter(this, new AppsProvider(this)); 27 | ListView listView = (ListView) findViewById(R.id.list); 28 | EditText filterView = (EditText) findViewById(R.id.filter); 29 | adapter = new AppsAdapter(); 30 | listView.setAdapter(adapter); 31 | filterView.addTextChangedListener(this); 32 | } 33 | 34 | @Override 35 | protected void onResume() { 36 | super.onResume(); 37 | appsPresenter.showAllApps(); 38 | } 39 | 40 | public void showApps(List installedApps) { 41 | adapter.setApps(installedApps); 42 | } 43 | 44 | @Override 45 | public void onTextChanged(CharSequence charSequence, int start, int before, int count) { 46 | appsPresenter.filterApps(charSequence.toString()); 47 | } 48 | 49 | @Override 50 | public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) { 51 | // Do nothing 52 | } 53 | 54 | @Override 55 | public void afterTextChanged(Editable editable) { 56 | // Do nothing 57 | } 58 | 59 | private class AppsAdapter extends BaseAdapter { 60 | 61 | private List apps = new ArrayList<>(); 62 | 63 | @Override 64 | public int getCount() { 65 | return apps.size(); 66 | } 67 | 68 | @Override 69 | public App getItem(int position) { 70 | return apps.get(position); 71 | } 72 | 73 | @Override 74 | public long getItemId(int position) { 75 | return getItem(position).hashCode(); 76 | } 77 | 78 | @Override 79 | public boolean isEnabled(int position) { 80 | return false; 81 | } 82 | 83 | @Override 84 | public TextView getView(int position, View convertView, ViewGroup viewGroup) { 85 | final TextView view; 86 | if (convertView == null) { 87 | view = new TextView(AppsActivity.this); 88 | view.setTextSize(20f); 89 | view.setPadding(30, 30, 30, 30); 90 | } else { 91 | view = (TextView) convertView; 92 | } 93 | view.setText(apps.get(position).title); 94 | return view; 95 | } 96 | 97 | public void setApps(List installedApps) { 98 | apps.clear(); 99 | apps.addAll(installedApps); 100 | notifyDataSetChanged(); 101 | } 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /app/src/androidTest/java/fr/xebia/jmartinez/installed/internal/ActivityRule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Jake Wharton 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package fr.xebia.jmartinez.installed.internal; 17 | 18 | import android.app.Activity; 19 | import android.app.Instrumentation; 20 | import android.content.Intent; 21 | import android.support.test.InstrumentationRegistry; 22 | import org.junit.Before; 23 | import org.junit.Rule; 24 | import org.junit.rules.TestRule; 25 | import org.junit.runner.Description; 26 | import org.junit.runners.model.Statement; 27 | 28 | /** 29 | * A JUnit {@link org.junit.Rule @Rule} which launches an activity when your test starts. Stop extending 30 | * gross {@code ActivityInstrumentationBarfCase2}! 31 | *

32 | * Usage: 33 | *

{@code
 34 |  * @Rule
 35 |  * public final ActivityRule example =
 36 |  *     new ActivityRule<>(ExampleActivity.class);
 37 |  * }
38 | * 39 | * This will automatically launch the activity for each test method. The instance will also be 40 | * created sooner should you need to use it in a {@link org.junit.Before @Before} method. 41 | *

42 | * You can also customize the way in which the activity is launched by overriding 43 | * {@link #getLaunchIntent(String, Class)} and customizing or replacing the {@link android.content.Intent}. 44 | *

{@code
 45 |  * @Rule
 46 |  * public final ActivityRule example =
 47 |  *     new ActivityRule(ExampleActivity.class) {
 48 |  *       @Override
 49 |  *       protected Intent getLaunchIntent(String packageName, Class activityClass) {
 50 |  *         Intent intent = super.getLaunchIntent(packageName, activityClass);
 51 |  *         intent.putExtra("Hello", "World!");
 52 |  *         return intent;
 53 |  *       }
 54 |  *     };
 55 |  * }
56 | */ 57 | public class ActivityRule implements TestRule { 58 | private final Class activityClass; 59 | 60 | private T activity; 61 | private Instrumentation instrumentation; 62 | 63 | public ActivityRule(Class activityClass) { 64 | this.activityClass = activityClass; 65 | } 66 | 67 | protected Intent getLaunchIntent(String targetPackage, Class activityClass) { 68 | Intent intent = new Intent(Intent.ACTION_MAIN); 69 | intent.setClassName(targetPackage, activityClass.getName()); 70 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 71 | return intent; 72 | } 73 | 74 | public final Instrumentation instrumentation() { 75 | launchActivity(); 76 | return instrumentation; 77 | } 78 | 79 | @Override public final Statement apply(final Statement base, Description description) { 80 | return new Statement() { 81 | @Override public void evaluate() throws Throwable { 82 | launchActivity(); 83 | 84 | base.evaluate(); 85 | 86 | if (!activity.isFinishing()) { 87 | activity.finish(); 88 | } 89 | activity = null; // Eager reference kill in case someone leaked our reference. 90 | } 91 | }; 92 | } 93 | 94 | private Instrumentation fetchInstrumentation() { 95 | Instrumentation result = instrumentation; 96 | return result != null ? result 97 | : (instrumentation = InstrumentationRegistry.getInstrumentation()); 98 | } 99 | 100 | @SuppressWarnings("unchecked") // Guarded by generics at the constructor. 101 | private void launchActivity() { 102 | if (activity != null) return; 103 | 104 | Instrumentation instrumentation = fetchInstrumentation(); 105 | 106 | String targetPackage = instrumentation.getTargetContext().getPackageName(); 107 | Intent intent = getLaunchIntent(targetPackage, activityClass); 108 | 109 | activity = (T) instrumentation.startActivitySync(intent); 110 | instrumentation.waitForIdleSync(); 111 | } 112 | } -------------------------------------------------------------------------------- /app/src/androidTest/java/fr/xebia/jmartinez/installed/internal/JUnitTestCase.java: -------------------------------------------------------------------------------- 1 | package fr.xebia.jmartinez.installed.internal; 2 | 3 | import android.app.Activity; 4 | import android.app.Instrumentation; 5 | import android.support.test.InstrumentationRegistry; 6 | import android.support.test.internal.runner.lifecycle.ActivityLifecycleMonitorRegistry; 7 | import android.support.test.runner.lifecycle.ActivityLifecycleMonitor; 8 | import android.support.test.runner.lifecycle.Stage; 9 | 10 | import com.android.support.test.deps.guava.base.Throwables; 11 | import com.android.support.test.deps.guava.collect.Iterables; 12 | import com.android.support.test.deps.guava.collect.Sets; 13 | 14 | import org.junit.After; 15 | import org.junit.Rule; 16 | 17 | import java.util.Collection; 18 | import java.util.Set; 19 | import java.util.concurrent.Callable; 20 | import java.util.concurrent.atomic.AtomicReference; 21 | 22 | public class JUnitTestCase { 23 | 24 | @Rule 25 | public final ActivityRule main; 26 | 27 | @Rule 28 | public final SpoonRule spoonRule = new SpoonRule(); 29 | 30 | public JUnitTestCase(Class mainActivity) { 31 | this.main = new ActivityRule<>(mainActivity); 32 | } 33 | 34 | public void takeScreenshot(String tag) { 35 | spoonRule.takeScreenshot(getCurrentActivity(), tag); 36 | } 37 | 38 | @After 39 | public void tearDown() throws Exception { 40 | closeAllActivities(main.instrumentation()); 41 | } 42 | 43 | protected Activity getCurrentActivity() { 44 | Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); 45 | instrumentation.waitForIdleSync(); 46 | final Activity[] activity = new Activity[1]; 47 | instrumentation.runOnMainSync(new Runnable() { 48 | @Override 49 | public void run() { 50 | ActivityLifecycleMonitor activityLifecycleMonitor = ActivityLifecycleMonitorRegistry.getInstance(); 51 | Collection resumedActivities = activityLifecycleMonitor.getActivitiesInStage(Stage.RESUMED); 52 | activity[0] = Iterables.getOnlyElement(resumedActivities); 53 | } 54 | }); 55 | return activity[0]; 56 | } 57 | 58 | protected static void closeAllActivities(Instrumentation instrumentation) throws Exception { 59 | final int NUMBER_OF_RETRIES = 100; 60 | int i = 0; 61 | while (closeActivity(instrumentation)) { 62 | if (i++ > NUMBER_OF_RETRIES) { 63 | throw new AssertionError("Limit of retries excesses"); 64 | } 65 | Thread.sleep(200); 66 | } 67 | } 68 | 69 | protected static X callOnMainSync(Instrumentation instrumentation, final Callable callable) throws Exception { 70 | final AtomicReference retAtomic = new AtomicReference<>(); 71 | final AtomicReference exceptionAtomic = new AtomicReference<>(); 72 | instrumentation.runOnMainSync(new Runnable() { 73 | @Override 74 | public void run() { 75 | try { 76 | retAtomic.set(callable.call()); 77 | } catch (Throwable e) { 78 | exceptionAtomic.set(e); 79 | } 80 | } 81 | }); 82 | final Throwable exception = exceptionAtomic.get(); 83 | if (exception != null) { 84 | Throwables.propagateIfInstanceOf(exception, Exception.class); 85 | Throwables.propagate(exception); 86 | } 87 | return retAtomic.get(); 88 | } 89 | 90 | public static Set getActivitiesInStages(Stage... stages) { 91 | final Set activities = Sets.newHashSet(); 92 | final ActivityLifecycleMonitor instance = ActivityLifecycleMonitorRegistry.getInstance(); 93 | for (Stage stage : stages) { 94 | final Collection activitiesInStage = instance.getActivitiesInStage(stage); 95 | if (activitiesInStage != null) { 96 | activities.addAll(activitiesInStage); 97 | } 98 | } 99 | return activities; 100 | } 101 | 102 | private static boolean closeActivity(Instrumentation instrumentation) throws Exception { 103 | final Boolean activityClosed = callOnMainSync(instrumentation, new Callable() { 104 | @Override 105 | public Boolean call() throws Exception { 106 | final Set activities = getActivitiesInStages(Stage.RESUMED, 107 | Stage.STARTED, Stage.PAUSED, Stage.STOPPED, Stage.CREATED); 108 | activities.removeAll(getActivitiesInStages(Stage.DESTROYED)); 109 | if (activities.size() > 0) { 110 | final Activity activity = activities.iterator().next(); 111 | activity.finish(); 112 | return true; 113 | } else { 114 | return false; 115 | } 116 | } 117 | }); 118 | if (activityClosed) { 119 | instrumentation.waitForIdleSync(); 120 | } 121 | return activityClosed; 122 | } 123 | 124 | } 125 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | --------------------------------------------------------------------------------