Fix null pointer in dashboard fragment test.

- use context instead of activity to retrieve resource details.
- revert the change previously made in getActivity() calls.
- add null checking in package name and tile intent.

Fix: 34396855
Test: make RunSettingsRoboTests

Change-Id: Ic853939fee3c381b663c0320354da51d3b2a0e11
diff --git a/src/com/android/settings/dashboard/DashboardFragment.java b/src/com/android/settings/dashboard/DashboardFragment.java
index 056efc7..de86bd5 100644
--- a/src/com/android/settings/dashboard/DashboardFragment.java
+++ b/src/com/android/settings/dashboard/DashboardFragment.java
@@ -299,15 +299,15 @@
         if (mSummaryLoader != null) {
             mSummaryLoader.release();
         }
-        final Activity activity = getActivity();
-        mSummaryLoader = new SummaryLoader(activity, getCategoryKey());
+        final Context context = getContext();
+        mSummaryLoader = new SummaryLoader(getActivity(), getCategoryKey());
         mSummaryLoader.setSummaryConsumer(this);
-        final TypedArray a = activity.obtainStyledAttributes(new int[] {
+        final TypedArray a = context.obtainStyledAttributes(new int[] {
             mDashboardFeatureProvider.isEnabled() ? android.R.attr.colorControlNormal
                 : android.R.attr.colorAccent});
-        final int tintColor = a.getColor(0, activity.getColor(android.R.color.white));
+        final int tintColor = a.getColor(0, context.getColor(android.R.color.white));
         a.recycle();
-        final String pkgName = activity.getPackageName();
+        final String pkgName = context.getPackageName();
         // Install dashboard tiles.
         for (Tile tile : tiles) {
             final String key = mDashboardFeatureProvider.getDashboardKeyForTile(tile);
@@ -318,7 +318,8 @@
             if (!displayTile(tile)) {
                 continue;
             }
-            if (!pkgName.equals(tile.intent.getComponent().getPackageName())) {
+            if (pkgName != null && tile.intent != null
+                && !pkgName.equals(tile.intent.getComponent().getPackageName())) {
                 // If this drawable is coming from outside Settings, tint it to match the color.
                 tile.icon.setTint(tintColor);
             }
@@ -326,12 +327,12 @@
                 // Have the key already, will rebind.
                 final Preference preference = mProgressiveDisclosureMixin.findPreference(
                         screen, key);
-                mDashboardFeatureProvider.bindPreferenceToTile(activity, preference, tile, key,
+                mDashboardFeatureProvider.bindPreferenceToTile(getActivity(), preference, tile, key,
                         mPlaceholderPreferenceController.getOrder());
             } else {
                 // Don't have this key, add it.
                 final Preference pref = new Preference(getPrefContext());
-                mDashboardFeatureProvider.bindPreferenceToTile(activity, pref, tile, key,
+                mDashboardFeatureProvider.bindPreferenceToTile(getActivity(), pref, tile, key,
                         mPlaceholderPreferenceController.getOrder());
                 mProgressiveDisclosureMixin.addPreference(screen, pref);
                 mDashboardTilePrefKeys.add(key);
diff --git a/tests/robotests/src/com/android/settings/dashboard/DashboardFragmentTest.java b/tests/robotests/src/com/android/settings/dashboard/DashboardFragmentTest.java
index f477202..d479e0a 100644
--- a/tests/robotests/src/com/android/settings/dashboard/DashboardFragmentTest.java
+++ b/tests/robotests/src/com/android/settings/dashboard/DashboardFragmentTest.java
@@ -79,6 +79,7 @@
         when(mFakeFeatureFactory.dashboardFeatureProvider.getTilesForCategory(anyString()))
                 .thenReturn(mDashboardCategory);
         mTestFragment.onAttach(ShadowApplication.getInstance().getApplicationContext());
+        when(mContext.getPackageName()).thenReturn("TestPackage");
     }
 
     @Test