Merge "Add suppressor text in SummaryProvider of Sound when it exists."

This commit is contained in:
Lei Yu
2017-01-03 21:11:15 +00:00
committed by Android (Google) Code Review
4 changed files with 133 additions and 24 deletions

View File

@@ -119,9 +119,7 @@ public class RingVolumePreferenceController extends VolumeSeekBarPreferenceContr
if (Objects.equals(suppressor, mSuppressor)) return; if (Objects.equals(suppressor, mSuppressor)) return;
mSuppressor = suppressor; mSuppressor = suppressor;
if (mPreference != null) { if (mPreference != null) {
final String text = suppressor != null ? final String text = SuppressorHelper.getSuppressionText(mContext, suppressor);
mContext.getString(com.android.internal.R.string.muted_by,
getSuppressorCaption(suppressor)) : null;
mPreference.setSuppressionText(text); mPreference.setSuppressionText(text);
} }
updatePreferenceIcon(); updatePreferenceIcon();
@@ -137,25 +135,6 @@ public class RingVolumePreferenceController extends VolumeSeekBarPreferenceContr
} }
} }
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 final class H extends Handler {
private static final int UPDATE_EFFECTS_SUPPRESSOR = 1; private static final int UPDATE_EFFECTS_SUPPRESSOR = 1;
private static final int UPDATE_RINGER_MODE = 2; private static final int UPDATE_RINGER_MODE = 2;

View File

@@ -19,6 +19,8 @@ package com.android.settings.notification;
import android.app.Activity; import android.app.Activity;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
@@ -40,7 +42,6 @@ import com.android.settings.core.lifecycle.Lifecycle;
import com.android.settings.dashboard.DashboardFragment; import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.dashboard.SummaryLoader; import com.android.settings.dashboard.SummaryLoader;
import com.android.settings.search.BaseSearchIndexProvider; import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.drawer.CategoryKey; import com.android.settingslib.drawer.CategoryKey;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.util.ArrayList; import java.util.ArrayList;
@@ -251,7 +252,15 @@ public class SoundSettings extends DashboardFragment {
AudioManager.STREAM_RING) / maxVolume); AudioManager.STREAM_RING) / maxVolume);
resId = R.string.sound_settings_summary; 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));
}
} }
} }

View File

@@ -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();
}
}

View File

@@ -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);
}
}