Skip to content

Instantly share code, notes, and snippets.

@iamswapnilsonar
Last active July 30, 2019 10:32
Show Gist options
  • Save iamswapnilsonar/1f8f54bd96fe942d1050 to your computer and use it in GitHub Desktop.
Save iamswapnilsonar/1f8f54bd96fe942d1050 to your computer and use it in GitHub Desktop.
package in.google.android.sample;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.util.Log;
/**
* @author Swapnil Sonar
* @Date 01/07/2015
*/
public class Utils {
private static final String TAG = "Utils";
/**
* It return all installed applications and along with its details.
* @param context
* @param getSysPackages
* @return
*/
public static List<PackageInfoBean> getInstalledApps(Context context, boolean getSysPackages) {
List<PackageInfoBean> mPackageInfos = new ArrayList<PackageInfoBean>();
long timeStamp = System.currentTimeMillis();
List<PackageInfo> packs = context.getPackageManager().getInstalledPackages(0);
for (int i = 0; i < packs.size(); i++) {
PackageInfo packageInfo = packs.get(i);
ApplicationInfo appInfo = packageInfo.applicationInfo;
if (((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 1) && !getSysPackages) {
//NOP
}else{
PackageInfoBean newInfo = new PackageInfoBean();
newInfo.mAppName = appInfo.loadLabel(context.getPackageManager());
newInfo.mAppPackageName = packageInfo.packageName;
newInfo.mDataDir = appInfo.dataDir;
// .apk file of application
newInfo.mApkFilePath = appInfo.publicSourceDir;
// apk file size
File file = new File(newInfo.mApkFilePath);
newInfo.mApkFileSize = file.length();
newInfo.mVersionName = packageInfo.versionName;
newInfo.mVersionCode = packageInfo.versionCode;
newInfo.mApplicationInfo = packageInfo.applicationInfo;
newInfo.icon = appInfo.loadIcon(context.getPackageManager());
mPackageInfos.add(newInfo);
}
}
long timeInterval = System.currentTimeMillis() - timeStamp;
Log.e(TAG, "getInstalledApps used: " + timeInterval);
return mPackageInfos;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment