• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 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.inputmethod.pinyin;
18 
19 import android.content.Context;
20 import android.media.AudioManager;
21 
22 /**
23  * Class used to manage related sound resources.
24  */
25 public class SoundManager {
26     private static SoundManager mInstance = null;
27     private Context mContext;
28     private AudioManager mAudioManager;
29     // Align sound effect volume on music volume
30     private final float FX_VOLUME = -1.0f;
31     private boolean mSilentMode;
32 
SoundManager(Context context)33     private SoundManager(Context context) {
34         mContext = context;
35         updateRingerMode();
36     }
37 
updateRingerMode()38     public void updateRingerMode() {
39         if (mAudioManager == null) {
40             mAudioManager = (AudioManager) mContext
41                     .getSystemService(Context.AUDIO_SERVICE);
42         }
43         mSilentMode = (mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL);
44     }
45 
getInstance(Context context)46     public static SoundManager getInstance(Context context) {
47         if (null == mInstance) {
48             if (null != context) {
49                 mInstance = new SoundManager(context);
50             }
51         }
52         return mInstance;
53     }
54 
playKeyDown()55     public void playKeyDown() {
56         if (mAudioManager == null) {
57             updateRingerMode();
58         }
59         if (!mSilentMode) {
60             int sound = AudioManager.FX_KEYPRESS_STANDARD;
61             mAudioManager.playSoundEffect(sound, FX_VOLUME);
62         }
63     }
64 }
65