-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFileAdapter.java
More file actions
212 lines (185 loc) · 8.57 KB
/
FileAdapter.java
File metadata and controls
212 lines (185 loc) · 8.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package com.example.scolarai;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.core.content.FileProvider;
import androidx.recyclerview.widget.RecyclerView;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
public class FileAdapter extends RecyclerView.Adapter<FileAdapter.FileViewHolder> {
public interface OnFileClickListener {
void onFileClick(File file);
}
private final ArrayList<Uri> files;
private final Context context;
private final OnFileClickListener clickListener;
public FileAdapter(Context context, ArrayList<Uri> files, OnFileClickListener clickListener) {
this.context = context;
this.files = files;
this.clickListener = clickListener;
}
@NonNull
@Override
public FileViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_file, parent, false);
return new FileViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull FileViewHolder holder, int position) {
Uri fileUri = files.get(position);
File file = new File(fileUri.getPath());
String fileName = file.getName();
holder.fileName.setText(fileName);
// Set icon based on file type
if (fileName.endsWith(".txt")) holder.fileIcon.setImageResource(android.R.drawable.ic_menu_edit);
else if (fileName.endsWith(".pdf")) holder.fileIcon.setImageResource(android.R.drawable.ic_dialog_info);
else if (fileName.endsWith(".ppt") || fileName.endsWith(".pptx"))
holder.fileIcon.setImageResource(android.R.drawable.ic_menu_slideshow);
else if (fileName.endsWith(".doc") || fileName.endsWith(".docx"))
holder.fileIcon.setImageResource(android.R.drawable.ic_menu_edit);
else holder.fileIcon.setImageResource(android.R.drawable.ic_menu_help);
// Click file: either open or select for attachment
holder.itemView.setOnClickListener(v -> {
if (clickListener != null) {
clickListener.onFileClick(file); // return selected file to MainActivity
} else {
openFile(file);
}
});
// Overflow menu
holder.fileMenu.setOnClickListener(v -> showPopupMenu(file, position));
}
private void showPopupMenu(File file, int position) {
PopupMenu menu = new PopupMenu(context, files.get(position) != null ? null : null);
menu.getMenu().add("Rename");
menu.getMenu().add("Delete");
menu.getMenu().add("Edit Text (if .txt)");
menu.setOnMenuItemClickListener(item -> {
String action = item.getTitle().toString();
if (action.equals("Rename")) renameFile(file, position);
else if (action.equals("Delete")) deleteFile(file, position);
else if (action.equals("Edit Text (if .txt)")) {
if (file.getName().endsWith(".txt")) editTextFile(file);
else Toast.makeText(context, "Only text files can be edited", Toast.LENGTH_SHORT).show();
}
return true;
});
menu.show();
}
private void openFile(File file) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri contentUri = FileProvider.getUriForFile(
context,
context.getApplicationContext().getPackageName() + ".provider",
file
);
intent.setDataAndType(contentUri, getMimeType(file.getName()));
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(intent);
} catch (Exception e) {
Toast.makeText(context, "Cannot open file: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
private String getMimeType(String fileName) {
if (fileName.endsWith(".pdf")) return "application/pdf";
else if (fileName.endsWith(".txt")) return "text/plain";
else if (fileName.endsWith(".doc") || fileName.endsWith(".docx")) return "application/msword";
else if (fileName.endsWith(".ppt") || fileName.endsWith(".pptx")) return "application/vnd.ms-powerpoint";
else return "*/*";
}
private void renameFile(File file, int position) {
EditText input = new EditText(context);
input.setText(file.getName());
input.setSingleLine(true);
new AlertDialog.Builder(context)
.setTitle("Rename File")
.setView(input)
.setPositiveButton("Rename", (dialog, which) -> {
String newName = input.getText().toString().trim();
if (!newName.isEmpty()) {
File newFile = new File(file.getParent(), newName);
if (file.renameTo(newFile)) {
files.set(position, Uri.fromFile(newFile));
notifyItemChanged(position);
Toast.makeText(context, "Renamed to " + newName, Toast.LENGTH_SHORT).show();
} else Toast.makeText(context, "Rename failed", Toast.LENGTH_SHORT).show();
} else Toast.makeText(context, "Filename cannot be empty", Toast.LENGTH_SHORT).show();
})
.setNegativeButton("Cancel", null)
.show();
}
private void deleteFile(File file, int position) {
new AlertDialog.Builder(context)
.setTitle("Delete File")
.setMessage("Are you sure you want to delete this file?")
.setPositiveButton("Delete", (dialog, which) -> {
if (file.delete()) {
files.remove(position);
notifyItemRemoved(position);
Toast.makeText(context, "File deleted", Toast.LENGTH_SHORT).show();
} else Toast.makeText(context, "Delete failed", Toast.LENGTH_SHORT).show();
})
.setNegativeButton("Cancel", null)
.show();
}
private void editTextFile(File file) {
try {
FileInputStream fis = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
fis.read(data);
fis.close();
String content = new String(data);
EditText editText = new EditText(context);
editText.setText(content);
editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
editText.setMinLines(5);
new AlertDialog.Builder(context)
.setTitle("Edit Text File")
.setView(editText)
.setPositiveButton("Save", (dialog, which) -> saveTextFile(file, editText.getText().toString()))
.setNegativeButton("Cancel", null)
.show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(context, "Error opening file", Toast.LENGTH_SHORT).show();
}
}
private void saveTextFile(File file, String content) {
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write(content.getBytes());
Toast.makeText(context, "File saved successfully", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(context, "Save failed", Toast.LENGTH_SHORT).show();
}
}
@Override
public int getItemCount() {
return files.size();
}
static class FileViewHolder extends RecyclerView.ViewHolder {
ImageView fileIcon, fileMenu;
TextView fileName;
public FileViewHolder(@NonNull View itemView) {
super(itemView);
fileIcon = itemView.findViewById(R.id.file_icon);
fileMenu = itemView.findViewById(R.id.file_menu);
fileName = itemView.findViewById(R.id.file_name);
}
}
}