Android app to search installed apps

AppSearch

AppSearch is a high-performance on-device search library for managing locally stored, structured data. It contains APIs for indexing data and retrieving data using full-text search. Applications can use AppSearch to offer custom in-app search capabilities, allowing users to search for content even while offline.

AppSearch provides the following features:

  • A fast, mobile-first storage implementation with low I/O use
  • Highly efficient indexing and querying over large data sets
  • Multi-language support, such as English and Spanish
  • Relevance ranking and usage scoring

Due to lower I/O use, AppSearch offers lower latency for indexing and searching over large datasets compared to SQLite. AppSearch simplifies cross-type queries by supporting single queries whereas SQLite merges results from multiple tables.

To illustrate AppSearch’s features, let’s take the example of a music application that manages users’ favorite songs and allows users to easily search for them. Users enjoy music from around the world with song titles in different languages, which AppSearch natively supports indexing and querying for. When the user searches for a song by title or artist name, the application simply passes the request to AppSearch to quickly and efficiently retrieve matching songs. The application surfaces the results, allowing its users to quickly start playing their favorite songs.

Setup

To use AppSearch in your application, add the following dependencies to your application’s build.gradle file:

Groovy

Kotlin

AppSearch concepts

The following diagram illustrates AppSearch concepts and their interactions.

Figure 1. Diagram of AppSearch concepts: AppSearch database, schema, schema types, documents, session, and search.

Database and session

An AppSearch database is a collection of documents that conforms to the database schema. Client applications create a database by providing their application context and a database name. Databases can be opened only by the application that created them. When a database is opened, a session is returned to interact with the database. The session is the entry point for calling the AppSearch APIs and remains open until it’s closed by the client application.

Schema and schema types

A schema represents the organizational structure of data within an AppSearch database.

The schema is composed of schema types that represent unique types of data. Schema types consist of properties that contain a name, data type, and cardinality. Once a schema type is added to the database schema, documents of that schema type can be created and added to the database.

Documents

In AppSearch, a unit of data is represented as a document. Each document in an AppSearch database is uniquely identified by its namespace and ID. Namespaces are used to separate data from different sources when only one source needs to be queried, such as user accounts.

Documents contain a creation timestamp, a time-to-live (TTL), and a score that can be used for ranking during retrieval. A document is also assigned a schema type that describes additional data properties the document must have.

A document class is an abstraction of a document. It contains annotated fields that represent the contents of a document. By default, the name of the document class sets the name of the schema type.

Documents are indexed and can be searched by providing a query. A document is matched and included in the search results if it contains the terms in the query or matches another search specification. Results are ordered based on their score and ranking strategy. Search results are represented by pages that you can retrieve sequentially.

Читайте также:  Распиновка проводов андроид 2 дин

AppSearch offers customizations for search, such as filters, page size configuration, and snippeting.

Platform Storage vs Local Storage

AppSearch offers two storage solutions: LocalStorage and PlatformStorage. With LocalStorage, your application manages an app-specific index that lives in your application data directory. With PlatformStorage, your application contributes to a system-wide central index. Data access within the central index is restricted to data your application has contributed and data that has been explicitly shared with you by another application. Both LocalStorage and PlatformStorage share the same API and can be interchanged based on a device’s version:

Kotlin

Using PlatformStorage, your application can securely share data with other applications to allow them to search over your app’s data as well. Read-only application data sharing is granted via a certificate handshake to ensure that the other application has permission to read the data. Read more about this API in the documentation for setSchemaTypeVisibilityForPackage().

Additionally, data that is indexed can be displayed on System UI surfaces. Applications can opt out of some or all of their data being displayed on System UI surfaces. Read more about this API in the documentation for setSchemaTypeDisplayedBySystem().

Features LocalStorage (compatible with Android 4.0+) PlatformStorage (compatible with Android 12+)
Efficient full-text search
Multi-language support
Reduced binary size
Application-to-application data sharing
Capability to display data on System UI surfaces
Unlimited document size and count can be indexed
Faster operations without additional binder latency

There are additional trade-offs to consider when choosing between LocalStorage and PlatformStorage. Because PlatformStorage wraps Jetpack APIs over the AppSearch system service, the APK size impact is minimal compared to using LocalStorage. However, this also means AppSearch operations incur additional binder latency when calling the AppSearch system service. With PlatformStorage, AppSearch limits the number of documents and size of documents an application can index to ensure an efficient central index.

Get started with AppSearch

The example in this section showcases how to use AppSearch APIs to integrate with a hypothetical note-keeping application.

Write a document class

