Playground
The Playground
class is a dedicated component within the Smart WebView Android project designed to facilitate plugin development and testing. It acts as a sandbox and configuration point, allowing developers to initialize, configure, and experiment with plugins without modifying the core Smart WebView code or the plugin source itself.
Purpose
- Centralized Configuration: Provides a single place (
initializePluginDefaults
) to define default configurations for various plugins your app might use. - Development Testing: Allows developers to easily trigger plugin actions, run diagnostic checks (
runPluginDiagnostics
), and inject test UIs (setupPluginDemoUI
) during the development cycle. - Initialization Hook: Uses the
SmartWebView.onPluginsInitialized
callback to ensure plugin tests and interactions happen only after the core plugin system is ready. - Example Interaction Point: Demonstrates how native application code can obtain plugin instances and call their methods.
Initialization & Setup
```java MainActivity.java (onCreate Excerpt)
// ... inside onCreate ...
SmartWebView.asw_view = findViewById(R.id.msw_view); // Ensure WebView is found
// ... other setup ...
// Initialize Playground, passing essential contexts
Playground playground = new Playground(this, SmartWebView.asw_view, fns);
// ... rest of onCreate ...
```
The `Playground` constructor handles:
1. Storing necessary contexts (`Activity`, `WebView`, `Functions`).
2. Setting up default configurations via `initializePluginDefaults()`.
3. Registering `initializePlugins()` to run *after* the `PluginManager` signals readiness.
Configuring Plugins via Playground
```java Playground.java - initializePluginDefaults() (Example)
private void initializePluginDefaults() {
// Example: Configuration for a hypothetical "AnalyticsPlugin"
Map<String, Object> analyticsConfig = new HashMap<>();
analyticsConfig.put("trackingId", "UA-XXXXX-Y");
analyticsConfig.put("enableAutoPageViews", true);
analyticsConfig.put("debugMode", false); // Default to production mode
// The 'savePluginConfig' is an internal Playground method,
// you might adapt it to store config persistently (e.g., SharedPreferences)
savePluginConfig("AnalyticsPlugin", analyticsConfig);
// Example: Configuration for "MyCustomPlugin"
Map<String, Object> customConfig = new HashMap<>();
customConfig.put("apiKey", "YOUR_DEFAULT_API_KEY");
customConfig.put("featureFlagX", true);
savePluginConfig("MyCustomPlugin", customConfig);
// Add configurations for other plugins (like ToastPlugin) here...
Map<String, Object> toastConfig = new HashMap<>();
toastConfig.put("defaultDuration", Toast.LENGTH_SHORT);
savePluginConfig("ToastPlugin", toastConfig);
}
// Placeholder for saving config (currently logs)
private void savePluginConfig(String pluginName, Map<String, Object> config) {
Log.d(TAG, "Default config set for " + pluginName + ": " + config);
// In a real app, you might store this persistently
// and have PluginManager pass it during initialization.
}
```
Plugins receive their configuration map during their `initialize` method. Developers can also add methods like `updatePluginConfig` or `configurePluginForProduction` within `Playground` to change settings dynamically or based on build types.
Testing Plugins with Playground
By using Playground
effectively, developers can streamline the process of integrating, configuring, and testing plugins within their Smart WebView application.