summaryrefslogtreecommitdiffstats
path: root/src/java/cyanogenmod/profiles/StreamSettings.java
blob: 9ce9415f97da08b31d8c56b7b28e0510e76332b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
 * Copyright (C) 2015 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cyanogenmod.profiles;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;


import cyanogenmod.os.Build;
import cyanogenmod.os.Concierge;
import cyanogenmod.os.Concierge.ParcelInfo;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

/**
 * The {@link StreamSettings} class allows for creating various {@link android.media.AudioManager}
 * overrides on the device depending on their capabilities.
 *
 * <p>Example for setting the alarm stream defaults and override:
 * <pre class="prettyprint">
 * StreamSettings alarmStreamSettings = new StreamSettings(AudioManager.STREAM_ALARM,
 *         am.getStreamVolume(AudioManager.STREAM_ALARM), true));
 * profile.setStreamSettings(alarmStreamSettings);
 * </pre>
 */
public final class StreamSettings implements Parcelable{

    private int mStreamId;
    private int mValue;
    private boolean mOverride;
    private boolean mDirty;

    /** @hide */
    public static final Parcelable.Creator<StreamSettings> CREATOR =
            new Parcelable.Creator<StreamSettings>() {
        public StreamSettings createFromParcel(Parcel in) {
            return new StreamSettings(in);
        }

        @Override
        public StreamSettings[] newArray(int size) {
            return new StreamSettings[size];
        }
    };

    /**
     * Unwrap {@link StreamSettings} from a parcel.
     * @param parcel
     */
    public StreamSettings(Parcel parcel) {
        readFromParcel(parcel);
    }

    /**
     * Construct a {@link StreamSettings} with a stream id and default states.
     * @param streamId ex: {@link android.media.AudioManager#STREAM_ALARM}
     */
    public StreamSettings(int streamId) {
        this(streamId, 0, false);
    }

    /**
     * Construct a {@link StreamSettings} with a stream id, default value,
     * and if the setting should override the user defaults.
     * @param streamId ex: {@link android.media.AudioManager#STREAM_ALARM}
     * @param value default value for the {@link StreamSettings}
     * @param override whether or not the {@link StreamSettings} should override user defaults
     */
    public StreamSettings(int streamId, int value, boolean override) {
        mStreamId = streamId;
        mValue = value;
        mOverride = override;
        mDirty = false;
    }

    /**
     * Retrieve the stream id id associated with the {@link StreamSettings}
     * @return an integer identifier
     */
    public int getStreamId() {
        return mStreamId;
    }

    /**
     * Get the default value for the {@link StreamSettings}
     * @return integer value corresponding with its state
     */
    public int getValue() {
        return mValue;
    }

    /**
     * Set the default value for the {@link StreamSettings}
     * @param value see {@link android.media.AudioManager} for viable values
     */
    public void setValue(int value) {
        mValue = value;
        mDirty = true;
    }

    /**
     * Set whether or not the {@link StreamSettings} should override default user values
     * @param override boolean override
     */
    public void setOverride(boolean override) {
        mOverride = override;
        mDirty = true;
    }

    /**
     * Check whether or not the {@link StreamSettings} overrides user settings.
     * @return true if override
     */
    public boolean isOverride() {
        return mOverride;
    }

    /** @hide */
    public boolean isDirty() {
        return mDirty;
    }

    /** @hide */
    public static StreamSettings fromXml(XmlPullParser xpp, Context context)
            throws XmlPullParserException, IOException {
        int event = xpp.next();
        StreamSettings streamDescriptor = new StreamSettings(0);
        while (event != XmlPullParser.END_TAG || !xpp.getName().equals("streamDescriptor")) {
            if (event == XmlPullParser.START_TAG) {
                String name = xpp.getName();
                if (name.equals("streamId")) {
                    streamDescriptor.mStreamId = Integer.parseInt(xpp.nextText());
                } else if (name.equals("value")) {
                    streamDescriptor.mValue = Integer.parseInt(xpp.nextText());
                } else if (name.equals("override")) {
                    streamDescriptor.mOverride = Boolean.parseBoolean(xpp.nextText());
                }
            } else if (event == XmlPullParser.END_DOCUMENT) {
                throw new IOException("Premature end of file while parsing stream settings");
            }
            event = xpp.next();
        }
        return streamDescriptor;
    }

    /** @hide */
    public void getXmlString(StringBuilder builder, Context context) {
        builder.append("<streamDescriptor>\n<streamId>");
        builder.append(mStreamId);
        builder.append("</streamId>\n<value>");
        builder.append(mValue);
        builder.append("</value>\n<override>");
        builder.append(mOverride);
        builder.append("</override>\n</streamDescriptor>\n");
        mDirty = false;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    /** @hide */
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        // Tell the concierge to prepare the parcel
        ParcelInfo parcelInfo = Concierge.prepareParcel(dest);

        // === BOYSENBERRY ===
        dest.writeInt(mStreamId);
        dest.writeInt(mOverride ? 1 : 0);
        dest.writeInt(mValue);
        dest.writeInt(mDirty ? 1 : 0);

        // Complete the parcel info for the concierge
        parcelInfo.complete();
    }

    /** @hide */
    public void readFromParcel(Parcel in) {
        // Read parcelable version via the Concierge
        ParcelInfo parcelInfo = Concierge.receiveParcel(in);
        int parcelableVersion = parcelInfo.getParcelVersion();

        // Pattern here is that all new members should be added to the end of
        // the writeToParcel method. Then we step through each version, until the latest
        // API release to help unravel this parcel
        if (parcelableVersion >= Build.CM_VERSION_CODES.BOYSENBERRY) {
            mStreamId = in.readInt();
            mOverride = in.readInt() != 0;
            mValue = in.readInt();
            mDirty = in.readInt() != 0;
        }

        // Complete parcel info for the concierge
        parcelInfo.complete();
    }
}