summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/notification/IncreasingRingVolumePreference.java
blob: 6b78bbe06a1c48aae8b70ed34e4b521ceeb5e049 (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 * Copyright (C) 2014 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 com.android.settings.notification;

import android.content.ContentResolver;
import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.preference.PreferenceManager;
import android.preference.Preference;
import android.provider.Settings;
import android.text.format.Formatter;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;

import com.android.settings.R;
import cyanogenmod.providers.CMSettings;

public class IncreasingRingVolumePreference extends Preference implements
        PreferenceManager.OnActivityStopListener, Handler.Callback,
        SeekBar.OnSeekBarChangeListener {
    private static final String TAG = "IncreasingRingMinVolumePreference";

    public interface Callback {
        void onStartingSample();
    }

    private SeekBar mStartVolumeSeekBar;
    private SeekBar mRampUpTimeSeekBar;
    private TextView mRampUpTimeValue;

    private Ringtone mRingtone;
    private Callback mCallback;

    private Handler mHandler;
    private final Handler mMainHandler = new Handler(this);

    private static final int MSG_START_SAMPLE = 1;
    private static final int MSG_STOP_SAMPLE = 2;
    private static final int MSG_INIT_SAMPLE = 3;
    private static final int MSG_SET_VOLUME = 4;
    private static final int CHECK_RINGTONE_PLAYBACK_DELAY_MS = 1000;

    public IncreasingRingVolumePreference(Context context) {
        this(context, null);
    }

    public IncreasingRingVolumePreference(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IncreasingRingVolumePreference(Context context, AttributeSet attrs,
            int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public IncreasingRingVolumePreference(Context context, AttributeSet attrs,
            int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        setLayoutResource(R.layout.preference_increasing_ring);
        initHandler();
    }

    public void setCallback(Callback callback) {
        mCallback = callback;
    }

    public void onActivityResume() {
        initHandler();
    }

    @Override
    public void onActivityStop() {
        if (mHandler != null) {
            postStopSample();
            mHandler.getLooper().quitSafely();
            mHandler = null;
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MSG_START_SAMPLE:
                onStartSample((float) msg.arg1 / 1000F);
                break;
            case MSG_STOP_SAMPLE:
                onStopSample();
                break;
            case MSG_INIT_SAMPLE:
                onInitSample();
                break;
            case MSG_SET_VOLUME:
                onSetVolume((float) msg.arg1 / 1000F);
                break;
        }
        return true;
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        getPreferenceManager().registerOnActivityStopListener(this);

        initHandler();

        final SeekBar seekBar = (SeekBar) view.findViewById(R.id.start_volume);
        if (seekBar == mStartVolumeSeekBar) return;

        mStartVolumeSeekBar = seekBar;
        mRampUpTimeSeekBar = (SeekBar) view.findViewById(R.id.ramp_up_time);
        mRampUpTimeValue = (TextView) view.findViewById(R.id.ramp_up_time_value);

        final ContentResolver cr = getContext().getContentResolver();
        float startVolume = CMSettings.System.getFloat(cr,
                CMSettings.System.INCREASING_RING_START_VOLUME, 0.1f);
        int rampUpTime = CMSettings.System.getInt(cr,
                CMSettings.System.INCREASING_RING_RAMP_UP_TIME, 10);

        mStartVolumeSeekBar.setProgress(Math.round(startVolume * 1000F));
        mStartVolumeSeekBar.setOnSeekBarChangeListener(this);
        mRampUpTimeSeekBar.setOnSeekBarChangeListener(this);
        mRampUpTimeSeekBar.setProgress((rampUpTime / 5) - 1);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        if (seekBar == mStartVolumeSeekBar) {
            postStartSample(seekBar.getProgress());
        }
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
        ContentResolver cr = getContext().getContentResolver();
        if (fromTouch && seekBar == mStartVolumeSeekBar) {
            CMSettings.System.putFloat(cr, CMSettings.System.INCREASING_RING_START_VOLUME,
                        (float) progress / 1000F);
        } else if (seekBar == mRampUpTimeSeekBar) {
            int seconds = (progress + 1) * 5;
            mRampUpTimeValue.setText(
                    Formatter.formatShortElapsedTime(getContext(), seconds * 1000));
            if (fromTouch) {
                CMSettings.System.putInt(cr,
                        CMSettings.System.INCREASING_RING_RAMP_UP_TIME, seconds);
            }
        }
    }

    private void initHandler() {
        if (mHandler != null) return;

        HandlerThread thread = new HandlerThread(TAG + ".CallbackHandler");
        thread.start();

        mHandler = new Handler(thread.getLooper(), this);
        mHandler.sendEmptyMessage(MSG_INIT_SAMPLE);
    }

    private void onInitSample() {
        mRingtone = RingtoneManager.getRingtone(getContext(),
                Settings.System.DEFAULT_RINGTONE_URI);
        if (mRingtone != null) {
            mRingtone.setStreamType(AudioManager.STREAM_RING);
            mRingtone.setAudioAttributes(
                    new AudioAttributes.Builder(mRingtone.getAudioAttributes())
                            .setFlags(AudioAttributes.FLAG_BYPASS_INTERRUPTION_POLICY |
                                    AudioAttributes.FLAG_BYPASS_MUTE)
                            .build());
        }
    }

    private void postStartSample(int progress) {
        boolean playing = isSamplePlaying();
        mHandler.removeMessages(MSG_START_SAMPLE);
        mHandler.removeMessages(MSG_SET_VOLUME);
        mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_START_SAMPLE, progress, 0),
                playing ? CHECK_RINGTONE_PLAYBACK_DELAY_MS : 0);
        if (playing) {
            mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_VOLUME, progress, 0));
        }
    }

    private void onStartSample(float volume) {
        if (mRingtone == null) {
            return;
        }
        if (!isSamplePlaying()) {
            if (mCallback != null) {
                mCallback.onStartingSample();
            }
            try {
                mRingtone.play();
            } catch (Throwable e) {
                Log.w(TAG, "Error playing ringtone", e);
            }
        }
        mRingtone.setVolume(volume);
    }

    private void onSetVolume(float volume) {
        if (mRingtone != null) {
            mRingtone.setVolume(volume);
        }
    }

    private boolean isSamplePlaying() {
        return mRingtone != null && mRingtone.isPlaying();
    }

    public void stopSample() {
        if (mHandler != null) {
            postStopSample();
        }
    }

    private void postStopSample() {
        // remove pending delayed start messages
        mHandler.removeMessages(MSG_START_SAMPLE);
        mHandler.removeMessages(MSG_STOP_SAMPLE);
        mHandler.sendEmptyMessage(MSG_STOP_SAMPLE);
    }

    private void onStopSample() {
        if (mRingtone != null) {
            mRingtone.stop();
        }
    }
}