1/*
2 * Copyright (C) 2008 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 */
16
17package com.android.fixvibratesetting;
18
19import android.app.Activity;
20import android.app.Notification;
21import android.app.NotificationManager;
22import android.app.PendingIntent;
23import android.content.Intent;
24import android.media.AudioManager;
25import android.util.Log;
26import android.view.View;
27import android.widget.TextView;
28import android.os.Bundle;
29
30public class FixVibrateSetting extends Activity implements View.OnClickListener
31{
32    AudioManager mAudioManager;
33    NotificationManager mNotificationManager;
34    TextView mCurrentSetting;
35    View mFix;
36    View mUnfix;
37    View mTest;
38
39    @Override
40    public void onCreate(Bundle icicle) {
41        super.onCreate(icicle);
42
43        setContentView(R.layout.fix_vibrate);
44
45        mAudioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
46        mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
47
48        mCurrentSetting = (TextView)findViewById(R.id.current_setting);
49
50        mFix = findViewById(R.id.fix);
51        mFix.setOnClickListener(this);
52
53        mUnfix = findViewById(R.id.unfix);
54        mUnfix.setOnClickListener(this);
55
56        mTest = findViewById(R.id.test);
57        mTest.setOnClickListener(this);
58    }
59
60    @Override
61    public void onResume() {
62        super.onResume();
63
64        update();
65    }
66
67    private String getSettingValue(int vibrateType) {
68        int setting = mAudioManager.getVibrateSetting(vibrateType);
69        switch (setting) {
70            case AudioManager.VIBRATE_SETTING_OFF:
71                return "off";
72            case AudioManager.VIBRATE_SETTING_ON:
73                return "on";
74            case AudioManager.VIBRATE_SETTING_ONLY_SILENT:
75                return "silent-only";
76            default:
77                return "unknown";
78        }
79    }
80
81    public void onClick(View v) {
82        if (v == mFix) {
83            fix();
84            update();
85        } else if (v == mUnfix) {
86            unfix();
87            update();
88        } else if (v == mTest) {
89            test();
90            update();
91        }
92    }
93
94    private void update() {
95        String ringer = getSettingValue(AudioManager.VIBRATE_TYPE_RINGER);
96        String notification = getSettingValue(AudioManager.VIBRATE_TYPE_NOTIFICATION);
97        String text = getString(R.string.current_setting, ringer, notification);
98        mCurrentSetting.setText(text);
99    }
100
101    private void fix() {
102        mAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION,
103                AudioManager.VIBRATE_SETTING_ON);
104    }
105
106    private void unfix() {
107        mAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION,
108                AudioManager.VIBRATE_SETTING_OFF);
109    }
110
111    private void test() {
112        Intent intent = new Intent(this, FixVibrateSetting.class);
113        PendingIntent pending = PendingIntent.getActivity(this, 0, intent, 0);
114
115        Notification n = new Notification.Builder(this)
116                .setSmallIcon(R.drawable.stat_sys_warning)
117                .setTicker("Test notification")
118                .setWhen(System.currentTimeMillis())
119                .setContentTitle("Test notification")
120                .setContentText("Test notification")
121                .setContentIntent(pending)
122                .setVibrate(new long[] { 0, 700, 500, 1000 })
123                .setAutoCancel(true)
124                .build();
125
126        mNotificationManager.notify(1, n);
127    }
128}
129
130