1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.cts.deviceandprofileowner; 18 19 import static com.android.compatibility.common.util.SystemUtil.runWithShellPermissionIdentity; 20 21 import android.content.Context; 22 import android.content.pm.PackageManager; 23 import android.content.res.Resources; 24 import android.media.AudioManager; 25 import android.media.MediaPlayer; 26 import android.net.Uri; 27 import android.os.SystemClock; 28 import android.os.UserManager; 29 import android.util.Log; 30 31 import java.util.Objects; 32 import java.util.concurrent.Callable; 33 import java.util.regex.Pattern; 34 35 public class AudioRestrictionTest extends BaseDeviceAdminTest { 36 private static final String TAG = AudioRestrictionTest.class.getSimpleName(); 37 private AudioManager mAudioManager; 38 private PackageManager mPackageManager; 39 private boolean mUseFixedVolume; 40 private boolean mUseFullVolume; 41 private final Callable<Boolean> mCheckIfMasterVolumeMuted = new Callable<Boolean>() { 42 @Override 43 public Boolean call() throws Exception { 44 return mDevicePolicyManager.isMasterVolumeMuted(ADMIN_RECEIVER_COMPONENT); 45 } 46 }; 47 48 @Override setUp()49 protected void setUp() throws Exception { 50 super.setUp(); 51 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); 52 mPackageManager = mContext.getPackageManager(); 53 mUseFixedVolume = mContext.getResources().getBoolean( 54 Resources.getSystem().getIdentifier("config_useFixedVolume", "bool", "android")); 55 mUseFullVolume = runWithShellPermissionIdentity(() -> mAudioManager.isFullVolumeDevice(), 56 android.Manifest.permission.QUERY_AUDIO_STATE); 57 } 58 59 // Here we test that DISALLOW_ADJUST_VOLUME disallows to unmute volume. testDisallowAdjustVolume_muted()60 public void testDisallowAdjustVolume_muted() throws Exception { 61 if (!mPackageManager.hasSystemFeature(PackageManager.FEATURE_AUDIO_OUTPUT)) { 62 return; 63 } 64 65 // If we check that some value did not change, we must wait until the action is applied. 66 // Method waitUntil() may check old value before changes took place. 67 final int WAIT_TIME_MS = 1000; 68 final boolean initVolumeMuted = 69 mDevicePolicyManager.isMasterVolumeMuted(ADMIN_RECEIVER_COMPONENT); 70 try { 71 // Unmute volume, if necessary. 72 if (initVolumeMuted) { 73 mDevicePolicyManager.setMasterVolumeMuted(ADMIN_RECEIVER_COMPONENT, false); 74 waitUntil(false, mCheckIfMasterVolumeMuted); 75 } 76 77 // DISALLOW_ADJUST_VOLUME must mute volume. 78 mDevicePolicyManager.addUserRestriction(ADMIN_RECEIVER_COMPONENT, 79 UserManager.DISALLOW_ADJUST_VOLUME); 80 waitUntil(true, mCheckIfMasterVolumeMuted); 81 82 // Unmute should not have effect because the restriction does not allow this. 83 mDevicePolicyManager.setMasterVolumeMuted(ADMIN_RECEIVER_COMPONENT, false); 84 Thread.sleep(WAIT_TIME_MS); 85 assertTrue(mDevicePolicyManager.isMasterVolumeMuted(ADMIN_RECEIVER_COMPONENT)); 86 } finally { 87 mDevicePolicyManager.clearUserRestriction(ADMIN_RECEIVER_COMPONENT, 88 UserManager.DISALLOW_ADJUST_VOLUME); 89 mDevicePolicyManager.setMasterVolumeMuted(ADMIN_RECEIVER_COMPONENT, initVolumeMuted); 90 } 91 } 92 testDisallowAdjustVolume()93 public void testDisallowAdjustVolume() throws Exception { 94 if (!mPackageManager.hasSystemFeature(PackageManager.FEATURE_AUDIO_OUTPUT) 95 || mUseFixedVolume || mUseFullVolume) { 96 return; 97 } 98 99 Uri uri = Uri.parse("android.resource://" + mContext.getPackageName() + "/" + R.raw.ringer); 100 MediaPlayer mediaPlayer = new MediaPlayer(); 101 mediaPlayer.setDataSource(mContext, uri); 102 mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 103 mediaPlayer.prepare(); 104 mediaPlayer.setLooping(true); 105 mediaPlayer.start(); 106 107 try { 108 // Set volume of music to be 1. 109 mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 1, /* flag= */ 0); 110 111 // Disallow adjusting volume. 112 mDevicePolicyManager.addUserRestriction(ADMIN_RECEIVER_COMPONENT, 113 UserManager.DISALLOW_ADJUST_VOLUME); 114 waitUntil(true, mCheckIfMasterVolumeMuted); 115 116 // Verify that volume can't be changed. 117 mAudioManager.adjustVolume(AudioManager.ADJUST_RAISE, /* flag= */ 0); 118 assertEquals(1, mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC)); 119 120 // Allowing adjusting volume. 121 mDevicePolicyManager.clearUserRestriction(ADMIN_RECEIVER_COMPONENT, 122 UserManager.DISALLOW_ADJUST_VOLUME); 123 waitUntil(false, mCheckIfMasterVolumeMuted); 124 125 // Verify the volume can be changed now. 126 mAudioManager.adjustVolume(AudioManager.ADJUST_RAISE, /* flag= */ 0); 127 waitUntil(2, new Callable<Integer>() { 128 @Override 129 public Integer call() throws Exception { 130 return mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 131 } 132 }); 133 } finally { 134 // Clear the restriction. 135 mDevicePolicyManager.clearUserRestriction(ADMIN_RECEIVER_COMPONENT, 136 UserManager.DISALLOW_ADJUST_VOLUME); 137 waitUntil(false, mCheckIfMasterVolumeMuted); 138 } 139 140 mediaPlayer.stop(); 141 mediaPlayer.release(); 142 mediaPlayer = null; 143 } 144 testDisallowUnmuteMicrophone()145 public void testDisallowUnmuteMicrophone() throws Exception { 146 if (!mPackageManager.hasSystemFeature(PackageManager.FEATURE_MICROPHONE)) { 147 return; 148 } 149 150 try { 151 mAudioManager.setMicrophoneMute(false); 152 if (mAudioManager.isMicrophoneMute()) { 153 Log.w(TAG, "Mic seems muted by hardware! Please unmute and rerrun the test."); 154 return; 155 } 156 157 // Disallow the microphone to be unmuted. 158 mDevicePolicyManager.addUserRestriction( 159 ADMIN_RECEIVER_COMPONENT, UserManager.DISALLOW_UNMUTE_MICROPHONE); 160 waitUntil(true, new Callable<Boolean>() { 161 @Override 162 public Boolean call() throws Exception { 163 return mAudioManager.isMicrophoneMute(); 164 } 165 }); 166 // Verify that we can't unmute the microphone. 167 mAudioManager.setMicrophoneMute(false); 168 assertTrue(mAudioManager.isMicrophoneMute()); 169 } finally { 170 // Clear the restriction 171 mDevicePolicyManager.clearUserRestriction( 172 ADMIN_RECEIVER_COMPONENT, UserManager.DISALLOW_UNMUTE_MICROPHONE); 173 waitUntil(false, new Callable<Boolean>() { 174 @Override 175 public Boolean call() throws Exception { 176 return mAudioManager.isMicrophoneMute(); 177 } 178 }); 179 } 180 } 181 waitUntil(T expected, Callable<T> c)182 private <T> void waitUntil(T expected, Callable<T> c) throws Exception { 183 final long start = SystemClock.elapsedRealtime(); 184 final int TIMEOUT_MS = 5 * 1000; 185 186 T actual; 187 while (!Objects.equals(expected, actual = c.call())) { 188 if ((SystemClock.elapsedRealtime() - start) >= TIMEOUT_MS) { 189 fail(String.format("Timed out waiting the value to change to %s (actual=%s)", 190 expected, actual)); 191 } 192 Thread.sleep(200); 193 } 194 } 195 } 196