|
| 1 | +package com.jimlake; |
| 2 | + |
| 3 | +import com.facebook.react.bridge.ReactApplicationContext; |
| 4 | +import com.facebook.react.bridge.ReactContextBaseJavaModule; |
| 5 | +import com.facebook.react.bridge.ReactMethod; |
| 6 | +import com.facebook.react.bridge.Callback; |
| 7 | + |
| 8 | +import javax.annotation.Nullable; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.HashMap; |
| 11 | + |
| 12 | +import android.content.pm.ApplicationInfo; |
| 13 | +import android.content.pm.PackageInfo; |
| 14 | +import android.content.pm.PackageManager; |
| 15 | +import android.os.Build; |
| 16 | +import android.os.Bundle; |
| 17 | +import android.util.Log; |
| 18 | + |
| 19 | +public class RNInfoManifestBridgeModule extends ReactContextBaseJavaModule { |
| 20 | + public static final String TAG = "RNInfoManifestBridgeModule"; |
| 21 | + |
| 22 | + private final ReactApplicationContext reactContext; |
| 23 | + |
| 24 | + public RNInfoManifestBridgeModule(ReactApplicationContext reactContext) { |
| 25 | + super(reactContext); |
| 26 | + this.reactContext = reactContext; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public String getName() { |
| 31 | + return "RNInfoManifestBridge"; |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public @Nullable Map<String, Object> getConstants() { |
| 36 | + final Map<String, Object> constants = new HashMap<>(); |
| 37 | + |
| 38 | + try { |
| 39 | + final ApplicationInfo ai = reactContext.getPackageManager().getApplicationInfo(reactContext.getPackageName(),PackageManager.GET_META_DATA); |
| 40 | + final Bundle bundle = ai.metaData; |
| 41 | + for (String key : bundle.keySet()) { |
| 42 | + final String value = bundle.get(key).toString(); |
| 43 | + constants.put(key,value); |
| 44 | + } |
| 45 | + |
| 46 | + final int labelId = ai.labelRes; |
| 47 | + final String displayName = labelId == 0 ? ai.nonLocalizedLabel.toString() : reactContext.getString(labelId); |
| 48 | + constants.put("displayName",displayName); |
| 49 | + constants.put("bundleName",displayName); |
| 50 | + } catch (final Exception e) { |
| 51 | + Log.e(TAG,"Failed to pull things from manifest",e); |
| 52 | + } |
| 53 | + |
| 54 | + try { |
| 55 | + final PackageInfo pInfo = reactContext.getPackageManager().getPackageInfo(reactContext.getPackageName(),0); |
| 56 | + final String versionCode = String.valueOf(pInfo.versionCode); |
| 57 | + constants.put("bundleIdentifier",pInfo.packageName); |
| 58 | + constants.put("shortVersion",pInfo.versionName); |
| 59 | + constants.put("version",versionCode); |
| 60 | + constants.put("androidVersionCode",versionCode); |
| 61 | + } catch (final Exception e) { |
| 62 | + Log.e(TAG,"Failed to pull from package info",e); |
| 63 | + } |
| 64 | + |
| 65 | + constants.put("androidDeviceName",getDeviceName()); |
| 66 | + return constants; |
| 67 | + } |
| 68 | + |
| 69 | + private String getDeviceName() { |
| 70 | + final String brand = Build.BRAND; //= "motorola" |
| 71 | + final String model = Build.MODEL; //= "XT1053" |
| 72 | + |
| 73 | + String deviceType = ""; |
| 74 | + if (model.startsWith(brand)) { |
| 75 | + deviceType = model; |
| 76 | + } else { |
| 77 | + deviceType = brand + " " + model; |
| 78 | + } |
| 79 | + return deviceType; |
| 80 | + } |
| 81 | +} |
0 commit comments