I am trying to translate a Java code into Delphi code in order to use the API of an Android application (DataWedge application).
To contextualize, DataWegde allows, among other things, to send to an Android application the barcode read by the laser reader of a Zebra terminal. To do this, it is necessary to create a profile dedicated to the application that indicates how the application receives the barcode. In particular this profile must contain the list of applications to which the profile applies.
The Java code below comes from DataWedge API documentation (techdocs.zebra.com/datawedge/13-0/guide/api/setconfig/)
// CREATE APP_LIST BUNDLES (apps and/or activities to be associated with the Profile)
Bundle bundleApp1 = new Bundle();
bundleApp1.putString("PACKAGE_NAME","com.symbol.emdk.simulscansample1");
bundleApp1.putStringArray("ACTIVITY_LIST", new String[]{
"com.symbol.emdk.simulscansample1.DeviceControl",
"com.symbol.emdk.simulscansample1.MainActivity",
"com.symbol.emdk.simulscansample1.ResultsActivity.*",
"com.symbol.emdk.simulscansample1.ResultsActivity2",
"com.symbol.emdk.simulscansample1.SettingsFragment1"});
Bundle bundleApp2 = new Bundle();
bundleApp2.putString("PACKAGE_NAME","com.example.intents.datawedgeintent");
bundleApp2.putStringArray("ACTIVITY_LIST", new String[]{
"com.example.intents.datawedgeintent.DeviceControl",
"com.example.intents.datawedgeintent.MainActivity",
"com.example.intents.datawedgeintent.ResultsActivity",
"com.example.intents.datawedgeintent.SettingsFragment1"});
// NEXT APP_LIST BUNDLE(S) INTO THE MAIN BUNDLE
bMain.putParcelableArray("APP_LIST", new Bundle[]{
bundleApp1
,bundleApp2
});
Intent i = new Intent();
i.setAction("com.symbol.datawedge.api.ACTION");
i.putExtra("com.symbol.datawedge.api.SET_CONFIG", bMain);
this.sendBroadcast(i);
I have difficulties with this part of the code:
bMain.putParcelableArray("APP_LIST", new Bundle[]{
bundleApp1
,bundleApp2
});
I'm not familiar with Java, but "new Bundle[]" creates an array of bundles, and that array is put into the bMain bundle using "ParcelableArray", which seems to be a different data type.
With Delphi, I can only do this by explicitly transtyping the variable. So I wrote the following code (simpler code because I only have one application and the API admits the wildcard "*" as an activities list):
// Variables declarations
var activitiesList : TJavaObjectArray<JString>;
iSetConfig: JIntent;
bMain : JBundle;
bApp : JBundle;
bAppList : TJavaObjectArray<JBundle>;
// Also tried that
// bAppList : TJavaObjectArray<JParcelable>;
begin
// Application
activitiesList := TJavaObjectArray<JString>.Create(1);
// "j" function converts Delphi string to Java String
// like this jStr := TAndroidHelper.StringToJString(dStr)
// just making the code more readable
activitiesList.Items[0] := j('*');
bApp := TJBundle.Create;
bApp.putString(j('PACKAGE_NAME'),j('my.application.test'));
bApp.putStringArray(j('ACTIVITY_LIST'), activitiesList);
bAppList := TJavaObjectArray<JBundle>.create(1);
// Also tried that
// bAppList := TJavaObjectArray<JParcelable>.create(1);
bAppList.Items[0] := bApp;
// Also tried that
// bAppList.Items[0] := JParcelable(bApp);
bMain := TJBundle.Create;
bMain.putString(j('CONFIG_MODE'),j('UPDATE'));
bMain.putString(j('PROFILE_NAME'),j('test'));
bMain.putString(j('RESET_CONFIG'), j('true'));
bMain.putString(j('PROFILE_ENABLED'),j('true'));
bMain.putParcelableArray(j('APP_LIST'),TJavaObjectArray<JParcelable>(bAppList));
// Also tried that
// bMain.putParcelableArray(j('APP_LIST'),bAppList);
iSetConfig := TJIntent.Create;
iSetConfig.setAction(j('com.symbol.datawedge.api.ACTION'));
iSetConfig.putExtra(j('com.symbol.datawedge.api.SET_CONFIG'),bMain);
TAndroidHelper.Context.sendBroadcast(iSetConfig);
end;
Running this code works except that the application list (bAppList) is not created in the DataWedge application, while the other parameters are applied properly. I have also created other configurations successfully where no ParcelableArray is involved.
I finally found the explanation. The code works fine. But I had another profile using the same app on the device and you just can't associate the same app with multiple profiles. So deleting the existing profile solved the problem.