1 | import * as common from './application-settings-common';
|
2 | import { Application } from '../application';
|
3 | let sharedPreferences;
|
4 | function ensureSharedPreferences() {
|
5 | if (!sharedPreferences) {
|
6 | sharedPreferences = Application.android.getNativeApplication().getApplicationContext().getSharedPreferences('prefs.db', 0);
|
7 | }
|
8 | }
|
9 | function verify(key) {
|
10 | if (!common.checkKey(key)) {
|
11 | return false;
|
12 | }
|
13 | ensureSharedPreferences();
|
14 | return true;
|
15 | }
|
16 | export function hasKey(key) {
|
17 | if (!verify(key)) {
|
18 | return;
|
19 | }
|
20 | return sharedPreferences.contains(key);
|
21 | }
|
22 |
|
23 | export function getBoolean(key, defaultValue) {
|
24 | if (!verify(key)) {
|
25 | return;
|
26 | }
|
27 | if (hasKey(key)) {
|
28 | return sharedPreferences.getBoolean(key, false);
|
29 | }
|
30 | return defaultValue;
|
31 | }
|
32 | export function getString(key, defaultValue) {
|
33 | if (!verify(key)) {
|
34 | return;
|
35 | }
|
36 | if (hasKey(key)) {
|
37 | return sharedPreferences.getString(key, '');
|
38 | }
|
39 | return defaultValue;
|
40 | }
|
41 | export function getNumber(key, defaultValue) {
|
42 | if (!verify(key)) {
|
43 | return;
|
44 | }
|
45 | if (hasKey(key)) {
|
46 | let val;
|
47 |
|
48 | try {
|
49 | val = sharedPreferences.getLong(key, long(0));
|
50 | }
|
51 | catch (err) {
|
52 |
|
53 | const oldVal = sharedPreferences.getFloat(key, float(0.0));
|
54 | setNumber(key, oldVal);
|
55 | val = sharedPreferences.getLong(key, long(0));
|
56 | }
|
57 |
|
58 | return java.lang.Double.longBitsToDouble(val);
|
59 | }
|
60 | return defaultValue;
|
61 | }
|
62 |
|
63 | export function setBoolean(key, value) {
|
64 | if (!verify(key)) {
|
65 | return;
|
66 | }
|
67 | if (!common.ensureValidValue(value, 'boolean')) {
|
68 | return;
|
69 | }
|
70 | const editor = sharedPreferences.edit();
|
71 | editor.putBoolean(key, value);
|
72 | editor.apply();
|
73 | }
|
74 | export function setString(key, value) {
|
75 | if (!verify(key)) {
|
76 | return;
|
77 | }
|
78 | if (!common.ensureValidValue(value, 'string')) {
|
79 | return;
|
80 | }
|
81 | const editor = sharedPreferences.edit();
|
82 | editor.putString(key, value);
|
83 | editor.apply();
|
84 | }
|
85 | export function setNumber(key, value) {
|
86 | if (!verify(key)) {
|
87 | return;
|
88 | }
|
89 | if (!common.ensureValidValue(value, 'number')) {
|
90 | return;
|
91 | }
|
92 | const editor = sharedPreferences.edit();
|
93 |
|
94 | editor.putLong(key, java.lang.Double.doubleToRawLongBits(double(value)));
|
95 | editor.apply();
|
96 | }
|
97 | export function remove(key) {
|
98 | if (!verify(key)) {
|
99 | return;
|
100 | }
|
101 | const editor = sharedPreferences.edit();
|
102 | editor.remove(key);
|
103 | editor.apply();
|
104 | }
|
105 | export function clear() {
|
106 | ensureSharedPreferences();
|
107 | sharedPreferences.edit().clear().apply();
|
108 | }
|
109 | export function flush() {
|
110 | ensureSharedPreferences();
|
111 | return sharedPreferences.edit().commit();
|
112 | }
|
113 | export function getAllKeys() {
|
114 | ensureSharedPreferences();
|
115 | const mappedPreferences = sharedPreferences.getAll();
|
116 | const iterator = mappedPreferences.keySet().iterator();
|
117 | const result = [];
|
118 | while (iterator.hasNext()) {
|
119 | const key = iterator.next();
|
120 | result.push(key);
|
121 | }
|
122 | return result;
|
123 | }
|
124 |
|
\ | No newline at end of file |