OSDN Git Service

Fix IllegalArgumentException in AudioHelper
[android-x86/packages-apps-Settings.git] / src / com / android / settings / notification / AudioHelper.java
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.settings.notification;
18
19 import android.annotation.UserIdInt;
20 import android.content.Context;
21 import android.media.AudioManager;
22 import android.media.AudioSystem;
23 import android.os.UserHandle;
24 import android.os.UserManager;
25 import android.util.Log;
26
27 import com.android.settings.Utils;
28
29 /**
30  * Helper class to wrap API for testing
31  */
32 public class AudioHelper {
33
34     private static final String TAG = "AudioHelper";
35     private Context mContext;
36     private AudioManager mAudioManager;
37
38     public AudioHelper(Context context) {
39         mContext = context;
40         mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
41     }
42
43     public boolean isSingleVolume() {
44         return AudioSystem.isSingleVolume(mContext);
45     }
46
47     public int getManagedProfileId(UserManager um) {
48         return Utils.getManagedProfileId(um, UserHandle.myUserId());
49     }
50
51     public boolean isUserUnlocked(UserManager um, @UserIdInt int userId) {
52         return um.isUserUnlocked(userId);
53     }
54
55     public Context createPackageContextAsUser(@UserIdInt int profileId) {
56         return Utils.createPackageContextAsUser(mContext, profileId);
57     }
58
59     public int getRingerModeInternal() {
60         return mAudioManager.getRingerModeInternal();
61     }
62
63     public int getLastAudibleStreamVolume(int stream) {
64         return mAudioManager.getLastAudibleStreamVolume(stream);
65     }
66
67     public int getStreamVolume(int stream) {
68         return mAudioManager.getStreamVolume(stream);
69     }
70
71     public boolean setStreamVolume(int stream, int volume) {
72         mAudioManager.setStreamVolume(stream, volume, 0);
73         return true;
74     }
75
76     public int getMaxVolume(int stream) {
77         return mAudioManager.getStreamMaxVolume(stream);
78     }
79
80     public int getMinVolume(int stream) {
81         int minVolume;
82         try {
83             minVolume = mAudioManager.getStreamMinVolume(stream);
84         } catch (IllegalArgumentException e) {
85             Log.w(TAG, "Invalid stream type " + stream);
86             // Fallback to STREAM_VOICE_CALL because CallVolumePreferenceController.java default
87             // return STREAM_VOICE_CALL in getAudioStream
88             minVolume = mAudioManager.getStreamMinVolume(AudioManager.STREAM_VOICE_CALL);
89         }
90         return minVolume;
91     }
92 }