Ver código fonte

1.添加FileProvider提供user参数。

master
wangwanlei 11 meses atrás
pai
commit
ccbe7081ee

+ 13
- 1
app/src/main/AndroidManifest.xml Ver arquivo

@@ -25,7 +25,9 @@
25 25
     <uses-permission android:name="android.permission.BLUETOOTH" />
26 26
     <uses-permission android:name="android.permission.LOCAL_MAC_ADDRESS" />
27 27
     <uses-permission android:name="android.permission.READ_PHONE_STATE" />
28
-
28
+    <!--位置权限-->
29
+    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
30
+    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
29 31
 
30 32
     <application
31 33
         android:name=".ManageApplication"
@@ -157,6 +159,16 @@
157 159
                 android:name="android.support.FILE_PROVIDER_PATHS"
158 160
                 android:resource="@xml/file_paths" />
159 161
         </provider>
162
+        <!--<provider
163
+            android:name="com.xhly.manageapp.contentprovider.UserProvider"
164
+            android:authorities="com.xhly.manageapp.userprovider"
165
+            android:enabled="true"
166
+            android:exported="true" />-->
167
+        <provider
168
+            android:name="com.xhly.manageapp.contentprovider.ZSpProvider"
169
+            android:authorities="com.xhkjedu.mc.sharedpreferencesprovider"
170
+            android:enabled="true"
171
+            android:exported="true" />
160 172
     </application>
161 173
 
162 174
 </manifest>

+ 65
- 0
app/src/main/java/com/xhly/manageapp/contentprovider/UserProvider.kt Ver arquivo

@@ -0,0 +1,65 @@
1
+package com.xhly.manageapp.contentprovider
2
+
3
+import android.content.ContentProvider
4
+import android.content.ContentValues
5
+import android.content.UriMatcher
6
+import android.database.Cursor
7
+import android.database.MatrixCursor
8
+import android.net.Uri
9
+import com.xhly.corelib.Const
10
+import com.xhly.corelib.utils.SharedPreferencesUtils
11
+import com.xhly.manageapp.bean.user.UserBean
12
+
13
+
14
+class UserProvider : ContentProvider() {
15
+    private var sputils: SharedPreferencesUtils? = null
16
+    var matcher = UriMatcher(UriMatcher.NO_MATCH)
17
+    var str="com.xhly.manageapp.userprovider"
18
+    override fun onCreate(): Boolean {
19
+        sputils = SharedPreferencesUtils.getInstance(context)
20
+        matcher.addURI(str,"user",999)
21
+        return true
22
+    }
23
+
24
+    override fun query(
25
+        uri: Uri,
26
+        projection: Array<out String>?,
27
+        selection: String?,
28
+        selectionArgs: Array<out String>?,
29
+        sortOrder: String?
30
+    ): Cursor? {
31
+        var match = matcher.match(uri)
32
+        try {
33
+            sputils?.let { sp ->
34
+                val userBean = sp.getFromJson(Const.USERINFO, UserBean().javaClass) as UserBean
35
+                val cursor = MatrixCursor(arrayOf("userid", "schoolid", "regionid"))
36
+                cursor.addRow(arrayListOf(userBean.userid, userBean.schoolid, userBean.regionid))
37
+                return cursor
38
+            }
39
+        } catch (e: Exception) {
40
+
41
+        }
42
+        return null
43
+    }
44
+
45
+    override fun getType(uri: Uri): String? {
46
+        return null
47
+    }
48
+
49
+    override fun insert(uri: Uri, values: ContentValues?): Uri? {
50
+        return null
51
+    }
52
+
53
+    override fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?): Int {
54
+        return 0
55
+    }
56
+
57
+    override fun update(
58
+        uri: Uri,
59
+        values: ContentValues?,
60
+        selection: String?,
61
+        selectionArgs: Array<out String>?
62
+    ): Int {
63
+        return 0
64
+    }
65
+}

+ 208
- 0
app/src/main/java/com/xhly/manageapp/contentprovider/ZSpProvider.java Ver arquivo

@@ -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
+}

Carregando…
Cancelar
Salvar