forked from reown-com/react-native-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added custom module to detect if an app is installed in Android
- Loading branch information
1 parent
ce83fd0
commit 7bb41f7
Showing
4 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
dapps/v2Explorer/android/app/src/main/java/com/v2explorer/CustomPackage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
dapps/v2Explorer/android/app/src/main/java/com/v2explorer/InstalledAppModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |