Using Android intents to launch and close Velocity

If you have a custom launcher app, it can use the following intents to launch or close Velocity.

You should have familiarity with Android software development in order to use these intents.

Velocity Action Main

This action can be used to start Velocity. This action is normally started from the system launcher.

Action

"android.intent.action.MAIN"

Category

"android.intent.category.LAUNCHER"

Component

Package: "com.wavelink.velocity"

Class: "com.wavelink.velocity.te.SplashActivity"

Extras

Name

Type

Description

Notes

"openProfile"

String

Optional

The name of a profile to open or switch to if already open. Velocity will launch as a separate single task. Finds first profile with a matching name to open, so unique names should be used. If specified, precludes any Auto Launch profiles. May fail if profile is not found or too many sessions are already open. The profile name is case-sensitive. Added in version 2.1.29

Result

This action does not respond with a result and should be sent using startActivity().

Example 1

Copy
// Start Velocity normally
private void startVelocityDemoProfile() {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.setComponent(new ComponentName("com.wavelink.velocity", "com.wavelink.velocity.te.SplashActivity"));
    try {
        startActivity(intent);
    } catch (ActivityNotFoundException activityNotFoundException) {
        Log.e(TAG, "Unable to find Velocity");
    }
}

Example 2

Copy
// Start the Velocity profile named "Demo"
private void startVelocityDemoProfile() {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.setComponent(new ComponentName("com.wavelink.velocity", "com.wavelink.velocity.te.SplashActivity"));
    intent.putExtra("openProfile", "Demo");
    try {
        startActivity(intent);
    } catch (ActivityNotFoundException activityNotFoundException) {
        Log.e(TAG, "Unable to find Velocity");
    }
}

Example 3

Copy
// Start the Velocity profile named "My Host"
private void startVelocityMyHostProfile() {
    // requires a manifest entry in the queries for "com.wavelink.velocity"
    Intent intent = getPackageManager().getLaunchIntentForPackage("com.wavelink.velocity");
    if(intent != null) {
        intent.putExtra("openProfile", "My Host");
        startActivity(intent);
    } else {
        Log.e(TAG, "Unable to find Velocity.");
    }
}

Example 4

Copy
// Velocity script to launch Velocity and open the Demo profile.
View.toast("Press F1 to launch Demo", true);

function goDemo(event) {
    Action.launch({
        action: "android.intent.action.MAIN",
        package: "com.wavelink.velocity",
        class: "com.wavelink.velocity.te.SplashActivity",
        extras: [{name: "openProfile", value: "Demo", type: "string"}]
    });
    event.eventHandled = true;
}

WLEvent.onKey(0xE03B, goDemo);

Velocity Action Close

This action can be used to shut down Velocity and optionally clear specific web data.

This action should be sent using sendOrderedBroadcast() and will return a result upon shut down.

The shut down will finish any open activities, whether foreground or background, as well as terminate all sessions. The action can also optionally clear web data as part of the shut down process.

Options exist to kill the suspended Velocity process after shut down as well.

Added in version 2.1.29

Package Query

A package query for Velocity is required in the AndroidManifest.xml file to access this action.

<queries>

<package android:name="com.wavelink.velocity" />

</queries>

Action

"com.wavelink.velocity.action.CLOSE_VELOCITY"

Category

"android.intent.category.DEFAULT"

Component

Package: "com.wavelink.velocity"

Extras

Name

Type

Description

Notes

"clearCache"

Boolean

Optional

Flag to clear the Android WebView resource cache. The default is false.

"clearCookies"

Boolean

Optional

Flag to remove all Android WebView cookies. The default is false.

"clearStorage"

Boolean

Optional

Flag to clear all storage inside Android WebView JavaScript storage APIs. The default is false.

"forceStop"

Boolean

Optional

(Not recommended). Flag to kill the Velocity process after shut down. The default is false. This option is not recommended and will not return a result code. It is better to use sendOrderedBroadcast() with a resultReceiver parameter. See section below on Killing the Velocity Process.

Killing the Velocity Process

The intent extra forceStop exists to kill the Velocity process after shut down. However, this option is not recommended as it may misbehave and will not return a result code due to being killed before returning from the action.

To receive a result code and operate with more safety, it is recommended to use sendOrderedBroadcast() with a resultReceiver parameter. Inside of this receiver the app may use the ActivityManager killBackgroundProcesses() method targeting the Velocity package. This method will require an extra permission be added to the AndroidManifest.xml file:

<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>

Velocity activities and session service need to finish shut down before process can be successfully killed. Use a short delay upon result receipt before executing. See example 3 below.

Result

Responding with result code Result.Ok means there was successful shut down of Velocity activities, session service, and data was cleared if flags were set.

Responding with result code Result.Canceled means an error occurred during shut down or the broadcast receiver was not listening. A common reason for this is that Velocity is not installed, or the Velocity installation is a version that does not support this feature.

If using the flag forceStop then no result code will be returned from the action.

Example 1

Copy
// Close Velocity
private void closeVelocity() {
    Intent intent = new Intent("com.wavelink.velocity.action.CLOSE_VELOCITY");
    intent.setPackage("com.wavelink.velocity");
    try {
        sendOrderedBroadcast(intent, null);
    } catch (Exception e) {
        Log.e(TAG, "Broadcast send failed");
    }
}

Example 2

Copy
// Close Velocity and clear web data
private void closeVelocityClearWebData() {
    Intent intent = new Intent("com.wavelink.velocity.action.CLOSE_VELOCITY");
    intent.setPackage("com.wavelink.velocity");
    intent.putExtra("clearCache", true);
    intent.putExtra("clearCookies", true);
    intent.putExtra("clearStorage", true);
    try {
        sendOrderedBroadcast(intent, null);
    } catch (Exception e) {
        Log.e(TAG, "Broadcast send failed");
    }
}

Example 3

Copy
// Close Velocity, kill process, and clear web data
private void closeVelocityAndKillProcess() {
    Intent intent = new Intent("com.wavelink.velocity.action.CLOSE_VELOCITY");
    intent.setPackage("com.wavelink.velocity");
    intent.putExtra("clearCache", true);
    intent.putExtra("clearCookies", true);
    intent.putExtra("clearStorage", true);

    try {
    sendOrderedBroadcast(intent, null, new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
                    mActivityManager.killBackgroundProcesses("com.wavelink.velocity");

                    Log.e(TAG, "Result: " + getResultCode());
                }
            }, 5000);  //5000 ms delay to wait for Velocity activites and session service to finish shut down. This value can be changed.
        }
    }, null, Activity.RESULT_CANCELED, null, null);
    } catch (Exception e) {
        Log.e(TAG, "Broadcast send failed");
    }
}