Fix NPE in InstalledAppDetails activity when app not found.

Added some extra checks to ensure that the activity exits from
onCreate, onResume and processMoveMsg promptly when the app
info cannot be refreshed.

Bug: b/2711730
Change-Id: Ied22fadce09326dd33cf201e5e9281990bf3abbd
This commit is contained in:
Jeff Brown
2010-05-27 14:29:34 -07:00
parent f3373c9949
commit b0bcf9f7f5

View File

@@ -290,14 +290,15 @@ public class InstalledAppDetails extends Activity implements View.OnClickListene
} }
} }
private void initAppInfo(String packageName) { private boolean initAppInfo(String packageName) {
try { try {
mAppInfo = mPm.getApplicationInfo(packageName, mAppInfo = mPm.getApplicationInfo(packageName,
PackageManager.GET_UNINSTALLED_PACKAGES); PackageManager.GET_UNINSTALLED_PACKAGES);
return true;
} catch (NameNotFoundException e) { } catch (NameNotFoundException e) {
Log.e(TAG, "Exception when retrieving package: " + packageName, e); Log.e(TAG, "Exception when retrieving package: " + packageName, e);
showDialogInner(DLG_APP_NOT_FOUND); showDialogInner(DLG_APP_NOT_FOUND);
return; return false;
} }
} }
@@ -305,20 +306,26 @@ public class InstalledAppDetails extends Activity implements View.OnClickListene
@Override @Override
protected void onCreate(Bundle icicle) { protected void onCreate(Bundle icicle) {
super.onCreate(icicle); super.onCreate(icicle);
// Get package manager // Get package manager
mPm = getPackageManager(); mPm = getPackageManager();
// Get application's name from intent // Get application's name from intent
Intent intent = getIntent(); Intent intent = getIntent();
final String packageName = intent.getStringExtra(ManageApplications.APP_PKG_NAME); final String packageName = intent.getStringExtra(ManageApplications.APP_PKG_NAME);
mComputingStr = getText(R.string.computing_size); if (! initAppInfo(packageName)) {
return; // could not find package, finish called
}
// Try retrieving package stats again // Try retrieving package stats again
CharSequence totalSizeStr, appSizeStr, dataSizeStr; CharSequence totalSizeStr, appSizeStr, dataSizeStr;
mComputingStr = getText(R.string.computing_size);
totalSizeStr = appSizeStr = dataSizeStr = mComputingStr; totalSizeStr = appSizeStr = dataSizeStr = mComputingStr;
if(localLOGV) Log.i(TAG, "Have to compute package sizes"); if(localLOGV) Log.i(TAG, "Have to compute package sizes");
mSizeObserver = new PkgSizeObserver(); mSizeObserver = new PkgSizeObserver();
initAppInfo(packageName);
setContentView(R.layout.installed_app_details); setContentView(R.layout.installed_app_details);
//TODO download str and download url //TODO download str and download url
// Set default values on sizes // Set default values on sizes
mTotalSize = (TextView)findViewById(R.id.total_size_text); mTotalSize = (TextView)findViewById(R.id.total_size_text);
mTotalSize.setText(totalSizeStr); mTotalSize.setText(totalSizeStr);
@@ -326,16 +333,19 @@ public class InstalledAppDetails extends Activity implements View.OnClickListene
mAppSize.setText(appSizeStr); mAppSize.setText(appSizeStr);
mDataSize = (TextView)findViewById(R.id.data_size_text); mDataSize = (TextView)findViewById(R.id.data_size_text);
mDataSize.setText(dataSizeStr); mDataSize.setText(dataSizeStr);
// Get Control button panel // Get Control button panel
View btnPanel = findViewById(R.id.control_buttons_panel); View btnPanel = findViewById(R.id.control_buttons_panel);
mForceStopButton = (Button) btnPanel.findViewById(R.id.left_button); mForceStopButton = (Button) btnPanel.findViewById(R.id.left_button);
mForceStopButton.setText(R.string.force_stop); mForceStopButton.setText(R.string.force_stop);
mUninstallButton = (Button)btnPanel.findViewById(R.id.right_button); mUninstallButton = (Button)btnPanel.findViewById(R.id.right_button);
mForceStopButton.setEnabled(false); mForceStopButton.setEnabled(false);
// Initialize clear data and move install location buttons // Initialize clear data and move install location buttons
View data_buttons_panel = findViewById(R.id.data_buttons_panel); View data_buttons_panel = findViewById(R.id.data_buttons_panel);
mClearDataButton = (Button) data_buttons_panel.findViewById(R.id.left_button); mClearDataButton = (Button) data_buttons_panel.findViewById(R.id.left_button);
mMoveAppButton = (Button) data_buttons_panel.findViewById(R.id.right_button); mMoveAppButton = (Button) data_buttons_panel.findViewById(R.id.right_button);
// Cache section // Cache section
mCacheSize = (TextView) findViewById(R.id.cache_size_text); mCacheSize = (TextView) findViewById(R.id.cache_size_text);
mCacheSize.setText(mComputingStr); mCacheSize.setText(mComputingStr);
@@ -344,6 +354,7 @@ public class InstalledAppDetails extends Activity implements View.OnClickListene
// Get list of preferred activities // Get list of preferred activities
mActivitiesButton = (Button)findViewById(R.id.clear_activities_button); mActivitiesButton = (Button)findViewById(R.id.clear_activities_button);
List<ComponentName> prefActList = new ArrayList<ComponentName>(); List<ComponentName> prefActList = new ArrayList<ComponentName>();
// Intent list cannot be null. so pass empty list // Intent list cannot be null. so pass empty list
List<IntentFilter> intentList = new ArrayList<IntentFilter>(); List<IntentFilter> intentList = new ArrayList<IntentFilter>();
mPm.getPreferredActivities(intentList, prefActList, packageName); mPm.getPreferredActivities(intentList, prefActList, packageName);
@@ -395,7 +406,15 @@ public class InstalledAppDetails extends Activity implements View.OnClickListene
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
initAppInfo(mAppInfo.packageName);
if (mAppInfo == null) {
setIntentAndFinish(true, true);
return; // onCreate must have failed, make sure to exit
}
if (! initAppInfo(mAppInfo.packageName)) {
return; // could not find package, finish called
}
PackageInfo pkgInfo = null; PackageInfo pkgInfo = null;
// Get application info again to refresh changed properties of application // Get application info again to refresh changed properties of application
try { try {
@@ -404,11 +423,13 @@ public class InstalledAppDetails extends Activity implements View.OnClickListene
} catch (NameNotFoundException e) { } catch (NameNotFoundException e) {
Log.e(TAG, "Exception when retrieving package:" + mAppInfo.packageName, e); Log.e(TAG, "Exception when retrieving package:" + mAppInfo.packageName, e);
showDialogInner(DLG_APP_NOT_FOUND); showDialogInner(DLG_APP_NOT_FOUND);
return; return; // could not find package, finish called
} }
checkForceStop(); checkForceStop();
setAppLabelAndIcon(pkgInfo); setAppLabelAndIcon(pkgInfo);
refreshButtons(); refreshButtons();
// Refresh size info // Refresh size info
if (mAppInfo != null && mAppInfo.packageName != null) { if (mAppInfo != null && mAppInfo.packageName != null) {
mPm.getPackageSizeInfo(mAppInfo.packageName, mSizeObserver); mPm.getPackageSizeInfo(mAppInfo.packageName, mSizeObserver);
@@ -500,8 +521,6 @@ public class InstalledAppDetails extends Activity implements View.OnClickListene
private void refreshButtons() { private void refreshButtons() {
if (!mMoveInProgress) { if (!mMoveInProgress) {
// Refresh application information again.
initAppInfo(mAppInfo.packageName);
initUninstallButtons(); initUninstallButtons();
initDataButtons(); initDataButtons();
initMoveButton(); initMoveButton();
@@ -517,15 +536,20 @@ public class InstalledAppDetails extends Activity implements View.OnClickListene
String packageName = mAppInfo.packageName; String packageName = mAppInfo.packageName;
// Refresh the button attributes. // Refresh the button attributes.
mMoveInProgress = false; mMoveInProgress = false;
refreshButtons();
if(result == PackageManager.MOVE_SUCCEEDED) { if(result == PackageManager.MOVE_SUCCEEDED) {
Log.i(TAG, "Moved resources for " + packageName); Log.i(TAG, "Moved resources for " + packageName);
// Refresh size information again. // Refresh size information again.
mPm.getPackageSizeInfo(mAppInfo.packageName, mSizeObserver); mPm.getPackageSizeInfo(packageName, mSizeObserver);
} else { } else {
mMoveErrorCode = result; mMoveErrorCode = result;
showDialogInner(DLG_MOVE_FAILED); showDialogInner(DLG_MOVE_FAILED);
} }
if (! initAppInfo(packageName)) {
return; // could not find package, finish called
}
refreshButtons();
} }
/* /*