/*
* Copyright (C) 2016 android@19code.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.code19.library;
import android.Manifest;
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.support.v4.app.ActivityCompat;
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 callPhone(Activity activity, String phoneNumber) {
if (activity != null && !TextUtils.isEmpty(phoneNumber)) {
if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
activity.startActivity(new Intent(Intent.ACTION_CALL, 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;
}
}