|
| 1 | + |
| 2 | +package com.apsl.versionnumber; |
| 3 | + |
| 4 | +import com.facebook.react.bridge.ReactApplicationContext; |
| 5 | +import com.facebook.react.bridge.ReactContextBaseJavaModule; |
| 6 | +import com.facebook.react.bridge.ReactMethod; |
| 7 | +import com.facebook.react.bridge.Callback; |
| 8 | + |
| 9 | +import android.content.pm.ApplicationInfo; |
| 10 | +import android.content.pm.PackageManager.NameNotFoundException; |
| 11 | +import android.content.pm.PackageManager; |
| 12 | + |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +public class RNVersionNumberModule extends ReactContextBaseJavaModule { |
| 17 | + |
| 18 | + private final ReactApplicationContext reactContext; |
| 19 | + |
| 20 | + private static final String APP_VERSION = "appVersion"; |
| 21 | + private static final String APP_BUILD = "buildVersion"; |
| 22 | + |
| 23 | + public RNVersionNumberModule(ReactApplicationContext reactContext) { |
| 24 | + super(reactContext); |
| 25 | + this.reactContext = reactContext; |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public String getName() { |
| 30 | + return "RNVersionNumber"; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public Map<String, Object> getConstants() { |
| 35 | + final Map<String, Object> constants = new HashMap<>(); |
| 36 | + final PackageManager packageManager = this.reactContext.getPackageManager(); |
| 37 | + final String packageName = this.reactContext.getPackageName(); |
| 38 | + try { |
| 39 | + constants.put(APP_VERSION, packageManager.getPackageInfo(packageName, 0).versionName); |
| 40 | + constants.put(APP_BUILD, packageManager.getPackageInfo(packageName, 0).versionCode); |
| 41 | + } catch (NameNotFoundException e) { |
| 42 | + e.printStackTrace(); |
| 43 | + } |
| 44 | + return constants; |
| 45 | + } |
| 46 | +} |
0 commit comments