OSDN Git Service

03836c56534fd0aa7966ddda0fcfb7a805acf08e
[android-x86/packages-apps-CMFileManager.git] / src / com / cyanogenmod / filemanager / activities / preferences / EditorPreferenceFragment.java
1 /*
2  * Copyright (C) 2012 The CyanogenMod 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
17 package com.cyanogenmod.filemanager.activities.preferences;
18
19 import android.content.Context;
20 import android.content.Intent;
21 import android.os.Bundle;
22 import android.preference.CheckBoxPreference;
23 import android.preference.Preference;
24 import android.preference.Preference.OnPreferenceChangeListener;
25 import android.util.Log;
26
27 import com.cyanogenmod.filemanager.R;
28 import com.cyanogenmod.filemanager.preferences.FileManagerSettings;
29 import com.cyanogenmod.filemanager.preferences.Preferences;
30
31 /**
32  * A class that manages the editor options
33  */
34 public class EditorPreferenceFragment extends TitlePreferenceFragment {
35
36     private static final String TAG = "EditorPreferenceFragment"; //$NON-NLS-1$
37
38     private static final boolean DEBUG = false;
39
40     private CheckBoxPreference mNoSuggestions;
41     private CheckBoxPreference mWordWrap;
42     private CheckBoxPreference mHexdump;
43
44     private CheckBoxPreference mSyntaxHighlight;
45
46
47     /**
48      * @hide
49      */
50     boolean mLoaded = false;
51
52     private final OnPreferenceChangeListener mOnChangeListener =
53             new OnPreferenceChangeListener() {
54         @Override
55         public boolean onPreferenceChange(final Preference preference, Object newValue) {
56             boolean ret = true;
57
58             String key = preference.getKey();
59             if (DEBUG) {
60                 Log.d(TAG,
61                     String.format("New value for %s: %s",  //$NON-NLS-1$
62                             key,
63                             String.valueOf(newValue)));
64             }
65
66             // Notify the change (only if fragment is loaded. Default values are loaded
67             // while not in loaded mode)
68             if (EditorPreferenceFragment.this.mLoaded && ret) {
69                 Intent intent = new Intent(FileManagerSettings.INTENT_SETTING_CHANGED);
70                 intent.putExtra(
71                         FileManagerSettings.EXTRA_SETTING_CHANGED_KEY, preference.getKey());
72                 getActivity().sendBroadcast(intent);
73             }
74
75             return ret;
76         }
77     };
78
79     /**
80      * {@inheritDoc}
81      */
82     @Override
83     public void onCreate(Bundle savedInstanceState) {
84         super.onCreate(savedInstanceState);
85
86         // Change the preference manager
87         getPreferenceManager().setSharedPreferencesName(Preferences.SETTINGS_FILENAME);
88         getPreferenceManager().setSharedPreferencesMode(Context.MODE_PRIVATE);
89         this.mLoaded = false;
90
91         // Add the preferences
92         addPreferencesFromResource(R.xml.preferences_editor);
93
94         // No suggestions
95         this.mNoSuggestions =
96                 (CheckBoxPreference)findPreference(
97                         FileManagerSettings.SETTINGS_EDITOR_NO_SUGGESTIONS.getId());
98         this.mNoSuggestions.setOnPreferenceChangeListener(this.mOnChangeListener);
99
100         // WordWrap
101         this.mWordWrap =
102                 (CheckBoxPreference)findPreference(
103                         FileManagerSettings.SETTINGS_EDITOR_WORD_WRAP.getId());
104         this.mWordWrap.setOnPreferenceChangeListener(this.mOnChangeListener);
105
106         // Hexdump
107         this.mHexdump =
108                 (CheckBoxPreference)findPreference(
109                         FileManagerSettings.SETTINGS_EDITOR_HEXDUMP.getId());
110         this.mHexdump.setOnPreferenceChangeListener(this.mOnChangeListener);
111
112         // Syntax highlight
113         this.mSyntaxHighlight =
114                 (CheckBoxPreference)findPreference(
115                         FileManagerSettings.SETTINGS_EDITOR_SYNTAX_HIGHLIGHT.getId());
116         this.mSyntaxHighlight.setOnPreferenceChangeListener(this.mOnChangeListener);
117
118         // Loaded
119         this.mLoaded = true;
120     }
121
122     /**
123      * {@inheritDoc}
124      */
125     @Override
126     public CharSequence getTitle() {
127         return getString(R.string.pref_editor);
128     }
129 }