also looking at this
refactor(android): one SharedPreferences helper, after the third caller turned out to be dead
#170
3 files
+165
-113
| @@ -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" { |