Skip to content

Instantly share code, notes, and snippets.

@mariodantas
Last active November 21, 2021 19:54
Show Gist options
  • Save mariodantas/429cb93d3f1f78ae625e0cdea6565a5b to your computer and use it in GitHub Desktop.
Save mariodantas/429cb93d3f1f78ae625e0cdea6565a5b to your computer and use it in GitHub Desktop.
This code shows how to call MPS Fuel API
package <YOURPACKAGENAME>;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
public class TestAPI extends AppCompatActivity {
// Here we declare the name of the package which handles the API and the API service itself
public static final String PACKAGENAME="com.mariodantas.mpsfuelfull"; // mandatory
public static final String APIPATH="services.api"; // mandatory
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// The below variable can be created inside onAttach or onCreate or global
ActivityResultLauncher<Intent> api=registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent data=result.getData();
// Check if Intent returns a valid result with result
if (data != null && data.hasExtra("responseCode") && data.hasExtra("responseString")) {
// Ok API returns valid data
// responseCode can be OK or KO
// Where OK means that the requested command has ben executed successfully
// Where KO means that the requested command execution has failed
// responseString can be a text returned by the API (like a message)
// Put response into an array of string
String[] response=new String[2];
response[0]=data.getStringExtra("responseCode");
response[1]=data.getStringExtra("responseString");
// Do whatever you want with this valid data (response[])
// i.e. I will just log the results
Log.d("myTag", response[0] + " " + response[1]);
//
} else {
// Here do what you want when the API does not return a valid result
Log.d("myTag", "Not valid result from API");
}
} else {
// Here do what you want when the API returns CANCELLED
Log.d("myTag", "API returns CANCELED");
}
});
//
//
// API calls examples
//
//
// Example of code to call the API to change the fuel gauge
if (isPackageInstalled(PACKAGENAME)) {
Intent intent;
try {
// We set the intent that will call the API
intent=new Intent(this, Class.forName((PACKAGENAME + "." + APIPATH)));
// We set the action required (list of actions can be retrieved in APP Manifest)
intent.setAction("action.setfuelgaugevalueinpercent"); // Action name
// Were we set the key and value to be set
intent.putExtra("fuel_gauge", "90");
// Here we launch the API call
api.launch(intent);
} catch (ClassNotFoundException e) {
// We print a stack trace if error occurs in commands above
e.printStackTrace();
}
} else {
Log.e("myTag", PACKAGENAME + " not installed");
}
// Example of code to call the API to change the Current Ethanol mix
if (isPackageInstalled(PACKAGENAME)) {
Intent intent;
try {
// We set the intent that will call the API
intent=new Intent(this, Class.forName((PACKAGENAME + "." + APIPATH)));
// We set the action required (list of actions can be retrieved in APP Manifest)
intent.setAction("action.setcurrentethanolmix"); // Action name
// Were we set the key and value to be set
intent.putExtra("ethanol_mix", "50");
// Here we launch the API call
api.launch(intent);
} catch (ClassNotFoundException e) {
// We print a stack trace if error occurs in commands above
e.printStackTrace();
}
} else {
Log.e("myTag", PACKAGENAME + " not installed");
}
}
// This will check if package is installed before attempt to call
public boolean isPackageInstalled(String p) {
try {
getPackageManager().getPackageInfo(p, PackageManager.GET_META_DATA);
} catch (PackageManager.NameNotFoundException e) {
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment