Skip to content
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
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ android {
buildToolsVersion "23.0.2"

defaultConfig {
applicationId "com.csc.lesson5"
applicationId "com.csc.fedorov.file_manager"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
Expand All @@ -21,4 +21,5 @@ android {

dependencies {
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:recyclerview-v7:+'
}
7 changes: 5 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.csc.lesson5">
package="com.csc.fedorov.file_manager">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity android:name="com.csc.fedorov.file_manager.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
104 changes: 104 additions & 0 deletions app/src/main/java/com/csc/fedorov/file_manager/FilesAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.csc.fedorov.file_manager;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
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 java.io.File;
import java.util.List;

/**
* Created by roman on 28.03.2016.
*/
public class FilesAdapter extends RecyclerView.Adapter<FilesAdapter.ViewHolder> {
public List<File> mFiles;
private Drawable fileIcon;
private Drawable folderIcon;
public Context context;

private static OnItemClickListener clickListener;
private static OnItemLongClickListener longClickListener;


public interface OnItemClickListener {
void onItemClick(View itemView, int position);
}

public interface OnItemLongClickListener {
void onItemLongClick(View itemView, int position);
}

public void setOnItemClickListener(OnItemClickListener listener) {
this.clickListener = listener;
}

public void setOnItemLongClickListener(OnItemLongClickListener listener) {
this.longClickListener = listener;
}

public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView textView;
public ImageView imageView;

public ViewHolder(final View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.file_item_name);
imageView = (ImageView) itemView.findViewById(R.id.file_item_image);

itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clickListener != null)
clickListener.onItemClick(itemView, getLayoutPosition());
}
});

itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (longClickListener != null) {
longClickListener.onItemLongClick(itemView, getLayoutPosition());
}
return true;
}
});
}
}

public FilesAdapter(List<File> myDataset) {
mFiles = myDataset;
}

// Create new views (invoked by the layout manager)
@Override
public FilesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
context = parent.getContext();
View fileView = LayoutInflater.from(context).inflate(R.layout.file_item, parent, false);
folderIcon = ContextCompat.getDrawable(context, R.drawable.folder_icon_512x512);
fileIcon = ContextCompat.getDrawable(context, R.drawable.file_icon);

return new ViewHolder(fileView);
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.textView.setText(mFiles.get(position).getName());
if (mFiles.get(position).isDirectory()) {
holder.imageView.setImageDrawable(folderIcon);
} else {
holder.imageView.setImageDrawable(fileIcon);
}
}

@Override
public int getItemCount() {
return mFiles.size();
}
}
83 changes: 83 additions & 0 deletions app/src/main/java/com/csc/fedorov/file_manager/MainActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.csc.fedorov.file_manager;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private FilesAdapter adapter;
private RecyclerView.LayoutManager layoutManager;
private File currentFile;
private final ArrayList<File> filesList = new ArrayList<>();

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

currentFile = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

recyclerView = (RecyclerView) findViewById(R.id.files_recycler_view);
final File[] filesArray = currentFile.listFiles();
Collections.addAll(filesList, filesArray);
adapter = new FilesAdapter(filesList);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter.setOnItemClickListener(new FilesAdapter.OnItemClickListener() {
@Override
public void onItemClick(View itemView, int position) {
File clickedFile = filesList.get(position);
if (clickedFile.isDirectory()) {
File directoryToOpen = clickedFile.getAbsoluteFile();
ArrayList<File> insideDirectoryList = new ArrayList<>();
Collections.addAll(insideDirectoryList, directoryToOpen.listFiles());
filesList.clear();
filesList.addAll(insideDirectoryList);
adapter.notifyDataSetChanged();
}
}
});
adapter.setOnItemLongClickListener(new FilesAdapter.OnItemLongClickListener() {
@Override
public void onItemLongClick(View itemView, final int position) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(R.string.choose_action).setItems(R.array.actions, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == 2) {
if (currentFile.delete()) {
Toast.makeText(MainActivity.this, R.string.delete_ok, Toast.LENGTH_LONG).show();
adapter.notifyItemRemoved(position);
} else {
Toast.makeText(MainActivity.this, R.string.delete_not_ok, Toast.LENGTH_LONG).show();
}
}
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
}

@Override
public void onBackPressed() {
File parentFile = currentFile.getParentFile();
ArrayList<File> parentDirectoryList = new ArrayList<>();
Collections.addAll(parentDirectoryList, parentFile.listFiles());
filesList.clear();
filesList.addAll(parentDirectoryList);
adapter.notifyDataSetChanged();
}
}
12 changes: 0 additions & 12 deletions app/src/main/java/com/csc/lesson5/MainActivity.java

This file was deleted.

Binary file added app/src/main/res/drawable-hdpi/file_icon.9.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-mdpi/file_icon.9.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-xhdpi/file_icon.9.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/file_icon.9.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-xxxhdpi/file_icon.9.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.
22 changes: 22 additions & 0 deletions app/src/main/res/layout/file_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground">

<ImageView
android:id="@+id/file_item_image"
android:layout_width="@dimen/icon_size"
android:layout_height="@dimen/icon_size"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:contentDescription="@string/file_icon" />

<TextView
android:id="@+id/file_item_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toEndOf="@+id/file_item_image"
android:layout_toRightOf="@+id/file_item_image" />

</RelativeLayout>
18 changes: 18 additions & 0 deletions app/src/main/res/layout/main_activity.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.RecyclerView
android:layout_marginTop="5dp"
android:id="@+id/files_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
4 changes: 4 additions & 0 deletions app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="icon_size">40dp</dimen>
</resources>
12 changes: 11 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
<resources>
<string name="app_name">lesson5</string>
<string name="app_name">File manager</string>
<string name="file_icon">File icon</string>
<string name="choose_action">Choose action</string>
<string name="delete_ok">Successfully deleted</string>
<string name="delete_not_ok">You can\'t delete this file</string>

<array name="actions">
<item>Open</item>
<item>Rename</item>
<item>Delete</item>
</array>
</resources>