Action: SAVE_GLOBAL_PROPERTIES

Background

AVM allows app writers to define global properties for the application. The global properties can be used to parameterize the following attributes

These attributes can include global property in their definition. When these attributes are evaluated, they are first deparameterized. That is, the global property is evaluated and substituted.

For example, an action “CALL_URL” has a target=”<GLOBAL:home_address>/user/auth” and the global property home_address is set to http://www.example.com. Before executing the URL, the target will be evaluated to http://www.example.com/user/auth

Global Properties are defined in “_properties” table in local Content database (Android - yourProject/assets/yourapp_content.sqlite, iOS - yourProject/Resources/assets/yourapp_content.sqlite). The global property can be added by inserting a row in the “_properties” table. See Working with local content database for more details.

Using SAVE_GLOBAL_PROPERTIES

This action is used to modify global property at run-time. The global property can be modified in an app definition file (XML) or using an action FUNCTION.

One of the most common use of Global Property is to save the server URL. A Global Property called server_address can be defined with value as “http://test.example.com”. Once tested and the app is ready for publishing the global property can be changed to point to the production URL http://prod.example.com. This change is done only at one place and now all the URL’s used in the app point to the production server.

Attributes Value Mandatory
action_name SAVE_GLOBAL_PROPERTIES Yes
event_list comma separated list of events, eg: TAP Yes
input_parameter_list comma separated list of values for a global property Yes
target_parameter_list comma separated list of global property to be saved Yes

Example 1

On application launch, save the global property “server_address” as “http://test.example.com”. Now “server_address” can be used in various attributes that may contact the server for example, attribute “target=<GLOBAL:server_address>/user/auth” of the action CALL_URL will evaluate to “target=http://test.example.com/user/auth”.

<screen_deck name="appemble-docs" version="1.0" short_description="Appemble Docs"
    long_description="Appemble tutorial app" publisher_name="Appemble"
    publish_date="2012-05-31" starting_screen_name="screen_1" background="tu_background.png"
    title_background="tu_titlebar.png">
    <actions>
        <action name="SET_GLOBAL_PROPERTY" event_list="ON_APP_LAUNCHED"
            input_parameter_list="CONSTANT:http://test.example.com"
            target_parameter_list="server_address"/>
    </actions>
</screen_deck>

Set Global Property

In this example, when the user presses the button, a FUNCTION is called. The FUNCTION then sets the global properties “server_address” and “region”.

<screens>
    <screen name="remotelist" screen_type="SCREEN"
            initial_layout="PORTRAIT" allowed_layouts="BOTH"
            width="100" height="100">
    </screen>
    <control type="PUSHBUTTON" x="39" y="24" width="50" height="8"
            appearance_name="button_gradient" data_type="VARCHAR"
            on_pressed_appearance_name="button_gradient_on_pressed"
            default_value="Button 2">
        <actions>
            <action event_list="TAP" action_name="FUNCTION"
                    target="SaveGlobalPropertiesFunction"/>
        </actions>
    </control>
</screens>

Define a custom function called SaveGlobalPropertiesFunction

public class SaveGlobalPropertiesFunction implements CustomFunctionInterface {

    public SaveGlobalPropertiesFunction() {}

    @Override
    public Object execute(final Context ctx, final View v, final View pView, ActionModel a,
            Bundle targetParameterValueList) {

        // Create a new action to change the appearance
        ActionModel saveGlobalPropertyAction = new ActionModel(a.sSystemDbName, a.sContentDbName);
        // Set the action name
        saveGlobalPropertyAction.setActionName("SAVE_GLOBAL_PROPERTIES");
        // Set the appearance_id = red. Appearance with name="red" or "green" are defined in the appearance.xml
        targetParameterValueList.putString("server_address", "http://test.example.com");
        targetParameterValueList.putString("region", "test");
        // Execute the action
        Action.callAction(ctx, v, pView, saveGlobalPropertyAction, targetParameterValueList);

        return Boolean.valueOf(true);
    }
}