Repository navigation
Expand file tree
/
Copy pathUtils.java
More file actions
113 lines (91 loc) · 3.09 KB
/
Copy pathUtils.java
File metadata and controls
113 lines (91 loc) · 3.09 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
package com.stacksync.android;
import java.io.File;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.util.Pair;
public class Utils {
private static String TAG = "Utils";
public static boolean isNetworkConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni == null) {
// There are no active networks.
return false;
} else
return true;
}
public static String getApplicationName(Context context) {
String name = context.getResources().getString(R.string.app_name);
return name;
}
public static String getReadableFileSize(long size) {
if (size <= 0)
return "0 Bytes";
final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " "
+ units[digitGroups];
}
public static File getCacheDirectory(Context context) {
File cacheDir;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
cacheDir = context.getExternalFilesDir(null);
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
Log.e(TAG, "Media is read-only. Using internal storage.");
// FIXME: files saved in this directory cannot be accessed by other
// apps
cacheDir = context.getFilesDir();
} else {
// Something else is wrong. It may be one of many other states, but
// all we need
// to know is we can neither read nor write
Log.e(TAG, "Media is not writeable nor read-only. Using internal storage.");
cacheDir = context.getFilesDir();
}
return cacheDir;
}
public static String buildUrl(String url, String path, List<Pair<String, String>> params) {
Uri uri = Uri.parse(url);
Uri.Builder uriBuilder = uri.buildUpon();
uriBuilder.path(uri.getPath() + path);
for (Pair<String, String> param : params) {
uriBuilder.appendQueryParameter(param.first, param.second);
}
return uriBuilder.build().toString();
}
public static Date convertStringToDate(String sDate) {
Date date;
try {
date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.ENGLISH).parse(sDate);
} catch (ParseException e) {
date = new Date(Long.MIN_VALUE);
}
return date;
}
public static boolean validateLoginFields(String authUrl, String username, String password) {
if (authUrl == null || username == null || password == null) {
return false;
} else if (!authUrl.startsWith("http://") && !authUrl.startsWith("https://")) {
return false;
} else if (username == "") {
return false;
} else if (password == "") {
return false;
}
return true;
}
}