summaryrefslogtreecommitdiff
path: root/src/com/android/im/app/SettingActivity.java
blob: 5e16c36d1178b051c882c34c6185f3161e782aa8 (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
/*
 * Copyright (C) 2007-2008 Esmertec AG.
 * Copyright (C) 2007-2008 The Android Open Source 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.im.app;

import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceScreen;
import android.preference.CheckBoxPreference;
import android.util.Log;

import com.android.im.R;
import com.android.im.provider.Imps;
import com.android.im.service.ImServiceConstants;

public class SettingActivity extends android.preference.PreferenceActivity {
    private static final String KEY_NOTIFICATION_SOUND = "notification-sound";
    private static final String KEY_NOTIFICATION_VIBRATE = "notification-vibrate";
    private static final String KEY_ENABLE_NOTIFICATIONS = "enable-notifications";
    private static final String KEY_HIDE_OFFLINE_CONTACTS = "hide-offline-contacts";

    private long mProviderId;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        addPreferencesFromResource(R.xml.preferences);
        Intent intent = getIntent();
        mProviderId = intent.getLongExtra(ImServiceConstants.EXTRA_INTENT_PROVIDER_ID, -1);
        if (mProviderId < 0) {
            Log.e(ImApp.LOG_TAG,"SettingActivity intent requires provider id extra");
            throw new RuntimeException("SettingActivity must be created with an provider id");
        }
        setInitialValues();
    }

    private void setInitialValues() {
        Imps.ProviderSettings.QueryMap settings = new Imps.ProviderSettings.QueryMap(
                getContentResolver(), mProviderId,
                false /* keep updated */, null /* no handler */);

        CheckBoxPreference pref = (CheckBoxPreference) findPreference(KEY_HIDE_OFFLINE_CONTACTS);
        pref.setChecked(settings.getHideOfflineContacts());

        pref = (CheckBoxPreference) findPreference(KEY_ENABLE_NOTIFICATIONS);
        pref.setChecked(settings.getEnableNotification());

        pref = (CheckBoxPreference) findPreference(KEY_NOTIFICATION_VIBRATE);
        pref.setChecked(settings.getVibrate());

        pref = (CheckBoxPreference) findPreference(KEY_NOTIFICATION_SOUND);
        pref.setChecked(settings.getRingtoneURI() != null);
        settings.close();
    }

    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
        if (preference instanceof CheckBoxPreference) {
            final Imps.ProviderSettings.QueryMap settings = new Imps.ProviderSettings.QueryMap(
                    getContentResolver(), mProviderId,
                    false /* keep updated */, null /* no handler */);
            String key = preference.getKey();
            boolean value = ((CheckBoxPreference) preference).isChecked();

            if (key.equals(KEY_HIDE_OFFLINE_CONTACTS)) {
                settings.setHideOfflineContacts(value);
            } else if (key.equals(KEY_ENABLE_NOTIFICATIONS)) {
                settings.setEnableNotification(value);
            } else if (key.equals(KEY_NOTIFICATION_VIBRATE)) {
                settings.setVibrate(value);
            } else if (key.equals(KEY_NOTIFICATION_SOUND)){
                if (!value) {
                    settings.setRingtoneURI(null);
                }
            }
            settings.close();
            return true;
        }
        
        return false;
    }
}