add base Cordova integration. But present issue with Fragment, Cordova WebView require to be in Application and initialize before it (to support all Cordova features). Current version done as Fragment - so some features commented.

This commit is contained in:
Yaroslav Dmytrotsa 2015-08-05 17:36:45 +03:00
parent a178f41032
commit fce7922e62
5 changed files with 399 additions and 55 deletions

View File

@ -79,7 +79,6 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/recyclerview-v7/22.2.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-v4/22.2.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.getbase/floatingactionbutton/1.9.0/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.github.ksoichiro/android-observablescrollview/1.5.2/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.squareup.leakcanary/leakcanary-android/1.3.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jacoco" />
@ -98,59 +97,59 @@
</content>
<orderEntry type="jdk" jdkName="Android API 22 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="haha-1.3" level="project" />
<orderEntry type="library" exported="" name="commons-io-2.4" level="project" />
<orderEntry type="library" exported="" name="json-io-2.4.1" level="project" />
<orderEntry type="library" exported="" name="floatingactionbutton-1.9.0" level="project" />
<orderEntry type="library" exported="" name="ormlite-android-4.48" level="project" />
<orderEntry type="library" exported="" name="dagger-producers-2.0-beta" level="project" />
<orderEntry type="library" exported="" name="glide-3.6.1" level="project" />
<orderEntry type="library" exported="" name="jackson-core-2.5.1" level="project" />
<orderEntry type="library" exported="" name="guava-18.0" level="project" />
<orderEntry type="library" exported="" name="commons-pool-1.5.4" level="project" />
<orderEntry type="library" exported="" name="support-v4-22.2.1" level="project" />
<orderEntry type="library" exported="" name="jackson-databind-2.5.1" level="project" />
<orderEntry type="library" exported="" name="leakcanary-android-1.3.1" level="project" />
<orderEntry type="library" exported="" name="leakcanary-watcher-1.3.1" level="project" />
<orderEntry type="library" exported="" name="jsonrpc2-base-1.36" level="project" />
<orderEntry type="library" exported="" name="log4j-1.2.17" level="project" />
<orderEntry type="library" exported="" name="library-1.1" level="project" />
<orderEntry type="library" exported="" name="leveldb-0.7" level="project" />
<orderEntry type="library" exported="" name="floatingactionbutton-1.9.0" level="project" />
<orderEntry type="library" exported="" name="design-22.2.1" level="project" />
<orderEntry type="library" exported="" name="dagger-2.1-SNAPSHOT" level="project" />
<orderEntry type="library" exported="" name="javassist-3.15.0-GA" level="project" />
<orderEntry type="library" exported="" name="commons-dbcp-1.4" level="project" />
<orderEntry type="library" exported="" name="recyclerview-v7-22.2.1" level="project" />
<orderEntry type="library" exported="" name="support-annotations-22.2.1" level="project" />
<orderEntry type="library" exported="" name="rxjava-android-0.20.7" level="project" />
<orderEntry type="library" exported="" name="jsr305-3.0.0" level="project" />
<orderEntry type="library" exported="" name="prov-1.51.0.0" level="project" />
<orderEntry type="library" exported="" name="leveldb-api-0.7" level="project" />
<orderEntry type="library" exported="" name="logback-android-core-1.1.1-3" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-22.2.1" level="project" />
<orderEntry type="library" exported="" name="java-util-1.8.0" level="project" />
<orderEntry type="library" exported="" name="jsonrpc2-client-1.15" level="project" />
<orderEntry type="library" exported="" name="javax.annotation-10.0-b28" level="project" />
<orderEntry type="library" exported="" name="json-smart-1.3.1" level="project" />
<orderEntry type="library" exported="" name="leakcanary-analyzer-1.3.1" level="project" />
<orderEntry type="library" exported="" name="jackson-annotations-2.5.0" level="project" />
<orderEntry type="library" exported="" name="core-1.51.0.0" level="project" />
<orderEntry type="library" exported="" name="dagger-compiler-2.0" level="project" />
<orderEntry type="library" exported="" name="rxjava-core-0.20.7" level="project" />
<orderEntry type="library" exported="" name="commons-collections4-4.0" level="project" />
<orderEntry type="library" exported="" name="netty-all-4.0.28.Final" level="project" />
<orderEntry type="library" exported="" name="android-observablescrollview-1.5.2" level="project" />
<orderEntry type="library" exported="" name="json-simple-1.1.1" level="project" />
<orderEntry type="library" exported="" name="javax.inject-1" level="project" />
<orderEntry type="library" exported="" name="dagger-producers-2.0-beta" level="project" />
<orderEntry type="library" exported="" name="apktool-lib-1.4.4-3" level="project" />
<orderEntry type="library" exported="" name="ormlite-core-4.48" level="project" />
<orderEntry type="library" exported="" name="jsonrpc2-server-1.11" level="project" />
<orderEntry type="library" exported="" name="multidex-1.0.1" level="project" />
<orderEntry type="library" exported="" name="persistence-api-1.0.2" level="project" />
<orderEntry type="library" exported="" name="logback-android-classic-1.1.1-3" level="project" />
<orderEntry type="library" exported="" name="slf4j-api-1.7.12" level="project" />
<orderEntry type="library" exported="" name="jsonrpc2-base-1.36" level="project" />
<orderEntry type="library" exported="" name="java-util-1.8.0" level="project" />
<orderEntry type="library" exported="" name="material-dialogs-0.7.7.0" level="project" />
<orderEntry type="library" exported="" name="leakcanary-analyzer-1.3.1" level="project" />
<orderEntry type="library" exported="" name="glide-3.6.1" level="project" />
<orderEntry type="library" exported="" name="commons-collections4-4.0" level="project" />
<orderEntry type="library" exported="" name="recyclerview-v7-22.2.1" level="project" />
<orderEntry type="library" exported="" name="logback-android-core-1.1.1-3" level="project" />
<orderEntry type="library" exported="" name="haha-1.3" level="project" />
<orderEntry type="library" exported="" name="jsonrpc2-server-1.11" level="project" />
<orderEntry type="library" exported="" name="leakcanary-android-1.3.1" level="project" />
<orderEntry type="library" exported="" name="logback-android-classic-1.1.1-3" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-22.2.1" level="project" />
<orderEntry type="library" exported="" name="json-io-2.4.1" level="project" />
<orderEntry type="library" exported="" name="leveldb-api-0.7" level="project" />
<orderEntry type="library" exported="" name="jackson-databind-2.5.1" level="project" />
<orderEntry type="library" exported="" name="leveldb-0.7" level="project" />
<orderEntry type="library" exported="" name="persistence-api-1.0.2" level="project" />
<orderEntry type="library" exported="" name="netty-all-4.0.28.Final" level="project" />
<orderEntry type="library" exported="" name="multidex-1.0.1" level="project" />
<orderEntry type="library" exported="" name="commons-io-2.4" level="project" />
<orderEntry type="library" exported="" name="log4j-1.2.17" level="project" />
<orderEntry type="library" exported="" name="library-1.1" level="project" />
<orderEntry type="library" exported="" name="guava-18.0" level="project" />
<orderEntry type="library" exported="" name="jackson-annotations-2.5.0" level="project" />
<orderEntry type="library" exported="" name="javassist-3.15.0-GA" level="project" />
<orderEntry type="library" exported="" name="javax.annotation-10.0-b28" level="project" />
<orderEntry type="library" exported="" name="json-smart-1.3.1" level="project" />
<orderEntry type="library" exported="" name="core-1.51.0.0" level="project" />
<orderEntry type="library" exported="" name="prov-1.51.0.0" level="project" />
<orderEntry type="library" exported="" name="rxjava-core-0.20.7" level="project" />
<orderEntry type="library" exported="" name="leakcanary-watcher-1.3.1" level="project" />
<orderEntry type="library" exported="" name="support-annotations-22.2.1" level="project" />
<orderEntry type="library" exported="" name="commons-pool-1.5.4" level="project" />
<orderEntry type="library" exported="" name="dagger-2.1-SNAPSHOT" level="project" />
<orderEntry type="library" exported="" name="jsr305-3.0.0" level="project" />
<orderEntry type="library" exported="" name="commons-dbcp-1.4" level="project" />
<orderEntry type="library" exported="" name="rxjava-android-0.20.7" level="project" />
<orderEntry type="library" exported="" name="slf4j-api-1.7.12" level="project" />
<orderEntry type="library" exported="" name="support-v4-22.2.1" level="project" />
<orderEntry type="library" exported="" name="javax.inject-1" level="project" />
<orderEntry type="library" exported="" name="json-simple-1.1.1" level="project" />
<orderEntry type="library" exported="" name="jsonrpc2-client-1.15" level="project" />
<orderEntry type="library" exported="" name="jackson-core-2.5.1" level="project" />
<orderEntry type="module" module-name="ethereumj-core-android" exported="" />
<orderEntry type="module" module-name="ethereumj-core" exported="" />
<orderEntry type="module" module-name="cordova-android" exported="" />
</component>
</module>

