1import { UnavailabilityError } from 'expo-modules-core';
2
3import ExpoIntentLauncher from './ExpoIntentLauncher';
4
5// @needsAudit
6/**
7 * Constants are from the source code of [Settings provider](https://developer.android.com/reference/android/provider/Settings).
8 */
9export enum ActivityAction {
10  ACCESSIBILITY_SETTINGS = 'android.settings.ACCESSIBILITY_SETTINGS',
11  ADD_ACCOUNT_SETTINGS = 'android.settings.ADD_ACCOUNT_SETTINGS',
12  AIRPLANE_MODE_SETTINGS = 'android.settings.AIRPLANE_MODE_SETTINGS',
13  APN_SETTINGS = 'android.settings.APN_SETTINGS',
14  APP_NOTIFICATION_REDACTION = 'android.settings.ACTION_APP_NOTIFICATION_REDACTION',
15  APP_NOTIFICATION_SETTINGS = 'android.settings.APP_NOTIFICATION_SETTINGS',
16  APP_OPS_SETTINGS = 'android.settings.APP_OPS_SETTINGS',
17  APPLICATION_DETAILS_SETTINGS = 'android.settings.APPLICATION_DETAILS_SETTINGS',
18  APPLICATION_DEVELOPMENT_SETTINGS = 'android.settings.APPLICATION_DEVELOPMENT_SETTINGS',
19  APPLICATION_SETTINGS = 'android.settings.APPLICATION_SETTINGS',
20  BATTERY_SAVER_SETTINGS = 'android.settings.BATTERY_SAVER_SETTINGS',
21  BLUETOOTH_SETTINGS = 'android.settings.BLUETOOTH_SETTINGS',
22  CAPTIONING_SETTINGS = 'android.settings.CAPTIONING_SETTINGS',
23  CAST_SETTINGS = 'android.settings.CAST_SETTINGS',
24  CONDITION_PROVIDER_SETTINGS = 'android.settings.ACTION_CONDITION_PROVIDER_SETTINGS',
25  DATA_ROAMING_SETTINGS = 'android.settings.DATA_ROAMING_SETTINGS',
26  DATE_SETTINGS = 'android.settings.DATE_SETTINGS',
27  DEVICE_INFO_SETTINGS = 'android.settings.DEVICE_INFO_SETTINGS',
28  DEVICE_NAME = 'android.settings.DEVICE_NAME',
29  DISPLAY_SETTINGS = 'android.settings.DISPLAY_SETTINGS',
30  DREAM_SETTINGS = 'android.settings.DREAM_SETTINGS',
31  HARD_KEYBOARD_SETTINGS = 'android.settings.HARD_KEYBOARD_SETTINGS',
32  HOME_SETTINGS = 'android.settings.HOME_SETTINGS',
33  IGNORE_BACKGROUND_DATA_RESTRICTIONS_SETTINGS = 'android.settings.IGNORE_BACKGROUND_DATA_RESTRICTIONS_SETTINGS',
34  IGNORE_BATTERY_OPTIMIZATION_SETTINGS = 'android.settings.IGNORE_BATTERY_OPTIMIZATION_SETTINGS',
35  INPUT_METHOD_SETTINGS = 'android.settings.INPUT_METHOD_SETTINGS',
36  INPUT_METHOD_SUBTYPE_SETTINGS = 'android.settings.INPUT_METHOD_SUBTYPE_SETTINGS',
37  INTERNAL_STORAGE_SETTINGS = 'android.settings.INTERNAL_STORAGE_SETTINGS',
38  LOCALE_SETTINGS = 'android.settings.LOCALE_SETTINGS',
39  LOCATION_SOURCE_SETTINGS = 'android.settings.LOCATION_SOURCE_SETTINGS',
40  MANAGE_ALL_APPLICATIONS_SETTINGS = 'android.settings.MANAGE_ALL_APPLICATIONS_SETTINGS',
41  MANAGE_APPLICATIONS_SETTINGS = 'android.settings.MANAGE_APPLICATIONS_SETTINGS',
42  MANAGE_DEFAULT_APPS_SETTINGS = 'android.settings.MANAGE_DEFAULT_APPS_SETTINGS',
43  MEMORY_CARD_SETTINGS = 'android.settings.MEMORY_CARD_SETTINGS',
44  MONITORING_CERT_INFO = 'android.settings.MONITORING_CERT_INFO',
45  NETWORK_OPERATOR_SETTINGS = 'android.settings.NETWORK_OPERATOR_SETTINGS',
46  NFC_PAYMENT_SETTINGS = 'android.settings.NFC_PAYMENT_SETTINGS',
47  NFC_SETTINGS = 'android.settings.NFC_SETTINGS',
48  NFCSHARING_SETTINGS = 'android.settings.NFCSHARING_SETTINGS',
49  NIGHT_DISPLAY_SETTINGS = 'android.settings.NIGHT_DISPLAY_SETTINGS',
50  NOTIFICATION_LISTENER_SETTINGS = 'android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS',
51  NOTIFICATION_POLICY_ACCESS_SETTINGS = 'android.settings.NOTIFICATION_POLICY_ACCESS_SETTINGS',
52  NOTIFICATION_SETTINGS = 'android.settings.NOTIFICATION_SETTINGS',
53  PAIRING_SETTINGS = 'android.settings.PAIRING_SETTINGS',
54  PRINT_SETTINGS = 'android.settings.ACTION_PRINT_SETTINGS',
55  PRIVACY_SETTINGS = 'android.settings.PRIVACY_SETTINGS',
56  QUICK_LAUNCH_SETTINGS = 'android.settings.QUICK_LAUNCH_SETTINGS',
57  REQUEST_IGNORE_BATTERY_OPTIMIZATIONS = 'android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS',
58  SECURITY_SETTINGS = 'android.settings.SECURITY_SETTINGS',
59  SETTINGS = 'android.settings.SETTINGS',
60  SHOW_ADMIN_SUPPORT_DETAILS = 'android.settings.SHOW_ADMIN_SUPPORT_DETAILS',
61  SHOW_INPUT_METHOD_PICKER = 'android.settings.SHOW_INPUT_METHOD_PICKER',
62  SHOW_REGULATORY_INFO = 'android.settings.SHOW_REGULATORY_INFO',
63  SHOW_REMOTE_BUGREPORT_DIALOG = 'android.settings.SHOW_REMOTE_BUGREPORT_DIALOG',
64  SOUND_SETTINGS = 'android.settings.SOUND_SETTINGS',
65  STORAGE_MANAGER_SETTINGS = 'android.settings.STORAGE_MANAGER_SETTINGS',
66  SYNC_SETTINGS = 'android.settings.SYNC_SETTINGS',
67  SYSTEM_UPDATE_SETTINGS = 'android.settings.SYSTEM_UPDATE_SETTINGS',
68  TETHER_PROVISIONING_UI = 'android.settings.TETHER_PROVISIONING_UI',
69  TRUSTED_CREDENTIALS_USER = 'android.settings.TRUSTED_CREDENTIALS_USER',
70  USAGE_ACCESS_SETTINGS = 'android.settings.USAGE_ACCESS_SETTINGS',
71  USER_DICTIONARY_INSERT = 'android.settings.USER_DICTIONARY_INSERT',
72  USER_DICTIONARY_SETTINGS = 'android.settings.USER_DICTIONARY_SETTINGS',
73  USER_SETTINGS = 'android.settings.USER_SETTINGS',
74  VOICE_CONTROL_AIRPLANE_MODE = 'android.settings.VOICE_CONTROL_AIRPLANE_MODE',
75  VOICE_CONTROL_BATTERY_SAVER_MODE = 'android.settings.VOICE_CONTROL_BATTERY_SAVER_MODE',
76  VOICE_CONTROL_DO_NOT_DISTURB_MODE = 'android.settings.VOICE_CONTROL_DO_NOT_DISTURB_MODE',
77  VOICE_INPUT_SETTINGS = 'android.settings.VOICE_INPUT_SETTINGS',
78  VPN_SETTINGS = 'android.settings.VPN_SETTINGS',
79  VR_LISTENER_SETTINGS = 'android.settings.VR_LISTENER_SETTINGS',
80  WEBVIEW_SETTINGS = 'android.settings.WEBVIEW_SETTINGS',
81  WIFI_IP_SETTINGS = 'android.settings.WIFI_IP_SETTINGS',
82  WIFI_SETTINGS = 'android.settings.WIFI_SETTINGS',
83  WIRELESS_SETTINGS = 'android.settings.WIRELESS_SETTINGS',
84  ZEN_MODE_AUTOMATION_SETTINGS = 'android.settings.ZEN_MODE_AUTOMATION_SETTINGS',
85  ZEN_MODE_EVENT_RULE_SETTINGS = 'android.settings.ZEN_MODE_EVENT_RULE_SETTINGS',
86  ZEN_MODE_EXTERNAL_RULE_SETTINGS = 'android.settings.ZEN_MODE_EXTERNAL_RULE_SETTINGS',
87  ZEN_MODE_PRIORITY_SETTINGS = 'android.settings.ZEN_MODE_PRIORITY_SETTINGS',
88  ZEN_MODE_SCHEDULE_RULE_SETTINGS = 'android.settings.ZEN_MODE_SCHEDULE_RULE_SETTINGS',
89  ZEN_MODE_SETTINGS = 'android.settings.ZEN_MODE_SETTINGS',
90}
91
92// @needsAudit
93export interface IntentLauncherParams {
94  /**
95   * A string specifying the MIME type of the data represented by the data parameter. Ignore this
96   * argument to allow Android to infer the correct MIME type.
97   */
98  type?: string;
99  /**
100   * Category provides more details about the action the intent performs. See [Intent.addCategory](https://developer.android.com/reference/android/content/Intent.html#addCategory(java.lang.String)).
101   */
102  category?: string;
103  /**
104   * A map specifying additional key-value pairs which are passed with the intent as `extras`.
105   * The keys must include a package prefix, for example the app `com.android.contacts` would use
106   * names like `com.android.contacts.ShowAll`.
107   */
108  extra?: Record<string, any>;
109  /**
110   * A URI specifying the data that the intent should operate upon. (_Note:_ Android requires the URI
111   * scheme to be lowercase, unlike the formal RFC.)
112   */
113  data?: string;
114  /**
115   * Bitmask of flags to be used. See [Intent.setFlags](<https://developer.android.com/reference/android/content/Intent.html#setFlags(int)>) for more details.
116   */
117  flags?: number;
118  /**
119   * Package name used as an identifier of ComponentName. Set this only if you want to explicitly
120   * set the component to handle the intent.
121   */
122  packageName?: string;
123  /**
124   * Class name of the ComponentName.
125   */
126  className?: string;
127}
128
129// @needsAudit
130export interface IntentLauncherResult {
131  /**
132   * Result code returned by the activity.
133   */
134  resultCode: ResultCode;
135  /**
136   * Optional data URI that can be returned by the activity.
137   */
138  data?: string;
139  /**
140   * Optional extras object that can be returned by the activity.
141   */
142  extra?: object;
143}
144
145// @needsAudit
146export enum ResultCode {
147  /**
148   * Indicates that the activity operation succeeded.
149   */
150  Success = -1,
151  /**
152   * Means that the activity was canceled, e.g. by tapping on the back button.
153   */
154  Canceled = 0,
155  /**
156   * First custom, user-defined value that can be returned by the activity.
157   */
158  FirstUser = 1,
159}
160
161// @needsAudit
162/**
163 * Starts the specified activity. The method will return a promise which resolves when the user
164 * returns to the app.
165 * @param activityAction The action to be performed, e.g. `IntentLauncher.ActivityAction.WIRELESS_SETTINGS`.
166 * There are a few pre-defined constants you can use for this parameter.
167 * You can find them at [expo-intent-launcher/src/IntentLauncher.ts](https://github.com/expo/expo/blob/main/packages/expo-intent-launcher/src/IntentLauncher.ts).
168 * @param params An object of intent parameters.
169 * @return A promise which fulfils with `IntentLauncherResult` object.
170 */
171export async function startActivityAsync(
172  activityAction: ActivityAction | string,
173  params: IntentLauncherParams = {}
174): Promise<IntentLauncherResult> {
175  if (!ExpoIntentLauncher.startActivity) {
176    throw new UnavailabilityError('IntentLauncher', 'startActivityAsync');
177  }
178  if (!activityAction || typeof activityAction !== 'string') {
179    throw new TypeError(`'activityAction' argument must be a non-empty string!`);
180  }
181  return ExpoIntentLauncher.startActivity(activityAction, params);
182}
183