目录

Common API Manual Conversion Guide

Return Value Is of the Non-Set Type

public java.lang.Object getExtra(java.lang.String name)

public java.lang.Object getExtra(java.lang.String name, java.lang.Object defaultValue)

public java.io.Serializable getSerializable(java.lang.String key)

public <T extends android.os.Parcelable> T getParcelable(java.lang.String key)

public java.lang.Object get(java.lang.String key)

If the return value is not of the XMS type, you do not need to perform manual conversion.

If the return value is of the XMS type, you need to perform manual conversion.

Example:

In the code before conversion, Detector is of the XMS type.

Bundle bundle = new Bundle();
Detector detector = null;
detector = (Detector) bundle.getParcelable("key");

Manually convert bundle.getParcelable() as follows:

Bundle bundle = new Bundle();
Detector detector = null;
if (org.xms.g.utils.GlobalEnvSetting.isHms()) {
    detector = (Detector) Utils.getXmsObjectWithHmsObject(bundle.getParcelable("key"));
} else {
    detector = (Detector) Utils.getXmsObjectWithGmsObject(bundle.getParcelable("key"));
}

For a dummy class, leave if empty.

Return Value Is of the Array Type

public android.os.Parcelable[] getParcelableArrayExtra(java.lang.String name)

public android.os.Parcelable[] getParcelableArray(java.lang.String key)

If the elements in the returned array are not of the XMS type, you do not need to perform manual conversion.

If the elements in the returned array are of the XMS type, you need to perform manual conversion.

Example:

In the code before conversion, Detector is of the XMS type.

Bundle bundle = new Bundle();
Detector[] detectors = null;
detectors = (Detector[]) bundle.getParcelableArray("key");

Manually convert bundle.getParcelableArray("key") as follows:

Bundle bundle = new Bundle();
Detector[] detectors = null;
detectors = Utils.genericArrayCopy(bundle.getParcelableArray("key"), Detector.class, new Function<Parcelable, Detector>() {
    @Override
    public Detector apply(Parcelable parcelable) {
        if (org.xms.g.utils.GlobalEnvSetting.isHms()) {
            return (Detector) Utils.getXmsObjectWithHmsObject(parcelable);
        } else {
            return (Detector) Utils.getXmsObjectWithGmsObject(parcelable);
        }
    }
});

For a dummy class, leave if empty.

Return Value Is of the ArrayList Type

public <T extends android.os.Parcelable> java.util.ArrayList<T> getParcelableArrayListExtra(java.lang.String name)

public <T extends android.os.Parcelable> java.util.ArrayList<T> getParcelableArrayList(java.lang.String key)

If the actual type of generic T is not XMS, you do not need to perform manual conversion.

If the actual type of generic T is XMS, you need to perform manual conversion.

Example:

In the code before conversion, Detector is of the XMS type.

ArrayList<Detector> list = new ArrayList<>(); // Here, new is used only as an example. In actual code, the corresponding ArrayList object needs to be obtained.
Intent intent = new Intent(); // Here, new is used only as an example. In actual code, the corresponding Intent object needs to be obtained.
ArrayList<Detector> detectedActivities = intent.getParcelableArrayListExtra("key");

Manually convert getParcelableArrayListExtra("key") as follows:

ArrayList<Detector> list = new ArrayList<>(); // Here, new is used only as an example. In actual code, the corresponding ArrayList object needs to be obtained.
Intent intent = new Intent(); // Here, new is used only as an example. In actual code, the corresponding Intent object needs to be obtained.
ArrayList<Detector> detectedActivities = Utils.mapList2X(intent.getParcelableArrayListExtra("key"),
GlobalEnvSetting.isHms());

For a dummy class, set the second parameter in the mapList2X method to false.

Return Value Is of the SparseArray Type

public <T extends android.os.Parcelable> android.util.SparseArray<T> getSparseParcelableArray(java.lang.String key)

If the actual type of generic T is not XMS, you do not need to perform manual conversion.

If the actual type of generic T is XMS, you need to perform manual conversion.

