1 /* 2 * Copyright (C) 2016 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.googlecode.android_scripting.facade; 18 19 import android.media.AudioManager; 20 import android.media.ToneGenerator; 21 22 import com.googlecode.android_scripting.jsonrpc.RpcReceiver; 23 import com.googlecode.android_scripting.rpc.Rpc; 24 import com.googlecode.android_scripting.rpc.RpcDefault; 25 import com.googlecode.android_scripting.rpc.RpcParameter; 26 27 /** 28 * Generate DTMF tones. 29 * 30 */ 31 public class ToneGeneratorFacade extends RpcReceiver { 32 33 private final ToneGenerator mToneGenerator; 34 ToneGeneratorFacade(FacadeManager manager)35 public ToneGeneratorFacade(FacadeManager manager) { 36 super(manager); 37 mToneGenerator = new ToneGenerator(AudioManager.STREAM_MUSIC, 100); 38 } 39 40 @Rpc(description = "Generate DTMF tones for the given phone number.") generateDtmfTones( @pcParametername = "phoneNumber") String phoneNumber, @RpcParameter(name = "toneDuration", description = "duration of each tone in milliseconds") @RpcDefault("100") Integer toneDuration)41 public void generateDtmfTones( 42 @RpcParameter(name = "phoneNumber") String phoneNumber, 43 @RpcParameter(name = "toneDuration", description = "duration of each tone in milliseconds") @RpcDefault("100") Integer toneDuration) 44 throws InterruptedException { 45 try { 46 for (int i = 0; i < phoneNumber.length(); i++) { 47 switch (phoneNumber.charAt(i)) { 48 case '0': 49 mToneGenerator.startTone(ToneGenerator.TONE_DTMF_0); 50 Thread.sleep(toneDuration); 51 break; 52 case '1': 53 mToneGenerator.startTone(ToneGenerator.TONE_DTMF_1); 54 Thread.sleep(toneDuration); 55 break; 56 case '2': 57 mToneGenerator.startTone(ToneGenerator.TONE_DTMF_2); 58 Thread.sleep(toneDuration); 59 break; 60 case '3': 61 mToneGenerator.startTone(ToneGenerator.TONE_DTMF_3); 62 Thread.sleep(toneDuration); 63 break; 64 case '4': 65 mToneGenerator.startTone(ToneGenerator.TONE_DTMF_4); 66 Thread.sleep(toneDuration); 67 break; 68 case '5': 69 mToneGenerator.startTone(ToneGenerator.TONE_DTMF_5); 70 Thread.sleep(toneDuration); 71 break; 72 case '6': 73 mToneGenerator.startTone(ToneGenerator.TONE_DTMF_6); 74 Thread.sleep(toneDuration); 75 break; 76 case '7': 77 mToneGenerator.startTone(ToneGenerator.TONE_DTMF_7); 78 Thread.sleep(toneDuration); 79 break; 80 case '8': 81 mToneGenerator.startTone(ToneGenerator.TONE_DTMF_8); 82 Thread.sleep(toneDuration); 83 break; 84 case '9': 85 mToneGenerator.startTone(ToneGenerator.TONE_DTMF_9); 86 Thread.sleep(toneDuration); 87 break; 88 case '*': 89 mToneGenerator.startTone(ToneGenerator.TONE_DTMF_S); 90 Thread.sleep(toneDuration); 91 break; 92 case '#': 93 mToneGenerator.startTone(ToneGenerator.TONE_DTMF_P); 94 Thread.sleep(toneDuration); 95 break; 96 default: 97 throw new RuntimeException("Cannot generate tone for '" + phoneNumber.charAt(i) + "'"); 98 } 99 } 100 } finally { 101 mToneGenerator.stopTone(); 102 } 103 } 104 105 @Override shutdown()106 public void shutdown() { 107 } 108 } 109