/*
* 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.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.util.List;
import java.util.regex.Pattern;
import static com.code19.library.SystemUtils.hexdigest;
/**
* Create by h4de5ing 2016/5/18 018
*/
public class AppUtils {
public static String getAppName(Context context, String packageName) {
PackageManager pm = context.getPackageManager();
String appName = null;
try {
ApplicationInfo applicationInfo = pm.getApplicationInfo(packageName, 0);
appName = String.valueOf(pm.getApplicationLabel(applicationInfo));
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return appName;
}
public static Drawable getAppIcon(Context context, String packageName) {
PackageManager pm = context.getPackageManager();
Drawable appIcon = null;
try {
ApplicationInfo applicationInfo = pm.getApplicationInfo(packageName, 0);
appIcon = applicationInfo.loadIcon(pm);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return appIcon;
}
public static long getAppFirstInstallTime(Context context, String packageName) {
long lastUpdateTime = 0;
try {
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
lastUpdateTime = packageInfo.firstInstallTime;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return lastUpdateTime;
}
public static long getAppLastUpdateTime(Context context, String packageName) {
long lastUpdateTime = 0;
try {
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
lastUpdateTime = packageInfo.lastUpdateTime;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return lastUpdateTime;
}
public static long getAppSize(Context context, String packageName) {
long appSize = 0;
try {
ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(packageName, 0);
appSize = new File(applicationInfo.sourceDir).length();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return appSize;
}
public static String getAppApk(Context context, String packageName) {
String sourceDir = null;
try {
ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(packageName, 0);
sourceDir = applicationInfo.sourceDir;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return sourceDir;
}
public static String getAppVersionName(Context context, String packageName) {
String appVersion = null;
try {
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
appVersion = packageInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return appVersion;
}
public static int getAppVersionCode(Context context, String packageName) {
int appVersionCode = 0;
try {
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
appVersionCode = packageInfo.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return appVersionCode;
}
public static String getAppInstaller(Context context, String packageName) {
return context.getPackageManager().getInstallerPackageName(packageName);
}
public static String getAppSign(Context context, String packageName) {
try {
PackageInfo pis = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
return hexdigest(pis.signatures[0].toByteArray());
} catch (PackageManager.NameNotFoundException e) {
throw new RuntimeException(AppUtils.class.getName() + "the " + packageName + "'s application not found");
}
}
public static int getAppTargetSdkVersion(Context context, String packageName) {
try {
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
ApplicationInfo applicationInfo = packageInfo.applicationInfo;
return applicationInfo.targetSdkVersion;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return 0;
}
}
public static int getAppUid(Context context, String packageName) {
try {
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
ApplicationInfo applicationInfo = packageInfo.applicationInfo;
return applicationInfo.uid;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return 0;
}
}
public static int getNumCores() {
try {
File dir = new File("/sys/devices/system/cpu/");
File[] files = dir.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return Pattern.matches("cpu[0-9]", pathname.getName());
}
});
return files.length;
} catch (Exception e) {
e.printStackTrace();
return 1;
}
}
public static boolean getRootPermission(Context context) {
String packageCodePath = context.getPackageCodePath();
Process process = null;
DataOutputStream os = null;
try {
String cmd = "chmod 777 " + packageCodePath;
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(cmd + "\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (Exception e) {
return false;
} finally {
try {
if (os != null) {
os.close();
}
process.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
return true;
}
public static String[] getAppPermissions(Context context, String packname) {
String[] requestedPermissions = null;
try {
PackageInfo info = context.getPackageManager().getPackageInfo(packname, PackageManager.GET_PERMISSIONS);
requestedPermissions = info.requestedPermissions;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return requestedPermissions;
}
public static boolean hasPermission(Context context, String permission) {
if (context != null && !TextUtils.isEmpty(permission)) {
try {
PackageManager packageManager = context.getPackageManager();
if (packageManager != null) {
if (PackageManager.PERMISSION_GRANTED == packageManager.checkPermission(permission, context
.getPackageName())) {
return true;
}
Log.d("AppUtils", "Have you declared permission " + permission + " in AndroidManifest.xml ?");
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
return false;
}
public static boolean isInstalled(Context context, String packageName) {
boolean installed = false;
if (TextUtils.isEmpty(packageName)) {
return false;
}
List<ApplicationInfo> installedApplications = context.getPackageManager().getInstalledApplications(0);
for (ApplicationInfo in : installedApplications) {
if (packageName.equals(in.packageName)) {
installed = true;
break;
} else {
installed = false;
}
}
return installed;
}
public static boolean installApk(Context context, String filePath) {
File file = new File(filePath);
if (!file.exists() || !file.isFile() || file.length() <= 0) {
return false;
}
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("file://" + filePath), "application/vnd.android.package-archive");
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
return true;
}
public static boolean uninstallApk(Context context, String packageName) {
if (TextUtils.isEmpty(packageName)) {
return false;
}
Intent i = new Intent(Intent.ACTION_DELETE, Uri.parse("package:" + packageName));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
return true;
}
public static boolean isSystemApp(Context context, String packageName) {
boolean isSys = false;
PackageManager pm = context.getPackageManager();
try {
ApplicationInfo applicationInfo = pm.getApplicationInfo(packageName, 0);
if (applicationInfo != null && (applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0) {
isSys = true;
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
isSys = false;
}
return isSys;
}
public static boolean isServiceRunning(Context context, String className) {
boolean isRunning = false;
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningServiceInfo> servicesList = activityManager.getRunningServices(Integer.MAX_VALUE);
for (RunningServiceInfo si : servicesList) {
if (className.equals(si.service.getClassName())) {
isRunning = true;
}
}
return isRunning;
}
public static boolean stopRunningService(Context context, String className) {
Intent intent_service = null;
boolean ret = false;
try {
intent_service = new Intent(context, Class.forName(className));
} catch (Exception e) {
e.printStackTrace();
}
if (intent_service != null) {
ret = context.stopService(intent_service);
}
return ret;
}
public static void killProcesses(Context context, int pid, String processName) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
String packageName;
try {
if (!processName.contains(":")) {
packageName = processName;
} else {
packageName = processName.split(":")[0];
}
activityManager.killBackgroundProcesses(packageName);
Method forceStopPackage = activityManager.getClass().getDeclaredMethod("forceStopPackage", String.class);
forceStopPackage.setAccessible(true);
forceStopPackage.invoke(activityManager, packageName);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String runScript(String script) {
String sRet;
try {
final Process m_process = Runtime.getRuntime().exec(script);
final StringBuilder sbread = new StringBuilder();
Thread tout = new Thread(new Runnable() {
public void run() {
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(m_process.getInputStream()),
8192);
String ls_1;
try {
while ((ls_1 = bufferedReader.readLine()) != null) {
sbread.append(ls_1).append("\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
tout.start();
final StringBuilder sberr = new StringBuilder();
Thread terr = new Thread(new Runnable() {
public void run() {
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(m_process.getErrorStream()),
8192);
String ls_1;
try {
while ((ls_1 = bufferedReader.readLine()) != null) {
sberr.append(ls_1).append("\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
terr.start();
m_process.waitFor();
while (tout.isAlive()) {
Thread.sleep(50);
}
if (terr.isAlive())
terr.interrupt();
String stdout = sbread.toString();
String stderr = sberr.toString();
sRet = stdout + stderr;
} catch (Exception e) {
e.printStackTrace();
return null;
}
return sRet;
}
public static void runApp(Context context, String packagename) {
context.startActivity(new Intent(context.getPackageManager().getLaunchIntentForPackage(packagename)));
}
public static void cleanCache(Context context) {
FileUtils.deleteFileByDirectory(context.getCacheDir());
}
public static void cleanDatabases(Context context) {
String filepath = String.format(String.format(context.getFilesDir().getParent() + File.separator + "%s", "databases"));
FileUtils.deleteFileByDirectory(new File(filepath));
}
public static void cleanSharedPreference(Context context) {
String filepath = String.format(String.format(context.getFilesDir().getParent() + File.separator + "%s", "shared_prefs"));
FileUtils.deleteFileByDirectory(new File(filepath));
}
}