Merge "Move mobile data dis/enable to Telephony" into lmp-preview-dev
diff --git a/src/com/android/settings/SettingsActivity.java b/src/com/android/settings/SettingsActivity.java
index f7e5dcd..a4aa60f 100644
--- a/src/com/android/settings/SettingsActivity.java
+++ b/src/com/android/settings/SettingsActivity.java
@@ -329,7 +329,9 @@
         public void handleMessage(Message msg) {
             switch (msg.what) {
                 case MSG_BUILD_CATEGORIES: {
-                    buildDashboardCategories(mCategories);
+                    if(mNeedToRebuildCategories) {
+                        buildDashboardCategories(mCategories);
+                    }
                 } break;
             }
         }
@@ -346,7 +348,7 @@
     }
 
     public List<DashboardCategory> getDashboardCategories() {
-        if (mNeedToRebuildCategories) {
+        if (mNeedToRebuildCategories || mCategories.size() == 0) {
             buildDashboardCategories(mCategories);
             mNeedToRebuildCategories = false;
         }
@@ -497,16 +499,11 @@
                 switchToFragment( initialFragmentName, initialArguments, true, false,
                         mInitialTitle, false);
             } else {
-                // We need to build the Categories in all cases
-                buildDashboardCategories(mCategories);
-
                 // No UP if we are displaying the main Dashboard
                 mDisplayHomeAsUpEnabled = false;
-                if (mCategories.size() > 0) {
-                    mInitialTitle = getText(R.string.dashboard_title);
-                    switchToFragment(DashboardSummary.class.getName(), null, false, false,
-                            mInitialTitle, false);
-                }
+                mInitialTitle = getText(R.string.dashboard_title);
+                switchToFragment(DashboardSummary.class.getName(), null, false, false,
+                        mInitialTitle, false);
             }
         }
 
diff --git a/src/com/android/settings/dashboard/DashboardSummary.java b/src/com/android/settings/dashboard/DashboardSummary.java
index faafad1..4aee7be 100644
--- a/src/com/android/settings/dashboard/DashboardSummary.java
+++ b/src/com/android/settings/dashboard/DashboardSummary.java
@@ -27,6 +27,7 @@
 import android.os.Handler;
 import android.os.Message;
 import android.text.TextUtils;
+import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -48,12 +49,12 @@
     private AuthenticatorHelper mAuthHelper;
     private boolean mAccountListenerAdded;
 
-    private static final int MSG_BUILD_CATEGORIES = 1;
+    private static final int MSG_REBUILD_UI = 1;
     private Handler mHandler = new Handler() {
         @Override
         public void handleMessage(Message msg) {
             switch (msg.what) {
-                case MSG_BUILD_CATEGORIES: {
+                case MSG_REBUILD_UI: {
                     final Context context = getActivity();
                     rebuildUI(context);
                 } break;
@@ -79,6 +80,12 @@
     }
 
     private void rebuildUI(Context context) {
+        if (!isAdded()) {
+            Log.w(LOG_TAG, "Cannot build the DashboardSummary UI yet as the Fragment is not added");
+            return;
+        }
+
+        long start = System.currentTimeMillis();
         final Resources res = getResources();
 
         mDashboard.removeAllViews();
@@ -116,6 +123,8 @@
             // Add the category
             mDashboard.addView(categoryView);
         }
+        long delta = System.currentTimeMillis() - start;
+        Log.d(LOG_TAG, "rebuildUI took: " + delta + " ms");
     }
 
     @Override
@@ -127,7 +136,7 @@
             mAccountListenerAdded = true;
         }
 
-        rebuildCategories();
+        sendRebuildUI();
     }
 
     @Override
@@ -172,9 +181,9 @@
         }
     }
 
-    private void rebuildCategories() {
-        if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
-            mHandler.sendEmptyMessage(MSG_BUILD_CATEGORIES);
+    private void sendRebuildUI() {
+        if (!mHandler.hasMessages(MSG_REBUILD_UI)) {
+            mHandler.sendEmptyMessage(MSG_REBUILD_UI);
         }
     }
 
@@ -182,6 +191,6 @@
     public void onAccountsUpdated(Account[] accounts) {
         final SettingsActivity sa = (SettingsActivity) getActivity();
         sa.setNeedToRebuildCategories(true);
-        rebuildCategories();
+        sendRebuildUI();
     }
 }