blob: ad20ed895805eaa28914cd121327ead5073b11ec [file] [log] [blame]
Jean-Michel Trivi24806db2015-10-01 15:00:59 -07001/*
2 * Copyright (C) 2015 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
17package com.android.server.audio;
18
19import android.content.Context;
jiabin383c1ce2017-06-26 13:48:14 -070020import android.hardware.display.DisplayManager;
Jean-Michel Trivi24806db2015-10-01 15:00:59 -070021import android.media.AudioSystem;
22import android.os.Handler;
23import android.util.Log;
Jean-Michel Trivi24806db2015-10-01 15:00:59 -070024import android.view.Surface;
25import android.view.WindowManager;
26
Jean-Michel Trivi24806db2015-10-01 15:00:59 -070027/**
28 * Class to handle device rotation events for AudioService, and forward device rotation
29 * to the audio HALs through AudioSystem.
30 *
31 * The role of this class is to monitor device orientation changes, and upon rotation,
32 * verify the UI orientation. In case of a change, send the new orientation, in increments
33 * of 90deg, through AudioSystem.
34 *
35 * Note that even though we're responding to device orientation events, we always
36 * query the display rotation so audio stays in sync with video/dialogs. This is
37 * done with .getDefaultDisplay().getRotation() from WINDOW_SERVICE.
38 */
39class RotationHelper {
40
41 private static final String TAG = "AudioService.RotationHelper";
42
jiabin383c1ce2017-06-26 13:48:14 -070043 private static AudioDisplayListener sDisplayListener;
Jean-Michel Trivi24806db2015-10-01 15:00:59 -070044
45 private static final Object sRotationLock = new Object();
46 private static int sDeviceRotation = Surface.ROTATION_0; // R/W synchronized on sRotationLock
47
48 private static Context sContext;
jiabin383c1ce2017-06-26 13:48:14 -070049 private static Handler sHandler;
Jean-Michel Trivi24806db2015-10-01 15:00:59 -070050
51 /**
52 * post conditions:
jiabin383c1ce2017-06-26 13:48:14 -070053 * - sDisplayListener != null
Jean-Michel Trivi24806db2015-10-01 15:00:59 -070054 * - sContext != null
55 */
56 static void init(Context context, Handler handler) {
57 if (context == null) {
58 throw new IllegalArgumentException("Invalid null context");
59 }
60 sContext = context;
jiabin383c1ce2017-06-26 13:48:14 -070061 sHandler = handler;
62 sDisplayListener = new AudioDisplayListener();
63 enable();
Jean-Michel Trivi24806db2015-10-01 15:00:59 -070064 }
65
66 static void enable() {
jiabin383c1ce2017-06-26 13:48:14 -070067 ((DisplayManager) sContext.getSystemService(Context.DISPLAY_SERVICE))
68 .registerDisplayListener(sDisplayListener, sHandler);
Jean-Michel Trivi24806db2015-10-01 15:00:59 -070069 updateOrientation();
70 }
71
72 static void disable() {
jiabin383c1ce2017-06-26 13:48:14 -070073 ((DisplayManager) sContext.getSystemService(Context.DISPLAY_SERVICE))
74 .unregisterDisplayListener(sDisplayListener);
Jean-Michel Trivi24806db2015-10-01 15:00:59 -070075 }
76
77 /**
78 * Query current display rotation and publish the change if any.
79 */
80 static void updateOrientation() {
81 // Even though we're responding to device orientation events,
82 // use display rotation so audio stays in sync with video/dialogs
83 int newRotation = ((WindowManager) sContext.getSystemService(
84 Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
85 synchronized(sRotationLock) {
86 if (newRotation != sDeviceRotation) {
87 sDeviceRotation = newRotation;
88 publishRotation(sDeviceRotation);
89 }
90 }
91 }
92
93 private static void publishRotation(int rotation) {
94 Log.v(TAG, "publishing device rotation =" + rotation + " (x90deg)");
95 switch (rotation) {
96 case Surface.ROTATION_0:
97 AudioSystem.setParameters("rotation=0");
98 break;
99 case Surface.ROTATION_90:
100 AudioSystem.setParameters("rotation=90");
101 break;
102 case Surface.ROTATION_180:
103 AudioSystem.setParameters("rotation=180");
104 break;
105 case Surface.ROTATION_270:
106 AudioSystem.setParameters("rotation=270");
107 break;
108 default:
109 Log.e(TAG, "Unknown device rotation");
110 }
111 }
112
113 /**
jiabin383c1ce2017-06-26 13:48:14 -0700114 * Uses android.hardware.display.DisplayManager.DisplayListener
Jean-Michel Trivi24806db2015-10-01 15:00:59 -0700115 */
jiabin383c1ce2017-06-26 13:48:14 -0700116 final static class AudioDisplayListener implements DisplayManager.DisplayListener {
117
118 @Override
119 public void onDisplayAdded(int displayId) {
Jean-Michel Trivi24806db2015-10-01 15:00:59 -0700120 }
121
122 @Override
jiabin383c1ce2017-06-26 13:48:14 -0700123 public void onDisplayRemoved(int displayId) {
124 }
125
126 @Override
127 public void onDisplayChanged(int displayId) {
Jean-Michel Trivi24806db2015-10-01 15:00:59 -0700128 updateOrientation();
129 }
130 }
Jean-Michel Trivi24806db2015-10-01 15:00:59 -0700131}