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