Chromium Code Reviews| Index: examples/android/App.java |
| diff --git a/examples/android/App.java b/examples/android/App.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..098927774e398f22e8305244c909b8f08fe6a4e9 |
| --- /dev/null |
| +++ b/examples/android/App.java |
| @@ -0,0 +1,169 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.examples.android; |
| + |
| +import android.content.Context; |
| +import android.util.Log; |
| + |
| +import org.chromium.mojo.bindings.Interface; |
| +import org.chromium.mojo.bindings.Interface.Manager; |
| +import org.chromium.mojo.bindings.InterfaceRequest; |
| +import org.chromium.mojo.system.Core; |
| +import org.chromium.mojo.system.MessagePipeHandle; |
| +import org.chromium.mojo.system.MojoException; |
| +import org.chromium.mojo.system.RunLoop; |
| +import org.chromium.mojom.mojo.Application; |
| +import org.chromium.mojom.mojo.ExampleService; |
| +import org.chromium.mojom.mojo.ServiceProvider; |
| +import org.chromium.mojom.mojo.Shell; |
| + |
| +import java.util.ArrayList; |
| +import java.util.HashMap; |
| +import java.util.List; |
| +import java.util.Map; |
| +import java.util.concurrent.Callable; |
| + |
| +/** |
| + * App holds all the code for this example application. |
| + */ |
| +class App { |
| + private static String TAG = "AndroidTestApp"; |
|
qsr
2015/02/06 17:16:14
Android doesn't really tell us anything when looki
etiennej
2015/02/09 14:09:51
Done.
|
| + |
| + public static class ApplicationImpl implements Application { |
| + private final Core mCore; |
| + private Shell mShell; |
| + private List<ServiceProvider> mProviders = new ArrayList<ServiceProvider>(); |
|
qsr
2015/02/06 17:16:14
final?
Also, I think you do not need those. Each
etiennej
2015/02/09 14:09:51
Done.
|
| + |
| + public ApplicationImpl(MessagePipeHandle applicationRequest, Core core) { |
| + ApplicationImpl.MANAGER.bind(this, applicationRequest); |
| + mCore = core; |
| + } |
| + |
| + @Override |
| + public void initialize(Shell shell, String[] args) { |
| + mShell = shell; |
| + } |
| + |
| + @Override |
| + public void acceptConnection(String requestorUrl, |
| + InterfaceRequest<ServiceProvider> services, ServiceProvider exposedServices) { |
| + ServiceProviderImpl serviceProvider = new ServiceProviderImpl(); |
| + serviceProvider.addService( |
| + new ServiceFactoryBinder<ExampleService>( |
| + ExampleServiceImpl.MANAGER, |
| + new Callable<ExampleService>() { |
| + @Override |
| + public ExampleService call() { |
| + return new ExampleServiceImpl(); |
| + } |
| + } |
| + )); |
| + ServiceProvider.MANAGER.bind(serviceProvider, services); |
| + mProviders.add(serviceProvider); |
| + exposedServices.close(); |
| + }; |
| + |
| + @Override |
| + public void requestQuit() { |
| + mCore.getCurrentRunLoop().quit(); |
| + } |
| + |
| + @Override |
| + public void close() { |
| + if (mShell != null) { |
| + mShell.close(); |
| + } |
| + } |
| + |
| + @Override |
| + public void onConnectionError(MojoException e) {} |
| + } |
| + |
| + /** |
| + * ServiceFactoryBinder holds the necessary information to bind a service interface to a |
| + * message pipe. |
| + */ |
| + private static class ServiceFactoryBinder<T extends Interface> { |
| + private final Callable<T> mFactory; |
| + private final Manager<T, ?> mManager; |
| + |
| + public ServiceFactoryBinder(Manager<T, ?> manager, |
| + Callable<T> factory) { |
| + mFactory = factory; |
| + mManager = manager; |
| + } |
| + |
| + public void bindNewInstanceToMessagePipe(MessagePipeHandle pipe) throws Exception { |
|
qsr
2015/02/06 17:16:14
Why do you need to throws Exception here? If that'
etiennej
2015/02/09 14:09:51
Done.
|
| + mManager.bind(mFactory.call(), pipe); |
| + } |
| + |
| + public String getInterfaceName() { |
| + return mManager.getName(); |
| + } |
| + } |
| + |
| + public static class ServiceProviderImpl implements ServiceProvider { |
| + private Map<String, ServiceFactoryBinder> mNameToServiceMap = |
|
qsr
2015/02/06 17:16:14
I'm surprise you do not have a warning. Please use
etiennej
2015/02/09 14:09:51
Done.
|
| + new HashMap<String, ServiceFactoryBinder>(); |
| + |
| + public ServiceProviderImpl() {} |
| + |
| + /** |
| + * AddService adds a new service to the list of services provided by this application. |
| + */ |
| + public void addService(ServiceFactoryBinder binder) { |
| + mNameToServiceMap.put(binder.getInterfaceName(), binder); |
| + } |
| + |
| + @Override |
| + public void connectToService(String interfaceName, MessagePipeHandle pipe) { |
| + if (mNameToServiceMap.containsKey(interfaceName)) { |
| + try { |
| + mNameToServiceMap.get(interfaceName).bindNewInstanceToMessagePipe(pipe); |
| + } catch (Exception e) { |
| + pipe.close(); |
| + throw new RuntimeException(e); |
| + } |
| + } else { |
| + pipe.close(); |
| + } |
| + } |
| + |
| + @Override |
| + public void close() {} |
| + |
| + @Override |
| + public void onConnectionError(MojoException e) {} |
| + } |
| + |
| + public static class ExampleServiceImpl implements ExampleService { |
| + public ExampleServiceImpl() {} |
| + |
| + @Override |
| + public void ping(short pingValue, ExampleService.PingResponse callback) { |
| + callback.call(pingValue); |
| + } |
| + |
| + @Override |
| + public void close() {} |
| + |
| + @Override |
| + public void onConnectionError(MojoException e) {} |
| + } |
| + |
| + public static void mojoMain(Context context, Core core, int applicationRequestHandle) { |
| + Log.i(TAG, "App.MojoMain called"); |
| + RunLoop runLoop = core.createDefaultRunLoop(); |
| + Log.i(TAG, "Run loop created"); |
| + |
| + MessagePipeHandle handle = |
| + core.acquireNativeHandle(applicationRequestHandle).toMessagePipeHandle(); |
| + Application application = new ApplicationImpl(handle, core); |
| + |
| + Log.i(TAG, "Setup done, starting run loop"); |
| + runLoop.run(); |
| + Log.i(TAG, "Run loop exited"); |
| + } |
| +} |