AbstractBlocklyActivity

public abstract class AbstractBlocklyActivity extends AppCompatActivity
Known Direct Subclasses

Base class for a Blockly activity that use a material design style tool bar, and optionally a navigation menu.

The default layout is filled with a workspace and the toolbox and trash each configured as fly-out views. Everything below the ERROR(/ActionBar) can be replaced by overriding onCreateContentView(int). After onCreateContentView(int), a BlocklyActivityHelper is constructed to help initialize the Blockly fragments, controller, and supporting UI. If overriding onCreateContentView(int) without unified_blockly_workspace.xml or otherwise using standard blockly fragment and view ids (blockly_workspace, blockly_toolbox_ui, blockly_trash_ui, etc.), override onCreateActivityHelper() and onCreateFragments() appropriately.

Once the controller and fragments are configured, if checkAllowRestoreBlocklyState(Bundle) returns true, the activity will attempt to load a prior workspace from the instance state bundle. If no workspace is loaded, it defers to onLoadInitialWorkspace().

Configure the workspace by providing definitions for getBlockDefinitionsJsonPaths(), getToolboxContentsXmlPath(). Alternate BlockViewFactorys are supported via onCreateBlockViewFactory(WorkspaceHelper). An initial workspace can be loaded during onLoadInitialWorkspace().

The block definitions can be updated at any time by calling resetBlockFactory(), which triggers another call to getBlockDefinitionsJsonPaths(). Similarly, The toolbox can be reloaded by calling reloadToolbox(), which triggers another call to getToolboxContentsXmlPath().

Field Summary

protected ActionBar mActionBar
protected BlocklyActivityHelper mBlocklyActivityHelper
protected DrawerLayout mDrawerLayout
protected ActionBarDrawerToggle mDrawerToggle
protected View mNavigationDrawer

Public Constructor Summary

Public Method Summary

final BlocklyController
boolean
void
onBackPressed()
Handles the back button.
void
onClearWorkspace()
Called when the user clicks the clear action.
boolean
onCreateOptionsMenu(Menu menu)
void
onLoadWorkspace()
Called when the user clicks the load action.
boolean
onOptionsItemSelected(MenuItem item)
void
onSaveInstanceState(Bundle outState)
Saves a snapshot of the workspace to outState.
void
onSaveWorkspace()
Called when the user clicks the save action.
void
setNavDrawerOpened(boolean open)
Opens or closes the navigation drawer.

Protected Method Summary

boolean
checkAllowRestoreBlocklyState(Bundle savedInstanceState)
Returns true if the app should proceed to restore the blockly state from the savedInstanceState Bundle or the auto save file.
void
configureBlockExtensions()
This method provides a hook to register BlockExtensions that support the block definitions in this activity.
void
configureCategoryFactories()
This method provides a hook to register custom CustomCategorys that support the toolboxes in this activity.
void
configureMutators()
This method provides a hook to register Mutator.Factorys and MutatorFragment.Factorys that support the block definitions in this activity.
int
abstract List<String>
LanguageDefinition
abstract CodeGenerationRequest.CodeGeneratorCallback
getCodeGenerationCallback()
Returns a generation callback to use for the most recently requested "Run" action.
abstract List<String>
getGeneratorsJsPaths()
Returns the asset file paths to the generators (JS files) to use for the most recently requested "Run" action.
abstract String
String
String
CharSequence
boolean
onAutoload()
Called when the activity tries to restore the autosaved workspace, typically by onCreate(Bundle) if there was no workspace data in the bundle.
void
onAutosave()
Called when an autosave of the workspace is triggered, typically by onPause().
boolean
void
onCreate(Bundle savedInstanceState)
Creates the activity's views and fragments (via onCreateActivityRootView(), and then initializes Blockly via onCreateActivityHelper(), using the values from getBlockDefinitionsJsonPaths() and getToolboxContentsXmlPath().
BlocklyActivityHelper
onCreateActivityHelper()
Create a BlocklyActivityHelper to use for this Activity.
void
onCreateActivityRootView()
Creates or loads the root content view (by default, drawers_and_action_bar) for the Activity.
View
View
onCreateContentView(int containerId)
Constructs (or inflates) the primary content view of the Activity.
void
onInitBlankWorkspace()
Hook for subclasses to initialize a new blank workspace.
void
onLoadInitialWorkspace()
Hook for subclasses to load an initial workspace.
void
onPause()
Propagate lifecycle event to BlocklyActivityHelper.
void
onRestart()
Propagate lifecycle event to BlocklyActivityHelper.
void
onResume()
Propagate lifecycle event to BlocklyActivityHelper.
void
onRunCode()
Runs the code generator.
void
onStart()
Propagate lifecycle event to BlocklyActivityHelper.
void
onStop()
Propagate lifecycle event to BlocklyActivityHelper.
void
reloadToolbox()
Reloads the toolbox contents using the path provided by getToolboxContentsXmlPath().
void
resetBlockFactory()
Reloads the block definitions, including extensions and mutators.
void
restoreActionBar()
Restores the ERROR(/ActionBar) contents when the navigation window closes, per Material design guidelines.
void
setupAppNaviagtionDrawer()
Configures the activity to support a navigation menu and drawer provided by onCreateAppNavigationDrawer().

