|
@@ -0,0 +1,208 @@
|
|
1
|
+package com.xhly.manageapp.contentprovider;
|
|
2
|
+
|
|
3
|
+import android.content.ContentProvider;
|
|
4
|
+import android.content.ContentValues;
|
|
5
|
+import android.content.Context;
|
|
6
|
+import android.content.SharedPreferences;
|
|
7
|
+import android.database.Cursor;
|
|
8
|
+import android.database.MatrixCursor;
|
|
9
|
+import android.net.Uri;
|
|
10
|
+import android.text.TextUtils;
|
|
11
|
+
|
|
12
|
+import androidx.annotation.NonNull;
|
|
13
|
+import androidx.annotation.Nullable;
|
|
14
|
+
|
|
15
|
+import java.util.Iterator;
|
|
16
|
+import java.util.Objects;
|
|
17
|
+
|
|
18
|
+public class ZSpProvider extends ContentProvider {
|
|
19
|
+ private static final String AUTHORITY = "com.xhkjedu.mc.sharedpreferencesprovider";
|
|
20
|
+ public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/spData");
|
|
21
|
+
|
|
22
|
+ public static final String PARAM_KEY = "key";
|
|
23
|
+
|
|
24
|
+ public static final String PARAM_VALUE = "value";
|
|
25
|
+
|
|
26
|
+ private SharedPreferences mStore;
|
|
27
|
+
|
|
28
|
+ public static Cursor query(Context context, String... keys) {
|
|
29
|
+ return context.getContentResolver().query(CONTENT_URI, keys, null, null, null);
|
|
30
|
+ }
|
|
31
|
+
|
|
32
|
+ public static String getString(Context context, String key) {
|
|
33
|
+ return getString(context, key, null);
|
|
34
|
+ }
|
|
35
|
+
|
|
36
|
+ public static String getString(Context context, String key, String defValue) {
|
|
37
|
+ Cursor cursor = query(context, key);
|
|
38
|
+ String ret = defValue;
|
|
39
|
+ if (cursor.moveToNext()) {
|
|
40
|
+ ret = cursor.getString(0);
|
|
41
|
+ if (TextUtils.isEmpty(ret)) {
|
|
42
|
+ ret = defValue;
|
|
43
|
+ }
|
|
44
|
+ }
|
|
45
|
+ cursor.close();
|
|
46
|
+ return ret;
|
|
47
|
+ }
|
|
48
|
+
|
|
49
|
+ public static int getInt(Context context, String key, int defValue) {
|
|
50
|
+ Cursor cursor = query(context, key);
|
|
51
|
+ int ret = defValue;
|
|
52
|
+ if (cursor.moveToNext()) {
|
|
53
|
+ try {
|
|
54
|
+ ret = cursor.getInt(0);
|
|
55
|
+ } catch (Exception ignored) {
|
|
56
|
+
|
|
57
|
+ }
|
|
58
|
+ }
|
|
59
|
+ cursor.close();
|
|
60
|
+ return ret;
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+ public static Uri save(Context context, ContentValues values) {
|
|
64
|
+ return context.getContentResolver().insert(CONTENT_URI, values);
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+ public static Uri save(Context context, String key, String value) {
|
|
68
|
+ ContentValues values = new ContentValues(1);
|
|
69
|
+ values.put(key, value);
|
|
70
|
+ return save(context, values);
|
|
71
|
+ }
|
|
72
|
+
|
|
73
|
+ public static Uri save(Context context, String key, int value) {
|
|
74
|
+ ContentValues values = new ContentValues(1);
|
|
75
|
+ values.put(key, value);
|
|
76
|
+ return save(context, values);
|
|
77
|
+ }
|
|
78
|
+
|
|
79
|
+ public static Uri remove(Context context, String key) {
|
|
80
|
+ return save(context, key, null);
|
|
81
|
+ }
|
|
82
|
+
|
|
83
|
+ private Cursor createSingleCursor(String key, String value) {
|
|
84
|
+ MatrixCursor cursor = new MatrixCursor(new String[]{key}, 1);
|
|
85
|
+ if (!TextUtils.isEmpty(value)) {
|
|
86
|
+ cursor.addRow(new Object[]{value});
|
|
87
|
+ }
|
|
88
|
+ return cursor;
|
|
89
|
+ }
|
|
90
|
+
|
|
91
|
+ private Cursor createCursor(String[] keys, String[] values) {
|
|
92
|
+ MatrixCursor cursor = new MatrixCursor(keys, 1);
|
|
93
|
+ cursor.addRow(values);
|
|
94
|
+ return cursor;
|
|
95
|
+ }
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+ private String getValue(String key, String defValue) {
|
|
99
|
+ return mStore.getString(key, defValue);
|
|
100
|
+ }
|
|
101
|
+
|
|
102
|
+ private void save(ContentValues values) {
|
|
103
|
+ String key;
|
|
104
|
+ String value;
|
|
105
|
+ Iterator<String> iterator = values.keySet().iterator();
|
|
106
|
+ SharedPreferences.Editor editor = mStore.edit();
|
|
107
|
+ while (iterator.hasNext()) {
|
|
108
|
+ key = iterator.next();
|
|
109
|
+ value = values.getAsString(key);
|
|
110
|
+ if (!TextUtils.isEmpty(key)) {
|
|
111
|
+ if (value != null) {
|
|
112
|
+ editor.putString(key, value);
|
|
113
|
+ } else {
|
|
114
|
+ editor.remove(key);
|
|
115
|
+ }
|
|
116
|
+ }
|
|
117
|
+ }
|
|
118
|
+ editor.apply();
|
|
119
|
+ }
|
|
120
|
+
|
|
121
|
+ private void save(String key, String value) {
|
|
122
|
+ SharedPreferences.Editor editor = mStore.edit();
|
|
123
|
+ if (value != null) {
|
|
124
|
+ editor.putString(key, value);
|
|
125
|
+ } else {
|
|
126
|
+ editor.remove(key);
|
|
127
|
+ }
|
|
128
|
+ editor.apply();
|
|
129
|
+ }
|
|
130
|
+
|
|
131
|
+ private void remove(String key) {
|
|
132
|
+ SharedPreferences.Editor editor = mStore.edit();
|
|
133
|
+ editor.remove(key);
|
|
134
|
+ editor.apply();
|
|
135
|
+ }
|
|
136
|
+
|
|
137
|
+ @Override
|
|
138
|
+ public boolean onCreate() {
|
|
139
|
+ String DB_NAME = "SPData";
|
|
140
|
+ mStore = Objects.requireNonNull(getContext()).getSharedPreferences(DB_NAME, Context.MODE_PRIVATE);
|
|
141
|
+ return true;
|
|
142
|
+ }
|
|
143
|
+
|
|
144
|
+ @Nullable
|
|
145
|
+ @Override
|
|
146
|
+ public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
|
|
147
|
+ int size = projection == null ? 0 : projection.length;
|
|
148
|
+ if (size > 0) {
|
|
149
|
+ String[] values = new String[size];
|
|
150
|
+ for (int i = 0; i < size; i++) {
|
|
151
|
+ values[i] = getValue(projection[i], null);
|
|
152
|
+ }
|
|
153
|
+ return createCursor(projection, values);
|
|
154
|
+ }
|
|
155
|
+ String key = uri.getQueryParameter(PARAM_KEY);
|
|
156
|
+ String value = null;
|
|
157
|
+ if (!TextUtils.isEmpty(key)) {
|
|
158
|
+ value = getValue(key, null);
|
|
159
|
+ }
|
|
160
|
+ return createSingleCursor(key, value);
|
|
161
|
+ }
|
|
162
|
+
|
|
163
|
+ @Nullable
|
|
164
|
+ @Override
|
|
165
|
+ public String getType(@NonNull Uri uri) {
|
|
166
|
+ return "";
|
|
167
|
+ }
|
|
168
|
+
|
|
169
|
+ @Nullable
|
|
170
|
+ @Override
|
|
171
|
+ public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
|
|
172
|
+ if (values != null && values.size() > 0) {
|
|
173
|
+ save(values);
|
|
174
|
+ } else {
|
|
175
|
+ String key = uri.getQueryParameter(PARAM_KEY);
|
|
176
|
+ String value = uri.getQueryParameter(PARAM_VALUE);
|
|
177
|
+ if (!TextUtils.isEmpty(key)) {
|
|
178
|
+ save(key, value);
|
|
179
|
+ }
|
|
180
|
+ }
|
|
181
|
+ return null;
|
|
182
|
+ }
|
|
183
|
+
|
|
184
|
+ @Override
|
|
185
|
+ public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
|
|
186
|
+ String key = selection != null ? selection : uri.getQueryParameter(PARAM_KEY);
|
|
187
|
+ if (!TextUtils.isEmpty(key)) {
|
|
188
|
+ remove(key);
|
|
189
|
+ return 1;
|
|
190
|
+ }
|
|
191
|
+ return 0;
|
|
192
|
+ }
|
|
193
|
+
|
|
194
|
+ @Override
|
|
195
|
+ public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
|
|
196
|
+ if (values != null && values.size() > 0) {
|
|
197
|
+ save(values);
|
|
198
|
+ return values.size();
|
|
199
|
+ }
|
|
200
|
+ String key = uri.getQueryParameter(PARAM_KEY);
|
|
201
|
+ String value = uri.getQueryParameter(PARAM_VALUE);
|
|
202
|
+ if (!TextUtils.isEmpty(key)) {
|
|
203
|
+ save(key, value);
|
|
204
|
+ return 1;
|
|
205
|
+ }
|
|
206
|
+ return 0;
|
|
207
|
+ }
|
|
208
|
+}
|