1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 package com.example.executorchllamademo;
10 
11 import android.content.Context;
12 import android.content.SharedPreferences;
13 import com.google.gson.Gson;
14 import com.google.gson.reflect.TypeToken;
15 import java.lang.reflect.Type;
16 import java.util.ArrayList;
17 
18 public class DemoSharedPreferences {
19   Context context;
20   SharedPreferences sharedPreferences;
21 
DemoSharedPreferences(Context context)22   public DemoSharedPreferences(Context context) {
23     this.context = context;
24     this.sharedPreferences = getSharedPrefs();
25   }
26 
getSharedPrefs()27   private SharedPreferences getSharedPrefs() {
28     return context.getSharedPreferences(
29         context.getString(R.string.demo_pref_file_key), Context.MODE_PRIVATE);
30   }
31 
getSavedMessages()32   public String getSavedMessages() {
33     return sharedPreferences.getString(context.getString(R.string.saved_messages_json_key), "");
34   }
35 
addMessages(MessageAdapter messageAdapter)36   public void addMessages(MessageAdapter messageAdapter) {
37     SharedPreferences.Editor editor = sharedPreferences.edit();
38     Gson gson = new Gson();
39     String msgJSON = gson.toJson(messageAdapter.getSavedMessages());
40     editor.putString(context.getString(R.string.saved_messages_json_key), msgJSON);
41     editor.apply();
42   }
43 
removeExistingMessages()44   public void removeExistingMessages() {
45     SharedPreferences.Editor editor = sharedPreferences.edit();
46     editor.remove(context.getString(R.string.saved_messages_json_key));
47     editor.apply();
48   }
49 
addSettings(SettingsFields settingsFields)50   public void addSettings(SettingsFields settingsFields) {
51     SharedPreferences.Editor editor = sharedPreferences.edit();
52     Gson gson = new Gson();
53     String settingsJSON = gson.toJson(settingsFields);
54     editor.putString(context.getString(R.string.settings_json_key), settingsJSON);
55     editor.apply();
56   }
57 
getSettings()58   public String getSettings() {
59     return sharedPreferences.getString(context.getString(R.string.settings_json_key), "");
60   }
61 
saveLogs()62   public void saveLogs() {
63     SharedPreferences.Editor editor = sharedPreferences.edit();
64     Gson gson = new Gson();
65     String msgJSON = gson.toJson(ETLogging.getInstance().getLogs());
66     editor.putString(context.getString(R.string.logs_json_key), msgJSON);
67     editor.apply();
68   }
69 
removeExistingLogs()70   public void removeExistingLogs() {
71     SharedPreferences.Editor editor = sharedPreferences.edit();
72     editor.remove(context.getString(R.string.logs_json_key));
73     editor.apply();
74   }
75 
getSavedLogs()76   public ArrayList<AppLog> getSavedLogs() {
77     String logsJSONString =
78         sharedPreferences.getString(context.getString(R.string.logs_json_key), null);
79     if (logsJSONString == null || logsJSONString.isEmpty()) {
80       return new ArrayList<>();
81     }
82     Gson gson = new Gson();
83     Type type = new TypeToken<ArrayList<AppLog>>() {}.getType();
84     ArrayList<AppLog> appLogs = gson.fromJson(logsJSONString, type);
85     if (appLogs == null) {
86       return new ArrayList<>();
87     }
88     return appLogs;
89   }
90 }
91