summaryrefslogtreecommitdiffstats
path: root/mobile/android/android-components/components/browser/domains/README.md
blob: b965609bc91896b87bc28299512a43fa244bd7f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# [Android Components](../../../README.md) > Browser > Domains

This component provides APIs for managing localized and customizable domain lists (see [Domains](#domains) and [CustomDomains](#customdomains)). It also contains auto-complete functionality for these lists (see [DomainAutoCompleteProvider](#domainautocompleteprovider)) which can be used in conjuction with our [UI autocomplete component](../../ui/autocomplete/README.md).

## Usage

### Setting up the dependency

Use Gradle to download the library from [maven.mozilla.org](https://maven.mozilla.org/) ([Setup repository](../../../README.md#maven-repository)):

```Groovy
implementation "org.mozilla.components:browser-domains:{latest-version}"
```

### Domains

The `Domains` object is used to load the built-in localized domain lists which are shipped as part of this component. These lists are grouped by country and can be found [in our repository](src/main/assets/domains).

```Kotlin
// Load the domain lists for all countries in the default locale (fallback is US)
val domains = Domains.load(context)
```

### CustomDomains

The `CustomDomains` object can be used to manage a custom domain list which will be stored in `SharedPreferences`.

```Kotlin
// Load the custom domain list
val domains = CustomDomains.load(context)

// Save custom domains
CustomDomains.save(context, listOf("mozilla.org", "getpocket.com"))

// Remove custom domains
CustomDomains.remove(context, listOf("nolongerexists.org"))
```

### DomainAutoCompleteProvider

The class provides auto-complete functionality for both `Domains` and `CustomDomains`.

```Kotlin
// Initialize the provider
val provider = DomainAutocompleteProvider()
provider.initialize(
  context,
  useShippedDomains = true,
  useCustomDomains = true,
  loadDomainsFromDisk = true
)
```

Note that when `loadDomainsFromDisk` is set to true there is no need to manually call `load` on either `Domains` or `CustomDomains`.

```Kotlin
// Autocomplete domain lists
val result = provider.autocomplete("moz")
```

The result will contain the autocompleted text (`result.text`), the URL (`result.url`), and the source of the match (`result.source`), which is either `DEFAULT_LIST` if a result was found in the shipped domain list or `CUSTOM_LIST` otherwise. The custom domain list takes precendece over the built-in shipped domain list and the API will only return the first match.

## License

    This Source Code Form is subject to the terms of the Mozilla Public
    License, v. 2.0. If a copy of the MPL was not distributed with this
    file, You can obtain one at http://mozilla.org/MPL/2.0/