auto import from //branches/cupcake_rel/...@140373

This commit is contained in:
The Android Open Source Project
2009-03-18 17:39:48 -07:00
parent 72ed6feab2
commit e6dd1fa185
32 changed files with 1135 additions and 604 deletions

View File

@@ -59,6 +59,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -138,8 +139,9 @@ public class ManageApplications extends ListActivity implements
private static final int REORDER_LIST = HANDLER_MESSAGE_BASE+4;
private static final int ADD_PKG_START = HANDLER_MESSAGE_BASE+5;
private static final int ADD_PKG_DONE = HANDLER_MESSAGE_BASE+6;
private static final int REFRESH_ICONS = HANDLER_MESSAGE_BASE+7;
private static final int NEXT_LOAD_STEP = HANDLER_MESSAGE_BASE+8;
private static final int REFRESH_APP_RESOURCE = HANDLER_MESSAGE_BASE+7;
private static final int REFRESH_DONE = HANDLER_MESSAGE_BASE+8;
private static final int NEXT_LOAD_STEP = HANDLER_MESSAGE_BASE+9;
// observer object used for computing pkg sizes
private PkgSizeObserver mObserver;
@@ -248,37 +250,10 @@ public class ManageApplications extends ListActivity implements
switch (msg.what) {
case INIT_PKG_INFO:
if(localLOGV) Log.i(TAG, "Message INIT_PKG_INFO");
List<ApplicationInfo> newList = getInstalledApps(FILTER_APPS_ALL);
if (!mJustCreated) {
// Add or delete newly created packages by comparing lists
List<ApplicationInfo> newList = getInstalledApps(FILTER_APPS_ALL);
int oldCount = mAppPropCache.size();
boolean idxArr[] = new boolean[oldCount];
for ( int i = 0; i < oldCount; i++) {
idxArr[i] = false;
}
if (newList != null) {
for (ApplicationInfo app : newList) {
AppInfo aInfo = mAppPropCache.get(app.packageName);
if ( aInfo == null) {
// New package. post an ADD_PKG message
if(localLOGV) Log.i(TAG, "Adding pkg: "+app.packageName);
updatePackageList(Intent.ACTION_PACKAGE_ADDED, app.packageName);
} else {
idxArr[aInfo.index] = true;
}
}
Set<String> keyList = mAppPropCache.keySet();
for (String key : keyList) {
AppInfo aInfo = mAppPropCache.get(key);
int idx = aInfo.index;
if (!idxArr[idx]) {
String pkg = aInfo.pkgName;
if(localLOGV) Log.i(TAG, "Deleting pkg: " + pkg);
updatePackageList(Intent.ACTION_PACKAGE_REMOVED, pkg);
}
}
}
updateAppList(newList);
}
// Retrieve the package list and init some structures
initAppList(mFilterApps);
@@ -308,46 +283,11 @@ public class ManageApplications extends ListActivity implements
mObserver.invokeGetSizeInfo(mAppInfoAdapter.getApplicationInfo(
mComputeIndex),
COMPUTE_PKG_SIZE_DONE);
} else {
// check for added/removed packages
Set<String> keys = mAddRemoveMap.keySet();
Iterator<String> iter = keys.iterator();
List<String> removeList = new ArrayList<String>();
boolean added = false;
boolean removed = false;
while (iter.hasNext()) {
String key = iter.next();
if (mAddRemoveMap.get(key) == Boolean.TRUE) {
// add
try {
info = mPm.getApplicationInfo(key, 0);
mAppInfoAdapter.addApplicationInfo(info);
added = true;
} catch (NameNotFoundException e) {
Log.w(TAG, "Invalid added package:"+key+" Ignoring entry");
}
} else {
// remove
removeList.add(key);
removed = true;
}
}
// remove uninstalled packages from list
if (removed) {
mAppInfoAdapter.removeFromList(removeList);
}
// handle newly installed packages
if (added) {
mObserver.invokeGetSizeInfo(mAppInfoAdapter.getApplicationInfo(
mComputeIndex),
COMPUTE_PKG_SIZE_DONE);
} else {
// end computation here
mComputeSizes = true;
mFirst = true;
mAppInfoAdapter.sortList(mSortOrder);
mHandler.sendEmptyMessage(NEXT_LOAD_STEP);
}
} else {
// End computation here
mComputeSizes = true;
mFirst = true;
mHandler.sendEmptyMessage(NEXT_LOAD_STEP);
}
break;
case REMOVE_PKG:
@@ -381,8 +321,7 @@ public class ManageApplications extends ListActivity implements
} else if(menuOption != mFilterApps) {
// Option to filter list
mFilterApps = menuOption;
boolean ret = mAppInfoAdapter.resetAppList(mFilterApps,
getInstalledApps(mFilterApps));
boolean ret = mAppInfoAdapter.resetAppList(mFilterApps);
if(!ret) {
// Reset cache
mAppPropCache = null;
@@ -398,7 +337,7 @@ public class ManageApplications extends ListActivity implements
Log.w(TAG, "Ignoring message:ADD_PKG_START for null pkgName");
break;
}
if (!mComputeSizes) {
if (!mComputeSizes || !mLoadLabels) {
Boolean currB = mAddRemoveMap.get(pkgName);
if (currB == null || (currB.equals(Boolean.FALSE))) {
mAddRemoveMap.put(pkgName, Boolean.TRUE);
@@ -406,11 +345,11 @@ public class ManageApplications extends ListActivity implements
break;
}
try {
info = mPm.getApplicationInfo(pkgName, 0);
} catch (NameNotFoundException e) {
Log.w(TAG, "Couldnt find application info for:"+pkgName);
break;
}
info = mPm.getApplicationInfo(pkgName, 0);
} catch (NameNotFoundException e) {
Log.w(TAG, "Couldnt find application info for:"+pkgName);
break;
}
mObserver.invokeGetSizeInfo(info, ADD_PKG_DONE);
break;
case ADD_PKG_DONE:
@@ -422,19 +361,33 @@ public class ManageApplications extends ListActivity implements
ps = data.getParcelable(ATTR_APP_PKG_STATS);
mAppInfoAdapter.addToList(pkgName, ps);
break;
case REFRESH_ICONS:
Map<String, AppInfo> iconMap = (Map<String, AppInfo>) msg.obj;
if(iconMap == null) {
case REFRESH_APP_RESOURCE:
AppInfo aInfo = (AppInfo) msg.obj;
if(aInfo == null) {
Log.w(TAG, "Error loading icons for applications");
} else {
mAppInfoAdapter.updateAppsResourceInfo(iconMap);
mAppInfoAdapter.updateAppsResourceInfo(aInfo);
}
break;
case REFRESH_DONE:
mLoadLabels = true;
mHandler.sendEmptyMessage(NEXT_LOAD_STEP);
break;
case NEXT_LOAD_STEP:
if (mComputeSizes && mLoadLabels) {
doneLoadingData();
// Check for added/removed packages
Set<String> keys = mAddRemoveMap.keySet();
for (String key : keys) {
if (mAddRemoveMap.get(key) == Boolean.TRUE) {
// Add the package
updatePackageList(Intent.ACTION_PACKAGE_ADDED, key);
} else {
// Remove the package
updatePackageList(Intent.ACTION_PACKAGE_REMOVED, key);
}
}
mAddRemoveMap.clear();
} else if (!mComputeSizes && !mLoadLabels) {
// Either load the package labels or initiate get size info
if (mSizesFirst) {
@@ -446,12 +399,13 @@ public class ManageApplications extends ListActivity implements
// Create list view from the adapter here. Wait till the sort order
// of list is defined. its either by label or by size. so atleast one of the
// first steps should be complete before filling the list
mAppInfoAdapter.sortList(mSortOrder);
if (mJustCreated) {
// Set the adapter here.
mJustCreated = false;
mListView.setAdapter(mAppInfoAdapter);
dismissLoadingMsg();
}
}
if (!mComputeSizes) {
initComputeSizes();
} else if (!mLoadLabels) {
@@ -465,7 +419,49 @@ public class ManageApplications extends ListActivity implements
}
};
/*
* This method compares the current cache against a new list of
* installed applications and tries to update the list with add or remove
* messages.
*/
private boolean updateAppList(List<ApplicationInfo> newList) {
if ((newList == null) || (mAppPropCache == null)) {
return false;
}
Set<String> existingList = new HashSet<String>();
boolean ret = false;
// Loop over new list and find out common elements between old and new lists
for (ApplicationInfo info : newList) {
String pkgName = info.packageName;
AppInfo aInfo = mAppPropCache.get(pkgName);
if (aInfo != null) {
existingList.add(pkgName);
} else {
// New package. update info by refreshing
if (localLOGV) Log.i(TAG, "New pkg :"+pkgName+" installed when paused");
updatePackageList(Intent.ACTION_PACKAGE_ADDED, pkgName);
ret = true;
}
}
// Loop over old list and figure out state entries
List<String> deletedList = null;
Set<String> staleList = mAppPropCache.keySet();
for (String pkgName : staleList) {
if (!existingList.contains(pkgName)) {
if (localLOGV) Log.i(TAG, "Pkg :"+pkgName+" deleted when paused");
if (deletedList == null) {
deletedList = new ArrayList<String>();
deletedList.add(pkgName);
}
ret = true;
}
}
// Delete right away
if (deletedList != null) {
mAppInfoAdapter.removeFromList(deletedList);
}
return ret;
}
private void doneLoadingData() {
setProgressBarIndeterminateVisibility(false);
@@ -524,16 +520,68 @@ public class ManageApplications extends ListActivity implements
}
}
/*
* Utility method used to figure out list of apps based on filterOption
* If the framework supports an additional flag to indicate running apps
* we can get away with some code here.
*/
List<ApplicationInfo> getFilteredApps(List<ApplicationInfo> pAppList, int filterOption) {
List<ApplicationInfo> retList = new ArrayList<ApplicationInfo>();
if(pAppList == null) {
return retList;
}
if (filterOption == FILTER_APPS_THIRD_PARTY) {
List<ApplicationInfo> appList =new ArrayList<ApplicationInfo> ();
for (ApplicationInfo appInfo : pAppList) {
boolean flag = false;
if ((appInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
// Updated system app
flag = true;
} else if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
// Non-system app
flag = true;
}
if (flag) {
appList.add(appInfo);
}
}
return appList;
} else if (filterOption == FILTER_APPS_RUNNING) {
List<ApplicationInfo> appList =new ArrayList<ApplicationInfo> ();
List<ActivityManager.RunningAppProcessInfo> procList = getRunningAppProcessesList();
if ((procList == null) || (procList.size() == 0)) {
return appList;
}
// Retrieve running processes from ActivityManager
HashMap<String, ActivityManager.RunningAppProcessInfo> runningMap =
new HashMap<String, ActivityManager.RunningAppProcessInfo>();
for (ActivityManager.RunningAppProcessInfo appProcInfo : procList) {
if ((appProcInfo != null) && (appProcInfo.pkgList != null)){
int size = appProcInfo.pkgList.length;
for (int i = 0; i < size; i++) {
runningMap.put(appProcInfo.pkgList[i], appProcInfo);
}
}
}
// Query list to find running processes in current list
for (ApplicationInfo appInfo : pAppList) {
if (runningMap.get(appInfo.packageName) != null) {
appList.add(appInfo);
}
}
return appList;
} else {
return pAppList;
}
}
private List<ActivityManager.RunningAppProcessInfo> getRunningAppProcessesList() {
ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
return am.getRunningAppProcesses();
}
// some initialization code used when kicking off the size computation
private void initAppList(int filterOption) {
// Initialize lists
List<ApplicationInfo> appList = getInstalledApps(filterOption);
initAppList(appList, filterOption);
initAppList(null, filterOption);
}
// some initialization code used when kicking off the size computation
@@ -544,7 +592,7 @@ public class ManageApplications extends ListActivity implements
mLoadLabels = false;
// Initialize lists
mAddRemoveMap = new TreeMap<String, Boolean>();
mAppInfoAdapter.resetAppList(filterOption, appList);
mAppInfoAdapter.initMapFromList(appList, filterOption);
}
// Utility method to start a thread to read application labels and icons
@@ -552,6 +600,7 @@ public class ManageApplications extends ListActivity implements
//load resources now
if(mResourceThread.isAlive()) {
mResourceThread.interrupt();
mResourceThread = new ResourceLoaderThread();
}
mResourceThread.loadAllResources(mAppInfoAdapter.getAppList());
}
@@ -605,13 +654,13 @@ public class ManageApplications extends ListActivity implements
for (ApplicationInfo appInfo : mAppList) {
CharSequence appName = appInfo.loadLabel(mPm);
Drawable appIcon = appInfo.loadIcon(mPm);
iconMap.put(appInfo.packageName,
new AppInfo(appInfo.packageName, appName, appIcon));
Message msg = mHandler.obtainMessage(REFRESH_APP_RESOURCE);
msg.obj = new AppInfo(appInfo.packageName, appName, appIcon);
mHandler.sendMessage(msg);
}
}
Message msg = mHandler.obtainMessage(REFRESH_ICONS);
msg.obj = iconMap;
mHandler.sendMessage(msg);
Message doneMsg = mHandler.obtainMessage(REFRESH_DONE);
mHandler.sendMessage(doneMsg);
}
}
@@ -700,6 +749,7 @@ public class ManageApplications extends ListActivity implements
*/
class AppInfoAdapter extends BaseAdapter {
private Map<String, AppInfo> mAppPropMap;
private List<ApplicationInfo> mAppList;
private List<ApplicationInfo> mAppLocalList;
ApplicationInfo.DisplayNameComparator mAlphaComparator;
AppInfoComparator mSizeComparator;
@@ -710,34 +760,51 @@ public class ManageApplications extends ListActivity implements
}
return mAppPropCache.get(packageName);
}
public AppInfoAdapter(Context c, List<ApplicationInfo> appList) {
mAppLocalList = appList;
boolean useCache = false;
int sortOrder = SORT_ORDER_ALPHA;
int imax = mAppLocalList.size();
if(mAppPropCache != null) {
useCache = true;
// Activity has been resumed. can use the cache to populate values initially
mAppPropMap = mAppPropCache;
sortOrder = mSortOrder;
// Make sure the cache or map contains entries for all elements
// in appList for a valid sort.
public void initMapFromList(List<ApplicationInfo> appList, int filterOption) {
if (appList == null) {
// Just refresh the list
appList = mAppList;
} else {
mAppList = appList;
}
mAppLocalList = getFilteredApps(appList, filterOption);
int sortOrder = SORT_ORDER_ALPHA;
if (mAppPropCache != null) {
// Retain previous sort order
sortOrder = mSortOrder;
mAppPropMap = mAppPropCache;
} else {
// Recreate property map
mAppPropMap = new TreeMap<String, AppInfo>();
}
// Re init the comparators
mAlphaComparator = null;
mSizeComparator = null;
sortAppList(sortOrder);
// Recreate property map
mAppPropMap = new TreeMap<String, AppInfo>();
int imax = appList.size();
for (int i = 0; i < imax; i++) {
ApplicationInfo info = mAppLocalList.get(i);
AppInfo aInfo = getFromCache(info.packageName);
ApplicationInfo info = appList.get(i);
AppInfo aInfo = mAppPropMap.get(info.packageName);
if(aInfo == null){
aInfo = new AppInfo(info.packageName, i,
info.packageName, mDefaultAppIcon, null);
info.packageName, mDefaultAppIcon, null);
if (localLOGV) Log.i(TAG, "Creating entry pkg:"+info.packageName+" to map");
} else {
aInfo.index = i;
if (localLOGV) Log.i(TAG, "Adding pkg:"+info.packageName+" to map");
}
mAppPropMap.put(info.packageName, aInfo);
}
}
public AppInfoAdapter(Context c, List<ApplicationInfo> appList) {
initMapFromList(appList, mFilterApps);
}
public int getCount() {
return mAppLocalList.size();
}
@@ -772,14 +839,6 @@ public class ManageApplications extends ListActivity implements
}
return mAppLocalList.get(position);
}
public void addApplicationInfo(ApplicationInfo info) {
if(info == null) {
Log.w(TAG, "Ignoring null add in List Adapter");
return;
}
mAppLocalList.add(info);
}
public long getItemId(int position) {
int imax = mAppLocalList.size();
@@ -843,9 +902,8 @@ public class ManageApplications extends ListActivity implements
private void adjustIndex() {
int imax = mAppLocalList.size();
ApplicationInfo info;
for (int i = 0; i < imax; i++) {
info = mAppLocalList.get(i);
ApplicationInfo info = mAppLocalList.get(i);
mAppPropMap.get(info.packageName).index = i;
}
}
@@ -855,21 +913,28 @@ public class ManageApplications extends ListActivity implements
}
public void sortList(int sortOrder) {
if (localLOGV) Log.i(TAG, "sortOrder = "+sortOrder);
sortAppList(sortOrder);
adjustIndex();
notifyDataSetChanged();
}
public boolean resetAppList(int filterOption, List<ApplicationInfo> appList) {
// Create application list based on the filter value
mAppLocalList = appList;
/*
* Reset the application list associated with this adapter.
* @param filterOption Sort the list based on this value
* @param appList the actual application list that is used to reset
* @return Return a boolean value to indicate inconsistency
*/
public boolean resetAppList(int filterOption) {
// Change application list based on filter option
mAppLocalList = getFilteredApps(mAppList, filterOption);
// Check for all properties in map before sorting. Populate values from cache
for(ApplicationInfo applicationInfo : mAppLocalList) {
AppInfo appInfo = mAppPropMap.get(applicationInfo.packageName);
if(appInfo == null) {
AppInfo rInfo = getFromCache(applicationInfo.packageName);
if(rInfo == null) {
// Need to load resources again. Inconsistency somewhere
// Need to load resources again. Inconsistency somewhere
return false;
}
mAppPropMap.put(applicationInfo.packageName, rInfo);
@@ -912,7 +977,7 @@ public class ManageApplications extends ListActivity implements
return true;
}
boolean changed = false;
for (ApplicationInfo info : mAppLocalList) {
for (ApplicationInfo info : mAppList) {
AppInfo pInfo = iconMap.get(info.packageName);
if(pInfo != null) {
AppInfo aInfo = mAppPropMap.get(info.packageName);
@@ -930,27 +995,39 @@ public class ManageApplications extends ListActivity implements
return true;
}
public boolean updateAppsResourceInfo(AppInfo pInfo) {
if(pInfo == null) {
Log.w(TAG, "Null info when refreshing icon in List Adapter");
return false;
}
AppInfo aInfo = mAppPropMap.get(pInfo.pkgName);
if (aInfo != null) {
aInfo.refreshIcon(pInfo);
notifyDataSetChanged();
return true;
}
return false;
}
private boolean shouldBeInList(int filterOption, ApplicationInfo info) {
// Match filter here
boolean addToCurrList = false;
if (filterOption == FILTER_APPS_RUNNING) {
List<ApplicationInfo> runningList = getInstalledApps(FILTER_APPS_RUNNING);
for (ApplicationInfo running : runningList) {
if (running.packageName.equalsIgnoreCase(info.packageName)) {
addToCurrList = true;
break;
return true;
}
}
} else if (filterOption == FILTER_APPS_THIRD_PARTY) {
if ((info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
addToCurrList = true;
return true;
} else if ((info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
addToCurrList = true;
return true;
}
} else {
return true;
}
return addToCurrList;
return false;
}
/*
@@ -961,20 +1038,15 @@ public class ManageApplications extends ListActivity implements
* @param ps PackageStats of new package
*/
public void addToList(String pkgName, PackageStats ps) {
if(pkgName == null) {
Log.w(TAG, "Adding null pkg to List Adapter");
if((pkgName == null) || (ps == null)) {
if (pkgName == null) {
Log.w(TAG, "Adding null pkg to List Adapter");
} else {
Log.w(TAG, "Adding pkg : "+pkgName+" with invalid PackageStats");
}
return;
}
boolean notInList = true;
int newIdx = getIndex(pkgName);
if (newIdx != -1) {
notInList = false;
if (mAppPropMap.get(pkgName) != null) {
// weird. just ignore entry
Log.i(TAG, "Package:"+pkgName+" already added");
return;
}
}
// Get ApplicationInfo
ApplicationInfo info = null;
try {
@@ -988,14 +1060,17 @@ public class ManageApplications extends ListActivity implements
Log.i(TAG, "Null ApplicationInfo for package:"+pkgName);
return;
}
// Add entry to map
mAppPropMap.put(pkgName, new AppInfo(pkgName, newIdx,
// Add entry to local list
mAppList.add(info);
// Add entry to map. Note that the index gets adjusted later on based on
// whether the newly added package is part of displayed list
mAppPropMap.put(pkgName, new AppInfo(pkgName, -1,
info.loadLabel(mPm), info.loadIcon(mPm), ps));
// Add to list
if (notInList && (shouldBeInList(mFilterApps, info))) {
// Binary search returns a negative index (ie -index) of the position where
// this might be inserted.
newIdx = Collections.binarySearch(mAppLocalList, info,
int newIdx = Collections.binarySearch(mAppLocalList, info,
getAppComparator(mSortOrder));
if(newIdx >= 0) {
Log.i(TAG, "Strange. Package:"+pkgName+" is not new");
@@ -1010,11 +1085,30 @@ public class ManageApplications extends ListActivity implements
}
}
private void removePkgListBase(List<String> pkgNames) {
for (String pkg : pkgNames) {
removePkgBase(pkg);
}
}
private void removePkgBase(String pkgName) {
int imax = mAppList.size();
for (int i = 0; i < imax; i++) {
ApplicationInfo app = mAppList.get(i);
if (app.packageName.equalsIgnoreCase(pkgName)) {
if (localLOGV) Log.i(TAG, "Removing pkg: "+pkgName+" from base list");
mAppList.remove(i);
return;
}
}
}
public void removeFromList(List<String> pkgNames) {
if(pkgNames == null) {
Log.w(TAG, "Removing null pkg list from List Adapter");
return;
}
removePkgListBase(pkgNames);
int imax = mAppLocalList.size();
boolean found = false;
ApplicationInfo info;
@@ -1042,7 +1136,7 @@ public class ManageApplications extends ListActivity implements
}
// Sort idxArr
Arrays.sort(idxArr);
// remove the packages based on decending indices
// remove the packages based on descending indices
for (k = kmax-1; k >= 0; k--) {
// Check if package has been found in the list of existing apps first
if(idxArr[k] == -1) {
@@ -1051,13 +1145,13 @@ public class ManageApplications extends ListActivity implements
info = mAppLocalList.get(idxArr[k]);
mAppLocalList.remove(idxArr[k]);
mAppPropMap.remove(info.packageName);
if (localLOGV) Log.i(TAG, "Removed pkg:"+info.packageName+ " list");
if (localLOGV) Log.i(TAG, "Removed pkg:"+info.packageName+ " from display list");
}
if (found) {
adjustIndex();
notifyDataSetChanged();
}
}
}
public void updateAppSize(String pkgName, PackageStats ps) {
if(pkgName == null) {
@@ -1213,6 +1307,7 @@ public class ManageApplications extends ListActivity implements
requestWindowFeature(Window.FEATURE_PROGRESS);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.compute_sizes);
showLoadingMsg();
mDefaultAppIcon =Resources.getSystem().getDrawable(
com.android.internal.R.drawable.sym_def_app_icon);
mInvalidSizeStr = getText(R.string.invalid_size_value);
@@ -1231,7 +1326,6 @@ public class ManageApplications extends ListActivity implements
lv.setItemsCanFocus(true);
lv.setOnItemClickListener(this);
mListView = lv;
showLoadingMsg();
}
@Override
@@ -1263,9 +1357,9 @@ public class ManageApplications extends ListActivity implements
super.onStart();
// Create a thread to load resources
mResourceThread = new ResourceLoaderThread();
sendMessageToHandler(INIT_PKG_INFO);
// register receiver
mReceiver.registerReceiver();
sendMessageToHandler(INIT_PKG_INFO);
}
@Override