-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathFsUtils.java
206 lines (179 loc) · 6.65 KB
/
FsUtils.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package me.sheimi.android.utils;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import androidx.core.content.FileProvider;
import android.webkit.MimeTypeMap;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import me.sheimi.android.activities.SheimiFragmentActivity;
import me.sheimi.sgit.R;
/**
* Created by sheimi on 8/8/13.
*/
public class FsUtils {
public static final SimpleDateFormat TIMESTAMP_FORMATTER = new SimpleDateFormat(
"yyyyMMdd_HHmmss", Locale.getDefault());
public static final String PROVIDER_AUTHORITY = "com.manichord.mgit.fileprovider";
public static final String TEMP_DIR = "temp";
private FsUtils() {
}
public static File createTempFile(String subfix) throws IOException {
File dir = getExternalDir(TEMP_DIR);
String fileName = TIMESTAMP_FORMATTER.format(new Date());
File file = File.createTempFile(fileName, subfix, dir);
file.deleteOnExit();
return file;
}
/**
* Get a File representing a dir within the external shared location where files can be stored specific to this app
* creating the dir if it doesn't already exist
*
* @param dirname
* @return
*/
public static File getExternalDir(String dirname) {
return getExternalDir(dirname, true);
}
/**
*
* @param dirname
* @return
*/
public static File getInternalDir(String dirname) { return getExternalDir(dirname, true, false); }
/**
* Get a File representing a dir within the external shared location where files can be stored specific to this app
*
* @param dirname
* @param isCreate create the dir if it does not already exist
* @return
*/
public static File getExternalDir(String dirname, boolean isCreate) { return getExternalDir(dirname, isCreate, true); }
/**
*
* Get a File representing a dir within the location where files can be stored specific to this app
*
* @param dirname name of the dir to return
* @param isCreate create the dir if it does not already exist
* @param isExternal if true, will use external *shared* storage
* @return
*/
public static File getExternalDir(String dirname, boolean isCreate, boolean isExternal) {
File mDir = new File(getAppDir(isExternal), dirname);
if (!mDir.exists() && isCreate) {
mDir.mkdir();
}
return mDir;
}
/**
* Get a File representing the location where files can be stored specific to this app
*
* @param isExternal if true, will use external *shared* storage
* @return
*/
public static File getAppDir(boolean isExternal) {
SheimiFragmentActivity activeActivity = BasicFunctions.getActiveActivity();
if (isExternal) {
return activeActivity.getExternalFilesDir(null);
} else {
return activeActivity.getFilesDir();
}
}
public static String getMimeType(String url) {
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(url
.toLowerCase(Locale.getDefault()));
if (extension != null && !overrideMimeToPlainText(extension)) {
MimeTypeMap mime = MimeTypeMap.getSingleton();
type = mime.getMimeTypeFromExtension(extension);
}
if (type == null) {
type = "text/plain";
}
return type;
}
public static String getMimeType(File file) {
return getMimeType(Uri.fromFile(file).toString());
}
public static void openFile(SheimiFragmentActivity activity, File file) {
Uri uri = FileProvider.getUriForFile(activity, PROVIDER_AUTHORITY, file);
String mimeType = FsUtils.getMimeType(uri.toString());
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setDataAndType(uri, mimeType);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
try {
activity.startActivity(intent);
activity.forwardTransition();
} catch (ActivityNotFoundException e) {
// Looks like many editor apps only register for VIEW not EDIT intents :-(
intent.setAction(Intent.ACTION_VIEW);
try {
activity.startActivity(intent);
activity.forwardTransition();
}
catch (ActivityNotFoundException e1) {
activity.showMessageDialog(R.string.dialog_error_title, activity.getString(R.string.error_can_not_open_file));
}
}
}
public static void deleteFile(File file) {
File to = new File(file.getAbsolutePath() + System.currentTimeMillis());
file.renameTo(to);
deleteFileInner(to);
}
/**
* For some file types, need to override their mimetype to be text/plain
* @param fileExtension
* @return
*/
private static boolean overrideMimeToPlainText(String fileExtension) {
// for now only Typescript needs this override as Mime DB uses ".ts" for mpg2 files
return "ts".equalsIgnoreCase(fileExtension);
}
private static void deleteFileInner(File file) {
if (!file.isDirectory()) {
file.delete();
return;
}
try {
FileUtils.deleteDirectory(file);
} catch (IOException e) {
//TODO
e.printStackTrace();
}
}
public static void copyFile(File from, File to) {
try {
FileUtils.copyFile(from, to);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void copyDirectory(File from, File to) {
if (!from.exists())
return;
try {
FileUtils.copyDirectory(from, to);
} catch (IOException e) {
e.printStackTrace();
}
}
public static boolean renameDirectory(File dir, String name) {
String newDirPath = dir.getParent() + File.separator + name;
File newDirFile = new File(newDirPath);
return dir.renameTo(newDirFile);
}
public static String getRelativePath(File file, File base) {
return base.toURI().relativize(file.toURI()).getPath();
}
public static File joinPath(File dir, String relative_path) {
return new File(dir.getAbsolutePath() + File.separator + relative_path);
}
public static boolean isTextMimeType(String mime) {
return mime.startsWith("text") || mime.equals("application/javascript") || mime.equals("application/json");
}
}