Repository navigation
Java Library
The API contract is one dependency-free Java file, ClusterTuneApi.java, in the api-contract module. It holds every public name and value (authority, methods, extras, reply keys, error codes, notification URIs) as constants; names and values never change within an API version. It has no Android or Kotlin dependency and works from Java and Kotlin.
Either copy the file into your sources (keep its package, com.aure.clustertune.api.contract), or publish the module to your local Maven repository:
git clone https://github.com/AurelioB/ClusterTune.git
cd ClusterTune
./gradlew :api-contract:publishToMavenLocal// settings.gradle.kts
dependencyResolutionManagement { repositories { mavenLocal(); google(); mavenCentral() } }
// app/build.gradle.kts
dependencies { implementation("com.aure.clustertune:api-contract:1.0.0") }No public Maven repository is configured.
Declare package visibility so Android lets your app reach ClusterTune (Android 11+):
<queries>
<provider android:authorities="com.aure.clustertune.api" />
</queries>There is no permission to declare or request. The first call asks the user to allow your app (see Access).
| Class | Contents |
|---|---|
ClusterTuneApi |
VERSION (1), PACKAGE, AUTHORITY, URI, frequency units, PATH_*/URI_* notification URIs |
ClusterTuneApi.Resource |
STATE, TUNING, PROFILE, APP, DEVICE, API (also the shortcut method names) |
ClusterTuneApi.Method |
STATE_GET, TUNING_GET, TUNING_SET, PROFILE_LIST, PROFILE_APPLY, APP_LIST, APP_SET, APP_CLEAR, DEVICE_GET, API_INFO, and REQUEST for a whole request object
|
ClusterTuneApi.Extra |
API_VERSION, IF_REVISION
|
ClusterTuneApi.Result |
Reply keys: API_VERSION, OK, CODE, MESSAGE, DATA
|
ClusterTuneApi.ErrorCode |
Every code value, including ACCESS_DENIED, ACCESS_PENDING, CONFLICT, AMBIGUOUS_PROFILE
|
ClusterTuneApi.Shell |
The ADB broadcast receiver and its extras |
Calls are synchronous Binder calls: run them off the main thread. The first call from your app can wait up to 60 seconds for the user's answer, and starting ClusterTune's privileged host can take a moment.
import android.content.Context
import android.net.Uri
import android.os.Bundle
import com.aure.clustertune.api.contract.ClusterTuneApi
import com.aure.clustertune.api.contract.ClusterTuneApi.ErrorCode
import com.aure.clustertune.api.contract.ClusterTuneApi.Method
import com.aure.clustertune.api.contract.ClusterTuneApi.Result
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.json.JSONObject
class ClusterTuneException(val code: String, message: String?) : Exception("$code: $message")
suspend fun Context.clusterTune(method: String, arg: String? = null, extras: Bundle? = null): JSONObject =
withContext(Dispatchers.IO) {
val reply = contentResolver.call(Uri.parse(ClusterTuneApi.URI), method, arg, extras)
?: throw ClusterTuneException(ErrorCode.OPERATION_FAILED, "ClusterTune is not installed")
if (!reply.getBoolean(Result.OK)) {
throw ClusterTuneException(reply.getString(Result.CODE)!!, reply.getString(Result.MESSAGE))
}
JSONObject(reply.getString(Result.DATA)!!)
}
// Usage, from a coroutine:
val profiles = context.clusterTune(Method.PROFILE_LIST).getJSONArray("profiles")
context.clusterTune(Method.PROFILE_APPLY, "medium")
val applied = context.clusterTune(Method.TUNING_SET, "cpu6.max=2.4GHz gpu.max=600")
applied.optJSONArray("adjusted") // values that were snapped to a supported step, if anyContentResolver.call throws IllegalArgumentException when ClusterTune is not installed or not visible (missing <queries>); catch it where you call.
import android.content.ContentResolver;
import android.net.Uri;
import android.os.Bundle;
import com.aure.clustertune.api.contract.ClusterTuneApi;
import org.json.JSONObject;
// Off the main thread, for example in an Executor:
Bundle reply = resolver.call(Uri.parse(ClusterTuneApi.URI), ClusterTuneApi.Method.STATE_GET, null, null);
if (reply != null && reply.getBoolean(ClusterTuneApi.Result.OK)) {
JSONObject state = new JSONObject(reply.getString(ClusterTuneApi.Result.DATA));
String profile = state.isNull("profile") ? "custom values" : state.getJSONObject("profile").getString("name");
} else if (reply != null) {
String code = reply.getString(ClusterTuneApi.Result.CODE); // e.g. ErrorCode.ACCESS_PENDING
String message = reply.getString(ClusterTuneApi.Result.MESSAGE);
}| Code | What to do |
|---|---|
access_pending |
The user has not answered the question yet, or it expired. Tell them to allow your app in ClusterTune's notification or dialog, then retry. |
access_denied |
The user denied your app. Explain that it can be allowed in ClusterTune under Settings → App access. |
Only the call that raised the question waits for it; other calls meanwhile return access_pending immediately, so a burst of calls cannot tie up threads.
Read the revision, then make the write conditional on it:
val revision = context.clusterTune(Method.TUNING_GET).getString("revision")
val extras = Bundle().apply { putString(ClusterTuneApi.Extra.IF_REVISION, revision) }
try {
context.clusterTune(Method.TUNING_SET, "cpu6.max=2.4GHz", extras)
} catch (e: ClusterTuneException) {
if (e.code == ErrorCode.CONFLICT) { /* someone changed the settings: re-read and decide again */ }
}A retry after a lost reply is then safe: if the first call went through, the retry fails with conflict instead of applying twice. See Conditional writes.
Observe instead of polling. Notifications carry no data; re-read with the matching method.
val observer = object : ContentObserver(Handler(Looper.getMainLooper())) {
override fun onChange(selfChange: Boolean, uri: Uri?) {
when (uri?.toString()) {
ClusterTuneApi.URI_STATE -> { /* re-read state.get */ }
ClusterTuneApi.URI_TUNING -> { /* re-read tuning.get */ }
ClusterTuneApi.URI_PROFILE -> { /* re-read profile.list */ }
ClusterTuneApi.URI_APP -> { /* re-read app.list */ }
}
}
}
contentResolver.registerContentObserver(Uri.parse(ClusterTuneApi.URI), true, observer)
// later: contentResolver.unregisterContentObserver(observer)Observing needs no approval. See Change notifications for when each URI fires.
- Store profile IDs, not names: users can rename profiles. Names are convenient for people typing commands.
- Ignore reply fields you do not know; new ones may appear within version 1.
- Read frequency steps, policy IDs and governors from
device.getinstead of assuming them; devices differ. - Pass
api_version(ClusterTuneApi.Extra.API_VERSION= 1) if you want calls to fail clearly against a future, incompatible API version.
The repository's API test client is a complete small example app.
External API
Project