Home | History | Annotate | Download | only in settings
      1 /*
      2  * Copyright (C) 2017 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 package com.example.android.autofill.service.settings;
     17 
     18 import android.content.Context;
     19 import android.content.SharedPreferences;
     20 import android.service.autofill.Dataset;
     21 import android.service.autofill.FillResponse;
     22 import android.support.annotation.NonNull;
     23 
     24 import com.example.android.autofill.service.util.Util;
     25 
     26 public class MyPreferences {
     27     private static final String RESPONSE_AUTH_KEY = "response_auth";
     28     private static final String DATASET_AUTH_KEY = "dataset_auth";
     29     private static final String MASTER_PASSWORD_KEY = "master_password";
     30     private static final String LOGGING_LEVEL = "logging_level";
     31     private static final String DAL_CHECK_REQUIRED = "dal_check_required";
     32     private static final String NUMBER_DATASETS = "number_datasets";
     33     private static MyPreferences sInstance;
     34     private final SharedPreferences mPrefs;
     35 
     36     private MyPreferences(Context context) {
     37         mPrefs = context.getApplicationContext().getSharedPreferences("my-settings",
     38                 Context.MODE_PRIVATE);
     39     }
     40 
     41     public static MyPreferences getInstance(Context context) {
     42         if (sInstance == null) {
     43             sInstance = new MyPreferences(context);
     44         }
     45         return sInstance;
     46     }
     47 
     48     /**
     49      * Gets whether {@link FillResponse}s should require authentication.
     50      */
     51     public boolean isResponseAuth() {
     52         return mPrefs.getBoolean(RESPONSE_AUTH_KEY, false);
     53     }
     54 
     55     /**
     56      * Enables/disables authentication for the entire autofill {@link FillResponse}.
     57      */
     58     public void setResponseAuth(boolean responseAuth) {
     59         mPrefs.edit().putBoolean(RESPONSE_AUTH_KEY, responseAuth).apply();
     60     }
     61 
     62     /**
     63      * Gets whether {@link Dataset}s should require authentication.
     64      */
     65     public boolean isDatasetAuth() {
     66         return mPrefs.getBoolean(DATASET_AUTH_KEY, false);
     67     }
     68 
     69     /**
     70      * Enables/disables authentication for individual autofill {@link Dataset}s.
     71      */
     72     public void setDatasetAuth(boolean datasetAuth) {
     73         mPrefs.edit().putBoolean(DATASET_AUTH_KEY, datasetAuth).apply();
     74     }
     75 
     76     /**
     77      * Gets autofill master username.
     78      */
     79     public String getMasterPassword() {
     80         return mPrefs.getString(MASTER_PASSWORD_KEY, null);
     81     }
     82 
     83     /**
     84      * Sets autofill master password.
     85      */
     86     public void setMasterPassword(@NonNull String masterPassword) {
     87         mPrefs.edit().putString(MASTER_PASSWORD_KEY, masterPassword).apply();
     88     }
     89 
     90     public void clearCredentials() {
     91         mPrefs.edit().remove(MASTER_PASSWORD_KEY).apply();
     92     }
     93 
     94     public Util.LogLevel getLoggingLevel() {
     95         return Util.LogLevel.values()[mPrefs.getInt(LOGGING_LEVEL, Util.LogLevel.Off.ordinal())];
     96     }
     97 
     98     public void setLoggingLevel(Util.LogLevel level) {
     99         mPrefs.edit().putInt(LOGGING_LEVEL, level.ordinal()).apply();
    100         Util.setLoggingLevel(level);
    101     }
    102 
    103     public Util.DalCheckRequirement getDalCheckRequirement() {
    104         return Util.DalCheckRequirement.values()[mPrefs.getInt(DAL_CHECK_REQUIRED,
    105                 Util.DalCheckRequirement.AllUrls.ordinal())];
    106     }
    107 
    108     public void setDalCheckRequired(Util.DalCheckRequirement level) {
    109         mPrefs.edit().putInt(DAL_CHECK_REQUIRED, level.ordinal()).apply();
    110     }
    111 
    112     public int getNumberDatasets(int defaultNumber) {
    113         return mPrefs.getInt(NUMBER_DATASETS, defaultNumber);
    114     }
    115 
    116     public void setNumberDatasets(int number) {
    117         mPrefs.edit().putInt(NUMBER_DATASETS, number).apply();
    118     }
    119 }
    120