-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathUtils.java
More file actions
246 lines (214 loc) · 9.81 KB
/
Utils.java
File metadata and controls
246 lines (214 loc) · 9.81 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package com.ravenwallet.tools.util;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.hardware.fingerprint.FingerprintManager;
import android.os.Build;
import android.provider.Settings;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;
import com.ravenwallet.presenter.activities.intro.IntroActivity;
import com.ravenwallet.tools.manager.BRSharedPrefs;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import static android.content.Context.FINGERPRINT_SERVICE;
/**
* RavenWallet
* <p/>
* Created by Mihail Gutan <mihail@breadwallet.com> on 6/21/16.
* Copyright (c) 2016 breadwallet LLC
* <p/>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p/>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p/>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
public class Utils {
public static final String TAG = Utils.class.getName();
public static boolean isUsingCustomInputMethod(Activity context) {
if (context == null) return false;
InputMethodManager imm = (InputMethodManager) context.getSystemService(
Context.INPUT_METHOD_SERVICE);
if (imm == null) {
return false;
}
List<InputMethodInfo> mInputMethodProperties = imm.getEnabledInputMethodList();
final int N = mInputMethodProperties.size();
for (int i = 0; i < N; i++) {
InputMethodInfo imi = mInputMethodProperties.get(i);
if (imi.getId().equals(
Settings.Secure.getString(context.getContentResolver(),
Settings.Secure.DEFAULT_INPUT_METHOD))) {
if ((imi.getServiceInfo().applicationInfo.flags &
ApplicationInfo.FLAG_SYSTEM) == 0) {
return true;
}
}
}
return false;
}
@SuppressWarnings("deprecation")
public static void printPhoneSpecs() {
String specsTag = "PHONE SPECS";
Log.e(specsTag, "");
Log.e(specsTag, "***************************PHONE SPECS***************************");
Log.e(specsTag, "* screen X: " + IntroActivity.screenParametersPoint.x + " , screen Y: " + IntroActivity.screenParametersPoint.y);
Log.e(specsTag, "* Build.CPU_ABI: " + Build.CPU_ABI);
Runtime rt = Runtime.getRuntime();
long maxMemory = rt.maxMemory();
Log.e(specsTag, "* maxMemory:" + Long.toString(maxMemory));
Log.e(specsTag, "----------------------------PHONE SPECS----------------------------");
Log.e(specsTag, "");
}
public static boolean isEmulatorOrDebug(Context app) {
String fing = Build.FINGERPRINT;
boolean isEmulator = false;
if (fing != null) {
isEmulator = fing.contains("vbox") || fing.contains("generic");
}
return isEmulator || (0 != (app.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE));
}
public static String getFormattedDateFromLong(Context app, long time) {
SimpleDateFormat formatter = new SimpleDateFormat("M/d@ha", Locale.getDefault());
boolean is24HoursFormat = false;
if (app != null) {
is24HoursFormat = android.text.format.DateFormat.is24HourFormat(app.getApplicationContext());
if (is24HoursFormat) {
formatter = new SimpleDateFormat("M/d H", Locale.getDefault());
}
}
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time);
String result = formatter.format(calendar.getTime()).toLowerCase().replace("am", "a").replace("pm", "p");
if (is24HoursFormat) result += "h";
return result;
}
public static String formatTimeStamp(long time, String pattern) {
// SimpleDateFormat formatter = new SimpleDateFormat(pattern, Locale.getDefault());
// Calendar calendar = Calendar.getInstance();
// calendar.setTimeInMillis(time);
return android.text.format.DateFormat.format(pattern, time).toString();
}
public static boolean isNullOrEmpty(String str) {
return str == null || str.isEmpty();
}
public static boolean isNullOrEmpty(byte[] arr) {
return arr == null || arr.length == 0;
}
public static boolean isNullOrEmpty(Collection collection) {
return collection == null || collection.size() == 0;
}
public static int getPixelsFromDps(Context context, int dps) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dps * scale + 0.5f);
}
public static String bytesToHex(byte[] in) {
final StringBuilder builder = new StringBuilder();
for (byte b : in) {
builder.append(String.format("%02x", b));
}
return builder.toString();
}
public static byte[] hexToBytes(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i + 1), 16));
}
return data;
}
public static boolean isFingerprintEnrolled(Context app) {
FingerprintManager fingerprintManager = (FingerprintManager) app.getSystemService(FINGERPRINT_SERVICE);
// Device doesn't support fingerprint authentication
return ActivityCompat.checkSelfPermission(app, Manifest.permission.USE_FINGERPRINT) == PackageManager.PERMISSION_GRANTED && fingerprintManager.isHardwareDetected() && fingerprintManager.hasEnrolledFingerprints();
}
public static boolean isFingerprintAvailable(Context app) {
FingerprintManager fingerprintManager = (FingerprintManager) app.getSystemService(FINGERPRINT_SERVICE);
if (fingerprintManager == null) return false;
// Device doesn't support fingerprint authentication
if (ActivityCompat.checkSelfPermission(app, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(app, "Fingerprint authentication permission not enabled", Toast.LENGTH_LONG).show();
return false;
}
return fingerprintManager.isHardwareDetected();
}
public static void hideKeyboard(Context app) {
if (app != null) {
View view = ((Activity) app).getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) app.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null)
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
}
public static String getAgentString(Context app, String cfnetwork) {
int versionNumber = 0;
if (app != null) {
try {
PackageInfo pInfo = null;
pInfo = app.getPackageManager().getPackageInfo(app.getPackageName(), 0);
versionNumber = pInfo.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
String release = Build.VERSION.RELEASE;
// return String.format("%s/%d %s %s/%s", "Bread", versionNumber, cfnetwork, "Android", release);
return "Bread/" + String.valueOf(versionNumber) + " " + cfnetwork + " Android/" + release;
}
public static String reverseHex(String hex) {
if (hex == null) return null;
StringBuilder result = new StringBuilder();
for (int i = 0; i <= hex.length() - 2; i = i + 2) {
result.append(new StringBuilder(hex.substring(i, i + 2)).reverse());
}
return result.reverse().toString();
}
@Nullable
public static String retrieveAddressChunk(String scanResult) {
String[] splitResult = scanResult.split(":");
if (splitResult.length > 1) {
return splitResult[1];
}
return null;
}
public static long getCurrentUnixTimestamp() {
return System.currentTimeMillis() / 1000;
}
//Integer division will always effectively floor the results here
public static int getCurrentFastRestoreKey() {
return (int)((getCurrentUnixTimestamp() - BRConstants.GENESIS_TIMESTAMP) / BRConstants.FAST_SYNC_INTERVAL_SECONDS);
}
public static long getSeedTimeFromFastRestoreKey(int fastRestoreKey) {
return (((long)fastRestoreKey) * BRConstants.FAST_SYNC_INTERVAL_SECONDS) + BRConstants.GENESIS_TIMESTAMP;
}
public static String getIpfsUrlFromHash(Context app, String hash) {
//TODO: Hash Validation?
return String.format(BRConstants.IPFS_URL_FORMAT, BRSharedPrefs.getPreferredIPFSGateway(app), hash);
}
}