The first step to integrate with AppSearch is to write a document class to describe the data to insert into the database. Mark a class as a document class by using the @Document annotation.You can use instances of the document class to put documents in and retrieve documents from the database.

The following code defines a Note document class with a @Document.StringProperty annotated field for indexing a Note object’s text.

Kotlin

Open a database

You must create a database before working with documents. The following code creates a new database with the name notes_app and gets a ListenableFuture for an AppSearchSession , which represents the connection to the database and provides the APIs for database operations.

Kotlin

Set a schema

You must set a schema before you can put documents in and retrieve documents from the database. The database schema consists of different types of structured data, referred to as «schema types.» The following code sets the schema by providing the document class as a schema type.

Kotlin

Put a document in the database

Once a schema type is added, you can add documents of that type to the database. The following code builds a document of schema type Note using the Note document class builder. It sets the document namespace user1 to represent an arbitrary user of this sample. The document is then inserted into the database and a listener is attached to process the result of the put operation.

Kotlin

Search

You can search documents that are indexed using the search operations covered in this section. The following code performs queries for the term «fruit» over the database for documents that belong to the user1 namespace.

Kotlin

Iterate through SearchResults

Searches return a SearchResults instance, which gives access to the pages of SearchResult objects. Each SearchResult holds its matched GenericDocument , the general form of a document that all documents are converted to. The following code gets the first page of search results and converts the result back into a Note document.

Kotlin

Remove a document

When the user deletes a note, the application deletes the corresponding Note document from the database. This ensures the note will no longer be surfaced in queries. The following code makes an explicit request to remove the Note document from the database by Id.

Kotlin

Persist to disk

Updates to a database should be periodically persisted to disk by calling requestFlush() . The following code calls requestFlush() with a listener to determine if the call was successful.

Kotlin

Close a session

An AppSearchSession should be closed when an application will no longer be calling any database operations. The following code closes the AppSearch session that was opened previously and persists all updates to disk.

Читайте также:  Как удалить netflix с андроида

Kotlin

Additional resources

To learn more about AppSearch, see the following additional resources:

Samples

  • Android AppSearch Sample (Kotlin), a note taking app that uses AppSearch to index a user’s notes and allows users to search over their notes.

Provide feedback

Share your feedback and ideas with us through these resources:

Report bugs so we can fix them.

Content and code samples on this page are subject to the licenses described in the Content License. Java is a registered trademark of Oracle and/or its affiliates.

Источник

Is your app installed? getInstalledRelatedApps() will tell you!

The getInstalledRelatedApps() method allows your website to check whether
your iOS/Android/desktop app or PWA is installed on a user’s device.

What is the getInstalledRelatedApps() API? #

The getInstalledRelatedApps() makes it possible for your page to check if your mobile or desktop app, or in some cases, if your Progressive Web App (PWA) is already installed on a user’s device, and allows you to customize the user experience if it is.

For example, if your app is already installed:

  • Redirecting the user from a product marketing page directly into your app.
  • Centralizing some functionality like notifications in the other app to prevent duplicate notifications.
  • Not promoting the installation of your PWA if your other app is already installed.

To use the getInstalledRelatedApps() API, you need to tell your app about your site, then tell your site about your app. Once you’ve defined the relationship between the two, you can check if the app is installed.

Supported app types you can check #

App type Checkable from
Android app Android only
Chrome 80 or later
Windows (UWP) app Windows only
Chrome 85 or later
Edge 85 or later
Progressive Web App
Installed in the same scope or a different scope.
Android only
Chrome 84 or later

The getInstalledRelatedApps() API only allows you to check if your apps are installed. You cannot get a list of all installed apps, or check if other 3rd party apps are installed.

Check if your Android app is installed #

Your website can check if your Android app is installed.

Android: Chrome 80 or later

Tell your Android app about your website #

First, you’ll need to update your Android app to define the relationship between your website and Android application using the Digital Asset Links system. This ensures that only your website can check if your Android app is installed.

In the AndroidManifest.xml of your Android app, add an asset_statements entry:

Then, in strings.xml , add the following asset statement, updating site with your domain. Be sure to include the escaping characters.

Once completed, publish your updated Android app to the Play store.

Tell your website about your Android app #

Next, tell your website about your Android app by adding a web app manifest to your page. The manifest must include the related_applications property, an array that provides the details about your app, including platform and id .

  • platform must be play
  • id is the GooglePlay application ID for your Android app

Check if your app is installed #

Finally, call navigator.getInstalledRelatedApps() to check if your Android app is installed.

Check if your Windows (UWP) app is installed #

