Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 90580d4

Browse files
committed
Add Shared Preferences plugin. Fixes flutter/flutter#4757
1 parent 031bdd5 commit 90580d4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+1771
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.DS_Store
2+
.atom/
3+
.idea
4+
.packages
5+
.pub/
6+
build/
7+
ios/.generated/
8+
packages
9+
pubspec.lock

packages/shared-preferences/LICENSE

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2017 Your Company. All rights reserved.
2+
//
3+
// Redistribution and use in source and binary forms, with or without
4+
// modification, are permitted provided that the following conditions are
5+
// met:
6+
//
7+
// * Redistributions of source code must retain the above copyright
8+
// notice, this list of conditions and the following disclaimer.
9+
// * Redistributions in binary form must reproduce the above
10+
// copyright notice, this list of conditions and the following disclaimer
11+
// in the documentation and/or other materials provided with the
12+
// distribution.
13+
// * Neither the name of Your Company nor the names of its
14+
// contributors may be used to endorse or promote products derived from
15+
// this software without specific prior written permission.
16+
//
17+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

packages/shared-preferences/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# shared_preferences
2+
3+
A new flutter plugin project.
4+
5+
## Getting Started
6+
7+
For help getting started with Flutter, view our online
8+
[documentation](http://flutter.io/).
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
6+
.DS_Store
7+
/build
8+
/captures
9+
10+
/gradle
11+
/gradlew
12+
/gradlew.bat
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
group 'com.yourcompany.shared_preferences'
2+
version '1.0-SNAPSHOT'
3+
4+
buildscript {
5+
repositories {
6+
jcenter()
7+
}
8+
9+
dependencies {
10+
classpath 'com.android.tools.build:gradle:2.3.0'
11+
}
12+
}
13+
14+
allprojects {
15+
repositories {
16+
jcenter()
17+
}
18+
}
19+
20+
apply plugin: 'com.android.library'
21+
22+
android {
23+
compileSdkVersion 25
24+
buildToolsVersion '25.0.0'
25+
26+
defaultConfig {
27+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
28+
}
29+
lintOptions {
30+
disable 'InvalidPackage'
31+
}
32+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.gradle.jvmargs=-Xmx1536M
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rootProject.name = 'shared_preferences'
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="com.yourcompany.shared_preferences"
3+
android:versionCode="1"
4+
android:versionName="0.0.1">
5+
6+
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="21" />
7+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.DS_Store
2+
.atom/
3+
.idea
4+
.packages
5+
.pub/
6+
build/
7+
ios/.generated/
8+
packages
9+
pubspec.lock
10+
.flutter-plugins

0 commit comments

Comments
 (0)