Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom File Chooser Implementation for SDcard location choosing #138

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ android:
# - tools

# The BuildTools version used by your project
- build-tools-19.1.0
- build-tools-22.0.1

# The SDK version used to compile your project
- android-19
- android-22

# Additional components
- extra-google-m2repository
Expand Down
6 changes: 3 additions & 3 deletions android_commons.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ repositories {


android {
compileSdkVersion 21
buildToolsVersion '21.1.2'
compileSdkVersion 22
buildToolsVersion '22.0.1'

defaultConfig {
minSdkVersion 14
targetSdkVersion 21
targetSdkVersion 22
}

lintOptions {
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ dependencies {


compile 'commons-io:commons-io:2.4'
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:appcompat-v7:22.1.1'
compile 'com.android.support:recyclerview-v7:21.0.3'
compile 'com.android.support:support-v4:21.0.3'
compile 'com.android.support:support-v4:22.1.1'
compile 'com.github.chrisbanes.photoview:library:1.2.3'
compile 'com.path:android-priority-jobqueue:1.1.2'
compile 'de.greenrobot:eventbus:2.2.0'
Expand Down
7 changes: 2 additions & 5 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
android:resource="@xml/file_path" />
</provider>

<!-- aFileChooser -->
<!-- Internal File Chooser -->
<activity
android:name="com.ipaulpro.afilechooser.FileChooserActivity"
android:name=".activities.FileChooserActivity"
android:icon="@drawable/ic_launcher"
android:label="@string/Dialog_header__pick_file" />
<activity
Expand All @@ -51,9 +51,6 @@
<data android:mimeType="*/*" />
</intent-filter>
</activity>
<activity
android:name=".activities.ChooseFolder"
android:label="@string/Page_header__choose_folder" />

<receiver android:name=".OutgoingCallReceiver">
<intent-filter android:priority="9999">
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 com.doplgangr.secrecy.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;

import com.doplgangr.secrecy.R;
import com.doplgangr.secrecy.fragments.FileChooserFragment;

import java.io.File;
import java.lang.reflect.Array;
import java.util.ArrayList;


public class FileChooserActivity extends ActionBarActivity implements FileChooserFragment.OnFileChosen{
public static final String FOLDERS_ONLY = "FOLDERS_ONLY"; // folders only extra
public static final String FILE_SELECTED = "FILE_SELECTED"; // selected file extra
public static final String ROOT_FOLDER = "ROOT_FOLDER"; // root folder extra
public static final String FILE_EXTENSIONS = "FILE_EXTENSIONS"; // file extensions to show
private File root = null;
private Boolean foldersOnly = false;
private ArrayList<String> fileExtensions = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file_chooser);
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
if (mToolbar!=null)
setSupportActionBar(mToolbar);
Intent intent = getIntent();
if (intent!=null) {
String root_folder = intent.getStringExtra(ROOT_FOLDER);
if (root_folder!=null)
root=new File(root_folder);
foldersOnly = intent.getBooleanExtra(FOLDERS_ONLY, false);
fileExtensions = intent.getStringArrayListExtra(FILE_EXTENSIONS);
}
FileChooserFragment fragment = FileChooserFragment.newInstance(root, foldersOnly, fileExtensions);
getFragmentManager().beginTransaction()
.replace(R.id.content_frame, fragment)
.setTransition(android.support.v4.app.FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit();
}

@Override
public void onFileSelected(File path, Boolean confirmed) {
if (confirmed) {
Intent returnIntent = new Intent();
returnIntent.putExtra(FILE_SELECTED,path);
setResult(RESULT_OK, returnIntent);
finish();
}else {
FileChooserFragment fragment = FileChooserFragment.newInstance(path, foldersOnly, fileExtensions);
getFragmentManager().beginTransaction()
.replace(R.id.content_frame, fragment)
.setTransition(android.support.v4.app.FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(path.getAbsolutePath()).commit();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package com.doplgangr.secrecy.adapters;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.doplgangr.secrecy.R;
import com.doplgangr.secrecy.filesystem.Storage;
import com.doplgangr.secrecy.fragments.FileChooserFragment;
import com.doplgangr.secrecy.utils.Util;

import java.io.File;
import java.util.List;

public class FileChooserAdapter extends RecyclerView.Adapter<FileChooserAdapter.FileChooserViewHolder> {

private Context context;
/**
* Inflater of the context;
*/
private LayoutInflater inflater;
/**
* File object of the directory to be displayed
*/
private File rootFile;
/**
* File object of the children to the directory to be displayed
*/
private List<File> files;

private FileChooserFragment.OnFileChosen mListener;


public FileChooserAdapter(Activity context,
List<File> files, File rootFile, FileChooserFragment.OnFileChosen mListener) {
this.context = context;
this.inflater = context.getLayoutInflater();
this.files = files;
this.rootFile = rootFile;
this.mListener = mListener;
}

@Override
public FileChooserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new FileChooserViewHolder(inflater.inflate(R.layout.listitem_single_line_text, parent, false));
}

@Override
public void onBindViewHolder(FileChooserViewHolder holder, int position) {

final File item = files.get(position);
Boolean isParent = rootFile.getParentFile()!=null && rootFile.getParentFile().getAbsolutePath().equals(item.getAbsolutePath());
Boolean isFile = item.isFile();
Boolean isReadableDir = Util.canReadDir(item);

if (holder.textView!=null){
// TODO: create a more intuitive way to let user know this is "up"
// If the rootFile has a parent, display as "up"
if (isParent)
holder.textView.setText("..");
else {
holder.textView.setText(item.getName());
}
holder.textView.setEnabled(isFile || isReadableDir);
if (isSubDirectory(item, Storage.getRoot()) && !isParent)
holder.textView.setTextColor(context.getResources().getColor(R.color.accent));
else
holder.textView.setTextColor(context.getResources().getColor(R.color.text_primary));
}
if (holder.iconView!=null){
if (isFile)
holder.iconView.setImageResource(R.drawable.ic_file);
if (isReadableDir)
holder.iconView.setImageResource(R.drawable.ic_action_folder);
if (isSubDirectory(item, Storage.getRoot()) && !isParent)
holder.iconView.setColorFilter(context.getResources().getColor(R.color.accent));
else
holder.iconView.setColorFilter(context.getResources().getColor(R.color.button));
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (null != mListener) {
// Notify the active callbacks interface (the activity, if the
// fragment is attached to one) that an item has been selected.
if (Util.canReadDir(item))
mListener.onFileSelected(item, false);
else
mListener.onFileSelected(item, true);
}
}
});
}

@Override
public int getItemCount() {
if (files!=null)
return files.size();
return 0;
}

/**
* Checks whether a directory is a subdirectory under certain root directory
* @param directory root directory to be checked against
* @param file file suspected of being a subdirectory
* @return true if file is in directory, false if not.
*/
public static boolean isSubDirectory(File directory, File file) {
if (file == null)
return false;
if (file.equals(directory))
return true;
return isSubDirectory(directory, file.getParentFile());
}

public class FileChooserViewHolder extends RecyclerView.ViewHolder{
TextView textView;
ImageView iconView;
public FileChooserViewHolder(View itemView) {
super(itemView);
textView =(TextView) itemView.findViewById(R.id.text1);
iconView =(ImageView) itemView.findViewById(R.id.icon1);
}
}
}
Loading