diff --git a/docs-src/content/example/override_back_button.md b/docs-src/content/example/override_back_button.md index 7c04707..10b70c2 100644 --- a/docs-src/content/example/override_back_button.md +++ b/docs-src/content/example/override_back_button.md @@ -11,12 +11,17 @@ title = "Override the back button" +++ -In case you want the back button navigate the hierarchy instead of instantly exiting the activity, this -is one approach you might take. +In case you want the back button to navigate the hierarchy instead of +instantly exiting the activity, this is one approach you might take. -## Create an activity which overrides the back button, and loads a custom fragment +## Create an activity which overrides the back button and loads a custom fragment ```java +import android.os.Environment; +import com.nononsenseapps.filepicker.AbstractFilePickerFragment; +import com.nononsenseapps.filepicker.FilePickerActivity; +import java.io.File; + public class BackHandlingFilePickerActivity extends FilePickerActivity { /** @@ -30,11 +35,17 @@ public class BackHandlingFilePickerActivity extends FilePickerActivity { @Override protected AbstractFilePickerFragment getFragment( final String startPath, final int mode, final boolean allowMultiple, - final boolean allowCreateDir) { + final boolean allowDirCreate, final boolean allowExistingFile, + final boolean singleClick) { + + // startPath is allowed to be null. + // In that case, default folder should be SD-card and not "/" + String path = (startPath != null ? startPath + : Environment.getExternalStorageDirectory().getPath()); + currentFragment = new BackHandlingFilePickerFragment(); - // startPath is allowed to be null. In that case, default folder should be SD-card and not "/" - currentFragment.setArgs(startPath != null ? startPath : Environment.getExternalStorageDirectory().getPath(), - mode, allowMultiple, allowCreateDir); + currentFragment.setArgs(path, mode, allowMultiple, allowDirCreate, + allowExistingFile, singleClick); return currentFragment; } @@ -54,9 +65,12 @@ public class BackHandlingFilePickerActivity extends FilePickerActivity { } ``` -## In you custom fragment, implement the goUp and isBackTop methods +## In your custom fragment, implement the goUp and isBackTop methods ```java +import com.nononsenseapps.filepicker.FilePickerFragment; +import java.io.File; + public class BackHandlingFilePickerFragment extends FilePickerFragment { /** @@ -86,7 +100,23 @@ public class BackHandlingFilePickerFragment extends FilePickerFragment { mCurrentPath = getParent(mCurrentPath); mCheckedItems.clear(); mCheckedVisibleViewHolders.clear(); - refresh(); + refresh(mCurrentPath); } } ``` + +## And finally, add the following to your manifest + +And make sure `android-theme` points to the correct theme. + +```xml + + + + + + +``` diff --git a/examples/.gitignore b/examples/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/examples/.gitignore @@ -0,0 +1 @@ +/build diff --git a/examples/build.gradle b/examples/build.gradle new file mode 100644 index 0000000..cd373f2 --- /dev/null +++ b/examples/build.gradle @@ -0,0 +1,27 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 24 + buildToolsVersion "24.0.1" + + defaultConfig { + applicationId "com.nononsenseapps.filepicker.examples" + minSdkVersion 15 + targetSdkVersion 24 + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) + compile project(':library') + testCompile 'junit:junit:4.12' + compile 'com.android.support:appcompat-v7:24.2.0' +} diff --git a/examples/proguard-rules.pro b/examples/proguard-rules.pro new file mode 100644 index 0000000..e1675e5 --- /dev/null +++ b/examples/proguard-rules.pro @@ -0,0 +1,17 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in /home/jonas/Android/Sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/examples/src/androidTest/java/com/nononsenseapps/filepicker/examples/ApplicationTest.java b/examples/src/androidTest/java/com/nononsenseapps/filepicker/examples/ApplicationTest.java new file mode 100644 index 0000000..3775895 --- /dev/null +++ b/examples/src/androidTest/java/com/nononsenseapps/filepicker/examples/ApplicationTest.java @@ -0,0 +1,13 @@ +package com.nononsenseapps.filepicker.examples; + +import android.app.Application; +import android.test.ApplicationTestCase; + +/** + * Testing Fundamentals + */ +public class ApplicationTest extends ApplicationTestCase { + public ApplicationTest() { + super(Application.class); + } +} \ No newline at end of file diff --git a/examples/src/main/AndroidManifest.xml b/examples/src/main/AndroidManifest.xml new file mode 100644 index 0000000..890a54d --- /dev/null +++ b/examples/src/main/AndroidManifest.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + diff --git a/examples/src/main/java/com/nononsenseapps/filepicker/examples/backbutton/BackHandlingFilePickerActivity.java b/examples/src/main/java/com/nononsenseapps/filepicker/examples/backbutton/BackHandlingFilePickerActivity.java new file mode 100644 index 0000000..42d6531 --- /dev/null +++ b/examples/src/main/java/com/nononsenseapps/filepicker/examples/backbutton/BackHandlingFilePickerActivity.java @@ -0,0 +1,48 @@ +package com.nononsenseapps.filepicker.examples.backbutton; + +import android.os.Environment; +import com.nononsenseapps.filepicker.AbstractFilePickerFragment; +import com.nononsenseapps.filepicker.FilePickerActivity; +import java.io.File; + +public class BackHandlingFilePickerActivity extends FilePickerActivity { + + /** + * Need access to the fragment + */ + BackHandlingFilePickerFragment currentFragment; + + /** + * Return a copy of the new fragment and set the variable above. + */ + @Override + protected AbstractFilePickerFragment getFragment( + final String startPath, final int mode, final boolean allowMultiple, + final boolean allowDirCreate, final boolean allowExistingFile, + final boolean singleClick) { + + // startPath is allowed to be null. + // In that case, default folder should be SD-card and not "/" + String path = (startPath != null ? startPath + : Environment.getExternalStorageDirectory().getPath()); + + currentFragment = new BackHandlingFilePickerFragment(); + currentFragment.setArgs(path, mode, allowMultiple, allowDirCreate, + allowExistingFile, singleClick); + return currentFragment; + } + + /** + * Override the back-button. + */ + @Override + public void onBackPressed() { + // If at top most level, normal behaviour + if (currentFragment.isBackTop()) { + super.onBackPressed(); + } else { + // Else go up + currentFragment.goUp(); + } + } +} diff --git a/examples/src/main/java/com/nononsenseapps/filepicker/examples/backbutton/BackHandlingFilePickerFragment.java b/examples/src/main/java/com/nononsenseapps/filepicker/examples/backbutton/BackHandlingFilePickerFragment.java new file mode 100644 index 0000000..be33bfe --- /dev/null +++ b/examples/src/main/java/com/nononsenseapps/filepicker/examples/backbutton/BackHandlingFilePickerFragment.java @@ -0,0 +1,37 @@ +package com.nononsenseapps.filepicker.examples.backbutton; + +import com.nononsenseapps.filepicker.FilePickerFragment; +import java.io.File; + +public class BackHandlingFilePickerFragment extends FilePickerFragment { + + /** + * For consistency, the top level the back button checks against should be the start path. + * But it will fall back on /. + */ + public File getBackTop() { + if (getArguments().containsKey(KEY_START_PATH)) { + return getPath(getArguments().getString(KEY_START_PATH)); + } else { + return new File("/"); + } + } + + /** + * + * @return true if the current path is the startpath or / + */ + public boolean isBackTop() { + return 0 == compareFiles(mCurrentPath, getBackTop()) || 0 == compareFiles(mCurrentPath, new File("/")); + } + + /** + * Go up on level, same as pressing on "..". + */ + public void goUp() { + mCurrentPath = getParent(mCurrentPath); + mCheckedItems.clear(); + mCheckedVisibleViewHolders.clear(); + refresh(mCurrentPath); + } +} \ No newline at end of file diff --git a/examples/src/main/res/mipmap-hdpi/ic_launcher.png b/examples/src/main/res/mipmap-hdpi/ic_launcher.png new file mode 100644 index 0000000..cde69bc Binary files /dev/null and b/examples/src/main/res/mipmap-hdpi/ic_launcher.png differ diff --git a/examples/src/main/res/mipmap-mdpi/ic_launcher.png b/examples/src/main/res/mipmap-mdpi/ic_launcher.png new file mode 100644 index 0000000..c133a0c Binary files /dev/null and b/examples/src/main/res/mipmap-mdpi/ic_launcher.png differ diff --git a/examples/src/main/res/mipmap-xhdpi/ic_launcher.png b/examples/src/main/res/mipmap-xhdpi/ic_launcher.png new file mode 100644 index 0000000..bfa42f0 Binary files /dev/null and b/examples/src/main/res/mipmap-xhdpi/ic_launcher.png differ diff --git a/examples/src/main/res/mipmap-xxhdpi/ic_launcher.png b/examples/src/main/res/mipmap-xxhdpi/ic_launcher.png new file mode 100644 index 0000000..324e72c Binary files /dev/null and b/examples/src/main/res/mipmap-xxhdpi/ic_launcher.png differ diff --git a/examples/src/main/res/mipmap-xxxhdpi/ic_launcher.png b/examples/src/main/res/mipmap-xxxhdpi/ic_launcher.png new file mode 100644 index 0000000..aee44e1 Binary files /dev/null and b/examples/src/main/res/mipmap-xxxhdpi/ic_launcher.png differ diff --git a/examples/src/main/res/values/colors.xml b/examples/src/main/res/values/colors.xml new file mode 100644 index 0000000..c4c54cb --- /dev/null +++ b/examples/src/main/res/values/colors.xml @@ -0,0 +1,6 @@ + + + #F44336 + #D32F2F + #FFAB00 + diff --git a/examples/src/main/res/values/strings.xml b/examples/src/main/res/values/strings.xml new file mode 100644 index 0000000..855b71e --- /dev/null +++ b/examples/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + Examples + diff --git a/examples/src/main/res/values/styles.xml b/examples/src/main/res/values/styles.xml new file mode 100644 index 0000000..a59f4d1 --- /dev/null +++ b/examples/src/main/res/values/styles.xml @@ -0,0 +1,17 @@ + + + + + + + diff --git a/examples/src/test/java/com/nononsenseapps/filepicker/examples/ExampleUnitTest.java b/examples/src/test/java/com/nononsenseapps/filepicker/examples/ExampleUnitTest.java new file mode 100644 index 0000000..c71797e --- /dev/null +++ b/examples/src/test/java/com/nononsenseapps/filepicker/examples/ExampleUnitTest.java @@ -0,0 +1,15 @@ +package com.nononsenseapps.filepicker.examples; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * To work on unit tests, switch the Test Artifact in the Build Variants view. + */ +public class ExampleUnitTest { + @Test + public void addition_isCorrect() throws Exception { + assertEquals(4, 2 + 2); + } +} \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 74357bc..8bd4fb4 100644 --- a/settings.gradle +++ b/settings.gradle @@ -4,4 +4,4 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -include ':sample', ':library' +include ':sample', ':library', ':examples'