diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..210b692 --- /dev/null +++ b/.gitignore @@ -0,0 +1,101 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild +.cxx +### Android template +# Built application files +*.apk +*.aar +*.ap_ +*.aab + +# Files for the ART/Dalvik VM +*.dex + +# Java class files +*.class + +# Generated files +bin/ +gen/ +out/ +# Uncomment the following line in case you need and you don't have the release build type files in your app +# release/ + +# Gradle files +.gradle/ +build/ + +# Local configuration file (sdk path, etc) +local.properties + +# Proguard folder generated by Eclipse +proguard/ + +# Log Files +*.log + +# Android Studio Navigation editor temp files +.navigation/ + +# Android Studio captures folder +captures/ + +# IntelliJ +*.iml +.idea/workspace.xml +.idea/tasks.xml +.idea/gradle.xml +.idea/assetWizardSettings.xml +.idea/dictionaries +.idea/libraries +# Android Studio 3 in .gitignore file. +.idea/caches +.idea/modules.xml +# Comment next line if keeping position of elements in Navigation Editor is relevant for you +.idea/navEditor.xml + +# Keystore files +# Uncomment the following lines if you do not want to check your keystore files in. +#*.jks +#*.keystore + +# External native build folder generated in Android Studio 2.2 and later +.externalNativeBuild +.cxx/ + +# Google Services (e.g. APIs or Firebase) +# google-services.json + +# Freeline +freeline.py +freeline/ +freeline_project_description.json + +# fastlane +fastlane/report.xml +fastlane/Preview.html +fastlane/screenshots +fastlane/test_output +fastlane/readme.md + +# Version control +vcs.xml + +# lint +lint/intermediates/ +lint/generated/ +lint/outputs/ +lint/tmp/ +# lint/reports/ + diff --git a/.idea/$CACHE_FILE$ b/.idea/$CACHE_FILE$ new file mode 100644 index 0000000..b341852 --- /dev/null +++ b/.idea/$CACHE_FILE$ @@ -0,0 +1,19 @@ + + + + + + + + + + + + + Android + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..88ea3aa --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,122 @@ + + + + + + + + + +
+ + + + xmlns:android + + ^$ + + + +
+
+ + + + xmlns:.* + + ^$ + + + BY_NAME + +
+
+ + + + .*:id + + http://schemas.android.com/apk/res/android + + + +
+
+ + + + .*:name + + http://schemas.android.com/apk/res/android + + + +
+
+ + + + name + + ^$ + + + +
+
+ + + + style + + ^$ + + + +
+
+ + + + .* + + ^$ + + + BY_NAME + +
+
+ + + + .* + + http://schemas.android.com/apk/res/android + + + ANDROID_ATTRIBUTE_ORDER + +
+
+ + + + .* + + .* + + + BY_NAME + +
+
+
+
+ + +
+
\ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..79ee123 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..97626ba --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..2370474 --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..089bb72 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,82 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..7f68460 --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..a7e45b4 --- /dev/null +++ b/README.md @@ -0,0 +1,82 @@ +## PowerfulPreference + +### 简介 + +重新封装[PreferenceHolder](https://github.com/MarcinMoskala/PreferenceHolder),让SharedPreferences使用更加简单 + +> 优化SharedPreferences缺陷降低ANR,利用ContentProviderr让原生sp支持多进程,支持`MMKV`,支持数据加密 + +### 集成 + +1 添加依赖和初始化 + +```kotlin + +repositories { + ... + maven { url 'https://jitpack.io' } +} + +dependencies { + implementation 'com.google.code.gson:gson:2.8.5' //存储对象需要 非必须 + implementation 'com.tencent:mmkv-static:1.2.1' // mmkv 非必须 + implementation 'com.github.forjrking:Preferences:1.2.1' // mmkv 非必须 +} + + //Application 中初始化MMKV + MMKV.initialize(this) ... + // 必须 + PreferenceHolder.context = this.applicationContext + // 非必须 用于序列化对象数据,sp不建议存大量数据 + PreferenceHolder.serializer = GsonSerializer() +``` + +2 写一个类添加方法 + +```kotlin +/* + * @param name xml名称 默认为包名 + * @param cryptKey 加密密钥 + * 注意{原生sp多进程不支持加密 多进程本身数据不安全而且性能比较差综合考虑不加密} + * @param isMMKV 是否使用mmkv + * @param isMultiProcess 是否使用多进程 建议mmkv搭配使用 sp性能很差 + */ +object TestSP : PreferenceHolder("name","cyptyKey",isMMKV,isMultiProcess) { + var testStr: String by bindToPreferenceField("") + var coin: Long by bindToPreferenceField(0L) + var tes: String? by bindToPreferenceFieldNullable() + //需要使用 GsonSerializer + var savedGame: Game? by bindToPreferenceFieldNullable() +} +//getValue +val str = TestSP.testStr +val coin = TestSP.coin +println(str) //"" or "something" +//setValue +TestSP.testStr = "AAAX${Random().nextInt(20)}" +TestSP.coin = 100 +``` + + + +### Retrofit 思想封装的实现 + +性能较差不建议使用 仅做学习 + +```kotlin +@SpConfig +interface JavaSP { + String getCoin(); + void setCoin(String coin); + boolean isFirstShare(boolean isFirst); + void setFirstShare(boolean isFirst); + void CLEAR(); +} + + val javaSP = SpRetrofit.create(this, JavaSP::class.java) + javaSP.coin = "212" //赋值 + val coin = javaSP.coin //取值 + val firstShare = javaSP.isFirstShare(true) //取值 默认值 + javaSP.setFirstShare(false) +``` + diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..ff6dc55 --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,39 @@ +apply plugin: 'com.android.application' +apply plugin: 'kotlin-android' +apply plugin: 'kotlin-android-extensions' + +android { + compileSdkVersion 29 + buildToolsVersion "29.0.3" + + defaultConfig { + applicationId "com.example.spholder" + minSdkVersion 16 + targetSdkVersion 29 + versionCode 1 + versionName "1.0" + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + implementation fileTree(dir: "libs", include: ["*.jar"]) + implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" + implementation 'androidx.core:core-ktx:1.3.0' + implementation 'androidx.appcompat:appcompat:1.1.0' + implementation 'androidx.constraintlayout:constraintlayout:1.1.3' + implementation project(path: ':preferences') + testImplementation 'junit:junit:4.12' + androidTestImplementation 'androidx.test.ext:junit:1.1.1' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' + + implementation 'com.tencent:mmkv-static:1.2.1' +} \ No newline at end of file diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/app/src/androidTest/java/com/example/spholder/ExampleInstrumentedTest.kt b/app/src/androidTest/java/com/example/spholder/ExampleInstrumentedTest.kt new file mode 100644 index 0000000..aae3d30 --- /dev/null +++ b/app/src/androidTest/java/com/example/spholder/ExampleInstrumentedTest.kt @@ -0,0 +1,44 @@ +package com.example.spholder + +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.ext.junit.runners.AndroidJUnit4 + +import org.junit.Test +import org.junit.runner.RunWith + +import org.junit.Assert.* +import java.util.* +import java.util.concurrent.TimeUnit + +/** + * Instrumented test, which will execute on an Android device. + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +@RunWith(AndroidJUnit4::class) +class ExampleInstrumentedTest { + @Test + fun useAppContext() { + // Context of the app under test. + val appContext = InstrumentationRegistry.getInstrumentation().targetContext + assertEquals("com.example.spholder", appContext.packageName) + } + + + @Test + fun benmark() { + + val s1 = System.nanoTime() + repeat(1) { + TestSP.testStr = "AAAXXEEEE0${Random().nextInt(it)}" + } + val s2 = System.nanoTime() + println("set Time: ${TimeUnit.NANOSECONDS.toMillis(s2 - s1)}") + + repeat(1) { + val s = TestSP.testStr + } + val s3 = System.nanoTime() + println("get Time: ${TimeUnit.NANOSECONDS.toMillis(s3 - s2)}") + } +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..f011e96 --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/example/spholder/JavaSP.java b/app/src/main/java/com/example/spholder/JavaSP.java new file mode 100644 index 0000000..3001a7e --- /dev/null +++ b/app/src/main/java/com/example/spholder/JavaSP.java @@ -0,0 +1,24 @@ +package com.example.spholder; + +import com.forjrking.preferences.proxy.SpConfig; + +/** + * @description: + * @author: 岛主 + * @date: 2020/7/21 15:03 + * @version: 1.0.0 + */ +@SpConfig +interface JavaSP { + + String getCoin(); + + void setCoin(String coin); + + boolean isFirstShare(boolean isFirst); + + void setFirstShare(boolean isFirst); + + void CLEAR(); + +} diff --git a/app/src/main/java/com/example/spholder/MainActivity.kt b/app/src/main/java/com/example/spholder/MainActivity.kt new file mode 100644 index 0000000..c1cdb93 --- /dev/null +++ b/app/src/main/java/com/example/spholder/MainActivity.kt @@ -0,0 +1,70 @@ +package com.example.spholder + +import androidx.appcompat.app.AppCompatActivity +import android.os.Bundle +import android.preference.PreferenceManager +import com.forjrking.preferences.kt.PreferenceHolder +import com.forjrking.preferences.proxy.SpRetrofit +import com.tencent.mmkv.MMKV +import kotlinx.android.synthetic.main.activity_main.* +import java.util.* +import java.util.concurrent.TimeUnit + +class MainActivity : AppCompatActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + MMKV.initialize(this) + PreferenceHolder.context = this.applicationContext +// PreferenceHolder.serializer = GsonSerializer() + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + + btn1.setOnClickListener { + + val s1 = System.nanoTime() + repeat(1000) { + TestSP.testStr = "BBBXXEEEE$it" + } + val s2 = System.nanoTime() + println("set Time: ${TimeUnit.NANOSECONDS.toMillis(s2 - s1)}") + + repeat(1000) { + val s = TestSP.testStr + } + val s3 = System.nanoTime() + println("get Time: ${TimeUnit.NANOSECONDS.toMillis(s3 - s2)}") + + text.text = TestSP.testStr + } + + + + btn2.setOnClickListener { + + val javaSP = SpRetrofit.create(this, JavaSP::class.java) + + val s1 = System.nanoTime() + repeat(1000) { +// javaSP.coin = "AAAXXEEEE0${Random().nextInt(20)}" + TestmmkvSP.testStr = "AAAXXEEEE$it" + } + val s2 = System.nanoTime() + println("mmkv set Time: ${TimeUnit.NANOSECONDS.toMillis(s2 - s1)}") + + repeat(1000) { + val s = TestmmkvSP.testStr +// val coin = javaSP.coin + } + val s3 = System.nanoTime() + println("mmkv get Time: ${TimeUnit.NANOSECONDS.toMillis(s3 - s2)}") + + text.text = TestmmkvSP.testStr + } + + +// javaSP.coin = "212" +// val coin = javaSP.coin +// val firstShare = javaSP.isFirstShare(true) +// javaSP.setFirstShare(false) + + } +} diff --git a/app/src/main/java/com/example/spholder/TestSP.kt b/app/src/main/java/com/example/spholder/TestSP.kt new file mode 100644 index 0000000..25b3141 --- /dev/null +++ b/app/src/main/java/com/example/spholder/TestSP.kt @@ -0,0 +1,19 @@ +package com.example.spholder + +import com.forjrking.preferences.kt.PreferenceHolder + +/** + * @description: + * @author: 岛主 + * @date: 2020/7/2 10:48 + * @version: 1.0.0 + */ +object TestSP : PreferenceHolder("test","12345678") { + + var testStr: String by bindToPreferenceField("",null,false) + + var coin: Long by bindToPreferenceField(0L) + + var tes: String? by bindToPreferenceFieldNullable() + +} \ No newline at end of file diff --git a/app/src/main/java/com/example/spholder/TestmmkvSP.kt b/app/src/main/java/com/example/spholder/TestmmkvSP.kt new file mode 100644 index 0000000..75e77ce --- /dev/null +++ b/app/src/main/java/com/example/spholder/TestmmkvSP.kt @@ -0,0 +1,17 @@ +package com.example.spholder + +import com.forjrking.preferences.kt.PreferenceHolder + +/** + * @description: + * @author: 岛主 + * @date: 2020/7/2 10:48 + * @version: 1.0.0 + */ +object TestmmkvSP : PreferenceHolder(null,"12345678",true, false) { + + var testStr: String by bindToPreferenceField("") + + var coin: Long by bindToPreferenceField(0L) + +} \ No newline at end of file diff --git a/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..2b068d1 --- /dev/null +++ b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..07d5da9 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..6e5efdd --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,36 @@ + + + + + + +