SparseArray<Credential> detectorSparseArray = Utils.genericArrayCopy(bundle.getSparseParcelableArray("key"), new Function<Parcelable, Credential>() {
    @Override
    public Credential apply(Parcelable parcelable) {
        if (org.xms.g.utils.GlobalEnvSetting.isHms()) {
            return (Credential) Utils.getXmsObjectWithHmsObject(parcelable);
        } else {
            return (Credential) Utils.getXmsObjectWithGmsObject(parcelable);
        }
    }
});

For a dummy class, leave if empty.

Input Parameter Object Is of the List Type

public android.content.Intent putParcelableArrayListExtra(java.lang.String name, java.util.ArrayList<? extends Parcelable> value)

public void putParcelableArrayList(java.lang.String key, java.util.ArrayList<? extends android.os.Parcelable> value)

public void putParcelableList(java.lang.String key, java.util.List<? extends android.os.Parcelable> value)

If the value is not of the XMS type, you do not need to perform manual conversion.

If the value is of the XMS type, you need to manually convert it to the GMS or HMS type.

Example:

In the code before conversion, Detector is of the XMS type.

ArrayList<Detector> list = new ArrayList<>(); // Here, new is used only as an example. In actual code, the corresponding ArrayList object needs to be obtained.
Intent intent = new Intent(); // Here, new is used only as an example. In actual code, the corresponding Intent object needs to be obtained.
intent.putParcelableArrayListExtra("key", list);

Manually convert list in putParcelableArrayListExtra("key", list) as follows:

ArrayList<Detector> list = new ArrayList<>(); // Here, new is used only as an example. In actual code, the corresponding ArrayList object needs to be obtained.
Intent intent = new Intent(); // Here, new is used only as an example. In actual code, the corresponding Intent object needs to be obtained.
intent.putParcelableArrayListExtra("key", ((ArrayList) Utils.mapList2GH((List<Detector>) list, org.xms.g.utils.GlobalEnvSetting.isHms())));

For a dummy class, set the second parameter in the mapList2GH method to false.

Input Parameter Object Is of the Parcelable or Serializable Type

public void putParcelable(java.lang.String key, android.os.Parcelable value)

public void putSerializable(java.lang.String key, java.io.Serializable value)

If the value is not of the XMS type, you do not need to perform manual conversion.

If the value is of the XMS type, you need to manually convert it to the GMS or HMS type.

Example:

DetectedActivity detectedActivity = new DetectedActivity(); // Here, new is used only as an example. In actual code, the corresponding Activity object needs to be obtained.
Intent intent = new Intent(BROADCAST_INTENT_ACTION);
intent.putExtra(DETECTED_ACTIVITY_EXTRA_ID, detectedActivity);

After conversion:

DetectedActivity detectedActivity = new DetectedActivity(); // Here, new is used only as an example. In actual code, the corresponding Activity object needs to be obtained.
Intent intent = new Intent(BROADCAST_INTENT_ACTION);
if (org.xms.g.utils.GlobalEnvSetting.isHms()) {
    intent.putExtra(DETECTED_ACTIVITY_EXTRA_ID, (com.huawei.hms.location.ActivityIdentificationData)detectedActivity.getHInstance());
} else {
    intent.putExtra(DETECTED_ACTIVITY_EXTRA_ID,(com.google.android.gms.location.DetectedActivity)detectedActivity.getGInstance());
}

For a dummy class, leave if empty.

Input Parameter Object Is of the Array Type

public void putParcelableArray(java.lang.String key, android.os.Parcelable[] value)

If the elements in the value are not of the XMS type, you do not need to perform manual conversion.

If the elements in the value are of the XMS type, you need to manually convert them to the GMS or HMS type.