Inherited Method Summary

Fields

protected ActionBar mActionBar

protected BlocklyActivityHelper mBlocklyActivityHelper

protected DrawerLayout mDrawerLayout

protected ActionBarDrawerToggle mDrawerToggle

protected View mNavigationDrawer

Public Constructors

public AbstractBlocklyActivity ()

Public Methods

public final BlocklyController getController ()

Returns

public boolean isNavDrawerOpen ()

Returns
  • Whether the navigation drawer is currently open.

public void onBackPressed ()

Handles the back button. Default implementation attempts to close the navigation menu, then the toolbox and trash flyouts, before allowing the system to back out of the activity.

public void onClearWorkspace ()

Called when the user clicks the clear action. Default implementation resets the workspace, removing all blocks from the workspace, and then calls onInitBlankWorkspace().

public boolean onCreateOptionsMenu (Menu menu)

Parameters
menu

public void onLoadWorkspace ()

Called when the user clicks the load action. Default implementation delegates handling to loadWorkspaceFromAppDir(String).

public boolean onOptionsItemSelected (MenuItem item)

Parameters
item

public void onSaveInstanceState (Bundle outState)

Saves a snapshot of the workspace to outState.

Parameters
outState The Bundle to save to.

public void onSaveWorkspace ()

Called when the user clicks the save action. Default implementation delegates handling to saveWorkspaceToAppDir(String) using getWorkspaceSavePath().

public void setNavDrawerOpened (boolean open)

Opens or closes the navigation drawer.

Parameters
open Opens the navigation drawer if true and closes it if false.

Protected Methods

protected boolean checkAllowRestoreBlocklyState (Bundle savedInstanceState)

Returns true if the app should proceed to restore the blockly state from the savedInstanceState Bundle or the auto save file. By default, it always returns true, but Activity developers can override this method to add conditional logic.

This does not prevent the state from saving to a Bundle during onSaveInstanceState(Bundle) or saving to a file in onAutosave().

Parameters
savedInstanceState The Bundle to restore state from.
Returns
  • True if Blockly state should be restored. Otherwise, null.

protected void configureBlockExtensions ()

This method provides a hook to register BlockExtensions that support the block definitions in this activity. By default, it adds all extensions in DefaultBlocks to the block factory, via the BlocklyActivityHelper implementation.

Extensions with the same key will replace existing extensions, so it is safe to call super and then update specific extensions.

Called from resetBlockFactory().

protected void configureCategoryFactories ()

This method provides a hook to register custom CustomCategorys that support the toolboxes in this activity. By default, it registers the categories in DefaultBlocks, via the BlocklyActivityHelper implementation.

Category factories with the same custom key will replace existing CustomCategorys, so it is safe to call super and then update specific categories.

Called once at activity creation.

protected void configureMutators ()

This method provides a hook to register Mutator.Factorys and MutatorFragment.Factorys that support the block definitions in this activity. By default, it adds the mutators in DefaultBlocks to the BlockFactory, via the BlocklyActivityHelper implementation.

Mutators with the same key will replace existing mutators, so it is safe to call super and then update specific mutators.

Called from resetBlockFactory().

protected int getActionBarMenuResId ()

Returns

protected abstract List<String> getBlockDefinitionsJsonPaths ()

Returns
  • The asset path for the json block definitions.

protected LanguageDefinition getBlockGeneratorLanguage ()

