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.autofillframework.multidatasetservice.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 public class MyPreferences { 25 private static final String RESPONSE_AUTH_KEY = "response_auth"; 26 private static final String DATASET_AUTH_KEY = "dataset_auth"; 27 private static final String MASTER_PASSWORD_KEY = "master_password"; 28 private static MyPreferences sInstance; 29 private final SharedPreferences mPrefs; 30 31 private MyPreferences(Context context) { 32 mPrefs = context.getApplicationContext().getSharedPreferences("my-settings", 33 Context.MODE_PRIVATE); 34 } 35 36 public static MyPreferences getInstance(Context context) { 37 if (sInstance == null) { 38 sInstance = new MyPreferences(context); 39 } 40 return sInstance; 41 } 42 43 /** 44 * Gets whether {@link FillResponse}s should require authentication. 45 */ 46 public boolean isResponseAuth() { 47 return mPrefs.getBoolean(RESPONSE_AUTH_KEY, false); 48 } 49 50 /** 51 * Enables/disables authentication for the entire autofill {@link FillResponse}. 52 */ 53 public void setResponseAuth(boolean responseAuth) { 54 mPrefs.edit().putBoolean(RESPONSE_AUTH_KEY, responseAuth).apply(); 55 } 56 57 /** 58 * Gets whether {@link Dataset}s should require authentication. 59 */ 60 public boolean isDatasetAuth() { 61 return mPrefs.getBoolean(DATASET_AUTH_KEY, false); 62 } 63 64 /** 65 * Enables/disables authentication for individual autofill {@link Dataset}s. 66 */ 67 public void setDatasetAuth(boolean datasetAuth) { 68 mPrefs.edit().putBoolean(DATASET_AUTH_KEY, datasetAuth).apply(); 69 } 70 71 /** 72 * Gets autofill master username. 73 */ 74 public String getMasterPassword() { 75 return mPrefs.getString(MASTER_PASSWORD_KEY, null); 76 } 77 78 /** 79 * Sets autofill master password. 80 */ 81 public void setMasterPassword(@NonNull String masterPassword) { 82 mPrefs.edit().putString(MASTER_PASSWORD_KEY, masterPassword).apply(); 83 } 84 85 public void clearCredentials() { 86 mPrefs.edit().remove(MASTER_PASSWORD_KEY).apply(); 87 } 88 } 89