also looking at this
refactor(android): one SharedPreferences helper, after the third caller turned out to be dead
#170
3 files
+165
-113
| @@ -0,0 +1,149 @@ | ||
| 1 | //! `SharedPreferences`, which three actions now reach for. | |
| 2 | //! | |
| 3 | //! The shim opens a preferences file by name in five places — the shared-item | |
| 4 | //! trio, the widget data, the voice actions — and each does the same four | |
| 5 | //! calls: `getSharedPreferences`, `edit`, `putString`/`remove`, `apply`. This | |
| 6 | //! is that sequence once. | |
| 7 | //! | |
| 8 | //! What it deliberately does *not* own is the file's name. Each caller decides | |
| 9 | //! that, because the names are the part that differs and the part that goes | |
| 10 | //! wrong: `craft_shared_<group>` is built from page text, `craft_widget_prefs` | |
| 11 | //! has to match what `CraftWidgetProvider` reads, and `craft_voice_actions` is | |
| 12 | //! read by nothing else at all. A helper that also chose the name would be | |
| 13 | //! four helpers wearing one signature. | |
| 14 | //! | |
| 15 | //! ## `apply`, not `commit` | |
| 16 | //! | |
| 17 | //! Every writer here uses `apply`: it hands the write to a background thread | |
| 18 | //! and returns void, so a failure is not observable at the call site. That is | |
| 19 | //! what the shim does, and it is why every one of these actions resolves with | |
| 20 | //! a promise about having *asked* rather than about having written. | |
| 21 | ||
| 22 | const std = ; | |
| 23 | const jni = ; | |
| 24 | ||
| 25 | const Jni = jni.Jni; | |
| 26 | const jobject = jni.jobject; | |
| 27 | ||
| 28 | /// `Context.MODE_PRIVATE`. | |
| 29 | /// | |
| 30 | /// A compile-time constant in Java, so the shim's DEX holds the literal 0 | |
| 31 | /// rather than a field read — which is why it is a literal here too. | |
| 32 | pub const mode_private: i32 = 0; | |
| 33 | ||
| 34 | /// `activity.getSharedPreferences(name, MODE_PRIVATE)`. | |
| 35 | /// | |
| 36 | /// `name` is a slice rather than a literal because one caller builds it from | |
| 37 | /// page text, and it goes through `newStringUtf8` for the same reason. | |
| 38 | pub fn open(j: Jni, allocator: std.mem.Allocator, activity: jobject, name: []const u8) !jobject { | |
| 39 | return j.callObjectMethodA( | |
| 40 | activity, | |
| 41 | try j.methodId( | |
| 42 | try j.objectClass(activity), | |
| 43 | "getSharedPreferences", | |
| 44 | "(Ljava/lang/String;I)Landroid/content/SharedPreferences;", | |
| 45 | ), | |
| 46 | &.{ .{ .l = try j.newStringUtf8(allocator, name) }, .{ .i = mode_private } }, | |
| 47 | ); | |
| 48 | } | |
| 49 | ||
| 50 | /// `prefs.edit()`. | |
| 51 | pub fn edit(j: Jni, prefs: jobject) !jobject { | |
| 52 | return j.callObjectMethod( | |
| 53 | prefs, | |
| 54 | try j.methodId( | |
| 55 | try j.objectClass(prefs), | |
| 56 | "edit", | |
| 57 | "()Landroid/content/SharedPreferences$Editor;", | |
| 58 | ), | |
| 59 | ); | |
| 60 | } | |
| 61 | ||
| 62 | /// `editor.putString(key, value)`, whose return value is the editor again. | |
| 63 | pub fn putString( | |
| 64 | j: Jni, | |
| 65 | allocator: std.mem.Allocator, | |
| 66 | editor: jobject, | |
| 67 | key: []const u8, | |
| 68 | value: []const u8, | |
| 69 | ) !void { | |
| 70 | try j.pushLocalFrame(4); | |
| 71 | defer _ = j.popLocalFrame(null); | |
| 72 | ||
| 73 | _ = try j.callObjectMethodA( | |
| 74 | editor, | |
| 75 | try j.methodId( | |
| 76 | try j.objectClass(editor), | |
| 77 | "putString", | |
| 78 | "(Ljava/lang/String;Ljava/lang/String;)Landroid/content/SharedPreferences$Editor;", | |
| 79 | ), | |
| 80 | &.{ | |
| 81 | .{ .l = try j.newStringUtf8(allocator, key) }, | |
| 82 | .{ .l = try j.newStringUtf8(allocator, value) }, | |
| 83 | }, | |
| 84 | ); | |
| 85 | } | |
| 86 | ||
| 87 | /// `editor.remove(key)`. | |
| 88 | pub fn remove(j: Jni, allocator: std.mem.Allocator, editor: jobject, key: []const u8) !void { | |
| 89 | try j.pushLocalFrame(4); | |
| 90 | defer _ = j.popLocalFrame(null); | |
| 91 | ||
| 92 | _ = try j.callObjectMethodA( | |
| 93 | editor, | |
| 94 | try j.methodId( | |
| 95 | try j.objectClass(editor), | |
| 96 | "remove", | |
| 97 | "(Ljava/lang/String;)Landroid/content/SharedPreferences$Editor;", | |
| 98 | ), | |
| 99 | &.{.{ .l = try j.newStringUtf8(allocator, key) }}, | |
| 100 | ); | |
| 101 | } | |
| 102 | ||
| 103 | /// `editor.apply()`. | |
| 104 | pub fn apply(j: Jni, editor: jobject) !void { | |
| 105 | try j.callVoidMethodA( | |
| 106 | editor, | |
| 107 | try j.methodId(try j.objectClass(editor), "apply", "()V"), | |
| 108 | &.{}, | |
| 109 | ); | |
| 110 | } | |
| 111 | ||
| 112 | /// `prefs.getString(key, null)`, null included. | |
| 113 | /// | |
| 114 | /// The caller owns the returned bytes. Null means the key is absent, which is | |
| 115 | /// a different answer from an empty string and is kept as one. | |
| 116 | pub fn getString( | |
| 117 | j: Jni, | |
| 118 | allocator: std.mem.Allocator, | |
| 119 | prefs: jobject, | |
| 120 | key: []const u8, | |
| 121 | ) !?[]u8 { | |
| 122 | try j.pushLocalFrame(8); | |
| 123 | defer _ = j.popLocalFrame(null); | |
| 124 | ||
| 125 | const value = try j.callObjectMethodA( | |
| 126 | prefs, | |
| 127 | try j.methodId( | |
| 128 | try j.objectClass(prefs), | |
| 129 | "getString", | |
| 130 | "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", | |
| 131 | ), | |
| 132 | &.{ .{ .l = try j.newStringUtf8(allocator, key) }, .{ .l = null } }, | |
| 133 | ); | |
| 134 | ||
| 135 | if (value == null) return null; | |
| 136 | return try j.stringToUtf8(allocator, value); | |
| 137 | } | |
| 138 | ||
| 139 | // ============================================================================= | |
| 140 | // Tests | |
| 141 | // ============================================================================= | |
| 142 | ||
| 143 | const testing = std.testing; | |
| 144 | ||
| 145 | test "MODE_PRIVATE is zero" { | |
| 146 | // The shim's DEX holds this literal, so a field read here would be the | |
| 147 | // slower way to get the same number and a chance to get a different one. | |
| 148 | try testing.expectEqual(@as(i32, 0), mode_private); | |
| 149 | } | |
| @@ -34,6 +34,7 @@ | ||
| 34 | 34 | |
| 35 | 35 | const std = ; |
| 36 | 36 | const jni = ; |
| 37 | const prefs_api = ; | |
| 37 | 38 | |
| 38 | 39 | const Jni = jni.Jni; |
| 39 | 40 | const jobject = jni.jobject; |
| @@ -50,9 +51,9 @@ pub const reject_global = "_craftWidgetReject"; | ||
| 50 | 51 | pub const updated_result = "{\"updated\":true}"; |
| 51 | 52 | pub const reloaded_result = "{\"reloaded\":true}"; |
| 52 | 53 | |
| 53 | /// `activity.getSharedPreferences("craft_widget_prefs", MODE_PRIVATE)`. | |
| 54 | /// `activity.getSharedPreferences("craft_widget_prefs", MODE_PRIVATE)` — the | |
| 55 | /// file `CraftWidgetProvider` reads back. | |
| 54 | 56 | const prefs_name = "craft_widget_prefs"; |
| 55 | const mode_private: i32 = 0; | |
| 56 | 57 | |
| 57 | 58 | /// The four fields, and the preference key each is written to. |
| 58 | 59 | /// |
| @@ -106,45 +107,16 @@ pub fn writeUpdate(j: Jni, allocator: std.mem.Allocator, activity: jobject, upda | ||
| 106 | 107 | try j.pushLocalFrame(16); |
| 107 | 108 | defer _ = j.popLocalFrame(null); |
| 108 | 109 | |
| 109 | const prefs = try j.callObjectMethodA( | |
| 110 | activity, | |
| 111 | try j.methodId( | |
| 112 | try j.objectClass(activity), | |
| 113 | "getSharedPreferences", | |
| 114 | "(Ljava/lang/String;I)Landroid/content/SharedPreferences;", | |
| 115 | ), | |
| 116 | &.{ .{ .l = try j.newStringUtf(prefs_name) }, .{ .i = mode_private } }, | |
| 117 | ); | |
| 118 | ||
| 119 | const editor = try j.callObjectMethod( | |
| 120 | prefs, | |
| 121 | try j.methodId( | |
| 122 | try j.objectClass(prefs), | |
| 123 | "edit", | |
| 124 | "()Landroid/content/SharedPreferences$Editor;", | |
| 125 | ), | |
| 126 | ); | |
| 127 | const editor_cls = try j.objectClass(editor); | |
| 128 | const put_string = try j.methodId( | |
| 129 | editor_cls, | |
| 130 | "putString", | |
| 131 | "(Ljava/lang/String;Ljava/lang/String;)Landroid/content/SharedPreferences$Editor;", | |
| 132 | ); | |
| 110 | const prefs = try prefs_api.open(j, allocator, activity, prefs_name); | |
| 111 | const editor = try prefs_api.edit(j, prefs); | |
| 133 | 112 | |
| 134 | 113 | inline for (fields, 0..) |field, i| { |
| 135 | if (update[i]) |text| { | |
| 136 | try j.pushLocalFrame(4); | |
| 137 | defer _ = j.popLocalFrame(null); | |
| 138 | _ = try j.callObjectMethodA(editor, put_string, &.{ | |
| 139 | .{ .l = try j.newStringUtf(field.key) }, | |
| 140 | .{ .l = try j.newStringUtf8(allocator, text) }, | |
| 141 | }); | |
| 142 | } | |
| 114 | if (update[i]) |text| try prefs_api.putString(j, allocator, editor, field.key, text); | |
| 143 | 115 | } |
| 144 | 116 | |
| 145 | 117 | // `apply`, as the shim does — the write lands on a background thread and |
| 146 | 118 | // the resolve is a promise about having asked. |
| 147 | try j.callVoidMethodA(editor, try j.methodId(editor_cls, "apply", "()V"), &.{}); | |
| 119 | try prefs_api.apply(j, editor); | |
| 148 | 120 | } |
| 149 | 121 | |
| 150 | 122 | /// `Intent(action).setPackage(activity.packageName)`, then `sendBroadcast`. |
| @@ -314,7 +286,6 @@ test "the results are the shim's constants" { | ||
| 314 | 286 | |
| 315 | 287 | test "the preferences file is the one CraftWidgetProvider reads" { |
| 316 | 288 | try testing.expectEqualStrings("craft_widget_prefs", prefs_name); |
| 317 | try testing.expectEqual(@as(i32, 0), mode_private); | |
| 318 | 289 | } |
| 319 | 290 | |
| 320 | 291 | test "the actions and globals match the shim exactly" { |