Skip to content

Commit

Permalink
Update Android Hello demo for 3.8 release (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
pepone authored Jan 9, 2025
1 parent 2fae807 commit a9166b7
Show file tree
Hide file tree
Showing 8 changed files with 474 additions and 61 deletions.
6 changes: 3 additions & 3 deletions java/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ allprojects {
}

ext {
compileSdkVersion = 33
minSdkVersion = 31
targetSdkVersion = 33
compileSdkVersion = 34
minSdkVersion = 34
targetSdkVersion = 34
}
3 changes: 0 additions & 3 deletions java/android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,3 @@ cppPlatform =
# The configuration uses by the C++ builds, supported values are `Debug` and `Release`, that is required to locate the
# slice2java compiler in the configuration depend directory.
cppConfiguration =
android.defaults.buildfeatures.buildconfig=true
android.nonTransitiveRClass=false
android.nonFinalResIds=false
428 changes: 416 additions & 12 deletions java/android/gradle/GRADLE_LICENSE

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion java/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
19 changes: 14 additions & 5 deletions java/android/gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand All @@ -80,10 +80,10 @@ do
esac
done

APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit

APP_NAME="Gradle"
# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
Expand Down Expand Up @@ -116,7 +116,6 @@ esac

CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar


# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
Expand All @@ -143,12 +142,16 @@ fi
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
Expand Down Expand Up @@ -205,6 +208,12 @@ set -- \
org.gradle.wrapper.GradleWrapperMain \
"$@"

# Stop when "xargs" is not available.
if ! command -v xargs >/dev/null 2>&1
then
die "xargs is not available"
fi

# Use "xargs" to parse quoted args.
#
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
Expand Down
16 changes: 9 additions & 7 deletions java/android/gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@rem limitations under the License.
@rem

@if "%DEBUG%" == "" @echo off
@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
Expand All @@ -25,7 +25,8 @@
if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
if "%DIRNAME%"=="" set DIRNAME=.
@rem This is normally unused
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

Expand All @@ -40,7 +41,7 @@ if defined JAVA_HOME goto findJavaFromJavaHome

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto execute
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Expand Down Expand Up @@ -69,19 +70,20 @@ goto fail

set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar


@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*

:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
if %ERRORLEVEL% equ 0 goto mainEnd

:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
set EXIT_CODE=%ERRORLEVEL%
if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%

:mainEnd
if "%OS%"=="Windows_NT" endlocal
Expand Down
4 changes: 2 additions & 2 deletions java/android/hello/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
minSdkVersion rootProject.minSdkVersion
targetSdkVersion rootProject.targetSdkVersion
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
applicationId "com.zeroc.hello"
}
Expand Down
57 changes: 29 additions & 28 deletions java/android/hello/src/main/java/com/zeroc/hello/HelloApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,22 @@
import com.zeroc.Ice.Communicator;
import com.zeroc.Ice.Connection;
import com.zeroc.Ice.InitializationData;
import com.zeroc.Ice.InitializationException;
import com.zeroc.Ice.InvocationFuture;
import com.zeroc.Ice.LocalException;
import com.zeroc.Ice.ObjectPrx;
import com.zeroc.Ice.Properties;
import com.zeroc.Ice.Util;
import com.zeroc.demos.android.hello.Demo.HelloPrx;

import java.security.KeyStore;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;

public class HelloApp extends Application
{
static class MessageReady
Expand Down Expand Up @@ -74,10 +80,7 @@ else if(m.what == MSG_EXCEPTION || m.what == MSG_RESPONSE)

WifiManager wifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);

//
// On some devices, a multicast lock must be acquired otherwise multicast packets are discarded.
// The lock is initially unacquired.
//
WifiManager.MulticastLock lock = wifiManager.createMulticastLock("com.zeroc.hello");
lock.acquire();

Expand All @@ -90,46 +93,44 @@ else if(m.what == MSG_EXCEPTION || m.what == MSG_RESPONSE)

initData.executor = (Runnable runnable, Connection connection) -> _uiHandler.post(runnable);

initData.properties = Util.createProperties();
initData.properties = new Properties();
initData.properties.setProperty("Ice.Trace.Network", "3");

initData.properties.setProperty("IceSSL.Trace.Security", "3");
initData.properties.setProperty("IceSSL.KeystoreType", "BKS");
initData.properties.setProperty("IceSSL.TruststoreType", "BKS");
initData.properties.setProperty("IceSSL.Password", "password");
initData.properties.setProperty("Ice.Plugin.IceSSL", "com.zeroc.IceSSL.PluginFactory");
initData.properties.setProperty("Ice.Plugin.IceDiscovery", "com.zeroc.IceDiscovery.PluginFactory");
initData.properties.setProperty("Ice.Default.Package", "com.zeroc.demos.android.hello");

//
// We need to postpone plug-in initialization so that we can configure IceSSL
// with a resource stream for the certificate information.
//
initData.properties.setProperty("Ice.InitPlugins", "0");
// Load the user KeyStore from client.bks application resource
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(getResources().openRawResource(R.raw.client), "password".toCharArray());

Communicator c = Util.initialize(initData);
// Create a trust manager factory that uses the certificates from our KeyStore.
TrustManagerFactory trustManagerFactory =
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keyStore);

// Create a SSLContext for TLS and initialize it with our custom trust manager factory.
var sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManagerFactory.getTrustManagers(), null);

//
// Now we complete the plug-in initialization.
//
com.zeroc.IceSSL.Plugin plugin = (com.zeroc.IceSSL.Plugin)c.getPluginManager().getPlugin("IceSSL");
//
// Be sure to pass the same input stream to the SSL plug-in for
// both the keystore and the truststore. This makes startup a
// little faster since the plug-in will not initialize
// two keystores.
//
java.io.InputStream certs = getResources().openRawResource(R.raw.client);
plugin.setKeystoreStream(certs);
plugin.setTruststoreStream(certs);
c.getPluginManager().initializePlugins();
// Setup the SSLEngine factory used by secure (TLS) client connections.
initData.clientSSLEngineFactory =
(String peerHost, int peerPort) -> sslContext.createSSLEngine(peerHost, peerPort);

Communicator c = Util.initialize(initData);
_uiHandler.sendMessage(Message.obtain(_uiHandler, MSG_READY, new MessageReady(c, null)));
}
catch(LocalException e)
{
_uiHandler.sendMessage(Message.obtain(_uiHandler, MSG_READY, new MessageReady(null, e)));
}
catch (Exception e)
{
_uiHandler.sendMessage(Message.obtain(
_uiHandler,
MSG_READY,
new MessageReady(null, new InitializationException("Communicator initialization failed", e))));
}
}).start();
}

Expand Down

0 comments on commit a9166b7

Please sign in to comment.