Skip to content
This repository has been archived by the owner on Sep 8, 2019. It is now read-only.

Commit

Permalink
- added simple FileExplorer to copy, move and delete items
Browse files Browse the repository at this point in the history
- Bug fixes
  • Loading branch information
kollerlukas committed Feb 12, 2017
1 parent cb3857f commit 66452a2
Show file tree
Hide file tree
Showing 22 changed files with 1,400 additions and 35 deletions.
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
android:theme="@style/Theme.CameraRoll.Translucent.AboutActivity"
android:launchMode="singleTop"/>

<activity android:name=".ui.FileExplorerActivity"
android:theme="@style/Theme.CameraRoll.Translucent.FileExplorerActivity"
android:launchMode="singleTop"/>

<activity android:name=".IntentReceiver">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/us/koller/cameraroll/IntentReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public void viewPhoto(Intent intent) {
.putExtra(ItemActivity.VIEW_ONLY, true)
.putExtra(ItemActivity.ALBUM, album)
.putExtra(ItemActivity.ITEM_POSITION, album.getAlbumItems().indexOf(albumItem))
.putExtra(ItemActivity.FINISH_AFTER, true)
.addFlags(intent.getFlags());
startActivity(view_photo);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
package us.koller.cameraroll.adapter.fileExplorer;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import us.koller.cameraroll.R;
import us.koller.cameraroll.adapter.fileExplorer.ViewHolder.FileHolder;
import us.koller.cameraroll.data.Album;
import us.koller.cameraroll.data.AlbumItem;
import us.koller.cameraroll.data.File_POJO;
import us.koller.cameraroll.ui.ItemActivity;

public class RecyclerViewAdapter extends RecyclerView.Adapter {

public static final int NORMAL_MODE = 0;
public static final int SELECTOR_MODE = 1;
public static final int PICK_TARGET_MODE = 2;


public interface Callback {
void onSelectorModeEnter();

void onSelectorModeExit(File_POJO[] selected_items);

void onItemSelected(int count);

void onPickTargetModeEnter();

void onPickTargetModeExit();

void onDataChanged();
}

private File_POJO files;

private int mode = NORMAL_MODE;
private boolean[] selected_items;

private Callback callback;

public RecyclerViewAdapter(Callback callback) {
this.callback = callback;
}

public RecyclerViewAdapter setFiles(File_POJO files) {
this.files = files;
selected_items = new boolean[files.getChildren().size()];
return this;
}

public File_POJO getFiles() {
return files;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.file_cover, parent, false);
return new FileHolder(v);
}

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
final File_POJO file = files.getChildren().get(position);

((FileHolder) holder).setFile(file);

((FileHolder) holder).setSelected(selected_items[position]);

holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mode == SELECTOR_MODE) {
onItemSelect(file);
} else if (file.isMedia) {

int index = file.getPath().lastIndexOf("/");
String path = file.getPath().substring(0, index);

//load Album
final Album album = new Album().setPath(path);
AlbumItem albumItem = AlbumItem.getInstance(holder.itemView.getContext(), file.getPath());
if (albumItem != null) {
album.getAlbumItems()
.add(albumItem);
}

//create intent
Intent intent = new Intent(holder.itemView.getContext(), ItemActivity.class)
.putExtra(ItemActivity.ALBUM_ITEM, albumItem)
.putExtra(ItemActivity.ALBUM, album)
.putExtra(ItemActivity.VIEW_ONLY, true)
.putExtra(ItemActivity.ITEM_POSITION, album.getAlbumItems().indexOf(albumItem))
.putExtra(ItemActivity.FINISH_AFTER, false);

try {
holder.itemView.getContext().startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
} else {
//to keep the ripple animation
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
RecyclerViewAdapter.this.setFiles(file);
RecyclerViewAdapter.this.notifyDataSetChanged();

if (callback != null) {
callback.onDataChanged();
}
}
}, 300);
}
}
});

holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
onItemSelect(file);
return true;
}
});

//clicking on folder icons also selects this item
holder.itemView.findViewById(R.id.folder_indicator).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onItemSelect(file);
}
});
}

private void onItemSelect(File_POJO file) {
if (mode == NORMAL_MODE) {
mode = SELECTOR_MODE;
selected_items = new boolean[files.getChildren().size()];
if (callback != null) {
callback.onSelectorModeEnter();
}
}

int position = files.getChildren().indexOf(file);
selected_items[position] = !selected_items[position];
notifyItemChanged(position);

if (callback != null) {
callback.onItemSelected(getSelectedCount());
}

checkSelectorMode();
}

