Skip to content
This repository has been archived by the owner on Feb 11, 2022. It is now read-only.

Commit

Permalink
Merge branch 'root'
Browse files Browse the repository at this point in the history
  • Loading branch information
spacecowboy committed May 29, 2016
2 parents 4080ad6 + cc17fc1 commit 34f4768
Show file tree
Hide file tree
Showing 12 changed files with 250 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -319,14 +319,13 @@ public void onActivityCreated(Bundle savedInstanceState) {
}
}
}

// If still null
if (mCurrentPath == null) {
mCurrentPath = getRoot();
}
}

refresh();
// If still null
if (mCurrentPath == null) {
mCurrentPath = getRoot();
}
refresh(mCurrentPath);
}

@Override
Expand Down Expand Up @@ -372,33 +371,39 @@ public void onDetach() {
* if permissions are granted and requests them if necessary. See hasPermission()
* and handlePermission(). By default, these methods do nothing. Override them if
* you need to request permissions at runtime.
*
* @param nextPath path to list files for
*/
protected void refresh() {
if (hasPermission()) {
protected void refresh(@NonNull T nextPath) {
if (hasPermission(nextPath)) {
mCurrentPath = nextPath;
isLoading = true;
getLoaderManager()
.restartLoader(0, null, AbstractFilePickerFragment.this);
} else {
handlePermission();
handlePermission(nextPath);
}
}

/**
* If permission has not been granted yet, this method should request it.
* <p/>
* Override only if you need to request a permission.
*
* @param path The path for which permission should be requested
*/
protected void handlePermission() {
protected void handlePermission(@NonNull T path) {
// Nothing to do by default
}

/**
* If your implementation needs to request a specific permission to function, check if it
* has been granted here. You should probably also override handlePermission() to request it.
*
* @param path the path for which permissions should be checked
* @return true if permission has been granted, false otherwise.
*/
protected boolean hasPermission() {
protected boolean hasPermission(@NonNull T path) {
// Nothing to request by default
return true;
}
Expand Down Expand Up @@ -573,10 +578,9 @@ public void onClickDir(@NonNull View view, @NonNull DirViewHolder viewHolder) {
*/
public void goToDir(@NonNull T file) {
if (!isLoading) {
mCurrentPath = file;
mCheckedItems.clear();
mCheckedVisibleViewHolders.clear();
refresh();
refresh(file);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class FilePickerFragment extends AbstractFilePickerFragment<File> {

protected static final int PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 1;
protected boolean showHiddenItems = false;
private File mRequestedPath = null;

public FilePickerFragment() {
}
Expand Down Expand Up @@ -55,7 +56,7 @@ public boolean areHiddenItemsShown(){
* @return true if app has been granted permission to write to the SD-card.
*/
@Override
protected boolean hasPermission() {
protected boolean hasPermission(@NonNull File path) {
return PackageManager.PERMISSION_GRANTED ==
ContextCompat.checkSelfPermission(getContext(),
Manifest.permission.WRITE_EXTERNAL_STORAGE);
Expand All @@ -65,13 +66,14 @@ protected boolean hasPermission() {
* Request permission to write to the SD-card.
*/
@Override
protected void handlePermission() {
protected void handlePermission(@NonNull File path) {
// Should we show an explanation?
// if (shouldShowRequestPermissionRationale(
// Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// Explain to the user why we need permission
// }

mRequestedPath = path;
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
}
Expand All @@ -97,7 +99,9 @@ public void onRequestPermissionsResult(int requestCode,
} else { // if (requestCode == PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE) {
if (PackageManager.PERMISSION_GRANTED == grantResults[0]) {
// Do refresh
refresh();
if (mRequestedPath != null) {
refresh(mRequestedPath);
}
} else {
Toast.makeText(getContext(), R.string.nnf_permission_external_write_denied,
Toast.LENGTH_SHORT).show();
Expand Down Expand Up @@ -303,8 +307,7 @@ public void onNewFolder(@NonNull final String name) {
File folder = new File(mCurrentPath, name);

if (folder.mkdir()) {
mCurrentPath = folder;
refresh();
refresh(folder);
} else {
Toast.makeText(getActivity(), R.string.nnf_create_folder_error,
Toast.LENGTH_SHORT).show();
Expand All @@ -322,7 +325,7 @@ public void onNewFolder(@NonNull final String name) {
* @return True if item should be added to the list, false otherwise
*/
protected boolean isItemVisible(final File file) {
if(!showHiddenItems && file.isHidden()){
if (!showHiddenItems && file.isHidden()) {
return false;
}
return (isDir(file) || (mode == MODE_FILE || mode == MODE_FILE_AND_DIR));
Expand Down
3 changes: 3 additions & 0 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,7 @@ dependencies {

// FTP browser sample
compile 'commons-net:commons-net:3.3'

// Root example
compile 'eu.chainfire:libsuperuser:1.0.0.+'
}
20 changes: 20 additions & 0 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@
</intent-filter>
</activity>

<activity
android:name=".root.SUPickerActivity"
android:label="@string/title_activity_no_nonsense_file_picker"
android:theme="@style/SampleThemeLight">
<intent-filter>
<action android:name="android.intent.action.GET_CONTENT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

<activity
android:name=".root.SUPickerActivity2"
android:label="@string/title_activity_no_nonsense_file_picker"
android:theme="@style/SampleTheme">
<intent-filter>
<action android:name="android.intent.action.GET_CONTENT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

<activity
android:name=".MultimediaPickerActivity2"
android:label="@string/title_activity_no_nonsense_file_picker"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import com.nononsenseapps.filepicker.sample.dropbox.DropboxSyncHelper;
import com.nononsenseapps.filepicker.sample.ftp.FtpPickerActivity;
import com.nononsenseapps.filepicker.sample.ftp.FtpPickerActivity2;
import com.nononsenseapps.filepicker.sample.root.SUPickerActivity;
import com.nononsenseapps.filepicker.sample.root.SUPickerActivity2;

import java.util.ArrayList;

Expand Down Expand Up @@ -235,6 +237,45 @@ public void onClick(final View v) {
}
}
});

findViewById(R.id.button_root).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i;

if (checkLightTheme.isChecked()) {
i = new Intent(NoNonsenseFilePicker.this,
SUPickerActivity2.class);
} else {
i = new Intent(NoNonsenseFilePicker.this,
SUPickerActivity.class);
}
i.setAction(Intent.ACTION_GET_CONTENT);

i.putExtra(SUPickerActivity.EXTRA_ALLOW_MULTIPLE,
checkAllowMultiple.isChecked());
i.putExtra(SUPickerActivity.EXTRA_ALLOW_CREATE_DIR,
checkAllowCreateDir.isChecked());

// What mode is selected (makes no sense to restrict to folders here)
final int mode;
switch (radioGroup.getCheckedRadioButtonId()) {
case R.id.radioFilesAndDirs:
mode =
AbstractFilePickerFragment.MODE_FILE_AND_DIR;
break;
case R.id.radioFile:
default:
mode = AbstractFilePickerFragment.MODE_FILE;
break;
}

i.putExtra(FilePickerActivity.EXTRA_MODE, mode);


startActivityForResult(i, CODE_SD);
}
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public class DropboxFilePickerFragment
extends AbstractFilePickerFragment<DropboxAPI.Entry> {

private final DropboxAPI<AndroidAuthSession> dbApi;
private FolderCreator folderCreator;
private ProgressBar progressBar;
private RecyclerView recyclerView;

Expand Down Expand Up @@ -114,10 +113,12 @@ public void onClick(final View v) {

/**
* If we are loading, then hide the list and show the progress bar instead.
*
* @param nextPath path to list files for
*/
@Override
protected void refresh() {
super.refresh();
protected void refresh(DropboxAPI.Entry nextPath) {
super.refresh(nextPath);
if (isLoading) {
progressBar.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.INVISIBLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,7 @@ protected FtpFile doInBackground(String... names) {
@Override
protected void onPostExecute(FtpFile folder) {
if (folder != null) {
mCurrentPath = folder;
refresh();
refresh(folder);
} else {
Toast.makeText(getContext(), R.string.nnf_create_folder_error, Toast.LENGTH_SHORT).show();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.nononsenseapps.filepicker.sample.root;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;

/**
* A dialog which tells the user that no SU binary is available
*/
public class SUErrorFragment extends DialogFragment {

private static final String TAG = "SUErrorFragment";

public static void showDialog(@NonNull final FragmentManager fm) {
SUErrorFragment d = new SUErrorFragment();
d.show(fm, TAG);
}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("No read permisson, root unavailable")
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
return builder.create();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.nononsenseapps.filepicker.sample.root;

import android.os.Environment;
import android.support.annotation.Nullable;

import com.nononsenseapps.filepicker.AbstractFilePickerActivity;
import com.nononsenseapps.filepicker.AbstractFilePickerFragment;

import java.io.File;

public class SUPickerActivity extends AbstractFilePickerActivity<File> {

public SUPickerActivity() {
super();
}

@Override
protected AbstractFilePickerFragment<File> getFragment(@Nullable String startPath,
int mode,
boolean allowMultiple,
boolean allowCreateDir) {
AbstractFilePickerFragment<File> fragment = new SUPickerFragment();
// 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);
return fragment;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.nononsenseapps.filepicker.sample.root;

/**
* Just for second theme
*/
public class SUPickerActivity2 extends SUPickerActivity {
}

0 comments on commit 34f4768

Please sign in to comment.