|
| 1 | +<span style="color:red">!!! DRAFT - Please note this page is still in draft as we work on platform specific details |
| 2 | +</span> |
| 3 | + |
| 4 | +### Local Key Helper on Android |
| 5 | +On android platform a Local Key Helper is a protocol interface (`ILocalKeyHelper.java`), and its implementation (`LocalKeyHelperImpl.java`) is yet to be determined. |
| 6 | + |
| 7 | +Local KeyHelpers on the Android platform are implemented using [content providers](https://developer.android.com/guide/topics/providers/content-providers). These content providers allow browser applications, such as Chrome, to interact with them by querying and accessing specific APIs defined by the Local KeyHelpers. |
| 8 | + |
| 9 | +#### Registering LocalkeyHelpers with Browser Interaction |
| 10 | +To register the localkeyhelpers with a browser (e.g. Chrome), the browser application exports a content provider with the authority `$browserPackageName.localkeyhelpersregistration` in it's manifest file. |
| 11 | + |
| 12 | +```xml |
| 13 | +<provider |
| 14 | + android:authorities="com.custombrowser.localkeyhelpersregistration" |
| 15 | + android:name="LocalKeyHelpersRegistrationProvider" |
| 16 | + android:exported="true" > |
| 17 | +</provider> |
| 18 | +``` |
| 19 | + |
| 20 | +##### Implementing localkeyhelpersregistration provider in Browser |
| 21 | +Browser application implements `localkeyhelpersregistration` content provider and provides path `/register` for the local key helpers to register themselves. |
| 22 | +When the LocalKeyHelper queries the browser's content provider, it register itself with the browser. |
| 23 | +Example implementation. |
| 24 | +``` java |
| 25 | +public class LocalKeyHelperConsumerProvider extends ContentProvider { |
| 26 | + public static final String AUTHORITY = "com.custombrowser.localkeyhelpersregistration"; |
| 27 | + private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); |
| 28 | + |
| 29 | + static { |
| 30 | + uriMatcher.addURI("AUTHORITY", "/register", 1); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public boolean onCreate() { |
| 35 | + return true; |
| 36 | + } |
| 37 | + |
| 38 | + @Nullable |
| 39 | + @Override |
| 40 | + public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) { |
| 41 | + switch (uriMatcher.match(uri)) { |
| 42 | + case 1 : |
| 43 | + return new MatrixCursor(new String[0]) { |
| 44 | + @Override |
| 45 | + public Bundle getExtras() { |
| 46 | + Bundle bundle = new Bundle(); |
| 47 | + bundle.putBoolean("registered", true); |
| 48 | + return bundle; |
| 49 | + } |
| 50 | + }; |
| 51 | + default: |
| 52 | + throw new IllegalArgumentException("Unknown URI: " + uri); |
| 53 | + } |
| 54 | + } |
| 55 | + ... |
| 56 | + |
| 57 | +} |
| 58 | +``` |
| 59 | +##### Calling Browser Content Provider to register a local key helper. |
| 60 | +LocalKeyhelpers make the browser content provider visible to them by adding queries tag with browser content provider authority to their manifest file. |
| 61 | +```xml |
| 62 | +<queries> |
| 63 | + <provider android:authorities="com.custombrowser.localkeyhelpersregistration" /> |
| 64 | +</queries> |
| 65 | +``` |
| 66 | + |
| 67 | +LocalKeyHelper call the browser's content provider to register itself with the browser. |
| 68 | +```java |
| 69 | +Uri uri = Uri.parse("content://com.custombrowser.localkeyhelpersregistration/register"); |
| 70 | +Cursor cursor = getContentResolver().query(uri, null, null, null, null); |
| 71 | +if (cursor != null) { |
| 72 | + try { |
| 73 | + val resultBundle = cursor.extras |
| 74 | + if (resultBundle != null) { |
| 75 | + val registered = resultBundle.getBoolean("registered") |
| 76 | + Log.i("TAG", "Registered: $registered") |
| 77 | + } else { |
| 78 | + Log.i("TAG", "Failed to register.") |
| 79 | + } |
| 80 | + } finally { |
| 81 | + cursor.close(); |
| 82 | + } |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +#### Calling Local KeyHelpers from Browser |
| 87 | +Once a localkeyhelper is registered with the Browser application, the browser can query the Local KeyHelper's content providers without defining the query tag. |
| 88 | +To implement the localkeyhelper protocol interface LocalKeyHelper defines a content provider with the authority `$packagename.**localkeyhelper**`. Different APIs are implemented as separate paths on this content provider. |
| 89 | + |
| 90 | +##### Sample Manifest Entry for Local KeyHelper Content Provider |
| 91 | +```xml |
| 92 | +<provider |
| 93 | + android:authorities="com.contoso.localkeyhelper" |
| 94 | + android:name="ContosoLocalKeyHelperProvider" /> |
| 95 | +``` |
| 96 | + |
| 97 | +##### Implementing the ContentProvider in Local KeyHelper |
| 98 | +Each Local KeyHelper must implement a ContentProvider to handle the interactions. Below is an example of how to implement a ContentProvider in a Local KeyHelper. |
| 99 | + |
| 100 | +```java |
| 101 | +public class LocalKeyHelperProvider extends ContentProvider { |
| 102 | + public static final String AUTHORITY = "com.contoso.localkeyhelper"; |
| 103 | + private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); |
| 104 | + |
| 105 | + static { |
| 106 | + uriMatcher.addURI(AUTHORITY, "sample_api_path", 1); |
| 107 | + // Add more paths as needed |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public boolean onCreate() { |
| 112 | + return true; |
| 113 | + } |
| 114 | + |
| 115 | + @Nullable |
| 116 | + @Override |
| 117 | + public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) { |
| 118 | + switch (uriMatcher.match(uri)) { |
| 119 | + // call the Implementation specific api based on the path |
| 120 | + default: |
| 121 | + throw new IllegalArgumentException("Unknown URI: " + uri); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | +``` |
0 commit comments