public boolean isModeActive() {
return mode == SELECTOR_MODE;
}

public int getMode() {
return mode;
}

private int getSelectedCount() {
int selected_items_count = 0;
for (int i = 0; i < selected_items.length; i++) {
selected_items_count += selected_items[i] ? 1 : 0;
}
return selected_items_count;
}

private void checkSelectorMode() {
int selected_items_count = getSelectedCount();
if (selected_items_count == 0) {
cancelMode();
}
}

public void cancelMode() {
if (mode == SELECTOR_MODE) {
mode = NORMAL_MODE;
if (callback != null) {
File_POJO[] files = new File_POJO[getSelectedCount()];
int index = 0;
for (int i = 0; i < selected_items.length; i++) {
if (selected_items[i]) {
files[index] = this.files.getChildren().get(i);
index++;
}
}
callback.onSelectorModeExit(files);
}
selected_items = new boolean[files.getChildren().size()];
} else if (mode == PICK_TARGET_MODE) {
mode = NORMAL_MODE;
if (callback != null) {
callback.onPickTargetModeExit();
}
}
notifyDataSetChanged();
}

public void pickTarget() {
mode = PICK_TARGET_MODE;
if (callback != null) {
callback.onPickTargetModeEnter();
}
}

@Override
public int getItemCount() {
return files.getChildren().size();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package us.koller.cameraroll.adapter.fileExplorer.ViewHolder;

import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.File;

import us.koller.cameraroll.R;
import us.koller.cameraroll.data.File_POJO;
import us.koller.cameraroll.util.MediaType;

public class FileHolder extends RecyclerView.ViewHolder {

private File_POJO file;

public FileHolder(View itemView) {
super(itemView);
}

public void setFile(File_POJO file) {
this.file = file;

ImageView folderIndicator = (ImageView) itemView.findViewById(R.id.folder_indicator);
if (!file.isMedia) {
folderIndicator.setImageResource(R.drawable.ic_folder_white_24dp);
} else if (MediaType.isVideo(folderIndicator.getContext(), file.getPath())) {
folderIndicator.setImageResource(R.drawable.ic_videocam_white_24dp);
} else {
folderIndicator.setImageResource(R.drawable.ic_photo_white_24dp);
}

TextView textView = (TextView) itemView.findViewById(R.id.text);
textView.setText(file.getName());
}

public void setSelected(boolean selected) {
itemView.setBackgroundColor(
ContextCompat.getColor(itemView.getContext(),
selected ? R.color.colorAccent_translucent :
android.R.color.transparent));

TextView textView = (TextView) itemView.findViewById(R.id.text);
textView.setTextColor(ContextCompat.getColor(itemView.getContext(),
selected ? R.color.grey_900_translucent : R.color.white_translucent1));

ImageView folderIndicator = (ImageView) itemView.findViewById(R.id.folder_indicator);
Drawable d = folderIndicator.getDrawable().mutate();
d.setTint(ContextCompat.getColor(itemView.getContext(),
selected ? R.color.grey_900_translucent : R.color.white));
folderIndicator.setImageDrawable(d);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package us.koller.cameraroll.adapter.item.ViewHolder;

import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
Expand Down Expand Up @@ -50,6 +49,12 @@ public void swapView(final boolean isReturning) {
}

private void bindImageView(View view, View transitionView) {
if (albumItem.error) {
transitionView.setVisibility(View.VISIBLE);
ItemViewUtil.bindTransitionView((ImageView) transitionView, albumItem);
return;
}

final SubsamplingScaleImageView imageView = (SubsamplingScaleImageView) view;

final GestureDetector gestureDetector
Expand Down
15 changes: 9 additions & 6 deletions app/src/main/java/us/koller/cameraroll/data/AlbumItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,16 @@ public long getDate(Activity context) {
public Uri getUri(Context context) {
Uri uri;
if (!contentUri) {
File file = new File(getPath());
uri = FileProvider.getUriForFile(context,
context.getApplicationContext().getPackageName() + ".provider", file);
} else {
uri = Uri.parse(getPath());
try {
File file = new File(getPath());
uri = FileProvider.getUriForFile(context,
context.getApplicationContext().getPackageName() + ".provider", file);
return uri;
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
return uri;
return Uri.parse(getPath());
}

AlbumItem(Parcel parcel) {
Expand Down
Loading

0 comments on commit 66452a2

Please sign in to comment.