Skip to content

Instantly share code, notes, and snippets.

@zeuxisoo
Created April 16, 2011 15:45
Show Gist options
  • Save zeuxisoo/923212 to your computer and use it in GitHub Desktop.
Save zeuxisoo/923212 to your computer and use it in GitHub Desktop.
AppInfo some codes
package com.zeuxislo.appsinfo;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Date;
import java.text.SimpleDateFormat;
import android.app.Activity;
import android.content.pm.IPackageStatsObserver;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.PackageStats;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
public class AppDetail extends Activity {
private long cacheSize;
private long codeSize;
private long dataSize;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_detail);
PackageManager packageManager = this.getPackageManager();
Bundle bundle = new Bundle();
bundle = this.getIntent().getExtras();
try {
PackageInfo packageInfo = packageManager.getPackageInfo(bundle.getString("SELECTED_APP_PACKAGE"), PackageManager.GET_PERMISSIONS);
File appFile = new File(packageInfo.applicationInfo.sourceDir);
ImageView icon = (ImageView) this.findViewById(R.id.app_detail_icon);
TextView appName = (TextView) this.findViewById(R.id.app_detail_app_name);
TextView appVersion = (TextView) this.findViewById(R.id.app_detail_app_version);
TextView appVersionCode = (TextView) this.findViewById(R.id.app_detail_app_version_code);
TextView appCreateDate = (TextView) this.findViewById(R.id.app_detail_app_create_date);
TextView appPackageString = (TextView) this.findViewById(R.id.app_detail_app_package_string);
TextView appFilePath = (TextView) this.findViewById(R.id.app_detail_file_path);
icon.setImageDrawable(packageInfo.applicationInfo.loadIcon(packageManager));
appName.setText(packageInfo.applicationInfo.loadLabel(packageManager).toString());
appVersion.setText(packageInfo.versionName);
appVersionCode.setText(String.valueOf(packageInfo.versionCode));
appCreateDate.setText( new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date(appFile.lastModified())).toString());
appPackageString.setText(packageInfo.packageName);
appFilePath.setText(packageInfo.applicationInfo.sourceDir);
// Fill file stats (code, cache and data size)
getPackageStats(packageManager, packageInfo);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
/*
ArrayList<Map<String, String>> permissionMapList = new ArrayList<Map<String, String>>();
for(String premission : p.requestedPermissions) {
Map<String, String> permissionMap = new HashMap<String, String>();
try {
PermissionInfo permissionInfo = this.getPackageManager().getPermissionInfo(premission, PackageManager.GET_META_DATA);
CharSequence label = permissionInfo.loadLabel(this.getPackageManager());
CharSequence description = permissionInfo.loadDescription(this.getPackageManager());
permissionMap.put("name", (label == null) ? permissionInfo.name : label.toString());
permissionMap.put("description", (description == null) ? "" : description.toString());
permissionMap.put("security_level", String.valueOf(permissionInfo.protectionLevel));
permissionMapList.add(permissionMap);
}catch(NameNotFoundException e) {
}
}
newInfo.setPackageName(p.packageName);
newInfo.setVersionCode(p.versionCode);
newInfo.setIcon(p.applicationInfo.loadIcon(this.getPackageManager()));
newInfo.setFileSize(this.convertFileSize(file.length()));
newInfo.setFileLastModifiedDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(file.lastModified())));
newInfo.setPermissionMapList(permissionMapList);
*/
}
private void getPackageStats(PackageManager packageManager, PackageInfo packageInfo) {
Method getPackageSizeInfo;
try {
getPackageSizeInfo = packageManager.getClass().getMethod("getPackageSizeInfo", String.class, IPackageStatsObserver.class);
Log.d("AppDetail", "Start Package Size Invoker");
try {
getPackageSizeInfo.invoke(packageManager, packageInfo.packageName, new IPackageStatsObserver.Stub() {
public void onGetStatsCompleted(PackageStats packageStats, boolean succeeded) throws RemoteException {
cacheSize = packageStats.cacheSize;
codeSize = packageStats.codeSize;
dataSize = packageStats.dataSize;
setFileSizeDetail(cacheSize, codeSize, dataSize);
Log.d("AppDetail", "Cache Size:" + cacheSize + ", Code Size: " + codeSize + ", Data Size: " + dataSize);
}
});
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
protected void setFileSizeDetail(long cacheSize, long codeSize, long dataSize) {
Log.d("AppDetail", "Cache Size 2:" + cacheSize + ", Code Size: " + codeSize + ", Data Size: " + dataSize);
((TextView) this.findViewById(R.id.app_detail_total_size)).setText(this.convertFileSize(codeSize + dataSize + cacheSize));
((TextView) this.findViewById(R.id.app_detail_file_size)).setText(this.convertFileSize(codeSize));
((TextView) this.findViewById(R.id.app_detail_data_size)).setText(this.convertFileSize(dataSize));
((TextView) this.findViewById(R.id.app_detail_cache_size)).setText(this.convertFileSize(cacheSize));
}
private String convertFileSize(long size) {
String unit = null;
float f = (float)size;
if (f >= 1024.0F) {
unit = "KB";
f = (float)(f / 1024.0D);
if (f >= 1024.0F) {
unit = "MB";
f = (float)(f / 1024.0D);
if (f >= 1024.0F) {
unit = "GB";
f = (float)(f / 1024.0D);
}
}
}
return unit == null ? "0" : f + " " + unit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment