Repository navigation
Java Library
Everything an app needs to call ClusterTune is in one small, dependency-free Java file: ClusterTuneApi.java, the api-contract module. It holds every public name and value (the authority, methods, extras, reply keys, error codes and notification URIs) as constants that never change within an API version. It works from Java and Kotlin.
-
Add the contract (below), and declare package visibility in your manifest:
<queries> <provider android:authorities="com.aure.clustertune.api" /> </queries>
-
Call the API off the main thread:
val reply = withContext(Dispatchers.IO) { contentResolver.call(Uri.parse(ClusterTuneApi.URI), ClusterTuneApi.Method.PROFILE_APPLY, "medium", null) } if (reply?.getBoolean(ClusterTuneApi.Result.OK) == true) { val applied = JSONObject(reply.getString(ClusterTuneApi.Result.DATA)!!) }
-
The first call shows the user Allow Your App to control ClusterTune? and waits for the answer. There is no permission to declare or request.
Either copy ClusterTuneApi.java into your sources, keeping its package (com.aure.clustertune.api.contract), or publish the module to your local Maven repository and depend on it:
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") }It's compiled for Java 17, which any current Android Gradle Plugin handles. No public Maven repository is set up yet.
| Class | Contents |
|---|---|
ClusterTuneApi |
VERSION (1), PACKAGE, AUTHORITY, URI, the frequency units, and the PATH_*/URI_* notification URIs |
ClusterTuneApi.Method |
STATE_GET, TUNING_GET, TUNING_SET, PROFILE_LIST, PROFILE_APPLY, APP_LIST, APP_SET, APP_CLEAR, DEVICE_GET, API_INFO
|
ClusterTuneApi.Resource |
STATE, TUNING, PROFILE, APP, DEVICE, API; each also works as a method name, meaning that resource's read method |
ClusterTuneApi.Extra |
API_VERSION, IF_REVISION
|
ClusterTuneApi.Result |
Reply keys: API_VERSION, OK, CODE, MESSAGE, DATA
|
ClusterTuneApi.ErrorCode |
Every code value, such as ACCESS_DENIED, ACCESS_PENDING, CONFLICT, AMBIGUOUS_PROFILE
|
ClusterTuneApi.Shell |
The ADB broadcast receiver and its extras |
Each method's argument and reply are in API methods.
Calls are synchronous Binder calls, so run them off the main thread. The first call from your app may wait up to 60 seconds for the user to 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.Result
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.json.JSONObject
class ClusterTuneException(val code: String, message: String?) : Exception("$code: $message")
/** Calls ClusterTune and returns the reply's data, or throws [ClusterTuneException]. */
suspend fun Context.clusterTune(method: String, arg: String? = null, extras: Bundle? = null): JSONObject =
withContext(Dispatchers.IO) {
val reply = try {
contentResolver.call(Uri.parse(ClusterTuneApi.URI), method, arg, extras)
} catch (notFound: IllegalArgumentException) {
null // ClusterTune isn't installed, or <queries> is missing
}
if (reply == null) throw ClusterTuneException(ErrorCode.OPERATION_FAILED, "ClusterTune is not installed")
if (!reply.getBoolean(Result.OK)) {
throw ClusterTuneException(reply.getString(Result.CODE) ?: ErrorCode.OPERATION_FAILED, reply.getString(Result.MESSAGE))
}
JSONObject(reply.getString(Result.DATA) ?: "{}")
}Using it from a coroutine:
import com.aure.clustertune.api.contract.ClusterTuneApi.Method
val profiles = context.clusterTune(Method.PROFILE_LIST).getJSONArray("profiles")
val ids = (0 until profiles.length()).map { profiles.getJSONObject(it).getString("id") }
context.clusterTune(Method.PROFILE_APPLY, ids.first())
val applied = context.clusterTune(Method.TUNING_SET, "cpu6.max=2.4GHz gpu.max=600")
val snapped = applied.optJSONArray("adjusted") // values moved to a supported step, if any
val state = context.clusterTune(Method.STATE_GET)
val live = if (state.isNull("profile")) "custom values" else state.getJSONObject("profile").getString("name")import android.net.Uri;
import android.os.Bundle;
import com.aure.clustertune.api.contract.ClusterTuneApi;
import org.json.JSONObject;
// On a background thread, for example from an Executor:
Bundle reply = getContentResolver().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 live = state.isNull("profile") ? "custom values" : state.getJSONObject("profile").getString("name");
} else if (reply != null) {
String code = reply.getString(ClusterTuneApi.Result.CODE); // for example ClusterTuneApi.ErrorCode.ACCESS_PENDING
String message = reply.getString(ClusterTuneApi.Result.MESSAGE);
}| Code | What to tell the user |
|---|---|
access_pending |
ClusterTune is waiting for them to allow your app: answer its notification or dialog, then try again |
access_denied |
Your app was denied; it can be allowed in ClusterTune under Settings → App access |
Only the call that raised the question waits for the answer. Other calls in the meantime return access_pending right away, so a burst of calls can't pile up waiting threads. The user can change their decision at any time, and it's checked on every call, so handle both codes wherever you call.
To change settings only if nobody else changed them since you looked, pass the revision you read:
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 meanwhile: re-read and decide again
}
}This also makes retrying after a lost reply 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.
import android.database.ContentObserver
import android.net.Uri
import android.os.Handler
import android.os.Looper
import com.aure.clustertune.api.contract.ClusterTuneApi
val observer = object : ContentObserver(Handler(Looper.getMainLooper())) {
override fun onChange(selfChange: Boolean, uri: Uri?) {
when (uri?.toString()) {
ClusterTuneApi.URI_STATE -> { /* something was applied: re-read state.get */ }
ClusterTuneApi.URI_TUNING -> { /* saved settings changed: re-read tuning.get */ }
ClusterTuneApi.URI_PROFILE -> { /* profiles changed: re-read profile.list */ }
ClusterTuneApi.URI_APP -> { /* assignments changed: re-read app.list */ }
}
}
}
contentResolver.registerContentObserver(Uri.parse(ClusterTuneApi.URI), true, observer)
// When done: contentResolver.unregisterContentObserver(observer)Observing needs no approval. Change notifications lists exactly when each URI fires.
- Store profile IDs, not names: users can rename profiles. Names are for people typing commands.
-
Read the device first. Policy IDs, frequency steps and governors differ between devices; get them from
device.get. -
Prefer the short settings syntax (
cpu6.max=2.4GHz) unless you already work with exact kHz/Hz steps; ClusterTune snaps values and tells you what it applied. - Ignore unknown reply fields, and treat unknown error codes as failures. Both may be added within version 1.
-
Pass
api_version= 1 (ClusterTuneApi.Extra.API_VERSION) if you'd rather fail clearly than talk to a future, incompatible version.
The repository's API test client (test-apps, flavor api) is a small app that calls the API from its own UID, handles the access question, and also acts as a minimal Tasker-style plugin host.
External API
Project