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 MyPreferences sInstance; 33 private final SharedPreferences mPrefs; 34 35 private MyPreferences(Context context) { 36 mPrefs = context.getApplicationContext().getSharedPreferences("my-settings", 37 Context.MODE_PRIVATE); 38 } 39 40 public static MyPreferences getInstance(Context context) { 41 if (sInstance == null) { 42 sInstance = new MyPreferences(context); 43 } 44 return sInstance; 45 } 46 47 /** 48 * Gets whether {@link FillResponse}s should require authentication. 49 */ 50 public boolean isResponseAuth() { 51 return mPrefs.getBoolean(RESPONSE_AUTH_KEY, false); 52 } 53 54 /** 55 * Enables/disables authentication for the entire autofill {@link FillResponse}. 56 */ 57 public void setResponseAuth(boolean responseAuth) { 58 mPrefs.edit().putBoolean(RESPONSE_AUTH_KEY, responseAuth).apply(); 59 } 60 61 /** 62 * Gets whether {@link Dataset}s should require authentication. 63 */ 64 public boolean isDatasetAuth() { 65 return mPrefs.getBoolean(DATASET_AUTH_KEY, false); 66 } 67 68 /** 69 * Enables/disables authentication for individual autofill {@link Dataset}s. 70 */ 71 public void setDatasetAuth(boolean datasetAuth) { 72 mPrefs.edit().putBoolean(DATASET_AUTH_KEY, datasetAuth).apply(); 73 } 74 75 /** 76 * Gets autofill master username. 77 */ 78 public String getMasterPassword() { 79 return mPrefs.getString(MASTER_PASSWORD_KEY, null); 80 } 81 82 /** 83 * Sets autofill master password. 84 */ 85 public void setMasterPassword(@NonNull String masterPassword) { 86 mPrefs.edit().putString(MASTER_PASSWORD_KEY, masterPassword).apply(); 87 } 88 89 public void clearCredentials() { 90 mPrefs.edit().remove(MASTER_PASSWORD_KEY).apply(); 91 } 92 93 public Util.LogLevel getLoggingLevel() { 94 return Util.LogLevel.values()[mPrefs.getInt(LOGGING_LEVEL, Util.LogLevel.Off.ordinal())]; 95 } 96 97 public void setLoggingLevel(Util.LogLevel level) { 98 mPrefs.edit().putInt(LOGGING_LEVEL, level.ordinal()).apply(); 99 Util.setLoggingLevel(level); 100 } 101 102 public Util.DalCheckRequirement getDalCheckRequirement() { 103 return Util.DalCheckRequirement.values()[mPrefs.getInt(DAL_CHECK_REQUIRED, 104 Util.DalCheckRequirement.AllUrls.ordinal())]; 105 } 106 107 public void setDalCheckRequired(Util.DalCheckRequirement level) { 108 mPrefs.edit().putInt(DAL_CHECK_REQUIRED, level.ordinal()).apply(); 109 } 110 } 111