public final class Android extends Activity
Use as a launching activity
of your
portable DukeScript application
or configure any WebView in your (existing) Android application to
give you easy access to HTML features (like
charts,
maps,
canvas or
more)
from Java by using the
configure(java.lang.String, android.webkit.WebView, java.lang.String, java.lang.Boolean)
method.
To use this presenter specify following dependency:
<dependency> <groupId>com.dukescript.presenters</groupId> <artifactId>android</artifactId> <version>1.7</version> </dependency>
Embedding
Read more about embedding DukeScript technology into existing
Android application. Start at the
configure
method.
Cross-Platform
Learn how to build fully portable (iOS, browser, desktop) Java applications
in DukeScript getting
started tutorial. Details about configuring Android manifest file
can be found in the activity definition section.
Context.BindServiceFlagsDEFAULT_KEYS_DIALER, DEFAULT_KEYS_DISABLE, DEFAULT_KEYS_SEARCH_GLOBAL, DEFAULT_KEYS_SEARCH_LOCAL, DEFAULT_KEYS_SHORTCUT, FOCUSED_STATE_SET, RESULT_CANCELED, RESULT_FIRST_USER, RESULT_OKBIND_ABOVE_CLIENT, BIND_ADJUST_WITH_ACTIVITY, BIND_ALLOW_OOM_MANAGEMENT, BIND_AUTO_CREATE, BIND_DEBUG_UNBIND, BIND_FOREGROUND_SERVICE, BIND_FOREGROUND_SERVICE_WHILE_AWAKE, BIND_IMPORTANT, BIND_NOT_FOREGROUND, BIND_NOT_VISIBLE, BIND_SHOWING_UI, BIND_TREAT_LIKE_ACTIVITY, BIND_VISIBLE, BIND_WAIVE_PRIORITY, MODE_APPEND, MODE_ENABLE_WRITE_AHEAD_LOGGING, MODE_MULTI_PROCESS, MODE_PRIVATE, MODE_WORLD_READABLE, MODE_WORLD_WRITEABLETRIM_MEMORY_BACKGROUND, TRIM_MEMORY_COMPLETE, TRIM_MEMORY_MODERATE, TRIM_MEMORY_RUNNING_CRITICAL, TRIM_MEMORY_RUNNING_LOW, TRIM_MEMORY_RUNNING_MODERATE, TRIM_MEMORY_UI_HIDDEN| Constructor and Description |
|---|
Android() |
| Modifier and Type | Method and Description |
|---|---|
static Executor |
configure(String licenseKey,
WebView view,
String page,
Boolean runOnUiThread)
Configures an instance of
WebView to give you easy access to
HTML features from Java. |
static Fn.Presenter |
create(WebView view,
String page)
Deprecated.
Consider using
configure(java.lang.String, android.webkit.WebView, java.lang.String, java.lang.Boolean)
which offers more control over behavior of Android DukeScript presenter. |
void |
onCreate(Bundle savedInstanceState)
|
addContentView, closeContextMenu, closeOptionsMenu, createPendingResult, dismissDialog, dispatchGenericMotionEvent, dispatchKeyEvent, dispatchKeyShortcutEvent, dispatchPopulateAccessibilityEvent, dispatchTouchEvent, dispatchTrackballEvent, dump, findViewById, finish, finishActivity, finishActivityFromChild, finishAffinity, finishFromChild, getActionBar, getApplication, getCallingActivity, getCallingPackage, getChangingConfigurations, getComponentName, getCurrentFocus, getFragmentManager, getIntent, getLastNonConfigurationInstance, getLayoutInflater, getLoaderManager, getLocalClassName, getMenuInflater, getParent, getParentActivityIntent, getPreferences, getRequestedOrientation, getSystemService, getTaskId, getTitle, getTitleColor, getVolumeControlStream, getWindow, getWindowManager, hasWindowFocus, invalidateOptionsMenu, isChangingConfigurations, isChild, isFinishing, isTaskRoot, managedQuery, moveTaskToBack, navigateUpTo, navigateUpToFromChild, onActionModeFinished, onActionModeStarted, onActivityResult, onApplyThemeResource, onAttachedToWindow, onAttachFragment, onBackPressed, onChildTitleChanged, onConfigurationChanged, onContentChanged, onContextItemSelected, onContextMenuClosed, onCreateContextMenu, onCreateDescription, onCreateDialog, onCreateDialog, onCreateNavigateUpTaskStack, onCreateOptionsMenu, onCreatePanelMenu, onCreatePanelView, onCreateThumbnail, onCreateView, onCreateView, onDestroy, onDetachedFromWindow, onGenericMotionEvent, onKeyDown, onKeyLongPress, onKeyMultiple, onKeyShortcut, onKeyUp, onLowMemory, onMenuItemSelected, onMenuOpened, onNavigateUp, onNavigateUpFromChild, onNewIntent, onOptionsItemSelected, onOptionsMenuClosed, onPanelClosed, onPause, onPostCreate, onPostResume, onPrepareDialog, onPrepareDialog, onPrepareNavigateUpTaskStack, onPrepareOptionsMenu, onPreparePanel, onRestart, onRestoreInstanceState, onResume, onRetainNonConfigurationInstance, onSaveInstanceState, onSearchRequested, onStart, onStop, onTitleChanged, onTouchEvent, onTrackballEvent, onTrimMemory, onUserInteraction, onUserLeaveHint, onWindowAttributesChanged, onWindowFocusChanged, onWindowStartingActionMode, openContextMenu, openOptionsMenu, overridePendingTransition, recreate, registerForContextMenu, removeDialog, requestWindowFeature, runOnUiThread, setContentView, setContentView, setContentView, setDefaultKeyMode, setFeatureDrawable, setFeatureDrawableAlpha, setFeatureDrawableResource, setFeatureDrawableUri, setFinishOnTouchOutside, setIntent, setProgress, setProgressBarIndeterminate, setProgressBarIndeterminateVisibility, setProgressBarVisibility, setRequestedOrientation, setResult, setResult, setSecondaryProgress, setTitle, setTitle, setTitleColor, setVisible, setVolumeControlStream, shouldUpRecreateTask, showDialog, showDialog, startActionMode, startActivities, startActivities, startActivity, startActivity, startActivityForResult, startActivityForResult, startActivityFromChild, startActivityFromChild, startActivityFromFragment, startActivityFromFragment, startActivityIfNeeded, startActivityIfNeeded, startIntentSender, startIntentSender, startIntentSenderForResult, startIntentSenderForResult, startIntentSenderFromChild, startIntentSenderFromChild, startManagingCursor, startNextMatchingActivity, startNextMatchingActivity, startSearch, stopManagingCursor, takeKeyEvents, triggerSearch, unregisterForContextMenuattachBaseContext, getTheme, setThemegetAssets, getBaseContext, getDatabasePath, getFilesDir, getPackageManager, getPackageName, getResources, openFileOutputpublic static Executor configure(String licenseKey, WebView view, String page, Boolean runOnUiThread)
WebView to give you easy access to
HTML features from Java. Use this method to embed HTML components
into your existing application. By configuring your WebView
you can use all the DukeScript
goodies like:
and still code in the same language that you use for the rest
of your application - e.g. in Java or Kotlin.
Accessing the HTML Content
There can be multiple WebView components in your application
and it is thus essential to always identify the one you are working with.
As such the HTML content of your WebView needs to be accessed from
a dedicated Executor provided to you as a return value of the
configure
method. Use Executor.execute(java.lang.Runnable) to switch
into the context of the WebView and perform an action.
Here is an example to display an alert in your view and
its associated executor created as a result of
The example is usingExecutorexecutor =Android.configure(null, view, null, null);executor.execute(newRunnable{ public void run() { alert("Hello World!"); }@JavaScriptBody(args = { "msg" }, body = "alert(msg);") native void alert(String msg); });
JavaScriptBody annotation (you can read
more about this building block of DukeScript system here),
but in most cases you are likely to use one of the predefined Java wrappers. For example there
are
JQuery bindings which you may consider to use instead.
Threading
The HTML content of your WebView needs to be accessed from a single
thread. The same thread is also making callbacks
to your Java code. To post a task into such thread use the returned
Executor's Executor.execute(java.lang.Runnable) method.
Those are the basics.
Now the tougher part: the thread for accessing the HTML content can be the same as Android UI thread, but due to bug 438255 this reliably works only on Android SDK 24 and newer - otherwise it may cause deadlocks.
To control whether the Android UI thread
is the one to make calls into your WebView or whether one shall use
a background thread and access it via returned executor's execute
method, one can use the runOnUiThread parameter of the
configure(java.lang.String, android.webkit.WebView, java.lang.String, java.lang.Boolean) method.
It is usually more comfortable to set the runOnUiThread parameter to true
as one can make direct calls between WebView HTML content and other Android widgets. Whether
that is safe depends on the minimal SDK you are targeting (24 and newer is safe). For now it is probably
more useful to stick with verbose:
Executorexecutor =Android.configure(null, view, null, false);executor.execute(newRunnable{ public void run() { // access the HTML Content of your viewrunOnUiThread(newRunnable{ // access native UI of your application }); } });
This presenter is licensed under GPLv3 license - visit DukeScript website for commercial licenses and support.
licenseKey - ignoredview - the WebView to configure and make ready for access from Java codepage - the initial page to load - usually read from assets - e.g. the URL looks like: file:///android_asset/mypage.htmlrunOnUiThread - true to run all the communication with the
HTML content of your view in Android UI thread,
false to use a background thread (safer on SDK 23 and older), null to use
the default which is the Android UI thread
on SDK 24 and newer and background thread otherwise@Deprecated public static Fn.Presenter create(WebView view, String page)
configure(java.lang.String, android.webkit.WebView, java.lang.String, java.lang.Boolean)
which offers more control over behavior of Android DukeScript presenter.view - the view to configurepage - the page to load into the view or nullExecutor interfacepublic void onCreate(Bundle savedInstanceState)
Activity by creating a WebView and
setting it up to allow execution of DukeScript application. The
configuration is performed in AndroidManifest.xml file.
Sample definition looks like:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="dstest"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
<activity android:name="com.dukescript.presenters.Android"
android:configChanges="orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="loadPage" android:value="file:///android_asset/pages/index.html" />
<meta-data android:name="loadClass" android:value="org.apidesign.bck2brwsr.dstest.AndroidMain" />
<meta-data android:name="invoke" android:value="main" />
</application>
The file specifies fully qualified name of this activity
and additional configuration parameters. Configuration section can define:
true or
false to specify whether to execute the code on standard
UI thread (only possible on SDK 24 and newer) or on a background thread -
more info.
To get more control over the WebView one can build the scene itself
and use
configure(java.lang.String, android.webkit.WebView, java.lang.String, java.lang.Boolean)
factory method to initialize the view(s).
This presenter is licensed under GPLv3 license - visit DukeScript website for commercial licenses and support.
Copyright © 2020. All rights reserved.