View File

@ -53,9 +53,11 @@ dependencies {
compile 'co.dift.ui.swipetoaction:library:1.1'
// compile 'de.hdodenhof:circleimageview:1.3.0'
compile 'com.getbase:floatingactionbutton:1.9.0'
compile 'com.github.ksoichiro:android-observablescrollview:1.5.2'
// compile 'com.github.ksoichiro:android-observablescrollview:1.5.2'
compile 'com.afollestad:material-dialogs:0.7.7.0'
compile 'com.github.bumptech.glide:glide:3.6.1'
compile(project(':cordova-android')) {
}
compile 'com.netflix.rxjava:rxjava-android:0.20.7'

View File

@ -1,23 +1,305 @@
package io.syng.fragment;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Color;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import com.github.ksoichiro.android.observablescrollview.ObservableWebView;
import com.squareup.leakcanary.RefWatcher;
import org.apache.cordova.ConfigXmlParser;
import org.apache.cordova.CordovaInterfaceImpl;
import org.apache.cordova.CordovaPreferences;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CordovaWebViewEngine;
import org.apache.cordova.CordovaWebViewImpl;
import org.apache.cordova.PluginEntry;
import org.apache.cordova.PluginManager;
import java.util.ArrayList;
import java.util.Locale;
import io.syng.R;
import io.syng.app.SyngApplication;
public class WebViewFragment extends Fragment {
protected CordovaWebView webView;
protected CordovaInterfaceImpl cordovaInterface;
protected CordovaPreferences preferences;
protected ArrayList<PluginEntry> pluginEntries;
protected boolean keepRunning = true;
protected boolean immersiveMode;
protected View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
loadConfig();
/*
TODO: it's can be only in activity before adding content and we may need it in Cordova Dapps
if(!preferences.getBoolean("ShowTitle", false))
{
try {
getActivity().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
} catch (Exception e) {
System.out.print(e);
}
}
if(preferences.getBoolean("SetFullscreen", false))
{
preferences.set("Fullscreen", true);
}
if(preferences.getBoolean("Fullscreen", false))
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
immersiveMode = true;
}
else
{
getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
} else {
getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
}
*/
view = inflater.inflate(R.layout.fragment_web_view, container, false);
cordovaInterface = makeCordovaInterface();
if(savedInstanceState != null)
{
cordovaInterface.restoreInstanceState(savedInstanceState);
}
init();
// loadUrl("http://trustdavis.meteor.com");
loadUrl("file:///android_asset/boilerplate/index.html");
return view;
}
public void loadUrl(String url) {
if (webView == null) {
init();
}
// If keepRunning
this.keepRunning = preferences.getBoolean("KeepRunning", true);
webView.loadUrlIntoView(url, true);
}
protected void init() {
webView = makeWebView();
createViews();
if (!webView.isInitialized()) {
webView.init(cordovaInterface, pluginEntries, preferences);
}
webView.getView().requestFocusFromTouch();
cordovaInterface.onCordovaInit(webView.getPluginManager());
// Wire the hardware volume controls to control media if desired.
String volumePref = preferences.getString("DefaultVolumeStream", "");
if ("media".equals(volumePref.toLowerCase(Locale.ENGLISH))) {
getActivity().setVolumeControlStream(AudioManager.STREAM_MUSIC);
}
}
@SuppressWarnings("deprecation")
protected void loadConfig() {
ConfigXmlParser parser = new ConfigXmlParser();
parser.parse(getActivity());
preferences = parser.getPreferences();
preferences.setPreferencesBundle(getActivity().getIntent().getExtras());
pluginEntries = parser.getPluginEntries();
}
@SuppressWarnings({"deprecation", "ResourceType"})
protected void createViews() {
//Why are we setting a constant as the ID? This should be investigated
webView.getView().setId(1000);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
p.addRule(RelativeLayout.BELOW, R.id.myToolbar);
webView.getView().setLayoutParams(p);
RelativeLayout rl = (RelativeLayout)view.findViewById(R.id.web_view_layout);
rl.addView(webView.getView());
if (preferences.contains("BackgroundColor")) {
int backgroundColor = preferences.getInteger("BackgroundColor", Color.BLACK);
// Background of activity:
webView.getView().setBackgroundColor(backgroundColor);
}
webView.getView().requestFocusFromTouch();
}
protected CordovaWebView makeWebView() {
return new CordovaWebViewImpl(makeWebViewEngine());
}
protected CordovaWebViewEngine makeWebViewEngine() {
return CordovaWebViewImpl.createEngine(getActivity(), preferences);
}
protected CordovaInterfaceImpl makeCordovaInterface() {
return new CordovaInterfaceImpl(getActivity()) {
@Override
public Object onMessage(String id, Object data) {
return WebViewFragment.this.onMessage(id, data);
}
};
}
public Object onMessage(String id, Object data) {
if ("onReceivedError".equals(id)) {
//TODO: do we need handle error and show it?
/*
JSONObject d = (JSONObject) data;
try {
this.onReceivedError(d.getInt("errorCode"), d.getString("description"), d.getString("url"));
} catch (JSONException e) {
e.printStackTrace();
}
*/
} else if ("exit".equals(id)) {
getActivity().finish();
}
return null;
}
@Override
public void onPause() {
super.onPause();
if (this.webView != null) {
boolean keepRunning = this.keepRunning;// || this.cordovaInterface.activityResultCallback != null;
this.webView.handlePause(keepRunning);
}
}
/*
TODO: it's can be only in activity and we may need it in Cordova Dapps
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
//Forward to plugins
if (this.webView != null)
this.webView.onNewIntent(intent);
}
*/
@Override
public void onResume() {
super.onResume();
if (this.webView == null) {
return;
}
// Force window to have focus, so application always
// receive user input. Workaround for some devices (Samsung Galaxy Note 3 at least)
getActivity().getWindow().getDecorView().requestFocus();
this.webView.handleResume(this.keepRunning);
}
@Override
public void onStop() {
super.onStop();
if (this.webView == null) {
return;
}
this.webView.handleStop();
}
@Override
public void onStart() {
super.onStart();
if (this.webView == null) {
return;
}
this.webView.handleStart();
}
@Override
public void onDestroy() {
super.onDestroy();
if (this.webView != null) {
webView.handleDestroy();
}
RefWatcher refWatcher = SyngApplication.getRefWatcher(getActivity());
refWatcher.watch(this);
}
/*
TODO: it's can be only in activity and we may need it in Cordova Dapps
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus && immersiveMode) {
final int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
getWindow().getDecorView().setSystemUiVisibility(uiOptions);
}
}
*/
@Override
public void startActivityForResult(Intent intent, int requestCode) {
cordovaInterface.setActivityResultRequestCode(requestCode);
super.startActivityForResult(intent, requestCode);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
cordovaInterface.onActivityResult(requestCode, resultCode, intent);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (this.webView == null) {
return;
}
PluginManager pm = this.webView.getPluginManager();
if (pm != null) {
pm.onConfigurationChanged(newConfig);
}
}
/*
private static final String HTTP_TRUSTDAVIS_METEOR_COM = "http://trustdavis.meteor.com";
public WebViewFragment() {
@ -46,6 +328,8 @@ public class WebViewFragment extends Fragment {
//// }
//// });
webView.loadUrl(HTTP_TRUSTDAVIS_METEOR_COM);
Activity tmp = getActivity();
tmp.getWindow().closeAllPanels();
return view;
}
@ -55,5 +339,5 @@ public class WebViewFragment extends Fragment {
RefWatcher refWatcher = SyngApplication.getRefWatcher(getActivity());
refWatcher.watch(this);
}
*/
}

