1 /*
2  * Copyright (C) 2023 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 
17 package com.android.settings.localepicker;
18 
19 import android.content.Context;
20 import android.content.SharedPreferences;
21 
22 import androidx.annotation.NonNull;
23 import androidx.annotation.VisibleForTesting;
24 
25 import com.android.settingslib.datastore.SharedPreferencesStorage;
26 
27 import com.google.gson.Gson;
28 
29 import java.util.HashMap;
30 import java.util.Map;
31 
32 /**
33  * A data manager that manages the {@link SharedPreferences} for the locale notification
34  * information.
35  */
36 public class LocaleNotificationDataManager {
37     public static final String LOCALE_NOTIFICATION = "locale_notification";
38     private Context mContext;
39 
40     /**
41      * Constructor
42      *
43      * @param context The context
44      */
LocaleNotificationDataManager(Context context)45     public LocaleNotificationDataManager(Context context) {
46         this.mContext = context;
47     }
48 
49     /** Returns the underlying {@link SharedPreferences} storage. */
50     @NonNull
getSharedPreferencesStorage(@onNull Context context)51     public static SharedPreferencesStorage getSharedPreferencesStorage(@NonNull Context context) {
52         return new SharedPreferencesStorage(context, LOCALE_NOTIFICATION, Context.MODE_PRIVATE);
53     }
54 
getSharedPreferences(Context context)55     private static SharedPreferences getSharedPreferences(Context context) {
56         return context.getSharedPreferences(LOCALE_NOTIFICATION, Context.MODE_PRIVATE);
57     }
58 
59     /**
60      * Adds one entry with the corresponding locale and {@link NotificationInfo} to the
61      * {@link SharedPreferences}.
62      *
63      * @param locale A locale which the application sets to
64      * @param info   The notification metadata
65      */
putNotificationInfo(String locale, NotificationInfo info)66     public void putNotificationInfo(String locale, NotificationInfo info) {
67         Gson gson = new Gson();
68         String json = gson.toJson(info);
69         SharedPreferences.Editor editor = getSharedPreferences(mContext).edit();
70         editor.putString(locale, json);
71         editor.apply();
72     }
73 
74     /**
75      * Removes one entry with the corresponding locale from the {@link SharedPreferences}.
76      *
77      * @param locale A locale which the application sets to
78      */
removeNotificationInfo(String locale)79     public void removeNotificationInfo(String locale) {
80         SharedPreferences.Editor editor = getSharedPreferences(mContext).edit();
81         editor.remove(locale);
82         editor.apply();
83     }
84 
85     /**
86      * Gets the {@link NotificationInfo} with the associated locale from the
87      * {@link SharedPreferences}.
88      *
89      * @param locale A locale which the application sets to
90      * @return {@link NotificationInfo}
91      */
getNotificationInfo(String locale)92     public NotificationInfo getNotificationInfo(String locale) {
93         Gson gson = new Gson();
94         String json = getSharedPreferences(mContext).getString(locale, "");
95         return json.isEmpty() ? null : gson.fromJson(json, NotificationInfo.class);
96     }
97 
98     /**
99      * Gets the locale notification map.
100      *
101      * @return A map which maps the locale to the corresponding {@link NotificationInfo}
102      */
getLocaleNotificationInfoMap()103     public Map<String, NotificationInfo> getLocaleNotificationInfoMap() {
104         Gson gson = new Gson();
105         Map<String, String> map = (Map<String, String>) getSharedPreferences(mContext).getAll();
106         Map<String, NotificationInfo> result = new HashMap<>(map.size());
107         map.forEach((key, value) -> {
108             result.put(key, gson.fromJson(value, NotificationInfo.class));
109         });
110         return result;
111     }
112 
113     /**
114      * Clears the locale notification map.
115      */
116     @VisibleForTesting
clearLocaleNotificationMap()117     void clearLocaleNotificationMap() {
118         getSharedPreferences(mContext).edit().clear().apply();
119     }
120 }
121