diff --git a/library/build.gradle b/library/build.gradle index 0c63620..900e8c1 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -14,8 +14,8 @@ def gitCommitCount = Integer.parseInt('git rev-list --count HEAD'.execute([], project.rootDir).text.trim()) android { - compileSdkVersion 23 - buildToolsVersion "23.0.2" + compileSdkVersion 24 + buildToolsVersion "23.0.3" defaultConfig { minSdkVersion 9 @@ -27,9 +27,9 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) - compile 'com.android.support:appcompat-v7:23.4.0' - compile 'com.android.support:support-v4:23.4.0' - compile 'com.android.support:recyclerview-v7:23.4.0' + compile 'com.android.support:appcompat-v7:24.2.1' + compile 'com.android.support:support-v4:24.2.1' + compile 'com.android.support:recyclerview-v7:24.2.1' testCompile 'junit:junit:4.12' } diff --git a/library/src/main/java/com/nononsenseapps/filepicker/AbstractFilePickerFragment.java b/library/src/main/java/com/nononsenseapps/filepicker/AbstractFilePickerFragment.java index 2f3e51f..79d5c06 100644 --- a/library/src/main/java/com/nononsenseapps/filepicker/AbstractFilePickerFragment.java +++ b/library/src/main/java/com/nononsenseapps/filepicker/AbstractFilePickerFragment.java @@ -14,11 +14,13 @@ import android.net.Uri; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.annotation.StyleRes; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.Loader; import android.support.v7.app.AppCompatActivity; import android.support.v7.util.SortedList; +import android.support.v7.view.ContextThemeWrapper; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; @@ -123,12 +125,12 @@ public abstract class AbstractFilePickerFragment extends Fragment *

