-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSystemUtils.java
More file actions
356 lines (308 loc) · 14.5 KB
/
SystemUtils.java
File metadata and controls
356 lines (308 loc) · 14.5 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package com.realmo.utils;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.KeyguardManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Build;
import android.os.Parcelable;
import android.text.TextUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
import java.security.MessageDigest;
import java.util.List;
import java.util.Locale;
import java.util.Random;
@SuppressLint("SimpleDateFormat")
public final class SystemUtils {
public static void sendSMS(Context cxt, String smsBody) {
Uri smsToUri = Uri.parse("smsto:");
Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
intent.putExtra("sms_body", smsBody);
cxt.startActivity(intent);
}
public static void forwardToDial(Activity activity, String phoneNumber) {
if (activity != null && !TextUtils.isEmpty(phoneNumber)) {
activity.startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber)));
}
}
public static void sendMail(Context mContext, String mailID) {
Uri uri = Uri.parse("mailto:" + mailID);
mContext.startActivity(new Intent(Intent.ACTION_SENDTO, uri));
}
public static void openWeb(Context context, String url) {
Uri uri = Uri.parse(url);
context.startActivity(new Intent(Intent.ACTION_VIEW, uri));
}
public static void openContacts(Activity context, int requestCode) {
Uri uri = Uri.parse("content://contacts/people");
context.startActivityForResult(new Intent(Intent.ACTION_PICK, uri), requestCode);
}
public static void openSettings(Activity context, String action) {
Intent intent = new Intent();
ComponentName comp = new ComponentName("com.android.settings", action);
intent.setComponent(comp);
intent.setAction("android.intent.action.VIEW");
context.startActivityForResult(intent, 0);
}
public static void hideKeyBoard(Activity aty) {
((InputMethodManager) aty.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(aty.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
/* public static void openInputKeyBoard(Context mContext, EditText mEditText){
InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
public static void closeInputKeyBoard(Context mContext, EditText mEditText){
InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mEditText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}*/
public static boolean isBackground(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
for (RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.processName.equals(context.getPackageName())) {
return appProcess.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND;
}
}
return false;
}
public static boolean isSleeping(Context context) {
KeyguardManager kgMgr = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
return kgMgr.inKeyguardRestrictedInputMode();
}
public static void installApk(Context context, File apkfile) {
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.setType("application/vnd.android.package-archive");
intent.setData(Uri.fromFile(apkfile));
intent.setDataAndType(Uri.fromFile(apkfile), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
private static final String suSearchPaths[] = {"/system/bin/", "/system/xbin/", "/system/sbin/", "/sbin/", "/vendor/bin/"};
public static boolean isRooted() {
File file;
boolean flag1 = false;
for (String suSearchPath : suSearchPaths) {
file = new File(suSearchPath + "su");
if (file.isFile() && file.exists()) {
flag1 = true;
break;
}
}
return flag1;
}
public static boolean isRunningOnEmulator() {
return Build.BRAND.contains("generic")
|| Build.DEVICE.contains("generic")
|| Build.PRODUCT.contains("sdk")
|| Build.HARDWARE.contains("goldfish")
|| Build.MANUFACTURER.contains("Genymotion")
|| Build.PRODUCT.contains("vbox86p")
|| Build.DEVICE.contains("vbox86p")
|| Build.HARDWARE.contains("vbox86");
}
public static void goHome(Context context) {
Intent mHomeIntent = new Intent(Intent.ACTION_MAIN);
mHomeIntent.addCategory(Intent.CATEGORY_HOME);
mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
context.startActivity(mHomeIntent);
}
public static String hexdigest(byte[] paramArrayOfByte) {
final char[] hexDigits = {48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102};
try {
MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
localMessageDigest.update(paramArrayOfByte);
byte[] arrayOfByte = localMessageDigest.digest();
char[] arrayOfChar = new char[32];
for (int i = 0, j = 0; ; i++, j++) {
if (i >= 16) {
return new String(arrayOfChar);
}
int k = arrayOfByte[i];
arrayOfChar[j] = hexDigits[(0xF & k >>> 4)];
arrayOfChar[++j] = hexDigits[(k & 0xF)];
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public static int getDeviceUsableMemory(Context cxt) {
ActivityManager am = (ActivityManager) cxt.getSystemService(Context.ACTIVITY_SERVICE);
MemoryInfo mi = new MemoryInfo();
am.getMemoryInfo(mi);
return (int) (mi.availMem / (1024 * 1024));
}
public static int gc(Context cxt) {
//long i = getDeviceUsableMemory(cxt);
int count = 0;
ActivityManager am = (ActivityManager) cxt.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningServiceInfo> serviceList = am.getRunningServices(100);
if (serviceList != null)
for (RunningServiceInfo service : serviceList) {
if (service.pid == android.os.Process.myPid())
continue;
try {
android.os.Process.killProcess(service.pid);
count++;
} catch (Exception e) {
e.getStackTrace();
//continue;
}
}
List<RunningAppProcessInfo> processList = am.getRunningAppProcesses();
if (processList != null)
for (RunningAppProcessInfo process : processList) {
if (process.importance > RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
String[] pkgList = process.pkgList;
for (String pkgName : pkgList) {
try {
am.killBackgroundProcesses(pkgName);
count++;
} catch (Exception e) {
e.getStackTrace();
//continue;
}
}
}
}
return count;
}
public static String getProcessName(Context appContext) {
String currentProcessName = null;
int pid = android.os.Process.myPid();
ActivityManager manager = (ActivityManager) appContext.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
if (processInfo.pid == pid) {
currentProcessName = processInfo.processName;
break;
}
}
return currentProcessName;
}
public static void createDeskShortCut(Context cxt, String shortCutName, int icon, Class<?> cls) {
Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcutIntent.putExtra("duplicate", false);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortCutName);
Parcelable ico = Intent.ShortcutIconResource.fromContext(cxt.getApplicationContext(), icon);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, ico);
Intent intent = new Intent(cxt, cls);
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
cxt.sendBroadcast(shortcutIntent);
}
public static void createShortcut(Context ctx, String shortCutName, int iconId, Intent presentIntent) {
Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcutIntent.putExtra("duplicate", false);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortCutName);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(ctx, iconId));
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, presentIntent);
ctx.sendBroadcast(shortcutIntent);
}
public static void shareText(Context ctx, String title, String text) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
//intent.putExtra(Intent.EXTRA_SUBJECT, title);
intent.putExtra(Intent.EXTRA_TEXT, text);
ctx.startActivity(Intent.createChooser(intent, title));
/* List<ResolveInfo> ris = getShareTargets(ctx);
if (ris != null && ris.size() > 0) {
ctx.startActivity(Intent.createChooser(intent, title));
}*/
}
public static void shareFile(Context ctx, String title, String filePath) {
FileUtils.shareFile(ctx, title, filePath);
}
public static List<ResolveInfo> getShareTargets(Context ctx) {
Intent intent = new Intent(Intent.ACTION_SEND, null);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setType("text/plain");
PackageManager pm = ctx.getPackageManager();
return pm.queryIntentActivities(intent, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
}
public static String getCurrentLanguage() {
return Locale.getDefault().getLanguage();
}
public static String getLanguage(Context ctx) {
if (ctx != null) {
return ctx.getResources().getConfiguration().locale.getLanguage();
}
return null;
}
//<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
public static boolean isGpsEnabled(Context context) {
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
return lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
public static void showSoftInputMethod(Context context, EditText editText) {
if (context != null && editText != null) {
editText.requestFocus();
InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(editText, 0);
}
}
public static void closeSoftInputMethod(Context context, EditText editText) {
if (context != null && editText != null) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
}
}
public static void showSoftInput(Context context) {
InputMethodManager inputMethodManager = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
public static void closeSoftInput(Context context) {
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null && ((Activity) context).getCurrentFocus() != null) {
inputMethodManager.hideSoftInputFromWindow(((Activity) context).getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
public static void toWeChatScan(Context context) {
try {
Uri uri = Uri.parse("weixin://dl/scan");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(intent);
} catch (Exception e) {
Toast.makeText(context, R.string.wechaterr, Toast.LENGTH_SHORT).show();
}
}
public static void toAliPayScan(Context context) {
try {
Uri uri = Uri.parse("alipayqr://platformapi/startapp?saId=10000007");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(intent);
} catch (Exception e) {
Toast.makeText(context, R.string.alipayerr, Toast.LENGTH_SHORT).show();
}
}
public static void toAliPayPayCode(Context context) {
try {
Uri uri = Uri.parse("alipayqr://platformapi/startapp?saId=20000056");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(intent);
} catch (Exception e) {
Toast.makeText(context, R.string.alipayerr, Toast.LENGTH_SHORT).show();
}
}
public static int getRandomNumber(int min, int max) {
return new Random().nextInt(max) % (max - min + 1) + min;
}
}