Activity selections not always persisted by AcitivtyChooserView.

1. ActivityChooserModel was delegating the rsponsibility for reading
   and persisting data to its clients i.e. ActivityChooserView and
   the latter was persisting on detach from window. However, it is
   not guaranteed that this callback will be received leading to
   lack of presistence sometimes. Now the model is responsible for
   loading and persisting thus voiding the posisbility a misbehaved
   client to cause data loss.

bug:5061638

Change-Id: Ia2b7bb659f7b9abe8869c94b3eaa726ddd4e4fad
diff --git a/core/java/android/widget/ActivityChooserModel.java b/core/java/android/widget/ActivityChooserModel.java
index d7429b3..4b0a6da 100644
--- a/core/java/android/widget/ActivityChooserModel.java
+++ b/core/java/android/widget/ActivityChooserModel.java
@@ -317,6 +317,7 @@
                 dataModel = new ActivityChooserModel(context, historyFileName);
                 sDataModelRegistry.put(historyFileName, dataModel);
             }
+            dataModel.readHistoricalData();
             return dataModel;
         }
     }
@@ -505,7 +506,7 @@
      *       data is read until this method is invoked.
      * <p>
      */
-    public void readHistoricalData() {
+    private void readHistoricalData() {
         synchronized (mInstanceLock) {
             if (!mCanReadHistoricalData || !mHistoricalRecordsChanged) {
                 return;
@@ -527,7 +528,7 @@
      * @throws IllegalStateException If this method is called before a call to
      *         {@link #readHistoricalData()}.
      */
-    public void persistHistoricalData() {
+    private void persistHistoricalData() {
         synchronized (mInstanceLock) {
             if (!mReadShareHistoryCalled) {
                 throw new IllegalStateException("No preceding call to #readHistoricalData");
@@ -629,6 +630,7 @@
             if (added) {
                 mHistoricalRecordsChanged = true;
                 pruneExcessiveHistoricalRecordsLocked();
+                persistHistoricalData();
                 sortActivities();
             }
             return added;
diff --git a/core/java/android/widget/ActivityChooserView.java b/core/java/android/widget/ActivityChooserView.java
index 5b69aa8..d85f8a4 100644
--- a/core/java/android/widget/ActivityChooserView.java
+++ b/core/java/android/widget/ActivityChooserView.java
@@ -307,7 +307,6 @@
         ActivityChooserModel dataModel = mAdapter.getDataModel();
         if (dataModel != null) {
             dataModel.registerObserver(mModelDataSetOberver);
-            dataModel.readHistoricalData();
         }
         mIsAttachedToWindow = true;
     }
@@ -318,7 +317,6 @@
         ActivityChooserModel dataModel = mAdapter.getDataModel();
         if (dataModel != null) {
             dataModel.unregisterObserver(mModelDataSetOberver);
-            dataModel.persistHistoricalData();
         }
         mIsAttachedToWindow = false;
     }