Skip to content

Commit

Permalink
#8: added File-FolderPicker cloned src from https://github.com/kashif…
Browse files Browse the repository at this point in the history
  • Loading branch information
k3b committed Dec 22, 2017
1 parent 42dccae commit 6e350d4
Show file tree
Hide file tree
Showing 28 changed files with 581 additions and 8 deletions.
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ this program. If not, see <http://www.gnu.org/licenses/>
</intent-filter>

</activity>
<activity
android:name="lib.folderpicker.FolderPicker"
android:screenOrientation="portrait"
></activity>

</application>

Expand Down
270 changes: 270 additions & 0 deletions app/src/main/java/lib/folderpicker/FolderPicker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
/*
from https://github.com/kashifo/android-folder-picker-library
Copyright 2017 Kashif Anwaar.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package lib.folderpicker;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;

import de.k3b.android.toGoZip.R;
import de.k3b.android.widget.LocalizedActivity;

public class FolderPicker extends LocalizedActivity {

ArrayList<String> namesList;
ArrayList<String> typesList;

ArrayList<String> foldersList;
ArrayList<String> filesList;

TextView tv_title;
TextView tv_location;

String location = Environment.getExternalStorageDirectory().getAbsolutePath();
boolean pickFiles;
Intent receivedIntent;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fp_main_layout);

if( !isExternalStorageReadable() ){
Toast.makeText(this, "Storage access permission not given", Toast.LENGTH_LONG).show();
finish();
}

tv_title = (TextView) findViewById(R.id.fp_tv_title);
tv_location = (TextView) findViewById(R.id.fp_tv_location);

try {
receivedIntent = getIntent();

if( receivedIntent.hasExtra("title") ) {
String receivedTitle = receivedIntent.getExtras().getString("title");
if (receivedTitle != null) {
tv_title.setText(receivedTitle);
}
}

if( receivedIntent.hasExtra("location") ) {
String reqLocation = receivedIntent.getExtras().getString("location");
if (reqLocation != null) {
File requestedFolder = new File(reqLocation);
if (requestedFolder.exists())
location = reqLocation;
}
}

if( receivedIntent.hasExtra("pickFiles") ) {
pickFiles = receivedIntent.getExtras().getBoolean("pickFiles");
if (pickFiles) {
findViewById(R.id.fp_btn_select).setVisibility(View.GONE);
findViewById(R.id.fp_btn_new).setVisibility(View.GONE);
}
}

} catch (Exception e) {
e.printStackTrace();
}

loadLists(location);

}

/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}

public void loadLists(String location) {
try {

File folder = new File(location);

if( !folder.isDirectory() )
return;

tv_location.setText( "Location : "+ folder.getAbsolutePath() );
File[] files = folder.listFiles();

foldersList = new ArrayList<>();
filesList = new ArrayList<>();

for ( File currentFile : files ) {
if (currentFile.isDirectory()) {
foldersList.add(currentFile.getName());
} else {
filesList.add(currentFile.getName());
}
}

// sort & add to final List
Collections.sort(foldersList);
namesList = new ArrayList<>();
namesList.addAll(foldersList);

// add types
typesList = new ArrayList<>();

for (int i = 0; i < foldersList.size(); i++)
typesList.add("folder");

if (pickFiles) {
Collections.sort(filesList);

namesList.addAll(filesList);

for (int i = 0; i < filesList.size(); i++)
typesList.add("file");
}

showList();

} catch (Exception e) {
e.printStackTrace();
}

} // load List

public void showList() {

try {
simpleadapter sa = new simpleadapter(this, namesList, typesList);
ListView lv = (ListView) findViewById(R.id.fp_listView);
lv.setAdapter(sa);

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
listClick(position);
}
});

} catch (Exception e) {
e.printStackTrace();
}

}


public void listClick(int position) {

if ( pickFiles && typesList.get(position).equals("file")) {
String data = location + File.separator + namesList.get(position);
receivedIntent.putExtra("data", data);
setResult(RESULT_OK, receivedIntent);
finish();
} else {
location = location + File.separator + namesList.get(position);
loadLists(location);
}

}

public void goBack(View v) {

if( location!=null && !location.equals("") && !location.equals("/") ) {
int start = location.lastIndexOf('/');
String newLocation = location.substring(0, start);
location = newLocation;
loadLists(location);
}

}

public void newFolder(String filename) {
try {

File file = new File(location + File.separator + filename);
file.mkdirs();
loadLists(location);

} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "Error:" + e.toString(), Toast.LENGTH_LONG)
.show();
}

}

public void newFolderDialog(View v) {
AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setTitle("Enter Folder Name");

final EditText et = new EditText(this);
dialog.setView(et);

dialog.setButton(DialogInterface.BUTTON_POSITIVE, "Create",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
newFolder(et.getText().toString());
}
});
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {

}
});

dialog.show();

}


public void select(View v) {

if (pickFiles) {
Toast.makeText(this, "You have to select a file", Toast.LENGTH_LONG).show();
} else if (receivedIntent != null) {
receivedIntent.putExtra("data", location);
setResult(RESULT_OK, receivedIntent);
finish();
}
}


public void cancel(View v) {
finish();
}


} // class
69 changes: 69 additions & 0 deletions app/src/main/java/lib/folderpicker/simpleadapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
from https://github.com/kashifo/android-folder-picker-library
Copyright 2017 Kashif Anwaar.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package lib.folderpicker;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import de.k3b.android.toGoZip.R;

public class simpleadapter extends ArrayAdapter<String> {

Activity context;
ArrayList<String> namesList;
ArrayList<String> typesList;

public simpleadapter(Activity context, ArrayList<String> namesList, ArrayList<String> typesList) {
super(context, R.layout.filerow, namesList);

this.context = context;
this.namesList = namesList;
this.typesList = typesList;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(R.layout.filerow, parent, false);

ImageView imageView = (ImageView) convertView.findViewById(R.id.fp_iv_icon);
TextView name = (TextView) convertView.findViewById(R.id.fp_tv_name);

if( typesList.get(position).equals("folder") )
{
imageView.setImageResource( R.drawable.folder );
}
else
{
imageView.setImageResource( R.drawable.file );
}

name.setText( namesList.get(position) );

return convertView;
}

}
Binary file added app/src/main/res/drawable-hdpi/ic_action_add.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-hdpi/ic_action_back.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-hdpi/ic_action_cancel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-hdpi/ic_action_up.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-mdpi/ic_action_add.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-mdpi/ic_action_back.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-mdpi/ic_action_cancel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-mdpi/ic_action_up.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xhdpi/ic_action_add.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xhdpi/ic_action_back.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xhdpi/ic_action_cancel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xhdpi/ic_action_up.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xxhdpi/ic_action_add.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xxhdpi/ic_action_back.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xxhdpi/ic_action_up.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/file.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/folder.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions app/src/main/res/drawable/outline_black_1dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

<solid android:color="@android:color/transparent"/>

<stroke android:width="1dp"
android:color="#000"
/>

<padding android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp"
/>

<corners
android:bottomRightRadius="1dp"
android:bottomLeftRadius="1dp"
android:topLeftRadius="1dp"
android:topRightRadius="1dp"
/>

</shape>
Loading

0 comments on commit 6e350d4

Please sign in to comment.