Virtualizer.java revision 1a5149e5d7f2dddc8b324f7695e69fd89af73c52
1fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent/* 217cb280e7f1ac3556eac90ab08263712e0348cb9Eric Laurent * Copyright (C) 2010 The Android Open Source Project 3fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * 4fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * Licensed under the Apache License, Version 2.0 (the "License"); 5fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * you may not use this file except in compliance with the License. 6fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * You may obtain a copy of the License at 7fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * 8fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * http://www.apache.org/licenses/LICENSE-2.0 9fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * 10fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * Unless required by applicable law or agreed to in writing, software 11fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * distributed under the License is distributed on an "AS IS" BASIS, 12fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * See the License for the specific language governing permissions and 14fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * limitations under the License. 15fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent */ 16fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent 171a5149e5d7f2dddc8b324f7695e69fd89af73c52Eric Laurentpackage android.media.audiofx; 18fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent 19fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurentimport android.app.Activity; 20fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurentimport android.content.Context; 21fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurentimport android.content.Intent; 221a5149e5d7f2dddc8b324f7695e69fd89af73c52Eric Laurentimport android.media.audiofx.AudioEffect; 23fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurentimport android.os.Bundle; 24fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurentimport android.util.Log; 25ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent 26fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurentimport java.nio.ByteOrder; 27fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurentimport java.nio.ByteBuffer; 28fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurentimport java.nio.CharBuffer; 29ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurentimport java.util.StringTokenizer; 30fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent 31fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent 32fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent/** 33fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * An audio virtualizer is a general name for an effect to spatialize audio channels. The exact 34fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * behavior of this effect is dependent on the number of audio input channels and the types and 35fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * number of audio output channels of the device. For example, in the case of a stereo input and 36fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * stereo headphone output, a stereo widening effect is used when this effect is turned on. 37fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * <p>An application creates a Virtualizer object to instantiate and control a virtualizer engine 38fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * in the audio framework. 39fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * <p>The methods, parameter types and units exposed by the Virtualizer implementation are directly 40fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * mapping those defined by the OpenSL ES 1.0.1 Specification (http://www.khronos.org/opensles/) 41fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * for the SLVirtualizerItf interface. Please refer to this specification for more details. 42fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * <p>To attach the Virtualizer to a particular AudioTrack or MediaPlayer, specify the audio session 43fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * ID of this AudioTrack or MediaPlayer when constructing the Virtualizer. If the audio session ID 0 44fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * is specified, the Virtualizer applies to the main audio output mix. 451a5149e5d7f2dddc8b324f7695e69fd89af73c52Eric Laurent * <p>Creating a Virtualizer on the output mix (audio session 0) requires permission 461a5149e5d7f2dddc8b324f7695e69fd89af73c52Eric Laurent * {@link android.Manifest.permission#MODIFY_AUDIO_SETTINGS} 471a5149e5d7f2dddc8b324f7695e69fd89af73c52Eric Laurent * <p>See {@link android.media.MediaPlayer#getAudioSessionId()} for details on audio sessions. 481a5149e5d7f2dddc8b324f7695e69fd89af73c52Eric Laurent * <p>See {@link android.media.audiofx.AudioEffect} class for more details on controlling 491a5149e5d7f2dddc8b324f7695e69fd89af73c52Eric Laurent * audio effects. 50fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent */ 51fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent 52fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurentpublic class Virtualizer extends AudioEffect { 53fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent 54fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent private final static String TAG = "Virtualizer"; 55fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent 56fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent // These constants must be synchronized with those in frameworks/base/include/media/EffectVirtualizerApi.h 57fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent /** 58fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * Is strength parameter supported by virtualizer engine. Parameter ID for getParameter(). 59fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent */ 60fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent public static final int PARAM_STRENGTH_SUPPORTED = 0; 61fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent /** 62fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * Virtualizer effect strength. Parameter ID for 631a5149e5d7f2dddc8b324f7695e69fd89af73c52Eric Laurent * {@link android.media.audiofx.Virtualizer.OnParameterChangeListener} 64fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent */ 65fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent public static final int PARAM_STRENGTH = 1; 66fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent 67fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent /** 68fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * Indicates if strength parameter is supported by the virtualizer engine 69fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent */ 70fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent private boolean mStrengthSupported = false; 71fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent 72fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent /** 73fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * Registered listener for parameter changes. 74fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent */ 75fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent private OnParameterChangeListener mParamListener = null; 76fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent 77fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent /** 78fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * Listener used internally to to receive raw parameter change event from AudioEffect super class 79fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent */ 80fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent private BaseParameterListener mBaseParamListener = null; 81fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent 82fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent /** 83fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * Lock for access to mParamListener 84fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent */ 85fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent private final Object mParamListenerLock = new Object(); 86fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent 87fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent /** 88fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * Class constructor. 89fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * @param priority the priority level requested by the application for controlling the Virtualizer 90fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * engine. As the same engine can be shared by several applications, this parameter indicates 91fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * how much the requesting application needs control of effect parameters. The normal priority 92fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * is 0, above normal is a positive number, below normal a negative number. 9317cb280e7f1ac3556eac90ab08263712e0348cb9Eric Laurent * @param audioSession system wide unique audio session identifier. If audioSession 94fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * is not 0, the Virtualizer will be attached to the MediaPlayer or AudioTrack in the 95fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * same audio session. Otherwise, the Virtualizer will apply to the output mix. 96fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * 97fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * @throws java.lang.IllegalStateException 98fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * @throws java.lang.IllegalArgumentException 99fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * @throws java.lang.UnsupportedOperationException 100fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * @throws java.lang.RuntimeException 101fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent */ 102fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent public Virtualizer(int priority, int audioSession) 103fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent throws IllegalStateException, IllegalArgumentException, 104fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent UnsupportedOperationException, RuntimeException { 105fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent super(EFFECT_TYPE_VIRTUALIZER, EFFECT_TYPE_NULL, priority, audioSession); 106fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent 107ba8da2e61b1d9ebb7a4758f1f7849ff8440bd20cEric Laurent int[] value = new int[1]; 108fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent checkStatus(getParameter(PARAM_STRENGTH_SUPPORTED, value)); 109fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent mStrengthSupported = (value[0] != 0); 110fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent } 111fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent 112fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent /** 113fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * Indicates whether setting strength is supported. If this method returns false, only one 114fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * strength is supported and the setStrength() method always rounds to that value. 115fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * @return true is strength parameter is supported, false otherwise 116fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent */ 117fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent public boolean getStrengthSupported() { 118fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent return mStrengthSupported; 119fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent } 120fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent 121fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent /** 122fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * Sets the strength of the virtualizer effect. If the implementation does not support per mille 123fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * accuracy for setting the strength, it is allowed to round the given strength to the nearest 124fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * supported value. You can use the {@link #getRoundedStrength()} method to query the 125fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * (possibly rounded) value that was actually set. 12617cb280e7f1ac3556eac90ab08263712e0348cb9Eric Laurent * @param strength strength of the effect. The valid range for strength strength is [0, 1000], 127fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * where 0 per mille designates the mildest effect and 1000 per mille designates the strongest. 128fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * @throws IllegalStateException 129fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * @throws IllegalArgumentException 130fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * @throws UnsupportedOperationException 131fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent */ 132fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent public void setStrength(short strength) 133fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException { 134fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent checkStatus(setParameter(PARAM_STRENGTH, strength)); 135fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent } 136fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent 137fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent /** 138fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * Gets the current strength of the effect. 13917cb280e7f1ac3556eac90ab08263712e0348cb9Eric Laurent * @return the strength of the effect. The valid range for strength is [0, 1000], where 0 per 140fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * mille designates the mildest effect and 1000 per mille the strongest 141fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * @throws IllegalStateException 142fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * @throws IllegalArgumentException 143fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * @throws UnsupportedOperationException 144fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent */ 145fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent public short getRoundedStrength() 146fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException { 147fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent short[] value = new short[1]; 148fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent checkStatus(getParameter(PARAM_STRENGTH, value)); 149fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent return value[0]; 150fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent } 151fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent 152fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent /** 153fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * The OnParameterChangeListener interface defines a method called by the Virtualizer when a 154fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * parameter value has changed. 155fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent */ 156fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent public interface OnParameterChangeListener { 157fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent /** 158fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * Method called when a parameter value has changed. The method is called only if the 159fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * parameter was changed by another application having the control of the same 160fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * Virtualizer engine. 161fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * @param effect the Virtualizer on which the interface is registered. 162fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * @param status status of the set parameter operation. 163fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * @param param ID of the modified parameter. See {@link #PARAM_STRENGTH} ... 164fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * @param value the new parameter value. 165fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent */ 166fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent void onParameterChange(Virtualizer effect, int status, int param, short value); 167fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent } 168fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent 169fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent /** 170fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * Listener used internally to receive unformatted parameter change events from AudioEffect 171fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * super class. 172fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent */ 173fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent private class BaseParameterListener implements AudioEffect.OnParameterChangeListener { 174fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent private BaseParameterListener() { 175fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent 176fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent } 177fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent public void onParameterChange(AudioEffect effect, int status, byte[] param, byte[] value) { 178fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent OnParameterChangeListener l = null; 179fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent 180fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent synchronized (mParamListenerLock) { 181fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent if (mParamListener != null) { 182fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent l = mParamListener; 183fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent } 184fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent } 185fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent if (l != null) { 186fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent int p = -1; 187fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent short v = -1; 188fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent 189fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent if (param.length == 4) { 190fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent p = byteArrayToInt(param, 0); 191fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent } 192fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent if (value.length == 2) { 193fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent v = byteArrayToShort(value, 0); 194fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent } 195fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent if (p != -1 && v != -1) { 196fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent l.onParameterChange(Virtualizer.this, status, p, v); 197fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent } 198fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent } 199fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent } 200fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent } 201fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent 202fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent /** 203fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * Registers an OnParameterChangeListener interface. 204fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * @param listener OnParameterChangeListener interface registered 205fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent */ 206fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent public void setParameterListener(OnParameterChangeListener listener) { 207fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent synchronized (mParamListenerLock) { 208fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent if (mParamListener == null) { 209fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent mParamListener = listener; 210fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent mBaseParamListener = new BaseParameterListener(); 211fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent super.setParameterListener(mBaseParamListener); 212fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent } 213fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent } 214fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent } 215ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent 216ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent /** 217ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent * The Settings class regroups all virtualizer parameters. It is used in 218ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent * conjuntion with getProperties() and setProperties() methods to backup and restore 219ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent * all parameters in a single call. 220ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent */ 221ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent public static class Settings { 222ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent public short strength; 223ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent 224ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent public Settings() { 225ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent } 226ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent 227ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent /** 228ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent * Settings class constructor from a key=value; pairs formatted string. The string is 229ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent * typically returned by Settings.toString() method. 230ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent * @throws IllegalArgumentException if the string is not correctly formatted. 231ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent */ 232ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent public Settings(String settings) { 233ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent StringTokenizer st = new StringTokenizer(settings, "=;"); 234ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent int tokens = st.countTokens(); 235ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent if (st.countTokens() != 3) { 236ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent throw new IllegalArgumentException("settings: " + settings); 237ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent } 238ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent String key = st.nextToken(); 239ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent if (!key.equals("Virtualizer")) { 240ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent throw new IllegalArgumentException( 241ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent "invalid settings for Virtualizer: " + key); 242ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent } 243ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent try { 244ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent key = st.nextToken(); 245ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent if (!key.equals("strength")) { 246ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent throw new IllegalArgumentException("invalid key name: " + key); 247ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent } 248ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent strength = Short.parseShort(st.nextToken()); 249ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent } catch (NumberFormatException nfe) { 250ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent throw new IllegalArgumentException("invalid value for key: " + key); 251ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent } 252ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent } 253ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent 254ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent @Override 255ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent public String toString() { 256ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent String str = new String ( 257ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent "Virtualizer"+ 258ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent ";strength="+Short.toString(strength) 259ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent ); 260ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent return str; 261ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent } 262ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent }; 263ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent 264ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent 265ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent /** 266ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent * Gets the virtualizer properties. This method is useful when a snapshot of current 267ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent * virtualizer settings must be saved by the application. 268ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent * @return a Virtualizer.Settings object containing all current parameters values 269ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent * @throws IllegalStateException 270ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent * @throws IllegalArgumentException 271ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent * @throws UnsupportedOperationException 272ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent */ 273ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent public Virtualizer.Settings getProperties() 274ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException { 275ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent Settings settings = new Settings(); 276ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent short[] value = new short[1]; 277ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent checkStatus(getParameter(PARAM_STRENGTH, value)); 278ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent settings.strength = value[0]; 279ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent return settings; 280ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent } 281ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent 282ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent /** 283ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent * Sets the virtualizer properties. This method is useful when virtualizer settings have to 284ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent * be applied from a previous backup. 28517cb280e7f1ac3556eac90ab08263712e0348cb9Eric Laurent * @param settings a Virtualizer.Settings object containing the properties to apply 286ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent * @throws IllegalStateException 287ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent * @throws IllegalArgumentException 288ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent * @throws UnsupportedOperationException 289ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent */ 290ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent public void setProperties(Virtualizer.Settings settings) 291ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException { 292ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent checkStatus(setParameter(PARAM_STRENGTH, settings.strength)); 293ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent } 294fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent} 295