DataSource[] dataSources = null;
Bundle bundle = new Bundle(); // Here, new is used only as an example. In actual code, the corresponding Bundle object needs to be obtained.
if (org.xms.g.utils.GlobalEnvSetting.isHms()) {
    bundle.putParcelableArray("key", Utils.genericArrayCopy(dataSources, com.huawei.hms.hihealth.data.DataCollector.class, new Function<DataSource, DataCollector>() {
        @Override
        public com.huawei.hms.hihealth.data.DataCollector apply(DataSource dataSource) {
            return (DataCollector) dataSource.getHInstance();
        }
    }));
} else {
    bundle.putParcelableArray("key", Utils.genericArrayCopy(dataSources, com.google.android.gms.fitness.data.DataSource.class, new Function<DataSource, com.google.android.gms.fitness.data.DataSource>() {
        @Override
        public com.google.android.gms.fitness.data.DataSource apply(DataSource dataSource) {
            return (com.google.android.gms.fitness.data.DataSource) dataSource.getGInstance();
        }
    }));
}

For a dummy class, leave if empty.

Input Parameter Object Is of the SparseArray Type

public void putSparseParcelableArray(java.lang.String key, android.util.SparseArray<? extends android.Parcelable> value)

If the elements in the value are not of the XMS type, you do not need to perform manual conversion.

If the elements in the value are of the XMS type, you need to manually convert them to the GMS or HMS type.

SparseArray<DataSource> sparseArray = null;
Bundle bundle = new Bundle(); // Here, new is used only as an example. In actual code, the corresponding Bundle object needs to be obtained.
if (org.xms.g.utils.GlobalEnvSetting.isHms()) {
    bundle.putSparseParcelableArray("key", Utils.genericArrayCopy(sparseArray, new Function<DataSource, com.huawei.hms.hihealth.data.DataCollector>() {
        @Override
        public com.huawei.hms.hihealth.data.DataCollector apply(DataSource dataSource) {
            return (com.huawei.hms.hihealth.data.DataCollector) dataSource.getHInstance();
        }
    }));
} else {
    bundle.putSparseParcelableArray("key", Utils.genericArrayCopy(sparseArray, new Function<DataSource, com.google.android.gms.fitness.data.DataSource>() {
        @Override
        public com.google.android.gms.fitness.data.DataSource apply(DataSource dataSource) {
            return (com.google.android.gms.fitness.data.DataSource) dataSource.getGInstance();
        }
    }));
}

For a dummy class, leave if empty.

toJson

public java.lang.String toJson(java.lang.Object src)

public void toJson(java.lang.Object src, java.lang.Appendable writer)

If src is not of the XMS type, you do not need to perform manual conversion.

If src is of the XMS type, you need to manually convert it to the GMS or HMS type.

Example:

In the code before conversion, DataSource is of the XMS type.

Gson gson = new Gson();
DataSource dataSource = null;
String str = gson.toJson(dataSource);

Manually convert gson.toJson(dataSource) as follows:

String string = null;
if (org.xms.g.utils.GlobalEnvSetting.isHms()) {
    string = gson.toJson(dataSource.getHInstance());
} else {
    string = gson.toJson(dataSource.getGInstance());
}

For a dummy class, leave if empty.

toJson Input Parameter Object Is of the Type Type

public java.lang.String toJson(java.lang.Object src, java.lang.reflect.Type typeOfSrc)

void toJson(java.lang.Object src, java.lang.reflect.Type typeOfSrc, java.lang.Appendable writer)

void toJson(java.lang.Object src, java.lang.reflect.Type typeOfSrc, JsonWriter writer)

If src is not of the XMS type, or src is of a set type (such as ArrayList, Set, Map, or Array) and its elements are not of the XMS type, you do not need to perform manual conversion.

If src is of the XMS type, or src is of a set type (such as ArrayList, Set, Map, or Array) and its elements are of the XMS type, you need to perform manual conversion.

Example:

If src is of the ArrayList type and its elements are of the XMS type, call the mapList2GH method in Utils to perform manual conversion as follows:

Gson gson = new Gson();
List<DataSource> dataSourceList = new ArrayList<>(); // Here, new is used only as an example. In actual code, the corresponding ArrayList object needs to be obtained.
gson.toJson(Utils.mapList2GH(dataSourceList, org.xms.g.utils.GlobalEnvSetting.isHms()), ArrayList.class);

If src is of the Map type and its elements are of the XMS type, call the converMap method in Utils to perform manual conversion as follows:

Map<String, DetectedActivity> entities_g_map = new HashMap<>(); // Here, new is used only as an example. In actual code, the corresponding Map object needs to be obtained.
if (org.xms.g.utils.GlobalEnvSetting.isHms()) {    
    Type typeOfSrc = new TypeToken<HashMap<String, com.huawei.hms.location.ActivityIdentificationData>>() {}.getType();
    Map<String, com.huawei.hms.location.ActivityIdentificationData> map = Utils.convertMap(entities_g_map, new Function<DetectedActivity, com.huawei.hms.location.ActivityIdentificationData>() {
        @Override
        public com.huawei.hms.location.ActivityIdentificationData apply(DetectedActivity detectedActivity) {
            return (ActivityIdentificationData) detectedActivity.getHInstance();
        }
    });
    new Gson().toJson(map, typeOfSrc);
} else {
    Type typeOfSrc = new TypeToken<HashMap<String, com.google.android.gms.location.DetectedActivity>>() {}.getType();
    Map<String, com.google.android.gms.location.DetectedActivity> map = Utils.convertMap(entities_g_map,
            new Function<DetectedActivity, com.google.android.gms.location.DetectedActivity>() {
                @Override
                public com.google.android.gms.location.DetectedActivity apply(DetectedActivity detectedActivity) {
                    return (com.google.android.gms.location.DetectedActivity) detectedActivity.getGInstance();
                }
            });
    new Gson().toJson(map, typeOfSrc);
}

If src is of the Set type and its elements are of the XMS type, call the mapCollection2GH method to perform manual conversion as follows:

List<Object> entities_g_set = new ArrayList<>(); // Here, new is used only as an example. In actual code, the corresponding Collection object needs to be obtained.
if (org.xms.g.utils.GlobalEnvSetting.isHms()) {
    HashSet set = (HashSet) Utils.mapCollection2GH(entities_g_set, true);
    Type typeOfSrc = new TypeToken<HashSet<com.huawei.hms.location.ActivityIdentificationData>>(){}.getType();
    new Gson().toJson(set, typeOfSrc);
} else {
    HashSet set = (HashSet) Utils.mapCollection2GH(entities_g_set, false);
    Type typeOfSrc = new TypeToken<HashSet<com.google.android.gms.location.DetectedActivity>>(){}.getType();
    new Gson().toJson(set, typeOfSrc);
}

If src is of the Array type and its elements are of the XMS type, call the genericArrayCopy method to perform manual conversion as follows:

if (org.xms.g.utils.GlobalEnvSetting.isHms()) {
    Type typeOfSrc = new TypeToken<com.huawei.hms.location.ActivityIdentificationData[]>() {}.getType();
    com.huawei.hms.location.ActivityIdentificationData[]  newDetectedActivities = Utils.genericArrayCopy(oldDetectedActivities, com.huawei.hms.location.ActivityIdentificationData.class, new Function<DetectedActivity, com.huawei.hms.location.ActivityIdentificationData>() {
    @Override
        public com.huawei.hms.location.ActivityIdentificationData apply(DetectedActivity detectedActivity) {
            return (com.huawei.hms.location.ActivityIdentificationData) detectedActivity.getHInstance();
       }
    });
    new Gson().toJson(newDetectedActivities, typeOfSrc);
} else {
    Type typeOfSrc = new TypeToken<com.google.android.gms.location.DetectedActivity[]>() {}.getType();
    com.google.android.gms.location.DetectedActivity[]  newDetectedActivities = Utils.genericArrayCopy(oldDetectedActivities, com.google.android.gms.location.DetectedActivity.class, new Function<DetectedActivity, com.google.android.gms.location.DetectedActivity>() {
    @Override
        public com.google.android.gms.location.DetectedActivity apply(DetectedActivity detectedActivity) {
            return (com.google.android.gms.location.DetectedActivity) detectedActivity.getGInstance();
       }
    });
    new Gson().toJson(newDetectedActivities, typeOfSrc);
}

If src is of the Object type, perform manual conversion as follows:

DataSource dataSource = new DataSource(); // Here, new is used only as an example. In actual code, the corresponding DataSource object needs to be obtained.
Gson gson = new Gson(); // Here, new is used only as an example. In actual code, the corresponding Gson object needs to be obtained.
if (org.xms.g.utils.GlobalEnvSetting.isHms()) {
    gson.toJson(dataSource.getHInstance(), com.huawei.hms.hihealth.data.DataCollector.class);
} else {
    gson.toJson(dataSource.getGInstance(), com.google.android.gms.fitness.data.DataSource.class);
}

For a dummy class, leave if empty.

toJson Input Parameter Object Is of the JsonElement Type

public java.lang.String toJson(com.google.gson.JsonElement jsonElement)

void toJson(com.google.gson.JsonElement jsonElement, java.lang.Appendable writer)

public void toJson(com.google.gson.JsonElement jsonElement, com.google.gson.stream.JsonWriter writer)

All input parameters of this API must be of the JsonElement type. If any parameter is of the XMS type, you need to convert it to the GMS or HMS type first and then to the JsonElement type. For details, please refer to toJson Input Parameter Object Is of the Type Type.

fromJson

public <T> T fromJson(java.lang.String json, java.lang.Class<T> classOfT)

public <T> T fromJson(java.lang.String json, java.lang.reflect.Type typeOfT)

public <T> T fromJson(java.io.Reader json, java.lang.Class<T> classOfT)

public <T> T fromJson(java.io.Reader json, java.lang.reflect.Type typeOfT)

public <T> T fromJson(com.google.gson.stream.JsonReader reader, java.lang.reflect.Type typeOfT)

public <T> T fromJson(com.google.gson.JsonElement json, java.lang.Class<T> classOfT)

public <T> T fromJson(com.google.gson.JsonElement json, java.lang.reflect.Type typeOfT)

If the second parameter is not of the XMS type or is of a set type (such as ArrayList, Set, or Map), you do not need to perform manual conversion.

If the second parameter is of the XMS type, you need to perform manual conversion.

Example:

If classOfT is not of the XMS type, you do not need to perform manual conversion.

If classOfT is of the XMS type, you need to perform manual conversion.

If the value returned by fromJson is not of a set type (such as List, Map, Set, or Array), perform manual conversion as follows:

DataSource dataSource = null;
String string =""; // JSON string to be converted.
Gson gson = new Gson(); // Here, new is used only as an example. In actual code, the corresponding Gson object needs to be obtained.
if (org.xms.g.utils.GlobalEnvSetting.isHms()) {
    dataSource = (DataSource) Utils.getXmsObjectWithHmsObject(gson.fromJson(string, com.huawei.hms.hihealth.data.DataCollector.class));
} else {
    dataSource = (DataSource) Utils.getXmsObjectWithGmsObject(gson.fromJson(string, com.google.android.gms.fitness.data.DataSource.class));
}

If the value returned by fromJson is of the ArrayList type, perform manual conversion as follows:

String str = "listJsonStr"; // JSON string to be converted.
Gson gson = new Gson(); // Here, new is used only as an example. In actual code, the corresponding Gson object needs to be obtained.
if (org.xms.g.utils.GlobalEnvSetting.isHms()) {
    Type typeOfSrc = new TypeToken<ArrayList<com.huawei.hms.location.ActivityIdentificationData>>() {}.getType();
    List<DetectedActivity> detectedActivityList = Utils.mapList2X(gson.fromJson(str, typeOfSrc), true);
} else {
    Type typeOfSrc = new TypeToken<ArrayList<com.google.android.gms.location.DetectedActivity>>() {}.getType();
    List<DetectedActivity> detectedActivityList = Utils.mapList2X(gson.fromJson(str, typeOfSrc), false);
}

If the value returned by fromJson is of the Map type, perform manual conversion as follows:

String str = "MapJsonStr";// JSON string to be converted.
Gson gson = new Gson(); // Here, new is used only as an example. In actual code, the corresponding Gson object needs to be obtained.
if (org.xms.g.utils.GlobalEnvSetting.isHms()) {
    Type typeOfSrc = new TypeToken<HashMap<String, com.huawei.hms.location.ActivityIdentificationData>>(){}.getType();
    Map<String, DetectedActivity> map = Utils.convertMap(gson.fromJson(str, typeOfSrc), new Function<com.huawei.hms.location.ActivityIdentificationData, DetectedActivity>() {
        @Override
        public DetectedActivity apply(ActivityIdentificationData activityIdentificationData) {
            return (DetectedActivity) Utils.getXmsObjectWithHmsObject(activityIdentificationData);
        }
    });
} else {
    Type typeOfSrc = new TypeToken<HashMap<String, com.google.android.gms.location.DetectedActivity>>(){}.getType();
    Map<String, DetectedActivity> map = Utils.convertMap(gson.fromJson(str, typeOfSrc), new Function<com.google.android.gms.location.DetectedActivity, DetectedActivity>() {
        @Override
        public DetectedActivity apply(com.google.android.gms.location.DetectedActivity detectedActivity) {
            return (DetectedActivity) Utils.getXmsObjectWithGmsObject(detectedActivity);
        }
    });
}

If the value returned by fromJson is of the Set type, perform manual conversion as follows:

String str = "MapJsonStr";// JSON string to be converted.
Gson gson = new Gson(); // Here, new is used only as an example. In actual code, the corresponding Gson object needs to be obtained.
if (org.xms.g.utils.GlobalEnvSetting.isHms()) {
    Type typeOfSrc = new TypeToken<HashSet<com.huawei.hms.location.ActivityIdentificationData>>(){}.getType();    
    Set<com.huawei.hms.location.ActivityIdentificationData> set = (Set<com.huawei.hms.location.ActivityIdentificationData>)gson.fromJson(str, typeOfSrc);    
    HashSet<DetectedActivity> newSet = (HashSet) Utils.mapCollection2X(set, true);
} else {
    Type typeOfSrc = new TypeToken<HashSet<com.google.android.gms.location.DetectedActivity>>(){}.getType();    
    Set<com.google.android.gms.location.DetectedActivity> set = (Set<com.google.android.gms.location.DetectedActivity>)gson.fromJson(str, typeOfSrc);   
    HashSet<DetectedActivity> newSet = (HashSet) Utils.mapCollection2X(set, false);
}

If the value returned by fromJson is of the Array type, perform manual conversion as follows:

String str = "MapJsonStr";// JSON string to be converted.
Gson gson = new Gson(); // Here, new is used only as an example. In actual code, the corresponding Gson object needs to be obtained.
if (org.xms.g.utils.GlobalEnvSetting.isHms()) {
    Type typeOfSrc = new TypeToken<com.huawei.hms.location.ActivityIdentificationData[]>(){}.getType();
    com.huawei.hms.location.ActivityIdentificationData[] activityIdentificationData = gson.fromJson(str, typeOfSrc);
    DetectedActivity[]  detectedActivities  = Utils.genericArrayCopy(activityIdentificationData,
            DetectedActivity.class, new Function<com.huawei.hms.location.ActivityIdentificationData, DetectedActivity>() {
                @Override
                public DetectedActivity apply(ActivityIdentificationData activityIdentificationData) {
                    return (DetectedActivity) Utils.getXmsObjectWithHmsObject(activityIdentificationData);
                }
            });
} else {
    Type typeOfSrc = new TypeToken<com.google.android.gms.location.DetectedActivity[]>(){}.getType();
    com.google.android.gms.location.DetectedActivity[] detectedActivities = gson.fromJson(str, typeOfSrc);
    DetectedActivity[]  newDetectedActivities  = Utils.genericArrayCopy(detectedActivities,
            DetectedActivity.class, new Function<com.google.android.gms.location.DetectedActivity, DetectedActivity>() {
                @Override
                public DetectedActivity apply(com.google.android.gms.location.DetectedActivity detectedActivity) {
                    return (DetectedActivity) Utils.getXmsObjectWithGmsObject(detectedActivity);
                }
            });
}

For a dummy class, leave if empty.