commonsguy/cwac-prefs

语言: Java

git: https://github.com/commonsguy/cwac-prefs

CWAC-Prefs:数据库支持和加密的SharedPreferences
CWAC-Prefs: Database-Backed and Encryped SharedPreferences
README.md (中文)

CWAC Prefs:当常规首选项不够时

这个项目已经中止

SharedPreferences持久保存在内部存储中的XML文件中 你的应用程序。在许多情况下,这非常好。在其他情况下, 但是,您可能希望将这些首选项存储在其他容器中。 该项目提供了SharedPreferences的另一个实现 接口,您可以在其中提供策略对象来定义方式 应该加载和保持值。内置策略支持 将值存储在SQLite中,或在SQLCipher for Android中加密。

这可以作为JAR文件使用。 请注意,如果您希望使用JAR并使用加密 首选项,您还需要兼容版的SQLCipher for Android。

项目本身设置为Android库项目, 如果您希望以这种方式使用源代码。但是,你仍然会 需要复制你的库中资源/目录的内容 项目,因为资产不会自动从库项目共享到它 主办。

用法

您对CWSharedPreferences类的大多数使用(在com.commonsware.cwac.prefs中) 将与使用SharedPreferences的方式相同。 SharedPreferences是 界面; CWSharedPreferences实现了该接口。因此,你仍然使用 getInt()方法用于检索首选项值,edit()用于获取编辑器 像putInt()这样的setter等等。

首先,您将如何获得CWSharedPreferences对象。您 不会从标准的PreferenceManager或通过Context上的某些方法获得一个。 相反,您将在CWSharedPreferences类上使用getInstance()方法,并提供 如果CWSharedPreferences对象尚不存在,则使用的策略对象。和。一样 SharedPreferences,CWSharedPreferences保留在静态数据成员中并且将保留 在您的流程的生命中生活,在每个名称的基础上被视为单身人士。

定义和使用战略

CWSharedPreferences上的静态getInstance()方法,为您提供CWSharedPreferences object,将CWSharedPreferences.StorageStrategy对象作为参数。这个界面 定义加载和存储首选项值的位置和方式。

StorageStrategy有两种标准实现:

  • SQLiteStrategy,它将数据存储在SQLite数据库中
  • SQLCipherStrategy,将其数据存储在Android加密数据库的SQLCipher中

SQLiteStrategy的构造函数有三个参数:

  • 用于内部SQLiteOpenHelper实现的Context对象
  • 表示要使用的数据库文件名称的字符串(也用作...的唯一键 CWSharedPreferences实例的静态缓存)
  • 一个LoadPolicy,下面有更详细的描述

SQLCipherStrategy的构造函数接受这些参数,加上密码 数据库。如果此密码不是现有数据库,则此密码将用于加密数据库, 加上它用于随着时间的推移解密数据库。

加载政策

LoadPolicy指示创建CWSharedPreferences时应发生的情况 并需要加载其数据。有三种可能的LoadPolicy枚举值:

  • SYNC表示CWSharedPreferences将加载其首选项的内存缓存 当前线程的值。如果您使用自己的后台线程来初始化,请使用此选项 CWSharedPreferences实例并且不需要CWSharedPreferences来fork 为此目的自己的线程。
  • ASYNC_BLOCK与您在股票实施中看到的基本行为相同 SharedPreferences:数据是在后台线程中加载的,但是你的 尝试读取数据块,直到加载完成。
  • 如果您尝试读取,ASYNC_EXCEPTION将抛出RuntimeException 来自尚未完成加载的CWSharedPreferences。

SYNC通常是最好的答案,因为您可以放心,您不会阻止 主要的应用程序线程。 ASYNC_BLOCK可以阻止主应用程序 线程如果您尝试立即使用首选项并且尚未加载 然而。

因此,例如,您可以通过以下方式创建SQLiteStrategy对象:

new SQLiteStrategy(getContext(), NAME, LoadPolicy.SYNC);

(假设一个名为NAME的静态数据成员是数据库名称,和 假设存在一些getContext()方法返回一个Context)

然后,您可以通过以下方式检索CWSharedPreferences:

CWSharedPreferences.getInstance(new SQLiteStrategy(getContext(), NAME, LoadPolicy.SYNC));

限制

由于一些不幸的限制 在PreferenceFragment和PreferenceActivity的工作方式中,没有人知道 在这些类中使用CWSharedPreferences的简单方法。

依赖

使用SQLCipherStrategy项目,或构建项目 从源(包括使用它作为Android库项目),你 将需要下载并安装SQLCipher for Android。图书馆 项目已经有兼容的版本,所以如果你克隆回购和 在原地使用库项目,你应该没问题。如果你愿意的话 请改用JAR,访问SQLCipher for Android 网站,了解有关如何将其添加到项目的更多信息。

除此之外,主项目没有依赖关系。 值得注意的是,如果您使用JAR而不使用SQLCipherStrategy, 你不应该需要SQLCipher for Android。

这是该模块的v0.0.2版本,这意味着这是一个证明 的概念,寻求有关方面的意见。

演示

通常,CWAC项目有一个演示/子项目。这个没有。 在某种程度上,这是由于上面提到的UI限制。

但是,它确实有一个实现JUnit的测试/子项目 用于执行API基本方面的测试套件。但请注意 这些测试都是“白盒子”,只要测试生活在同一个测试中 打包为CWSharedPreferences并访问package-private方法。

执照

该项目中的代码是在Apache下许可的 软件许可2.0,根据所包含的许可条款 文件。

