Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Add openPackage function for android #1902

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions android/capacitor/src/main/java/com/getcapacitor/plugin/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,24 @@ public void openUrl(PluginCall call) {
}
}

@PluginMethod()
public void openPackage(PluginCall call) {
String packageName = call.getString("packageName");
if (packageName == null) {
call.error("Must provide a packageName to open");
return;
}

final PackageManager manager = getContext().getPackageManager();
final Intent launchIntent = manager.getLaunchIntentForPackage(packageName);

try {
getActivity().startActivity(launchIntent);
} catch(Exception ex) {
call.error("Unable to open package", ex);
}
}

/**
* Handle ACTION_VIEW intents to store a URL that was used to open the app
* @param intent
Expand Down
5 changes: 5 additions & 0 deletions core/src/core-plugin-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ export interface AppPlugin extends Plugin {
*/
openUrl(options: { url: string }): Promise<{completed: boolean}>;

/**
* Opens the main activity of an app with the given package name (Android Only)
*/
openPackage(options: { packageName: string }): Promise<{ completed: boolean }>;

/**
* Get the URL the app was launched with, if any
*/
Expand Down
4 changes: 4 additions & 0 deletions core/src/web/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export class AppPluginWeb extends WebPlugin implements AppPlugin {
return Promise.resolve({ completed: true });
}

openPackage(_options: { packageName: string; }): Promise<{ completed: boolean; }> {
return Promise.resolve({ completed: true });
}

getLaunchUrl(): Promise<AppLaunchUrl> {
return Promise.resolve({ url: '' });
}
Expand Down
4 changes: 4 additions & 0 deletions site/docs-md/apis/app/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ console.log('Can open url: ', ret.value);
ret = await App.openUrl({ url: 'com.getcapacitor.myapp://page?id=ionicframework' });
console.log('Open url response: ', ret);

// Android Only
ret = await App.openPackage({ packageName: 'com.instagram.android' });
console.log('Open package response: ', ret);

ret = await App.getLaunchUrl();
if(ret && ret.url) {
console.log('App opened with URL: ' + ret.url);
Expand Down