Returns
  • The asset path for the core language file used to generate code.

protected abstract CodeGenerationRequest.CodeGeneratorCallback getCodeGenerationCallback ()

Returns a generation callback to use for the most recently requested "Run" action. Called from onRunCode().

Returns
  • The generation callback.

protected abstract List<String> getGeneratorsJsPaths ()

Returns the asset file paths to the generators (JS files) to use for the most recently requested "Run" action. Called from onRunCode(). This is expected to be a list of JavaScript files that contain the block generators.

Returns
  • The list of file paths to the block generators.

protected abstract String getToolboxContentsXmlPath ()

Returns
  • The asset path for the xml toolbox config.

protected String getWorkspaceAutosavePath ()

Returns
  • The path to the automatically saved workspace file on the local device. By default, "autosave_workspace.xml".

protected String getWorkspaceSavePath ()

Returns
  • The path to the saved workspace file on the local device. By default, "workspace.xml".

protected CharSequence getWorkspaceTitle ()

Returns

protected boolean onAutoload ()

Called when the activity tries to restore the autosaved workspace, typically by onCreate(Bundle) if there was no workspace data in the bundle.

Returns
  • true if a previously saved workspace was loaded, false otherwise.

protected void onAutosave ()

Called when an autosave of the workspace is triggered, typically by onPause(). By default this saves the workspace to a file in the app's directory.

protected boolean onBackToCloseNavMenu ()

Returns
  • True if the navigation menu was closed and the back event should be consumed. Otherwise false.

protected void onCreate (Bundle savedInstanceState)

Creates the activity's views and fragments (via onCreateActivityRootView(), and then initializes Blockly via onCreateActivityHelper(), using the values from getBlockDefinitionsJsonPaths() and getToolboxContentsXmlPath(). Subclasses should override those methods to configure the Blockly environment.

Once the controller and fragments are configured, if checkAllowRestoreBlocklyState(Bundle) returns true, the activity will attempt to load a prior workspace from the instance state bundle. If no workspace is loaded, it defers to onLoadInitialWorkspace().

Parameters
savedInstanceState

protected BlocklyActivityHelper onCreateActivityHelper ()

Create a BlocklyActivityHelper to use for this Activity.

protected void onCreateActivityRootView ()

Creates or loads the root content view (by default, drawers_and_action_bar) for the Activity. It is also responsible for assigning mActionBar and mDrawerLayout, and adding the view returned by onCreateContentView(int).

protected View onCreateAppNavigationDrawer ()

Returns
  • The View to be used for the navigation menu. Otherwise null.

protected View onCreateContentView (int containerId)

Constructs (or inflates) the primary content view of the Activity.

Parameters
containerId The container id to target if using a ERROR(/Fragment)
Returns

protected void onInitBlankWorkspace ()

Hook for subclasses to initialize a new blank workspace. Initialization may include configuring default variables or other setup.

protected void onLoadInitialWorkspace ()

Hook for subclasses to load an initial workspace. Default implementation just calls onInitBlankWorkspace().

protected void onPause ()

Propagate lifecycle event to BlocklyActivityHelper.

protected void onRestart ()

Propagate lifecycle event to BlocklyActivityHelper.

protected void onResume ()

Propagate lifecycle event to BlocklyActivityHelper.

protected void onRunCode ()

Runs the code generator. Called when user selects "Run" action.

Gets the latest block definitions and generator code by calling getBlockDefinitionsJsonPaths() and getGeneratorsJsPaths() just before invoking generation.

protected void onStart ()

Propagate lifecycle event to BlocklyActivityHelper.

protected void onStop ()

Propagate lifecycle event to BlocklyActivityHelper.

protected void reloadToolbox ()

Reloads the toolbox contents using the path provided by getToolboxContentsXmlPath().

protected void resetBlockFactory ()

Reloads the block definitions, including extensions and mutators. Calls getBlockDefinitionsJsonPaths() and configureBlockExtensions().

Throws
IOException If there is a fundamental problem with the input.
BlockLoadingException If the definition is malformed.

protected void restoreActionBar ()

Restores the ERROR(/ActionBar) contents when the navigation window closes, per Material design guidelines.

protected void setupAppNaviagtionDrawer ()

Configures the activity to support a navigation menu and drawer provided by onCreateAppNavigationDrawer().