Merge "Skip Data usage in Settings Widgets" into udc-qpr-dev
diff --git a/src/com/android/settings/shortcut/CreateShortcutPreferenceController.java b/src/com/android/settings/shortcut/CreateShortcutPreferenceController.java
index bf95348..8871135 100644
--- a/src/com/android/settings/shortcut/CreateShortcutPreferenceController.java
+++ b/src/com/android/settings/shortcut/CreateShortcutPreferenceController.java
@@ -45,11 +45,14 @@
 
 import com.android.settings.R;
 import com.android.settings.Settings;
+import com.android.settings.Settings.DataUsageSummaryActivity;
 import com.android.settings.Settings.TetherSettingsActivity;
 import com.android.settings.Settings.WifiTetherSettingsActivity;
 import com.android.settings.activityembedding.ActivityEmbeddingUtils;
 import com.android.settings.core.BasePreferenceController;
 import com.android.settings.gestures.OneHandedSettingsUtils;
+import com.android.settings.network.SubscriptionUtil;
+import com.android.settings.network.telephony.MobileNetworkUtils;
 import com.android.settings.overlay.FeatureFactory;
 import com.android.settings.wifi.WifiUtils;
 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
@@ -204,6 +207,12 @@
                 Log.d(TAG, "Skipping non-system app: " + info.activityInfo);
                 continue;
             }
+            if (info.activityInfo.name.endsWith(DataUsageSummaryActivity.class.getSimpleName())) {
+                if (!canShowDataUsage()) {
+                    Log.d(TAG, "Skipping data usage settings:" + info.activityInfo);
+                    continue;
+                }
+            }
             shortcuts.add(info);
         }
         Collections.sort(shortcuts, SHORTCUT_COMPARATOR);
@@ -211,6 +220,12 @@
     }
 
     @VisibleForTesting
+    boolean canShowDataUsage() {
+        return SubscriptionUtil.isSimHardwareVisible(mContext)
+                && !MobileNetworkUtils.isMobileNetworkUserRestricted(mContext);
+    }
+
+    @VisibleForTesting
     boolean canShowWifiHotspot() {
         return WifiUtils.canShowWifiHotspot(mContext);
     }
diff --git a/tests/robotests/src/com/android/settings/shortcut/CreateShortcutPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/shortcut/CreateShortcutPreferenceControllerTest.java
index df1fec3..fdb7feb 100644
--- a/tests/robotests/src/com/android/settings/shortcut/CreateShortcutPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/shortcut/CreateShortcutPreferenceControllerTest.java
@@ -36,8 +36,11 @@
 import android.content.pm.ResolveInfo;
 import android.content.pm.ShortcutInfo;
 import android.content.pm.ShortcutManager;
+import android.content.res.Resources;
 import android.os.SystemProperties;
+import android.os.UserManager;
 
+import com.android.settings.R;
 import com.android.settings.Settings;
 import com.android.settings.testutils.shadow.ShadowConnectivityManager;
 
@@ -69,6 +72,10 @@
     private ShortcutManager mShortcutManager;
     @Mock
     private Activity mHost;
+    @Mock
+    private Resources mResources;
+    @Mock
+    private UserManager mUserManager;
 
     private Context mContext;
     private ShadowConnectivityManager mShadowConnectivityManager;
@@ -188,6 +195,70 @@
         assertThat(mController.queryShortcuts()).hasSize(0);
     }
 
+    @Test
+    public void queryShortcuts_configShowDataUsage_ShouldEnableShortcuts() {
+        doReturn(true).when(mController).canShowDataUsage();
+        setupActivityInfo(Settings.DataUsageSummaryActivity.class.getSimpleName());
+
+        assertThat(mController.queryShortcuts()).hasSize(1);
+    }
+
+    @Test
+    public void queryShortcuts_configNotShowDataUsage_ShouldDisableShortcuts() {
+        doReturn(false).when(mController).canShowDataUsage();
+        setupActivityInfo(Settings.DataUsageSummaryActivity.class.getSimpleName());
+
+        assertThat(mController.queryShortcuts()).hasSize(0);
+    }
+
+    @Test
+    public void canShowDataUsage_configShowDataUsage_returnTrue() {
+        when(mContext.getResources()).thenReturn(mResources);
+        when(mResources.getBoolean(R.bool.config_show_sim_info)).thenReturn(true);
+        when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
+        when(mUserManager.isGuestUser()).thenReturn(false);
+        when(mUserManager.hasUserRestriction(
+                UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)).thenReturn(false);
+
+        assertThat(mController.canShowDataUsage()).isTrue();
+    }
+
+    @Test
+    public void canShowDataUsage_noSimCapability_returnFalse() {
+        when(mContext.getResources()).thenReturn(mResources);
+        when(mResources.getBoolean(R.bool.config_show_sim_info)).thenReturn(false);
+        when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
+        when(mUserManager.isGuestUser()).thenReturn(false);
+        when(mUserManager.hasUserRestriction(
+                UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)).thenReturn(false);
+
+        assertThat(mController.canShowDataUsage()).isFalse();
+    }
+
+    @Test
+    public void canShowDataUsage_isGuestUser_returnFalse() {
+        when(mContext.getResources()).thenReturn(mResources);
+        when(mResources.getBoolean(R.bool.config_show_sim_info)).thenReturn(true);
+        when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
+        when(mUserManager.isGuestUser()).thenReturn(true);
+        when(mUserManager.hasUserRestriction(
+                UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)).thenReturn(false);
+
+        assertThat(mController.canShowDataUsage()).isFalse();
+    }
+
+    @Test
+    public void canShowDataUsage_isMobileNetworkUserRestricted_returnFalse() {
+        when(mContext.getResources()).thenReturn(mResources);
+        when(mResources.getBoolean(R.bool.config_show_sim_info)).thenReturn(true);
+        when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
+        when(mUserManager.isGuestUser()).thenReturn(false);
+        when(mUserManager.hasUserRestriction(
+                UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)).thenReturn(true);
+
+        assertThat(mController.canShowDataUsage()).isFalse();
+    }
+
     private void setupActivityInfo(String name) {
         ResolveInfo ri = new ResolveInfo();
         ri.activityInfo = new ActivityInfo();