Home | History | Annotate | Download | only in pinyin
      1 /*
      2  * Copyright (C) 2009 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 
     17 package com.android.inputmethod.pinyin;
     18 
     19 import android.content.SharedPreferences;
     20 import android.content.SharedPreferences.Editor;
     21 
     22 /**
     23  * Class used to maintain settings.
     24  */
     25 public class Settings {
     26     private static final String ANDPY_CONFS_KEYSOUND_KEY = "Sound";
     27     private static final String ANDPY_CONFS_VIBRATE_KEY = "Vibrate";
     28     private static final String ANDPY_CONFS_PREDICTION_KEY = "Prediction";
     29 
     30     private static boolean mKeySound;
     31     private static boolean mVibrate;
     32     private static boolean mPrediction;
     33 
     34     private static Settings mInstance = null;
     35 
     36     private static int mRefCount = 0;
     37 
     38     private static SharedPreferences mSharedPref = null;
     39 
     40     protected Settings(SharedPreferences pref) {
     41         mSharedPref = pref;
     42         initConfs();
     43     }
     44 
     45     public static Settings getInstance(SharedPreferences pref) {
     46         if (mInstance == null) {
     47             mInstance = new Settings(pref);
     48         }
     49         assert (pref == mSharedPref);
     50         mRefCount++;
     51         return mInstance;
     52     }
     53 
     54     public static void writeBack() {
     55         Editor editor = mSharedPref.edit();
     56         editor.putBoolean(ANDPY_CONFS_VIBRATE_KEY, mVibrate);
     57         editor.putBoolean(ANDPY_CONFS_KEYSOUND_KEY, mKeySound);
     58         editor.putBoolean(ANDPY_CONFS_PREDICTION_KEY, mPrediction);
     59         editor.commit();
     60     }
     61 
     62     public static void releaseInstance() {
     63         mRefCount--;
     64         if (mRefCount == 0) {
     65             mInstance = null;
     66         }
     67     }
     68 
     69     private void initConfs() {
     70         mKeySound = mSharedPref.getBoolean(ANDPY_CONFS_KEYSOUND_KEY, true);
     71         mVibrate = mSharedPref.getBoolean(ANDPY_CONFS_VIBRATE_KEY, false);
     72         mPrediction = mSharedPref.getBoolean(ANDPY_CONFS_PREDICTION_KEY, true);
     73     }
     74 
     75     public static boolean getKeySound() {
     76         return mKeySound;
     77     }
     78 
     79     public static void setKeySound(boolean v) {
     80         if (mKeySound == v) return;
     81         mKeySound = v;
     82     }
     83 
     84     public static boolean getVibrate() {
     85         return mVibrate;
     86     }
     87 
     88     public static void setVibrate(boolean v) {
     89         if (mVibrate == v) return;
     90         mVibrate = v;
     91     }
     92 
     93     public static boolean getPrediction() {
     94         return mPrediction;
     95     }
     96 
     97     public static void setPrediction(boolean v) {
     98         if (mPrediction == v) return;
     99         mPrediction = v;
    100     }
    101 }
    102