* The key/value-pairs listed below will be overwritten however. * - * @param startPath path to directory the picker will show upon start - * @param mode what is allowed to be selected (dirs, files, both) - * @param allowMultiple selecting a single item or several? - * @param allowDirCreate can new directories be created? + * @param startPath path to directory the picker will show upon start + * @param mode what is allowed to be selected (dirs, files, both) + * @param allowMultiple selecting a single item or several? + * @param allowDirCreate can new directories be created? * @param allowExistingFile if selecting a "new" file, can existing files be chosen - * @param singleClick selecting an item does not require a press on OK + * @param singleClick selecting an item does not require a press on OK */ public void setArgs(@Nullable final String startPath, final int mode, final boolean allowMultiple, final boolean allowDirCreate, @@ -162,7 +164,9 @@ public abstract class AbstractFilePickerFragment extends Fragment @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - final View view = inflateRootView(inflater, container); + final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), getStyleId()); + final LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper); + final View view = localInflater.inflate(R.layout.nnf_fragment_filepicker, container, false); Toolbar toolbar = (Toolbar) view.findViewById(R.id.nnf_picker_toolbar); if (toolbar != null) { @@ -177,7 +181,7 @@ public abstract class AbstractFilePickerFragment extends Fragment layoutManager = new LinearLayoutManager(getActivity()); recyclerView.setLayoutManager(layoutManager); // Set Item Decoration if exists - configureItemDecoration(inflater, recyclerView); + configureItemDecoration(localInflater, recyclerView); // Set adapter mAdapter = new FileItemAdapter<>(this); recyclerView.setAdapter(mAdapter); @@ -191,11 +195,11 @@ public abstract class AbstractFilePickerFragment extends Fragment }); view.findViewById(R.id.nnf_button_ok).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(final View v) { - onClickOk(v); - } - }); + @Override + public void onClick(final View v) { + onClickOk(v); + } + }); view.findViewById(R.id.nnf_button_ok_newfile).setOnClickListener( new View.OnClickListener() { @Override @@ -207,6 +211,8 @@ public abstract class AbstractFilePickerFragment extends Fragment mNewFileButtonContainer = view.findViewById(R.id.nnf_newfile_button_container); mRegularButtonContainer = view.findViewById(R.id.nnf_button_container); + setModeView(view); + mEditTextFileName = (EditText) view.findViewById(R.id.nnf_text_filename); mEditTextFileName.addTextChangedListener(new TextWatcher() { @Override @@ -235,10 +241,6 @@ public abstract class AbstractFilePickerFragment extends Fragment return view; } - protected View inflateRootView(LayoutInflater inflater, ViewGroup container) { - return inflater.inflate( R.layout.nnf_fragment_filepicker, container, false); - } - /** * Checks if a divider drawable has been defined in the current theme. If it has, will apply * an item decoration with the divider. If no divider has been specified, then does nothing. @@ -323,7 +325,6 @@ public abstract class AbstractFilePickerFragment extends Fragment } /** - * * @return filename as entered/picked by the user for the new file */ @NonNull @@ -390,24 +391,7 @@ public abstract class AbstractFilePickerFragment extends Fragment super.onCreate(savedInstanceState); setHasOptionsMenu(true); - } - /** - * Called when the fragment's activity has been created and this - * fragment's view hierarchy instantiated. It can be used to do final - * initialization once these pieces are in place, such as retrieving - * views or restoring state. It is also useful for fragments that use - * {@link #setRetainInstance(boolean)} to retain their instance, - * as this callback tells the fragment when it is fully associated with - * the new activity instance. This is called after {@link #onCreateView} - * and before {@link #onViewStateRestored(Bundle)}. - * - * @param savedInstanceState If the fragment is being re-created from - * a previous saved state, this is the state. - */ - @Override - public void onActivityCreated(Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); // Only if we have no state if (mCurrentPath == null) { if (savedInstanceState != null) { @@ -450,8 +434,6 @@ public abstract class AbstractFilePickerFragment extends Fragment } } - setModeView(); - // If still null if (mCurrentPath == null) { mCurrentPath = getRoot(); @@ -459,16 +441,17 @@ public abstract class AbstractFilePickerFragment extends Fragment refresh(mCurrentPath); } + /** * Hides/Shows appropriate views depending on mode */ - protected void setModeView() { + protected void setModeView(@NonNull View view) { boolean nf = mode == MODE_NEW_FILE; mNewFileButtonContainer.setVisibility(nf ? View.VISIBLE : View.GONE); mRegularButtonContainer.setVisibility(nf ? View.GONE : View.VISIBLE); if (!nf && singleClick) { - getActivity().findViewById(R.id.nnf_button_ok).setVisibility(View.GONE); + view.findViewById(R.id.nnf_button_ok).setVisibility(View.GONE); } } @@ -627,23 +610,28 @@ public abstract class AbstractFilePickerFragment extends Fragment @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View v; + final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), getStyleId()); + final LayoutInflater localInflater = LayoutInflater.from(getActivity()).cloneInContext(contextThemeWrapper); switch (viewType) { case LogicHandler.VIEWTYPE_HEADER: - v = LayoutInflater.from(getActivity()).inflate(R.layout.nnf_filepicker_listitem_dir, + v = localInflater.inflate(R.layout.nnf_filepicker_listitem_dir, parent, false); return new HeaderViewHolder(v); case LogicHandler.VIEWTYPE_CHECKABLE: - v = LayoutInflater.from(getActivity()).inflate(R.layout.nnf_filepicker_listitem_checkable, + v = localInflater.inflate(R.layout.nnf_filepicker_listitem_checkable, parent, false); return new CheckableViewHolder(v); case LogicHandler.VIEWTYPE_DIR: default: - v = LayoutInflater.from(getActivity()).inflate(R.layout.nnf_filepicker_listitem_dir, + v = localInflater.inflate(R.layout.nnf_filepicker_listitem_dir, parent, false); return new DirViewHolder(v); } } + @StyleRes + protected abstract int getStyleId(); + /** * @param vh to bind data from either a file or directory * @param position 0 - n, where the header has been subtracted diff --git a/library/src/main/java/com/nononsenseapps/filepicker/FilePickerActivity.java b/library/src/main/java/com/nononsenseapps/filepicker/FilePickerActivity.java deleted file mode 100644 index 54afc39..0000000 --- a/library/src/main/java/com/nononsenseapps/filepicker/FilePickerActivity.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ - -package com.nononsenseapps.filepicker; - -import android.annotation.SuppressLint; -import android.os.Environment; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; - -import java.io.File; -import java.util.ArrayList; -import java.util.List; - -@SuppressLint("Registered") -public class FilePickerActivity extends AbstractFilePickerActivity { - - @NonNull - private final List onBackPressedListeners = new ArrayList<>(); - - public FilePickerActivity() { - super(); - } - - @Override - protected AbstractFilePickerFragment getFragment( - @Nullable final String startPath, final int mode, final boolean allowMultiple, - final boolean allowCreateDir, final boolean allowExistingFile, - final boolean singleClick) { - AbstractFilePickerFragment fragment = new FilePickerFragment(); - // startPath is allowed to be null. In that case, default folder should be SD-card and not "/" - fragment.setArgs(startPath != null ? startPath : Environment.getExternalStorageDirectory().getPath(), - mode, allowMultiple, allowCreateDir, allowExistingFile, singleClick); - return fragment; - } - - public void addOnBackPressedListener(@NonNull final OnBackPressedListener onBackPressedListener) { - onBackPressedListeners.add(onBackPressedListener); - } - - public void removeOnBackPressedListener(@NonNull final OnBackPressedListener onBackPressedListener) { - onBackPressedListeners.remove(onBackPressedListener); - } - - @Override - public void onBackPressed() { - for (final OnBackPressedListener onBackPressedListener : onBackPressedListeners) { - if (onBackPressedListener.onBackPressed()) { - return; - } - } - - if (getSupportFragmentManager().getBackStackEntryCount() <= 1) { - supportFinishAfterTransition(); - } else { - getSupportFragmentManager().popBackStack(); - } - } - - public interface OnBackPressedListener { - - /** - * Calls when user presses device back button. - * - * @return True if it is processed by this object. - */ - boolean onBackPressed(); - - } - -} diff --git a/library/src/main/java/com/nononsenseapps/filepicker/FilePickerFragment.java b/library/src/main/java/com/nononsenseapps/filepicker/FilePickerFragment.java index 97ec795..7d5b371 100644 --- a/library/src/main/java/com/nononsenseapps/filepicker/FilePickerFragment.java +++ b/library/src/main/java/com/nononsenseapps/filepicker/FilePickerFragment.java @@ -9,7 +9,6 @@ package com.nononsenseapps.filepicker; import android.Manifest; import android.content.pm.PackageManager; import android.net.Uri; -import android.os.Bundle; import android.os.FileObserver; import android.support.annotation.NonNull; import android.support.v4.content.AsyncTaskLoader; @@ -25,7 +24,7 @@ import java.io.File; * An implementation of the picker which allows you to select a file from the internal/external * storage (SD-card) on a device. */ -public class FilePickerFragment extends AbstractFilePickerFragment implements FilePickerActivity.OnBackPressedListener { +public abstract class FilePickerFragment extends AbstractFilePickerFragment { private static final String[] extensions = new String[]{".doc", ".docx", ".xlsx", ".xls", ".png", ".jpg", ".tif", ".pdf", ".jpeg"}; @@ -33,15 +32,6 @@ public class FilePickerFragment extends AbstractFilePickerFragment impleme protected boolean showHiddenItems = false; private File mRequestedPath = null; - public FilePickerFragment() { - } - - @Override - public void onCreate(final Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - ((FilePickerActivity) getActivity()).addOnBackPressedListener(this); - } - /** * This method is used to dictate whether hidden files and folders should be shown or not * @@ -375,20 +365,4 @@ public class FilePickerFragment extends AbstractFilePickerFragment impleme } } - @Override - public boolean onBackPressed() { - if ("/".equals(mCurrentPath.toString())) { - return false; - } else { - goUp(); - return true; - } - } - - @Override - public void onDestroy() { - super.onDestroy(); - ((FilePickerActivity) getActivity()).removeOnBackPressedListener(this); - } - } diff --git a/library/src/main/res/layout/nnf_fragment_filepicker.xml b/library/src/main/res/layout/nnf_fragment_filepicker.xml index 8d59c22..101f387 100644 --- a/library/src/main/res/layout/nnf_fragment_filepicker.xml +++ b/library/src/main/res/layout/nnf_fragment_filepicker.xml @@ -7,10 +7,9 @@ --> + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent">