Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0f23bfcd authored by Lei Yu's avatar Lei Yu Committed by Android (Google) Code Review
Browse files

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

parents d51b81d8 d2c0eef2
Loading
Loading
Loading
Loading
+1 −22
Original line number Diff line number Diff line
@@ -119,9 +119,7 @@ public class RingVolumePreferenceController extends VolumeSeekBarPreferenceContr
        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 @@ 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 static final int UPDATE_EFFECTS_SUPPRESSOR = 1;
        private static final int UPDATE_RINGER_MODE = 2;
+11 −2
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@ package com.android.settings.notification;
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.core.lifecycle.Lifecycle;
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,9 +252,17 @@ public class SoundSettings extends DashboardFragment {
                                AudioManager.STREAM_RING) / maxVolume);
                resId = R.string.sound_settings_summary;
            }

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

    public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY
            = new SummaryLoader.SummaryProviderFactory() {
+55 −0
Original line number Diff line number Diff line
/*
 * 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();
    }

}
+66 −0
Original line number Diff line number Diff line
/*
 * 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);
    }
}