获得帮助

这个项目已经中止

发行说明

  • v0.0.3:错误修复
  • v0.0.2:次要问题修复
  • v0.0.1:初始导入

是谁做的?

CommonsWare

本文使用googletrans自动翻译,仅供参考, 原文来自github.com

en_README.md

CWAC Prefs: For When Regular Preferences Just Aren't Enough

THIS PROJECT IS DISCONTINUED

SharedPreferences are persisted in an XML file, in the internal storage
of your app. In many cases, this is perfectly fine. In other cases,
though, you might want to store those preferences in some other container.
This project provides another implementation of the SharedPreferences
interface, one where you can supply a strategy object to define how
the values should be loaded and persisted. Built-in strategies support
storing the values in SQLite, or encrypted in SQLCipher for Android.

This is available as a JAR file.
Note that if you wish to use the JAR and use the encrypted
preferences, you will also need a compatible edition of SQLCipher for Android.

The project itself is set up as an Android library project,
in case you wish to use the source code in that fashion. However, you will still
need to make a copy of the contents of the library's assets/ directory in your
project, as assets are not automatically shared from a library project to its
host.

Usage

Most of your usage of the CWSharedPreferences class (in com.commonsware.cwac.prefs)
will be the same as how you would use SharedPreferences. SharedPreferences is
an interface; CWSharedPreferences implements that interface. Hence, you still use
methods like getInt() to retrieve preference values, edit() to get an Editor
with setters like putInt(), and so on.

What differs is how you get the CWSharedPreferences object in the first place. You
will not get one from the standard PreferenceManager or via some method on Context.
Rather, you will use a getInstance() method on the CWSharedPreferences class, supplying
a strategy object to use if the CWSharedPreferences object does not yet exist. As with
SharedPreferences, CWSharedPreferences are held onto in a static data member and will
live for the life of your process, treated as a singleton on a per-name basis.

Defining and Using a Strategy

The static getInstance() method on CWSharedPreferences, to give you a CWSharedPreferences
object, takes a CWSharedPreferences.StorageStrategy object as a parameter. This interface
defines where and how the preference values are loaded and stored.

There are two standard implementations of StorageStrategy:

  • SQLiteStrategy, which stores its data in a SQLite database
  • SQLCipherStrategy, which stores its data in a SQLCipher for Android encrypted database

The constructor for SQLiteStrategy takes three parameters:

  • a Context object for use with an internal SQLiteOpenHelper implementation
  • a string representing the name of the database file to use (also serves as a unique key for
    the static cache of CWSharedPreferences instances)
  • a LoadPolicy, described in greater detail below

The constructor for SQLCipherStrategy takes those parameters, plus the password for the
database. This password will be used to encrypt the database if this is no existing database,
plus it is used to decrypt the database as needed over time.

Load Policies

The LoadPolicy indicates what should happen when the CWSharedPreferences is created
and needs to load its data. There are three possible LoadPolicy enum values:

  • SYNC means that the CWSharedPreferences will load its in-memory cache of the preference
    values on the current thread. Use this if you are using your own background thread to initialize the
    CWSharedPreferences instance and do not need CWSharedPreferences to fork
    its own thread for this purpose.

  • ASYNC_BLOCK is the same basic behavior you see with the stock implementation
    of SharedPreferences: the data is loaded in a background thread, but your
    attempt to read that data blocks until the load is complete.

  • ASYNC_EXCEPTION will throw a RuntimeException if you attempt to read
    from a CWSharedPreferences where the load has not yet completed.

SYNC is generally the best answer, as you are assured that you will not block
the main application thread. ASYNC_BLOCK could block the main application
thread if you try using the preferences right away and they have not been loaded
yet.

So, for example, you could create a SQLiteStrategy object via:

new SQLiteStrategy(getContext(), NAME, LoadPolicy.SYNC);

(assuming a static data member named NAME that is the database name, and
assuming the existence of some getContext() method to return a Context)

Then, you could retrieve a CWSharedPreferences via:

CWSharedPreferences.getInstance(new SQLiteStrategy(getContext(), NAME, LoadPolicy.SYNC));

Limitations

Due to some unfortunate limitations
in the way PreferenceFragment and PreferenceActivity work, there is no known
simple way to use CWSharedPreferences with these classes.

Dependencies

To use the project with SQLCipherStrategy, or to build the project
from source (including using it as an Android library project), you
will need to download and install SQLCipher for Android. The library
project already has a compatible edition, so if you clone the repo and
use the library project in situ, you should be OK. If you wish to
use the JAR instead, visit the SQLCipher for Android
Web site to learn more about how to add it to your project.

Other than that, there are no dependencies for the main project.
Notably, if you are using the JAR and not using SQLCipherStrategy,
you should not need SQLCipher for Android.

Version

This is version v0.0.2 of this module, meaning that this is a proof
of concept, seeking input from interested parties.

Demo

Normally, CWAC projects have a demo/ subproject. This one does not.
In part, that is due to the UI limitations mentioned above.

It does, however, have a tests/ sub-project that implements a JUnit
test suite for exercising basic aspects of the API. Note, though, that
these tests are "white box", insofar as the tests live in the same
package as CWSharedPreferences and access package-private methods.

License

The code in this project is licensed under the Apache
Software License 2.0, per the terms of the included LICENSE
file.

Getting Help

THIS PROJECT IS DISCONTINUED

Release Notes

  • v0.0.3: bug fixes
  • v0.0.2: minor issue fixes
  • v0.0.1: Initial import

Who Made This?

CommonsWare