Skip to content

Commit

Permalink
Added custom module to detect if an app is installed in Android
Browse files Browse the repository at this point in the history
  • Loading branch information
ignaciosantise committed Mar 15, 2023
1 parent ce83fd0 commit 7bb41f7
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.v2explorer;

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CustomPackage implements ReactPackage {

@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}

@Override
public List<NativeModule> createNativeModules(
ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();

modules.add(new InstalledAppModule(reactContext));

return modules;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.v2explorer;

import android.content.pm.PackageManager;
import android.os.Build;

import androidx.annotation.RequiresApi;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

// This module will be added to the SDK in the future
public class InstalledAppModule extends ReactContextBaseJavaModule {

public InstalledAppModule(ReactApplicationContext reactContext) {
super(reactContext);
}

@Override
public String getName() {
return "InstalledAppModule";
}

@RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
@ReactMethod
public void isAppInstalled(String packageName, Promise promise) {
PackageManager packageManager = getReactApplicationContext().getPackageManager();

try {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
packageManager.getPackageInfo(packageName, PackageManager.PackageInfoFlags.of(0));
} else {
packageManager.getPackageInfo(packageName, 0);
}
promise.resolve(true);
} catch (PackageManager.NameNotFoundException e) {
promise.resolve(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected List<ReactPackage> getPackages() {
@SuppressWarnings("UnnecessaryLocalVariable")
List<ReactPackage> packages = new PackageList(this).getPackages();
// Packages that cannot be autolinked yet can be added manually here, for example:
// packages.add(new MyReactNativePackage());
packages.add(new CustomPackage());
return packages;
}

Expand Down
26 changes: 26 additions & 0 deletions dapps/v2Explorer/src/modules/InstalledAppModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* This exposes the native CalendarModule module as a JS module. This has a
* function 'isAppInstalled' which takes the following parameter:
*
* 1. String name: A string representing the name of the app
* For Android: package name is required (e.g. com.facebook.katana)
* For iOS: scheme name is required (e.g. fb://) -> schemes need to be added to info.plist inside LSApplicationQueriesSchemes
*/
import {Linking, NativeModules, Platform} from 'react-native';

const {InstalledAppModule} = NativeModules;

interface InstalledAppInterface {
isAppInstalled(name: string): Promise<boolean>;
}

function isAppInstalled(name: string): Promise<boolean> {
const isAndroid = Platform.OS === 'android';
if (isAndroid) {
return InstalledAppModule.isAppInstalled(name);
} else {
return Linking.canOpenURL(name);
}
}

export default {isAppInstalled} as InstalledAppInterface;

0 comments on commit 7bb41f7

Please sign in to comment.