Repository navigation
EN Plagin
Below is the full source of the class in documentation form. The comments inside the block explain the purpose of the parts of the class, but they are not kept in the working file Plagin.java: the production code stays clean, and the explanations live in the wiki.
package com.velitask.plugin.example;
import com.velitask.sdk.IFileSource;
import com.velitask.sdk.IIndicator;
import com.velitask.sdk.IPlagin;
import com.velitask.sdk.db.PluginDatabase;
/* Plugin entry point: the class is named in the jar manifest (Velitask-Plugin-Class). */
public class Plagin implements IPlagin {
/* Stable plugin identifier. It is stored in projects and must not be changed. */
public static final String UID = "com.velitask.plugin.example";
/* Prefix of the plugin localization keys. */
public static final String KEY = "PluginExample";
/* Prefix of the indicator localization keys. */
public static final String KEY_INDICATOR = KEY + ".Indicator";
@Override
public String getUID() {
return UID;
}
@Override
/* Plugin version (a string). */
public String getVersion() {
return "1.0.0";
}
@Override
/* Plugin DB schema version: create.sql describes exactly this version. */
public int getDbVersion() {
return 1;
}
@Override
/* Additional locales: the Russian strings are loaded for any interface language. */
public String[] defineAdditionLocales() {
return new String[] { "ru" };
}
@Override
/* The plugin's indicator list: just one here. */
public IIndicator[] defineIndicators() {
return new IIndicator[] {
new SpeedometerIndicator()
};
}
@Override
/* Hook after a source is imported: writes a row to the import_log table. */
public void onSourceImported(IFileSource source, PluginDatabase db) {
if (db == null || source == null) {
return;
}
/*
* ${table:import_log} is substituted with the plugin-prefixed table name.
* INSERT OR REPLACE by source_id: a repeated call creates no duplicates.
*/
db.execute(
"INSERT OR REPLACE INTO ${table:import_log}"
+ " (source_id, source_path, processed_at) VALUES (?, ?, ?)",
source.getId(),
source.getRelativePath(),
System.currentTimeMillis()
);
}
}Plagin is the entry point of the example plugin. It implements the IPlagin contract from the SDK: it tells the application the plugin's identifier and version, the version of its database schema, the additional languages and the list of indicators, and when a source is imported, it writes a row to its own table.
When forking the template, UID and KEY must be changed, as well as the Velitask-Plugin-Class attribute in build.gradle if you rename the package or the class.
Registration and identification. The application finds the class by the Velitask-Plugin-Class attribute in the jar manifest and creates an instance of it. getUID() returns com.velitask.plugin.example: this identifier is written into saved projects and templates, so it must not be changed without a migration. getVersion() returns the string 1.0.0. defineIndicators() returns an array of one indicator — SpeedometerIndicator.
Localization. The constant KEY (PluginExample) is the common prefix of string keys, and KEY_INDICATOR (PluginExample.Indicator) is the prefix of indicator keys; the indicator builds its keys from it. defineAdditionLocales() returns ["ru"]: the Russian strings are loaded together with the strings of the current interface language. The files themselves are described on the Resources page.
Plugin database. getDbVersion() returns 1: the plugin has its own database inside the project.
- If the project has no saved database version for this plugin, only
create.sqlis run — the full snapshot of the current schema version, after which the version is recorded asgetDbVersion(). Theupgrade-N.sqlfiles are not executed in this case. - If the project stores an older version, the chain
upgrade-(old+1)…upgrade-getDbVersion()is run: the fileupgrade-N.sqlmoves the database from versionN-1toN. There is currently one version, and the plugin has noupgrade-N.sqlfiles. - If the database cannot be brought to the required version, the plugin is disconnected from the project, the project opens without it, and an entry goes to the application log.
The only table is import_log (source_id, source_path, processed_at). It exists only as an example of working with PluginDatabase; there is no business logic on its data. More details are on the Resources page.
Source import hook. onSourceImported(source, db) is called after a source is imported and runs on the same thread as the import, so db.execute(...) can be called without synchronization. The hook is also called for already existing sources when the plugin is connected to a project or the version of its database is raised, so the write must be idempotent. In the template this is ensured by INSERT OR REPLACE on the source_id key. If db or source is null, the method does nothing.
-
KEYintentionally differs fromUID: the identifier is long, while string keys are better kept short. When forking, change both values. - The version
1.0.0is set as a string ingetVersion(); the version in build.gradle (version) is used in the jar name and is not automatically linked togetVersion(). - The SQL query uses the substitution
${table:import_log}: the runtime replaces it with the table name carrying the plugin prefix (plugin_com_velitask_plugin_example_import_log), so that tables of different plugins do not collide. - If the plugin does not need its own database, return
0fromgetDbVersion(), deletesql/setup/and theonSourceImportedmethod. -
Plaginhas no nested classes: it is a pure implementation ofIPlagin.