Skip to content

Commit b213dbd

Browse files
committed
SystemUI: Fix shutter sound
* When shutter sound for camera is forced on as required in some states, (config_camera_sound_forced, set via mcc/mnc), we also want to (or should) play it when a screenshot is taken from the preview instead of an actual picture * This change is loosely based on https://android-review.googlesource.com/c/platform/frameworks/base/+/1517742/ but uses publicly available APIs Testing: - Set config_camera_sound_forced to true and push a build to device - Turn down all stream volumes to muted - Take screenshot of any normal screen -> No sound played - Open camera, take screenshot -> Sound played - Turn up volume and repeat the screenshots -> Sound played in all cases Change-Id: Iacbb577c64f73b79cdfae7fb0487fc6a34ffe41c
1 parent 24463ec commit b213dbd

1 file changed

Lines changed: 37 additions & 2 deletions

File tree

packages/SystemUI/src/com/android/systemui/screenshot/GlobalScreenshot.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import android.graphics.drawable.Drawable;
5353
import android.graphics.drawable.InsetDrawable;
5454
import android.graphics.drawable.LayerDrawable;
55+
import android.hardware.camera2.CameraManager;
5556
import android.media.AudioManager;
5657
import android.media.MediaActionSound;
5758
import android.net.Uri;
@@ -60,6 +61,7 @@
6061
import android.os.Message;
6162
import android.os.RemoteException;
6263
import android.os.ServiceManager;
64+
import android.os.SystemProperties;
6365
import android.os.VibrationEffect;
6466
import android.os.Vibrator;
6567
import android.provider.Settings;
@@ -230,6 +232,9 @@ abstract static class ActionsReadyListener {
230232
private AudioManager mAudioManager;
231233
private Vibrator mVibrator;
232234

235+
private CameraManager mCameraManager;
236+
private int mCamsInUse = 0;
237+
233238
private int mNavMode;
234239
private int mLeftInset;
235240
private int mRightInset;
@@ -329,6 +334,9 @@ public GlobalScreenshot(
329334
// Grab system services needed for screenshot sound
330335
mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
331336
mVibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
337+
mCameraManager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
338+
mCameraManager.registerAvailabilityCallback(mCamCallback,
339+
new Handler(Looper.getMainLooper()));
332340

333341
// Store UI background executor
334342
mUiBgExecutor = uiBgExecutor;
@@ -1255,6 +1263,8 @@ private Drawable createScreenDrawable(Bitmap bitmap, Insets insets) {
12551263
}
12561264

12571265
private void playShutterSound() {
1266+
boolean playSound = readCameraSoundForced() && mCamsInUse > 0;
1267+
12581268
switch (mAudioManager.getRingerMode()) {
12591269
case AudioManager.RINGER_MODE_SILENT:
12601270
// do nothing
@@ -1266,9 +1276,34 @@ private void playShutterSound() {
12661276
}
12671277
break;
12681278
case AudioManager.RINGER_MODE_NORMAL:
1269-
// Play the shutter sound to notify that we've taken a screenshot
1270-
mCameraSound.play(MediaActionSound.SHUTTER_CLICK);
1279+
// in this case we want to play sound even if not forced on
1280+
playSound = true;
12711281
break;
12721282
}
1283+
1284+
// We want to play the shutter sound when it's either forced or
1285+
// when we use normal ringer mode
1286+
if (playSound) {
1287+
mCameraSound.play(MediaActionSound.SHUTTER_CLICK);
1288+
}
1289+
}
1290+
1291+
private CameraManager.AvailabilityCallback mCamCallback =
1292+
new CameraManager.AvailabilityCallback() {
1293+
@Override
1294+
public void onCameraOpened(String cameraId, String packageId) {
1295+
mCamsInUse++;
1296+
}
1297+
1298+
@Override
1299+
public void onCameraClosed(String cameraId) {
1300+
mCamsInUse--;
1301+
}
1302+
};
1303+
1304+
private boolean readCameraSoundForced() {
1305+
return SystemProperties.getBoolean("audio.camerasound.force", false) ||
1306+
mContext.getResources().getBoolean(
1307+
com.android.internal.R.bool.config_camera_sound_forced);
12731308
}
12741309
}

0 commit comments

Comments
 (0)