TextKeyListener.java revision 47e6b1b5eef8ee99872f278f66bc498c4fcca0d8
19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2007 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpackage android.text.method;
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.ContentResolver;
209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.Context;
219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.database.ContentObserver;
229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.Handler;
239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.provider.Settings;
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.provider.Settings.System;
259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.text.*;
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.view.KeyCharacterMap;
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.view.KeyEvent;
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.view.View;
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.text.InputType;
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.lang.ref.WeakReference;
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/**
349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * This is the key listener for typing normal text.  It delegates to
359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * other key listeners appropriate to the current keyboard and language.
369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic class TextKeyListener extends BaseKeyListener implements SpanWatcher {
389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static TextKeyListener[] sInstance =
399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        new TextKeyListener[Capitalize.values().length * 2];
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* package */ static final Object ACTIVE = new NoCopySpan.Concrete();
429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* package */ static final Object CAPPED = new NoCopySpan.Concrete();
439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* package */ static final Object INHIBIT_REPLACEMENT = new NoCopySpan.Concrete();
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* package */ static final Object LAST_TYPED = new NoCopySpan.Concrete();
459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private Capitalize mAutoCap;
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private boolean mAutoText;
489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private int mPrefs;
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private boolean mPrefsInited;
519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* package */ static final int AUTO_CAP = 1;
539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* package */ static final int AUTO_TEXT = 2;
549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* package */ static final int AUTO_PERIOD = 4;
559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* package */ static final int SHOW_PASSWORD = 8;
569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private WeakReference<ContentResolver> mResolver;
579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private TextKeyListener.SettingsObserver mObserver;
589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Creates a new TextKeyListener with the specified capitalization
619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * and correction properties.
629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param cap when, if ever, to automatically capitalize.
649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param autotext whether to automatically do spelling corrections.
659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public TextKeyListener(Capitalize cap, boolean autotext) {
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mAutoCap = cap;
689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mAutoText = autotext;
699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns a new or existing instance with the specified capitalization
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * and correction properties.
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param cap when, if ever, to automatically capitalize.
769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param autotext whether to automatically do spelling corrections.
779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static TextKeyListener getInstance(boolean autotext,
799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                              Capitalize cap) {
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int off = cap.ordinal() * 2 + (autotext ? 1 : 0);
819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (sInstance[off] == null) {
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            sInstance[off] = new TextKeyListener(cap, autotext);
849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return sInstance[off];
879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns a new or existing instance with no automatic capitalization
919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * or correction.
929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static TextKeyListener getInstance() {
949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return getInstance(false, Capitalize.NONE);
959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns whether it makes sense to automatically capitalize at the
999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * specified position in the specified text, with the specified rules.
1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param cap the capitalization rules to consider.
1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param cs the text in which an insertion is being made.
1039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param off the offset into that text where the insertion is being made.
1049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
1059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return whether the character being inserted should be capitalized.
1069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static boolean shouldCap(Capitalize cap, CharSequence cs, int off) {
1089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int i;
1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        char c;
1109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (cap == Capitalize.NONE) {
1129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return false;
1139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (cap == Capitalize.CHARACTERS) {
1159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return true;
1169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return TextUtils.getCapsMode(cs, off, cap == Capitalize.WORDS
1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                ? TextUtils.CAP_MODE_WORDS : TextUtils.CAP_MODE_SENTENCES)
1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                != 0;
1219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int getInputType() {
1249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return makeTextContentType(mAutoCap, mAutoText);
1259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    @Override
1289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public boolean onKeyDown(View view, Editable content,
1299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             int keyCode, KeyEvent event) {
1309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        KeyListener im = getKeyListener(event);
1319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return im.onKeyDown(view, content, keyCode, event);
1339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    @Override
1369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public boolean onKeyUp(View view, Editable content,
1379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                           int keyCode, KeyEvent event) {
1389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        KeyListener im = getKeyListener(event);
1399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return im.onKeyUp(view, content, keyCode, event);
1419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    @Override
1449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public boolean onKeyOther(View view, Editable content, KeyEvent event) {
1459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        KeyListener im = getKeyListener(event);
1469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return im.onKeyOther(view, content, event);
1489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Clear all the input state (autotext, autocap, multitap, undo)
1529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * from the specified Editable, going beyond Editable.clear(), which
1539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * just clears the text but not the input state.
1549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
1559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param e the buffer whose text and state are to be cleared.
1569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static void clear(Editable e) {
1589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        e.clear();
1599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        e.removeSpan(ACTIVE);
1609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        e.removeSpan(CAPPED);
1619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        e.removeSpan(INHIBIT_REPLACEMENT);
1629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        e.removeSpan(LAST_TYPED);
1639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        QwertyKeyListener.Replaced[] repl = e.getSpans(0, e.length(),
1659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                   QwertyKeyListener.Replaced.class);
1669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        final int count = repl.length;
1679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        for (int i = 0; i < count; i++) {
1689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            e.removeSpan(repl[i]);
1699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void onSpanAdded(Spannable s, Object what, int start, int end) { }
1739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void onSpanRemoved(Spannable s, Object what, int start, int end) { }
1749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void onSpanChanged(Spannable s, Object what, int start, int end,
1769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                              int st, int en) {
1779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (what == Selection.SELECTION_END) {
1789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            s.removeSpan(ACTIVE);
1799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private KeyListener getKeyListener(KeyEvent event) {
1836b53e8daa69cba1a2a5a7c95a01e37ce9c53226cJeff Brown        KeyCharacterMap kmap = event.getKeyCharacterMap();
1849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int kind = kmap.getKeyboardType();
1859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (kind == KeyCharacterMap.ALPHA) {
1879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return QwertyKeyListener.getInstance(mAutoText, mAutoCap);
1889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else if (kind == KeyCharacterMap.NUMERIC) {
1899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return MultiTapKeyListener.getInstance(mAutoText, mAutoCap);
1906b53e8daa69cba1a2a5a7c95a01e37ce9c53226cJeff Brown        } else if (kind == KeyCharacterMap.FULL
1916b53e8daa69cba1a2a5a7c95a01e37ce9c53226cJeff Brown                || kind == KeyCharacterMap.SPECIAL_FUNCTION) {
19247e6b1b5eef8ee99872f278f66bc498c4fcca0d8Jeff Brown            // We consider special function keyboards full keyboards as a workaround for
19347e6b1b5eef8ee99872f278f66bc498c4fcca0d8Jeff Brown            // devices that do not have built-in keyboards.  Applications may try to inject
19447e6b1b5eef8ee99872f278f66bc498c4fcca0d8Jeff Brown            // key events using the built-in keyboard device id which may be configured as
19547e6b1b5eef8ee99872f278f66bc498c4fcca0d8Jeff Brown            // a special function keyboard using a default key map.  Ideally, as of Honeycomb,
19647e6b1b5eef8ee99872f278f66bc498c4fcca0d8Jeff Brown            // these applications should be modified to use KeyCharacterMap.VIRTUAL_KEYBOARD.
19747e6b1b5eef8ee99872f278f66bc498c4fcca0d8Jeff Brown            return QwertyKeyListener.getInstanceForFullKeyboard();
1989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return NullKeyListener.getInstance();
2019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public enum Capitalize {
2049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NONE, SENTENCES, WORDS, CHARACTERS,
2059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static class NullKeyListener implements KeyListener
2089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {
2099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public int getInputType() {
2109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return InputType.TYPE_NULL;
2119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public boolean onKeyDown(View view, Editable content,
2149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                 int keyCode, KeyEvent event) {
2159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return false;
2169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public boolean onKeyUp(View view, Editable content, int keyCode,
2199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                        KeyEvent event) {
2209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return false;
2219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public boolean onKeyOther(View view, Editable content, KeyEvent event) {
2249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return false;
2259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public void clearMetaKeyState(View view, Editable content, int states) {
2289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public static NullKeyListener getInstance() {
2319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (sInstance != null)
2329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return sInstance;
2339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            sInstance = new NullKeyListener();
2359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return sInstance;
2369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        private static NullKeyListener sInstance;
2399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void release() {
2429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mResolver != null) {
2439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            final ContentResolver contentResolver = mResolver.get();
2449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (contentResolver != null) {
2459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                contentResolver.unregisterContentObserver(mObserver);
2469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                mResolver.clear();
2479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
2489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mObserver = null;
2499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mResolver = null;
2509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mPrefsInited = false;
2519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void initPrefs(Context context) {
2559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        final ContentResolver contentResolver = context.getContentResolver();
2569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mResolver = new WeakReference<ContentResolver>(contentResolver);
257d35180cf52d8a84b5bd45a3f6428449bbc0a3283Gilles Debunne        if (mObserver == null) {
258d35180cf52d8a84b5bd45a3f6428449bbc0a3283Gilles Debunne            mObserver = new SettingsObserver();
259d35180cf52d8a84b5bd45a3f6428449bbc0a3283Gilles Debunne            contentResolver.registerContentObserver(Settings.System.CONTENT_URI, true, mObserver);
260d35180cf52d8a84b5bd45a3f6428449bbc0a3283Gilles Debunne        }
2619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        updatePrefs(contentResolver);
2639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mPrefsInited = true;
2649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private class SettingsObserver extends ContentObserver {
2679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public SettingsObserver() {
2689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            super(new Handler());
2699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        @Override
2729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public void onChange(boolean selfChange) {
2739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (mResolver != null) {
2749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                final ContentResolver contentResolver = mResolver.get();
2759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (contentResolver == null) {
2769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    mPrefsInited = false;
2779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } else {
2789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    updatePrefs(contentResolver);
2799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
2809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            } else {
2819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                mPrefsInited = false;
2829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
2839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void updatePrefs(ContentResolver resolver) {
2879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        boolean cap = System.getInt(resolver, System.TEXT_AUTO_CAPS, 1) > 0;
2889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        boolean text = System.getInt(resolver, System.TEXT_AUTO_REPLACE, 1) > 0;
2899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        boolean period = System.getInt(resolver, System.TEXT_AUTO_PUNCTUATE, 1) > 0;
2909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        boolean pw = System.getInt(resolver, System.TEXT_SHOW_PASSWORD, 1) > 0;
2919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mPrefs = (cap ? AUTO_CAP : 0) |
2939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                 (text ? AUTO_TEXT : 0) |
2949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                 (period ? AUTO_PERIOD : 0) |
2959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                 (pw ? SHOW_PASSWORD : 0);
2969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* package */ int getPrefs(Context context) {
2999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        synchronized (this) {
3009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (!mPrefsInited || mResolver.get() == null) {
3019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                initPrefs(context);
3029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
3039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return mPrefs;
3069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
308