Add suppressor text in SummaryProvider of Sound when it exists.

When suppressor exists for the sound effect, the sound summary
should show "muted by ...", not the "Ring volume at ..."

Bug: 31461022
Test: make -j40 RunSettingsRoboTests
Change-Id: I5d6895a7a330c993aac40bdca3bb1c79f8b7fbfe
diff --git a/src/com/android/settings/notification/RingVolumePreferenceController.java b/src/com/android/settings/notification/RingVolumePreferenceController.java
index 2297837..a94d086 100644
--- a/src/com/android/settings/notification/RingVolumePreferenceController.java
+++ b/src/com/android/settings/notification/RingVolumePreferenceController.java
@@ -119,9 +119,7 @@
         if (Objects.equals(suppressor, mSuppressor)) return;
         mSuppressor = suppressor;
         if (mPreference != null) {
-            final String text = suppressor != null ?
-                mContext.getString(com.android.internal.R.string.muted_by,
-                    getSuppressorCaption(suppressor)) : null;
+            final String text = SuppressorHelper.getSuppressionText(mContext, suppressor);
             mPreference.setSuppressionText(text);
         }
         updatePreferenceIcon();
@@ -137,25 +135,6 @@
         }
     }
 
-    private String getSuppressorCaption(ComponentName suppressor) {
-        final PackageManager pm = mContext.getPackageManager();
-        try {
-            final ServiceInfo info = pm.getServiceInfo(suppressor, 0);
-            if (info != null) {
-                final CharSequence seq = info.loadLabel(pm);
-                if (seq != null) {
-                    final String str = seq.toString().trim();
-                    if (str.length() > 0) {
-                        return str;
-                    }
-                }
-            }
-        } catch (Throwable e) {
-            Log.w(TAG, "Error loading suppressor caption", e);
-        }
-        return suppressor.getPackageName();
-    }
-
     private final class H extends Handler {
         private static final int UPDATE_EFFECTS_SUPPRESSOR = 1;
         private static final int UPDATE_RINGER_MODE = 2;
diff --git a/src/com/android/settings/notification/SoundSettings.java b/src/com/android/settings/notification/SoundSettings.java
index 5f53d9a..b82931a 100644
--- a/src/com/android/settings/notification/SoundSettings.java
+++ b/src/com/android/settings/notification/SoundSettings.java
@@ -19,6 +19,8 @@
 import android.app.Activity;
 import android.app.NotificationManager;
 import android.content.BroadcastReceiver;
+import android.content.ComponentName;
+import android.content.ContentResolver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
@@ -40,7 +42,6 @@
 import com.android.settings.dashboard.DashboardFragment;
 import com.android.settings.dashboard.SummaryLoader;
 import com.android.settings.search.BaseSearchIndexProvider;
-
 import com.android.settingslib.drawer.CategoryKey;
 import java.text.NumberFormat;
 import java.util.ArrayList;
@@ -251,7 +252,15 @@
                                 AudioManager.STREAM_RING) / maxVolume);
                 resId = R.string.sound_settings_summary;
             }
-            mSummaryLoader.setSummary(this, mContext.getString(resId, percent));
+
+            final ComponentName suppressor = NotificationManager.from(mContext)
+                    .getEffectsSuppressor();
+            if (suppressor != null) {
+                String suppressText = SuppressorHelper.getSuppressionText(mContext, suppressor);
+                mSummaryLoader.setSummary(this, suppressText);
+            } else {
+                mSummaryLoader.setSummary(this, mContext.getString(resId, percent));
+            }
         }
     }
 
diff --git a/src/com/android/settings/notification/SuppressorHelper.java b/src/com/android/settings/notification/SuppressorHelper.java
new file mode 100644
index 0000000..8bf4d83
--- /dev/null
+++ b/src/com/android/settings/notification/SuppressorHelper.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.notification;
+
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.pm.PackageManager;
+import android.content.pm.ServiceInfo;
+import android.support.annotation.VisibleForTesting;
+import android.util.Log;
+
+public class SuppressorHelper {
+    private static final String TAG = "SuppressorHelper";
+
+    public static String getSuppressionText(Context context, ComponentName suppressor) {
+        return suppressor != null ?
+                context.getString(com.android.internal.R.string.muted_by,
+                        getSuppressorCaption(context, suppressor)) : null;
+    }
+
+    @VisibleForTesting
+    static String getSuppressorCaption(Context context, ComponentName suppressor) {
+        final PackageManager pm = context.getPackageManager();
+        try {
+            final ServiceInfo info = pm.getServiceInfo(suppressor, 0);
+            if (info != null) {
+                final CharSequence seq = info.loadLabel(pm);
+                if (seq != null) {
+                    final String str = seq.toString().trim();
+                    if (str.length() > 0) {
+                        return str;
+                    }
+                }
+            }
+        } catch (Throwable e) {
+            Log.w(TAG, "Error loading suppressor caption", e);
+        }
+        return suppressor.getPackageName();
+    }
+
+}
diff --git a/tests/robotests/src/com/android/settings/notification/SuppressorHelperTest.java b/tests/robotests/src/com/android/settings/notification/SuppressorHelperTest.java
new file mode 100644
index 0000000..86f3d81
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/notification/SuppressorHelperTest.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.settings.notification;
+
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.pm.PackageManager;
+import android.content.pm.ServiceInfo;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import static com.google.common.truth.Truth.assertThat;
+import static org.mockito.Mockito.when;
+
+public class SuppressorHelperTest {
+    private static final String SUPPRESSOR_NAME = "wear";
+
+    private ComponentName mSuppressor;
+    @Mock
+    private Context mContext;
+    @Mock
+    private PackageManager mPackageManager;
+    @Mock
+    private ServiceInfo mServiceInfo;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mSuppressor = new ComponentName("", "");
+
+        try {
+            when(mContext.getPackageManager()).thenReturn(mPackageManager);
+            when(mPackageManager.getServiceInfo(mSuppressor, 0)).thenReturn(mServiceInfo);
+            when(mServiceInfo.loadLabel(mPackageManager)).thenReturn(new String(SUPPRESSOR_NAME));
+        } catch (PackageManager.NameNotFoundException e) {
+            // Do nothing. This exception will never happen in mock
+        }
+    }
+
+    @Test
+    public void testGetSuppressionText_SuppressorNull_ReturnNull() {
+        String text = SuppressorHelper.getSuppressionText(mContext, null);
+        assertThat(text).isNull();
+    }
+
+    @Test
+    public void testGetSuppressorCaption_SuppressorNotNull_ReturnSuppressorName() {
+        String text = SuppressorHelper.getSuppressorCaption(mContext, mSuppressor);
+        assertThat(text).isEqualTo(SUPPRESSOR_NAME);
+    }
+}