|
| 1 | +// Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +package io.flutter.plugins.shared_preferences; |
| 6 | + |
| 7 | +import android.content.Context; |
| 8 | +import io.flutter.app.FlutterActivity; |
| 9 | +import io.flutter.plugin.common.MethodChannel; |
| 10 | +import io.flutter.plugin.common.MethodChannel.MethodCallHandler; |
| 11 | +import io.flutter.plugin.common.MethodChannel.Result; |
| 12 | +import io.flutter.plugin.common.MethodCall; |
| 13 | +import io.flutter.view.FlutterView; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.HashSet; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +/** |
| 20 | + * SharedPreferencesPlugin |
| 21 | + */ |
| 22 | +public class SharedPreferencesPlugin implements MethodCallHandler { |
| 23 | + private static final String SHARED_PREFERENCES_NAME = "FlutterSharedPreferences"; |
| 24 | + private static final String CHANNEL_NAME = "plugins.flutter.io/shared_preferences"; |
| 25 | + |
| 26 | + private final android.content.SharedPreferences preferences; |
| 27 | + private final android.content.SharedPreferences.Editor editor; |
| 28 | + |
| 29 | + public static SharedPreferencesPlugin register(FlutterActivity activity) { |
| 30 | + return new SharedPreferencesPlugin(activity); |
| 31 | + } |
| 32 | + |
| 33 | + private SharedPreferencesPlugin(FlutterActivity activity) { |
| 34 | + preferences = activity.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE); |
| 35 | + editor = preferences.edit(); |
| 36 | + new MethodChannel(activity.getFlutterView(), CHANNEL_NAME).setMethodCallHandler(this); |
| 37 | + } |
| 38 | + |
| 39 | + // Filter preferences to only those set by the flutter app. |
| 40 | + private Map<String, Object> getAllPrefs() { |
| 41 | + Map<String, ?> allPrefs = preferences.getAll(); |
| 42 | + Map<String, Object> filteredPrefs = new HashMap<>(); |
| 43 | + for (String key : allPrefs.keySet()) { |
| 44 | + if (key.startsWith("flutter.")) { |
| 45 | + filteredPrefs.put(key, allPrefs.get(key)); |
| 46 | + } |
| 47 | + } |
| 48 | + return filteredPrefs; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void onMethodCall(MethodCall call, MethodChannel.Result result) { |
| 53 | + String key = call.argument("key"); |
| 54 | + switch (call.method) { |
| 55 | + case "setBool": |
| 56 | + editor.putBoolean(key, (boolean) call.argument("value")).apply(); |
| 57 | + result.success(null); |
| 58 | + break; |
| 59 | + case "setDouble": |
| 60 | + editor.putFloat(key, (float) call.argument("value")).apply(); |
| 61 | + result.success(null); |
| 62 | + break; |
| 63 | + case "setInt": |
| 64 | + editor.putInt(key, (int) call.argument("value")).apply(); |
| 65 | + result.success(null); |
| 66 | + break; |
| 67 | + case "setString": |
| 68 | + editor.putString(key, (String) call.argument("value")).apply(); |
| 69 | + result.success(null); |
| 70 | + break; |
| 71 | + case "setStringSet": |
| 72 | + editor.putStringSet(key, new HashSet<>((List<String>) call.argument("value"))).apply(); |
| 73 | + result.success(null); |
| 74 | + break; |
| 75 | + case "commit": |
| 76 | + result.success(editor.commit()); |
| 77 | + break; |
| 78 | + case "getAll": |
| 79 | + result.success(getAllPrefs()); |
| 80 | + break; |
| 81 | + case "clear": |
| 82 | + for (String keyToDelete : getAllPrefs().keySet()) { |
| 83 | + editor.remove(keyToDelete); |
| 84 | + } |
| 85 | + result.success(editor.commit()); |
| 86 | + break; |
| 87 | + default: |
| 88 | + result.notImplemented(); |
| 89 | + break; |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments