Skip to content

Commit

Permalink
Localized strings. Added proper permission handling for Android marsh…
Browse files Browse the repository at this point in the history
…mellow.
  • Loading branch information
DeveloperPaul123 committed Dec 6, 2015
1 parent 22ae6b2 commit de66e42
Show file tree
Hide file tree
Showing 12 changed files with 271 additions and 78 deletions.
6 changes: 3 additions & 3 deletions .idea/libraries/MaterialLibrary_1_0_4.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions FPlib/FPlib.iml
Expand Up @@ -72,6 +72,7 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dex" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dex-cache" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.github.DeveloperPaul123/MaterialLibrary/1.0.4/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jacoco" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/javaResources" />
Expand Down
114 changes: 95 additions & 19 deletions FPlib/src/main/java/com/devpaul/filepickerlibrary/FilePicker.java
@@ -1,18 +1,22 @@
package com.devpaul.filepickerlibrary;

import android.Manifest;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewTreeObserver;
Expand All @@ -24,6 +28,7 @@
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.afollestad.materialdialogs.MaterialDialog;
import com.devpaul.filepickerlibrary.adapter.FileRecyclerViewAdapter;
import com.devpaul.filepickerlibrary.enums.FileScopeType;
import com.devpaul.filepickerlibrary.enums.FileType;
Expand Down Expand Up @@ -230,7 +235,14 @@ public class FilePicker extends AppCompatActivity implements NameFileDialogInter
*/
private Context mContext;

/**
* Request code for app permissions.
*/
private static final int REQUEST_FOR_READ_EXTERNAL_STORAGE = 101;

/**
* Layout manager for the Recycler View.
*/
private LinearLayoutManager mLinearLayoutManager;

@Override
Expand Down Expand Up @@ -306,8 +318,7 @@ public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (Math.abs(dy) >= 5) {
if (dy > 0) {
toggleButton(false);
}
else if(dy < 0) {
} else if (dy < 0) {
toggleButton(true);
}
if (areButtonsShowing) {
Expand All @@ -318,8 +329,7 @@ else if(dy < 0) {
hideButtons();
adapter.setSelectedPosition(-1);
}
}
else {
} else {
mLastFirstVisibleItem = firstVisibleItem;
}
super.onScrolled(recyclerView, dx, dy);
Expand All @@ -332,23 +342,83 @@ else if(dy < 0) {
//drawable has not been set so set the color.
setHeaderBackground(colorId, drawableId);

//check for proper permissions.
if(Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
int permissionCheck = ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE);
if(permissionCheck != PackageManager.PERMISSION_GRANTED) {
if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
//Show permission rationale.
new MaterialDialog.Builder(FilePicker.this)
.title(R.string.file_picker_permission_rationale_dialog_title)
.content(R.string.file_picker_permission_rationale_dialog_content)
.positiveText(R.string.file_picker_ok)
.negativeText(R.string.file_picker_cancel)
.callback(new MaterialDialog.ButtonCallback() {
@Override
public void onPositive(MaterialDialog dialog) {
ActivityCompat.requestPermissions(FilePicker.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_FOR_READ_EXTERNAL_STORAGE);
}

@Override
public void onNegative(MaterialDialog dialog) {
setResult(RESULT_CANCELED);
finish();
}
})
.show();
}
else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_FOR_READ_EXTERNAL_STORAGE);
}
}
} else {
init();
}
}

/**
* Initialize the current directory.
*/
private void init() {
curDirectory = new File(Environment.getExternalStorageDirectory().getPath());
currentFile = new File(curDirectory.getPath());
lastDirectory = curDirectory.getParentFile();

if (curDirectory.isDirectory()) {
Log.d("FILEPICKER", "Is directory");
new UpdateFilesTask(this).execute(curDirectory);
} else {
Log.d("FILEPICKER", "Is not directory");
try {
throw new Exception("Initial file must be a directory.");
throw new Exception(getString(R.string.file_picker_directory_error));
} catch (Exception e) {
e.printStackTrace();
}
}
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
//see if we got the permission.
case REQUEST_FOR_READ_EXTERNAL_STORAGE:
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED
&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {
init();
}
else {
setResult(RESULT_CANCELED);
finish();
}
return;
}
}

private static OvershootInterpolator interpolator = new OvershootInterpolator();
/**
* Toggles the material floating action button.
Expand Down Expand Up @@ -415,7 +485,7 @@ public void onClick(View view) {
finish();
} else {
SnackbarManager.show(Snackbar.with(FilePicker.this)
.text("Please select a directory.")
.text(R.string.file_picker_snackbar_select_directory_message)
.duration(1500));
}
} else { //request code is for a file
Expand All @@ -433,7 +503,8 @@ public void onClick(View view) {
finish();
} else {
SnackbarManager.show(Snackbar.with(FilePicker.this)
.text("Please select a " + requiredExtension + " file.")
.text(String.format(getString(R.string.file_picker_snackbar_select_file_ext_message),
requiredExtension))
.duration(1500));
}
} else {
Expand Down Expand Up @@ -465,11 +536,11 @@ public void onClick(View view) {
startActivity(newIntent);
} catch (android.content.ActivityNotFoundException e) {
SnackbarManager.show(Snackbar.with(FilePicker.this)
.text("No handler for this type of file."));
.text(R.string.file_picker_snackbar_no_file_type_handler));
}
} else {
SnackbarManager.show(Snackbar.with(FilePicker.this)
.text("Couldn't get file type."));
.text(R.string.file_picker_snackbar_no_read_type));
}

}
Expand Down Expand Up @@ -556,7 +627,7 @@ public void onReturnFileName(String fileName) {
if (fileName.equalsIgnoreCase("") || fileName.isEmpty()) {
fileName = null;
}
if (fileName != null) {
if (fileName != null && curDirectory != null) {
File file = new File(curDirectory.getPath() + "//" + fileName);
boolean created = false;
if (!file.exists()) {
Expand Down Expand Up @@ -613,9 +684,8 @@ private UpdateFilesTask(Context context) {

@Override
protected void onPreExecute() {
Log.i("FilePicker", "AsyncCalled");
dialog = new ProgressDialog(mContext);
dialog.setMessage("Loading...");
dialog.setMessage(getString(R.string.file_picker_progress_dialog_loading));
dialog.setCancelable(false);
dialog.show();
hideButtons();
Expand All @@ -635,7 +705,7 @@ protected void onPostExecute(File[] localFiles) {
files = localFiles;
if (directory.getPath().equalsIgnoreCase(Environment
.getExternalStorageDirectory().getPath())) {
toolbar.setTitle("Parent Directory");
toolbar.setTitle(getString(R.string.file_picker_default_directory_title));

} else {
toolbar.setTitle(directory.getName());
Expand All @@ -647,9 +717,15 @@ protected void onPostExecute(File[] localFiles) {
// for(int i = 0; i < files.length; i++) {
// adapter.addFile(files[i]);
// }
adapter = new FileRecyclerViewAdapter(FilePicker.this, files, scopeType, callback);
//TODO: Fix this, figure out how to add and remove the header.
recyclerView.setAdapter(adapter);
if(files != null) {
adapter = new FileRecyclerViewAdapter(FilePicker.this, files, scopeType, callback);
//TODO: Fix this, figure out how to add and remove the header.
recyclerView.setAdapter(adapter);
}
//make sure the button is showing.
if(!isFabShowing) {
toggleButton(true);
}
if (dialog.isShowing()) {
dialog.dismiss();
}
Expand Down

0 comments on commit de66e42

Please sign in to comment.