From 37fc147d77beab74a882ee86665e9f8be5e5fe22 Mon Sep 17 00:00:00 2001 From: Jonas Kalderstam Date: Sat, 22 Oct 2016 17:23:40 +0200 Subject: [PATCH 01/10] Fix missing test activity --- .../filepicker/sample/NoNonsenseFilePickerTest.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename sample/src/{androidTestUitests => main}/java/com/nononsenseapps/filepicker/sample/NoNonsenseFilePickerTest.java (100%) diff --git a/sample/src/androidTestUitests/java/com/nononsenseapps/filepicker/sample/NoNonsenseFilePickerTest.java b/sample/src/main/java/com/nononsenseapps/filepicker/sample/NoNonsenseFilePickerTest.java similarity index 100% rename from sample/src/androidTestUitests/java/com/nononsenseapps/filepicker/sample/NoNonsenseFilePickerTest.java rename to sample/src/main/java/com/nononsenseapps/filepicker/sample/NoNonsenseFilePickerTest.java From 9187962cac4f231f6c87469028db3bbe40df2209 Mon Sep 17 00:00:00 2001 From: Jonas Kalderstam Date: Tue, 18 Oct 2016 23:09:37 +0200 Subject: [PATCH 02/10] Use databinding in sample app --- sample/build.gradle | 5 + .../sample/NoNonsenseFilePicker.java | 138 ++++---- .../activity_no_nonsense_file_picker.xml | 327 +++++++++--------- 3 files changed, 232 insertions(+), 238 deletions(-) diff --git a/sample/build.gradle b/sample/build.gradle index 6260d462..79488f8e 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -14,6 +14,10 @@ android { compileSdkVersion 24 buildToolsVersion "23.0.3" + dataBinding { + enabled = true + } + packagingOptions { exclude 'META-INF/LICENSE.txt' exclude 'META-INF/NOTICE.txt' @@ -69,6 +73,7 @@ dependencies { compile 'com.simplecityapps:recyclerview-fastscroll:1.0.9' // UI Tests + androidTestCompile 'junit:junit:4.12' uitestsCompile 'com.android.support.test.espresso:espresso-core:2.2.2' uitestsCompile 'com.android.support.test:runner:0.5' uitestsCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2' diff --git a/sample/src/main/java/com/nononsenseapps/filepicker/sample/NoNonsenseFilePicker.java b/sample/src/main/java/com/nononsenseapps/filepicker/sample/NoNonsenseFilePicker.java index ea60f37d..f10b3cc5 100644 --- a/sample/src/main/java/com/nononsenseapps/filepicker/sample/NoNonsenseFilePicker.java +++ b/sample/src/main/java/com/nononsenseapps/filepicker/sample/NoNonsenseFilePicker.java @@ -10,22 +10,19 @@ import android.app.Activity; import android.content.ClipData; import android.content.Intent; +import android.databinding.DataBindingUtil; import android.os.Build; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; -import android.widget.CheckBox; -import android.widget.RadioGroup; -import android.widget.TextView; -import android.widget.Toast; import com.dropbox.client2.DropboxAPI; import com.dropbox.client2.android.AndroidAuthSession; -import com.nononsenseapps.filepicker.AbstractFilePickerActivity; import com.nononsenseapps.filepicker.AbstractFilePickerFragment; import com.nononsenseapps.filepicker.FilePickerActivity; +import com.nononsenseapps.filepicker.sample.databinding.ActivityNoNonsenseFilePickerBinding; import com.nononsenseapps.filepicker.sample.dropbox.DropboxFilePickerActivity; import com.nononsenseapps.filepicker.sample.dropbox.DropboxFilePickerActivity2; import com.nononsenseapps.filepicker.sample.dropbox.DropboxSyncHelper; @@ -46,39 +43,19 @@ public class NoNonsenseFilePicker extends Activity { static final int CODE_SD = 0; static final int CODE_DB = 1; static final int CODE_FTP = 2; - TextView textView; DropboxAPI mDBApi = null; - CheckBox checkAllowCreateDir; - CheckBox checkAllowMultiple; - CheckBox checkSingleClick; - CheckBox checkLightTheme; - RadioGroup radioGroup; - CheckBox checkAllowExistingFile; + ActivityNoNonsenseFilePickerBinding binding; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.activity_no_nonsense_file_picker); - - checkAllowCreateDir = - (CheckBox) findViewById(R.id.checkAllowCreateDir); - checkAllowMultiple = - (CheckBox) findViewById(R.id.checkAllowMultiple); - checkAllowExistingFile = - (CheckBox) findViewById(R.id.checkAllowExistingFile); - checkSingleClick = - (CheckBox) findViewById(R.id.checkSingleClick); - checkLightTheme = - (CheckBox) findViewById(R.id.checkLightTheme); - radioGroup = - (RadioGroup) findViewById(R.id.radioGroup); - textView = (TextView) findViewById(R.id.text); - - findViewById(R.id.button_sd) + binding = DataBindingUtil.setContentView(this, R.layout.activity_no_nonsense_file_picker); + + binding.buttonSd .setOnClickListener(new View.OnClickListener() { @Override public void onClick(final View v) { - if (checkLightTheme.isChecked()) { + if (binding.checkLightTheme.isChecked()) { startActivity(CODE_SD, FilePickerActivity2.class); } else { startActivity(CODE_SD, FilePickerActivity.class); @@ -86,11 +63,11 @@ public void onClick(final View v) { } }); - findViewById(R.id.button_image) + binding.buttonImage .setOnClickListener(new View.OnClickListener() { @Override public void onClick(final View v) { - if (checkLightTheme.isChecked()) { + if (binding.checkLightTheme.isChecked()) { startActivity(CODE_SD, MultimediaPickerActivity2.class); } else { startActivity(CODE_SD, MultimediaPickerActivity.class); @@ -98,11 +75,11 @@ public void onClick(final View v) { } }); - findViewById(R.id.button_ftp) + binding.buttonFtp .setOnClickListener(new View.OnClickListener() { @Override public void onClick(final View v) { - if (checkLightTheme.isChecked()) { + if (binding.checkLightTheme.isChecked()) { startActivity(CODE_FTP, FtpPickerActivity2.class); } else { startActivity(CODE_FTP, FtpPickerActivity.class); @@ -110,7 +87,7 @@ public void onClick(final View v) { } }); - findViewById(R.id.button_dropbox) + binding.buttonDropbox .setOnClickListener(new View.OnClickListener() { @Override public void onClick(final View v) { @@ -126,8 +103,7 @@ public void onClick(final View v) { mDBApi.getSession().startOAuth2Authentication( NoNonsenseFilePicker.this); } else { // User is authorized, open file picker - Intent i; - if (checkLightTheme.isChecked()) { + if (binding.checkLightTheme.isChecked()) { startActivity(CODE_DB, DropboxFilePickerActivity2.class); } else { startActivity(CODE_DB, DropboxFilePickerActivity.class); @@ -136,27 +112,29 @@ public void onClick(final View v) { } }); - findViewById(R.id.button_root).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - if (checkLightTheme.isChecked()) { - startActivity(CODE_SD, SUPickerActivity.class); - } else { - startActivity(CODE_SD, SUPickerActivity2.class); - } - } - }); - - findViewById(R.id.button_fastscroll).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - if (checkLightTheme.isChecked()) { - startActivity(CODE_SD, FastScrollerFilePickerActivity.class); - } else { - startActivity(CODE_SD, FastScrollerFilePickerActivity2.class); - } - } - }); + binding.buttonRoot + .setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (binding.checkLightTheme.isChecked()) { + startActivity(CODE_SD, SUPickerActivity.class); + } else { + startActivity(CODE_SD, SUPickerActivity2.class); + } + } + }); + + binding.buttonFastscroll + .setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (binding.checkLightTheme.isChecked()) { + startActivity(CODE_SD, FastScrollerFilePickerActivity.class); + } else { + startActivity(CODE_SD, FastScrollerFilePickerActivity2.class); + } + } + }); } protected void startActivity(final int code, final Class klass) { @@ -165,17 +143,17 @@ protected void startActivity(final int code, final Class klass) { i.setAction(Intent.ACTION_GET_CONTENT); i.putExtra(SUPickerActivity.EXTRA_ALLOW_MULTIPLE, - checkAllowMultiple.isChecked()); + binding.checkAllowMultiple.isChecked()); i.putExtra(FilePickerActivity.EXTRA_SINGLE_CLICK, - checkSingleClick.isChecked()); + binding.checkSingleClick.isChecked()); i.putExtra(SUPickerActivity.EXTRA_ALLOW_CREATE_DIR, - checkAllowCreateDir.isChecked()); + binding.checkAllowCreateDir.isChecked()); i.putExtra(FilePickerActivity.EXTRA_ALLOW_EXISTING_FILE, - checkAllowExistingFile.isChecked()); + binding.checkAllowExistingFile.isChecked()); // What mode is selected final int mode; - switch (radioGroup.getCheckedRadioButtonId()) { + switch (binding.radioGroup.getCheckedRadioButtonId()) { case R.id.radioDir: mode = AbstractFilePickerFragment.MODE_DIR; break; @@ -239,38 +217,46 @@ public boolean onOptionsItemSelected(MenuItem item) { @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { - if ((CODE_SD == requestCode || CODE_DB == requestCode || CODE_FTP == requestCode) && - resultCode == Activity.RESULT_OK) { - if (data.getBooleanExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, - false)) { - + // Always check the resultCode! + // Checking for the requestCodes is a bit redundant but good style + if (resultCode == Activity.RESULT_OK && + (CODE_SD == requestCode || CODE_DB == requestCode || CODE_FTP == requestCode)) { + // If we handled multiple files, we need to get the result differently + if (data.getBooleanExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false)) { + // This is the typical style on Android 4.2 and above if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { ClipData clip = data.getClipData(); StringBuilder sb = new StringBuilder(); + // clip data CAN be null in case of an empty result if (clip != null) { for (int i = 0; i < clip.getItemCount(); i++) { - sb.append(clip.getItemAt(i).getUri().toString()); - sb.append("\n"); + if (i > 0) { + sb.append("\n"); + } + // Returns Strings to workaround a bug in Android 7.0 Nougat + sb.append(clip.getItemAt(i).getText()); } } - textView.setText(sb.toString()); - } else { + binding.text.setText(sb.toString()); + } else /* This style is available in all SDK versions */ { ArrayList paths = data.getStringArrayListExtra( FilePickerActivity.EXTRA_PATHS); StringBuilder sb = new StringBuilder(); if (paths != null) { for (String path : paths) { + if (sb.length() > 0) { + sb.append("\n"); + } sb.append(path); - sb.append("\n"); } } - textView.setText(sb.toString()); + binding.text.setText(sb.toString()); } - } else { - textView.setText(data.getData().toString()); + } else /* Single file mode */ { + binding.text.setText(data.getData().toString()); } } } diff --git a/sample/src/main/res/layout/activity_no_nonsense_file_picker.xml b/sample/src/main/res/layout/activity_no_nonsense_file_picker.xml index 83cb96bf..8b993721 100644 --- a/sample/src/main/res/layout/activity_no_nonsense_file_picker.xml +++ b/sample/src/main/res/layout/activity_no_nonsense_file_picker.xml @@ -3,213 +3,216 @@ ~ 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/. --> - - - - - - + + + + + + - + android:layout_gravity="center_horizontal" + android:orientation="vertical"> - + + + + + + + + + + + + + android:text="Multiple items" /> - + android:text="Single selection returns immediately" /> - + android:text="Allow creation of directories" /> - + android:text="Allow selection of existing (new) file" /> - + - + +