[ActivityManager] Avoid system server crash due to package removed

Symptom:
When application is not responding, an ANR dialog will shown.
In certain timing, user uninstall the application when the ANR
occurs, but before ANR dialog shown. The system server will crash
when looking up the errorReportReceiver because the package is removed.

Here shows how the exception occurs.
java.lang.IllegalArgumentException: Unknown package: app.package.name
 at com.android.server.pm.Settings.getInstallerPackageNameLPr
 at com.android.server.pm.PackageManagerService.getInstallerPackageName
 at android.app.ApplicationPackageManager.getInstallerPackageName
 at android.app.ApplicationErrorReport.getErrorReportReceiver
 at com.android.server.am.ActivityManagerService.startAppProblemLocked
 at com.android.server.am.ActivityManagerService.makeAppNotRespondingLocked
 at com.android.server.am.ActivityManagerService.appNotResponding

Change-Id: Iced4287bd44dc25b1db2c1e3a583892eb6c041a2
[ActivityManager] Ensure provider external access count will be released.

Sympton: Oom-adj of provider process will be always 0.

Root Cause: Exception happend in openContentUri
may not call removeContentProviderExternalUnchecked.

Flow:
application [MediaPlayer.setDataSource(Context context, Uri uri)]
    uri is invalid, IOException will be caught and below log will appear:
    "Couldn't open file on client side, trying server side"
    Continue to call setDataSource(String path, Map<String, String> headers)
 ->mediaserver [MediaPlayerService::Client::setDataSource]
    setDataSource(httpService, url, headers) calls openContentProviderFile
 ->system_server (ActivityManagerService) [openContentUri]
    Increase externalProcessNoHandleCount for media provider.
    Call [openFile] to media provider.
 ->android.process.media [check permission for openFile]
    enforceReadPermissionInner
 ->system_server [checkComponentPermission]
    Use mediaserver's uid to check READ_EXTERNAL_STORAGE
      -> not granted due to it is pure native process, not a known package.
    Throw security exception then the flow of openContentUri is broke.
    And externalProcessNoHandleCount is unable to decrease.

Application sample code:
 String invalidId = "54321";
 Uri externalUri = MediaStore.Files.getContentUri("external");
 Uri uri = Uri.withAppendedPath(externalUri, invalidId);
 mediaPlayer.setDataSource(mContext, uri);

Solution: Move removeContentProviderExternalUnchecked to finally block.

Change-Id: I75eec1f9631e9c6bb18449946d707d51ac21e8a7
Prevent showing immersive confirmation unexpectedly

By design, if the user presses the power key twice within 5 seconds
(panic), it will show immersive confirmation again at the next time
entering immersive mode.

But there is a bug on calculating the time between pressing the power
key. The time get from event.getDownTime() will stop when the device
is sleeping without being charged.

So here uses SystemClock.elapsedRealtime() instead.

https://code.google.com/p/android/issues/detail?id=80772

Change-Id: I4dca9dc260904f1c5b60464834781e2a82a9bcf4
Reset killedByAm flag at attachApplicationLocked

ActivityManager reuses a process record object that killed
by him under some situation. That reused process record inherits
a killedByAm flag unexpectedly.
The killedByAm flag must be reset otherwise ActivityManager can't
judge the process can be killed or not.

Change-Id: If95137d91939cc44882ad2813131bcde0edd0c1b
Fix pointer index out of range exception

Avoid pointer index out of range error if return value is -1 of
findPointerIndex() when there is no data available for that pointer
identifier.

Change-Id: I2a08f04c678164e220852094baa124ba157e2e42
Signed-off-by: tingna_sung <tingna_sung@htc.com>
[ActivityManager] Prevent provider from been created twice.

Root Cause:
If a process which contains provider is starting but not
from accessing provider, e.g. service/activity/receiver,
its providers' will be created during handleBindApplication.
And before the process complete publish provider to
ActivityManagerService, if there is an access call to
process provider before the process completes publishing
providers to ActivityManagerService, then the providers
will be created again.


Solution:
Don't schedule install provider if the provider record
already exist and will be published later.

Change-Id: Ic200ceae042c014c86e7edee0acd013b513af334
Merge "Prevent showing immersive confirmation unexpectedly"
Merge "Reset killedByAm flag at attachApplicationLocked"
Merge "[ActivityManager] Avoid system server crash due to package removed"
Merge "Fix pointer index out of range exception"
Merge "[ActivityManager] Prevent provider from been created twice."
Merge "[ActivityManager] Ensure provider external access count will be released."