View File

@ -1,12 +1,14 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:id="@+id/web_view_layout"
>
<!--
<com.github.ksoichiro.android.observablescrollview.ObservableWebView
android:id="@+id/web_view"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
-->
</RelativeLayout>

View File

@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<widget xmlns = "http://www.w3.org/ns/widgets"
id = "io.cordova.helloCordova"
version = "2.0.0">
<name>Syng</name>
<description>
</description>
<author href="http://jarradhope.com" email="me@jarradhope.com">
Jarrad Hope
</author>
<!-- <content src="http://mysite.com/myapp.html" /> for external pages -->
<content src="index.html" />
<!-- Whitelist docs: https://github.com/apache/cordova-plugin-whitelist -->
<access origin="*" />
<!-- Grant certain URLs the ability to launch external applications. This
behaviour is set to match that of Cordova versions before 3.6.0, and
should be reviewed before launching an application in production. It
may be changed in the future. -->
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
<allow-intent href="market:*" />
<preference name="loglevel" value="DEBUG" />
<!--
<preference name="splashscreen" value="splash" />
<preference name="backgroundColor" value="0xFFF" />
<preference name="loadUrlTimeoutValue" value="20000" />
<preference name="InAppBrowserStorageEnabled" value="true" />
<preference name="disallowOverscroll" value="true" />
-->
</widget>