-
Notifications
You must be signed in to change notification settings - Fork 3
/
FileSelector.java
191 lines (152 loc) · 6.13 KB
/
FileSelector.java
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
package com.freestyler.classnotebook.criss.helper;
import android.app.Activity;
import android.app.Dialog;
import android.graphics.Typeface;
import android.os.Environment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.io.File;
import java.util.ArrayList;
/**
* Created by Javad on 2019-11-28 at 4:43 PM.
*/
public class FileSelector {
private Activity context;
private String[] extensions;
private ArrayList<SelectedFile> itemsData = new ArrayList<>();
public static final String MP4 = ".mp4", MP3 = ".mp3", JPG = ".jpg", JPEG = ".jpeg", PNG = ".png", DOC = ".doc", DOCX = ".docx", XLS = ".xls", XLSX = ".xlsx", PDF = ".pdf";
public FileSelector(Activity context, String[] extensions) {
this.context = context;
this.extensions = extensions;
}
public interface OnSelectListener {
void onSelect(String path);
}
public void selectFile(OnSelectListener listener) {
String sdCard = Environment.getExternalStorageDirectory().getAbsolutePath();
listOfFile(new File(sdCard));
dialogFileList(listener);
}
private void listOfFile(File dir) {
File[] list = dir.listFiles();
for (File file : list) {
if (file.isDirectory()) {
if (!new File(file, ".nomedia").exists() && !file.getName().startsWith(".")) {
Log.w("LOG", "IS DIR " + file);
listOfFile(file);
}
} else {
String path = file.getAbsolutePath();
for (String ext : extensions) {
if (path.endsWith(ext)) {
SelectedFile selectedFile = new SelectedFile();
selectedFile.path = path;
String[] split = path.split("/");
selectedFile.name = split[split.length - 1];
itemsData.add(selectedFile);
Log.i("LOG", "ADD " + selectedFile.path + " " + selectedFile.name);
}
}
}
}
Log.d("LOG", itemsData.size() + " DONE");
}
private void dialogFileList(OnSelectListener listener) {
LinearLayout lytMain = new LinearLayout(context);
lytMain.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
lytMain.setOrientation(LinearLayout.VERTICAL);
int p = convertToPixels(12);
lytMain.setPadding(p, p, p, p);
lytMain.setGravity(Gravity.CENTER);
TextView textView = new TextView(context);
textView.setLayoutParams(new LinearLayout.LayoutParams(screenWidth(), ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setGravity(Gravity.CENTER);
textView.setText("~JDM File Selecor~");
RecyclerView recyclerView = new RecyclerView(context);
lytMain.addView(textView);
lytMain.addView(recyclerView);
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
// dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.setContentView(lytMain);
dialog.setCancelable(true);
dialog.show();
AdapterFile adapter = new AdapterFile(dialog, listener, itemsData);
recyclerView.setAdapter(adapter);
LinearLayoutManager LayoutManager = new LinearLayoutManager(context);
LayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(LayoutManager);
}
private int convertToPixels(int dp) {
float scale = context.getResources().getDisplayMetrics().density;
return (int) (dp * scale + 0.5f);
}
private int screenWidth() {
return context.getResources().getDisplayMetrics().widthPixels;
}
private class SelectedFile {
public String path = "";
public String name = "";
}
private class AdapterFile extends RecyclerView.Adapter<AdapterFile.ViewHolder> {
private ArrayList<SelectedFile> itemsData;
private OnSelectListener listener;
private Dialog dialog;
public AdapterFile(Dialog dialog, OnSelectListener listener, ArrayList<SelectedFile> itemsData) {
this.itemsData = itemsData;
this.listener = listener;
this.dialog = dialog;
}
// Create new views (invoked by the layout manager)
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.VERTICAL);
TextView txtName = new TextView(context);
TextView txtPath = new TextView(context);
txtPath.setTypeface(txtPath.getTypeface(), Typeface.ITALIC);
txtPath.setTextSize(TypedValue.COMPLEX_UNIT_SP, 11);
linearLayout.addView(txtName);
linearLayout.addView(txtPath);
ViewHolder viewHolder = new ViewHolder(linearLayout);
return viewHolder;
}
// inner class to hold a reference to each item of RecyclerView
public class ViewHolder extends RecyclerView.ViewHolder {
public LinearLayout linearLayout;
public TextView txtName;
public TextView txtPath;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
linearLayout = (LinearLayout) itemLayoutView;
txtName = (TextView) linearLayout.getChildAt(0);
txtPath = (TextView) linearLayout.getChildAt(1);
}
}
@Override
public int getItemCount() {
return itemsData.size();
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
final SelectedFile selectedFile = itemsData.get(position);
viewHolder.txtName.setText(selectedFile.name);
viewHolder.txtPath.setText(selectedFile.path);
viewHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
listener.onSelect(selectedFile.path);
}
});
}
}
}