Your website can check if your Windows app (built using UWP) is installed.

Windows: Chrome 85 or later, Edge 85 or later

Tell your Windows app about your website #

You’ll need to update your Windows app to define the relationship between your website and Windows application using URI Handlers. This ensures that only your website can check if your Windows app is installed.

Add the Windows.appUriHandler extension registration to your app’s manifest file Package.appxmanifest . For example, if your website’s address is example.com you would add the following entry in your app’s manifest:

Note, you may need to add the uap3 namespace to your

Then, create a JSON file (without the .json file extension) named windows-app-web-link and provide your app’s package family name. Place that file either on your server root, or in the /.well-known/ directory. You can find the package family name in the Packaging section in the app manifest designer.

See Enable apps for websites using app URI handlers for complete details on setting up URI handlers.

Tell your website about your Windows app #

Next, tell your website about your Windows app by adding a web app manifest to your page. The manifest must include related_applications property, an array that provides the details about your app, including platform and id .

  • platform must be windows
  • id is your app’s package family name, appended by the Id value in your Package.appxmanifest file.

Check if your app is installed #

Finally, call navigator.getInstalledRelatedApps() to check if your Windows app is installed.

Check if your Progressive Web App is already installed (in scope) #

Your PWA can check to see if it is already installed. In this case, the page making the request must be on the same domain, and within the scope of your PWA, as defined by the scope in the web app manifest.

Android: Chrome 84 or later

Tell your PWA about itself #

Tell your PWA about itself by adding a related_applications entry in your PWAs web app manifest.

  • platform must be webapp
  • url is the full path to the web app manifest of your PWA

Check if your PWA is installed #

Finally, call navigator.getInstalledRelatedApps() from within the scope of your PWA to check if it is installed. If getInstalledRelatedApps() is called outside the scope of your PWA, it will return false. See the next section for details.

Check if your Progressive Web App is installed (out of scope) #

Your website can check if your PWA is installed, even if the page is outside the scope of your PWA. For example, a landing page served from /landing/ can check if the PWA served from /pwa/ is installed, or if your landing page is served from www.example.com and your PWA is served from app.example.com .

Android: Chrome 84 or later

Tell your PWA about your website #

First, you’ll need to add digital asset links to the server where your PWA is served from. This will help define the relationship between your website and your PWA, and ensures that only your website can check if your PWA is installed.

Add an assetlinks.json file to the /.well-known/ directory of the domain where the PWA lives, for example app.example.com . In the site property, provide the full path to the web app manifest that will perform the check (not the web app manifest of your PWA).

Double check the file name when you create your assetlinks.json file, I’ve wasted many hours debugging, only to realize I’d added an extra ‘s’ in the file name.

Tell your website about your PWA #

Next, tell your website about your PWA app by adding a web app manifest to your page. The manifest must include the related_applications property, an array that provides the details about your PWA, including platform and url .

  • platform must be webapp
  • url is the full path to the web app manifest of your PWA

Check if your PWA is installed #

Finally, call navigator.getInstalledRelatedApps() to check if your PWA is installed.

Calling getInstalledRelatedApps() #

Calling navigator.getInstalledRelatedApps() returns a promise that resolves with an array of your apps that are installed on the user’s device.

To prevent sites from testing an overly broad set of their own apps, only the first three apps declared in the web app manifest will be taken into account.

Like most other powerful web APIs, the getInstalledRelatedApps() API is only available when served over HTTPS.

Still have questions? #

Still have questions? Check the getInstalledRelatedApps tag on StackOverflow to see if anyone else has had similar questions. If not, ask your question there, and be sure to tag it with the progressive-web-apps tag. Our team frequently monitors that tag and tries to answer your questions.

Feedback #

Did you find a bug with Chrome’s implementation? Or is the implementation different from the spec?

  • File a bug at https://new.crbug.com. Include as much detail as you can, provide simple instructions for reproducing the bug, and enter Mobile>WebAPKs in the Components box. Glitch works great for sharing quick and easy repros.

Show support for the API #

Are you planning to use the getInstalledRelatedApps() API? Your public support helps the Chrome team to prioritize features and shows other browser vendors how critical it is to support them.

  • Share how you plan to use the API on the WICG Discourse thread.
  • Send a tweet to @ChromiumDev using the hashtag #getInstalledRelatedApps and let us know where and how you’re using it.

Thanks #

Special thanks to Sunggook Chue at Microsoft for helping with the details for testing Windows apps, and Rayan Kanso for help with the Chrome details.

Источник

Читайте также:  Чтение nfc меток android
Оцените статью