aboutsummaryrefslogtreecommitdiff
path: root/input/autofill/AutofillFramework/afservice/src/main/java/com/example/android/autofill/service/settings/MyPreferences.java
blob: f006b2db28c9ace0b0b6e7184dae662aa2ad339b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.android.autofill.service.settings;

import android.content.Context;
import android.content.SharedPreferences;
import android.service.autofill.Dataset;
import android.service.autofill.FillResponse;
import android.support.annotation.NonNull;

import com.example.android.autofill.service.util.Util;

public class MyPreferences {
    private static final String RESPONSE_AUTH_KEY = "response_auth";
    private static final String DATASET_AUTH_KEY = "dataset_auth";
    private static final String MAIN_PASSWORD_KEY = "main_password";
    private static final String LOGGING_LEVEL = "logging_level";
    private static final String DAL_CHECK_REQUIRED = "dal_check_required";
    private static final String NUMBER_DATASETS = "number_datasets";
    private static MyPreferences sInstance;
    private final SharedPreferences mPrefs;

    private MyPreferences(Context context) {
        mPrefs = context.getApplicationContext().getSharedPreferences("my-settings",
                Context.MODE_PRIVATE);
    }

    public static MyPreferences getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new MyPreferences(context);
        }
        return sInstance;
    }

    /**
     * Gets whether {@link FillResponse}s should require authentication.
     */
    public boolean isResponseAuth() {
        return mPrefs.getBoolean(RESPONSE_AUTH_KEY, false);
    }

    /**
     * Enables/disables authentication for the entire autofill {@link FillResponse}.
     */
    public void setResponseAuth(boolean responseAuth) {
        mPrefs.edit().putBoolean(RESPONSE_AUTH_KEY, responseAuth).apply();
    }

    /**
     * Gets whether {@link Dataset}s should require authentication.
     */
    public boolean isDatasetAuth() {
        return mPrefs.getBoolean(DATASET_AUTH_KEY, false);
    }

    /**
     * Enables/disables authentication for individual autofill {@link Dataset}s.
     */
    public void setDatasetAuth(boolean datasetAuth) {
        mPrefs.edit().putBoolean(DATASET_AUTH_KEY, datasetAuth).apply();
    }

    /**
     * Gets autofill main username.
     */
    public String getMainPassword() {
        return mPrefs.getString(MAIN_PASSWORD_KEY, null);
    }

    /**
     * Sets autofill main password.
     */
    public void setMainPassword(@NonNull String mainPassword) {
        mPrefs.edit().putString(MAIN_PASSWORD_KEY, mainPassword).apply();
    }

    public void clearCredentials() {
        mPrefs.edit().remove(MAIN_PASSWORD_KEY).apply();
    }

    public Util.LogLevel getLoggingLevel() {
        return Util.LogLevel.values()[mPrefs.getInt(LOGGING_LEVEL, Util.LogLevel.Off.ordinal())];
    }

    public void setLoggingLevel(Util.LogLevel level) {
        mPrefs.edit().putInt(LOGGING_LEVEL, level.ordinal()).apply();
        Util.setLoggingLevel(level);
    }

    public Util.DalCheckRequirement getDalCheckRequirement() {
        return Util.DalCheckRequirement.values()[mPrefs.getInt(DAL_CHECK_REQUIRED,
                Util.DalCheckRequirement.AllUrls.ordinal())];
    }

    public void setDalCheckRequired(Util.DalCheckRequirement level) {
        mPrefs.edit().putInt(DAL_CHECK_REQUIRED, level.ordinal()).apply();
    }

    public int getNumberDatasets(int defaultNumber) {
        return mPrefs.getInt(NUMBER_DATASETS, defaultNumber);
    }

    public void setNumberDatasets(int number) {
        mPrefs.edit().putInt(NUMBER_DATASETS, number).apply();
    }
}