1/*
2 * Copyright 2018 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 */
16package com.android.media;
17
18import static android.media.VolumeProvider2.VOLUME_CONTROL_ABSOLUTE;
19import static android.media.VolumeProvider2.VOLUME_CONTROL_FIXED;
20import static android.media.VolumeProvider2.VOLUME_CONTROL_RELATIVE;
21
22import android.media.VolumeProvider2;
23import android.media.update.VolumeProvider2Provider;
24
25public class VolumeProvider2Impl implements VolumeProvider2Provider {
26    private final VolumeProvider2 mInstance;
27    private final int mControlType;
28    private final int mMaxVolume;
29
30    private int mCurrentVolume;
31    private Callback mCallback;
32
33    public VolumeProvider2Impl(VolumeProvider2 instance,
34            @VolumeProvider2.ControlType int controlType, int maxVolume, int currentVolume) {
35        if (controlType != VOLUME_CONTROL_FIXED && controlType != VOLUME_CONTROL_RELATIVE
36                && controlType != VOLUME_CONTROL_ABSOLUTE) {
37            throw new IllegalArgumentException("wrong controlType " + controlType);
38        }
39        if (maxVolume < 0 || currentVolume < 0) {
40            throw new IllegalArgumentException("volume shouldn't be negative"
41                    + ", maxVolume=" + maxVolume + ", currentVolume=" + currentVolume);
42        }
43        if (currentVolume > maxVolume) {
44            throw new IllegalArgumentException("currentVolume shouldn't be greater than maxVolume"
45                    + ", maxVolume=" + maxVolume + ", currentVolume=" + currentVolume);
46        }
47        mInstance = instance;
48        mControlType = controlType;
49        mMaxVolume = maxVolume;
50        mCurrentVolume = currentVolume;
51    }
52
53    @Override
54    public int getControlType_impl() {
55        return mControlType;
56    }
57
58    @Override
59    public int getMaxVolume_impl() {
60        return mMaxVolume;
61    }
62
63    @Override
64    public int getCurrentVolume_impl() {
65        return mCurrentVolume;
66    }
67
68    @Override
69    public void setCurrentVolume_impl(int currentVolume) {
70        if (currentVolume < 0) {
71            throw new IllegalArgumentException("currentVolume shouldn't be negative"
72                    + ", currentVolume=" + currentVolume);
73        }
74        mCurrentVolume = currentVolume;
75        if (mCallback != null) {
76            mCallback.onVolumeChanged(mInstance);
77        }
78    }
79
80    /**
81     * Sets a callback to receive volume changes.
82     */
83    public void setCallback(Callback callback) {
84        mCallback = callback;
85    }
86
87    /**
88     * Listens for changes to the volume.
89     */
90    public static abstract class Callback {
91        public abstract void onVolumeChanged(VolumeProvider2 volumeProvider);
92    }
93}
94