Fix some bugs.

Bug #2376231: Apps lose window focus (and back key causes ANR) if the
lock screen is dismissed while the phone is in landscape mode

This is another case where we weren't recomputing the focused window
after changing the visibility policy.

bug #2479958: Investigate source of "Resources don't contain package
for resource number 0x7f0a0000"

Um, okay, so it turns out there were bugs all over the place where
we would load an XML resource from a another application, but not
use the Resources for that application to retrieve its resources...!
I think the only reason any of this stuff was working at all was
because it typically only cared about retrieving the resource
identifiers of the items (it would look up the values later).

Bug #2401082: Passion ERE26 monkey crash - InputMethodManagerService

Add some null checks.
diff --git a/services/java/com/android/server/AppWidgetService.java b/services/java/com/android/server/AppWidgetService.java
index e5a5e03..5de68f9 100644
--- a/services/java/com/android/server/AppWidgetService.java
+++ b/services/java/com/android/server/AppWidgetService.java
@@ -29,6 +29,7 @@
 import android.content.pm.PackageManager;
 import android.content.pm.PackageInfo;
 import android.content.pm.ResolveInfo;
+import android.content.res.Resources;
 import android.content.res.TypedArray;
 import android.content.res.XmlResourceParser;
 import android.net.Uri;
@@ -709,7 +710,10 @@
             info.provider = component;
             p.uid = activityInfo.applicationInfo.uid;
 
-            TypedArray sa = mContext.getResources().obtainAttributes(attrs,
+            Resources res = mPackageManager.getResourcesForApplication(
+                    activityInfo.applicationInfo);
+            
+            TypedArray sa = res.obtainAttributes(attrs,
                     com.android.internal.R.styleable.AppWidgetProviderInfo);
             
             // These dimensions has to be resolved in the application's context.
diff --git a/services/java/com/android/server/InputMethodManagerService.java b/services/java/com/android/server/InputMethodManagerService.java
index 60813f1..e336a35 100644
--- a/services/java/com/android/server/InputMethodManagerService.java
+++ b/services/java/com/android/server/InputMethodManagerService.java
@@ -1526,13 +1526,15 @@
         mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
                 new AlertDialog.OnClickListener() {
                     public void onClick(DialogInterface dialog, int which) {
-                        if (mIms == null) {
-                            return;
-                        }
                         synchronized (mMethodMap) {
+                            if (mIms == null || mIms.length <= which) {
+                                return;
+                            }
                             InputMethodInfo im = mIms[which];
                             hideInputMethodMenu();
-                            setInputMethodLocked(im.getId());
+                            if (im != null) {
+                                setInputMethodLocked(im.getId());
+                            }
                         }
                     }
                 });
diff --git a/services/java/com/android/server/WindowManagerService.java b/services/java/com/android/server/WindowManagerService.java
index 8781263..f5d3e8e 100644
--- a/services/java/com/android/server/WindowManagerService.java
+++ b/services/java/com/android/server/WindowManagerService.java
@@ -9516,6 +9516,9 @@
 
                         if (mPolicy.doesForceHide(w, attrs)) {
                             if (!wasAnimating && animating) {
+                                if (DEBUG_VISIBILITY) Slog.v(TAG,
+                                        "Animation done that could impact force hide: "
+                                        + w);
                                 wallpaperForceHidingChanged = true;
                                 mFocusMayChange = true;
                             } else if (w.isReadyForDisplay() && w.mAnimation == null) {
@@ -9525,19 +9528,31 @@
                             boolean changed;
                             if (forceHiding) {
                                 changed = w.hideLw(false, false);
+                                if (DEBUG_VISIBILITY && changed) Slog.v(TAG,
+                                        "Now policy hidden: " + w);
                             } else {
                                 changed = w.showLw(false, false);
-                                if (changed && wallpaperForceHidingChanged
-                                        && w.isReadyForDisplay()) {
-                                    // Assume we will need to animate.  If
-                                    // we don't (because the wallpaper will
-                                    // stay with the lock screen), then we will
-                                    // clean up later.
-                                    Animation a = mPolicy.createForceHideEnterAnimation();
-                                    if (a != null) {
-                                        w.setAnimation(a);
+                                if (DEBUG_VISIBILITY && changed) Slog.v(TAG,
+                                        "Now policy shown: " + w);
+                                if (changed) {
+                                    if (wallpaperForceHidingChanged
+                                            && w.isReadyForDisplay()) {
+                                        // Assume we will need to animate.  If
+                                        // we don't (because the wallpaper will
+                                        // stay with the lock screen), then we will
+                                        // clean up later.
+                                        Animation a = mPolicy.createForceHideEnterAnimation();
+                                        if (a != null) {
+                                            w.setAnimation(a);
+                                        }
                                     }
-                                    mFocusMayChange = true;
+                                    if (mCurrentFocus == null ||
+                                            mCurrentFocus.mLayer < w.mLayer) {
+                                        // We are showing on to of the current
+                                        // focus, so re-evaluate focus to make
+                                        // sure it is correct.
+                                        mFocusMayChange = true;
+                                    }
                                 }
                             }
                             if (changed && (attrs.flags
diff --git a/services/java/com/android/server/am/ActivityManagerService.java b/services/java/com/android/server/am/ActivityManagerService.java
index 2c82d9c..a263b23 100644
--- a/services/java/com/android/server/am/ActivityManagerService.java
+++ b/services/java/com/android/server/am/ActivityManagerService.java
@@ -1863,6 +1863,8 @@
                 + " pid=" + (app != null ? app.pid : -1));
         if (app != null && app.pid > 0) {
             if (!knownToBeDead || app.thread == null) {
+                // We already have the app running, or are waiting for it to
+                // come up (we have a pid but not yet its thread), so keep it.
                 return app;
             } else {
                 // An application record is attached to a previous process,