Home | History | Annotate | Download | only in service
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.googlecode.android_scripting.service;
     18 
     19 import android.app.Service;
     20 import android.content.Intent;
     21 import android.os.HandlerThread;
     22 import android.os.IBinder;
     23 import android.os.Messenger;
     24 
     25 import com.googlecode.android_scripting.facade.FacadeConfiguration;
     26 import com.googlecode.android_scripting.facade.FacadeManagerFactory;
     27 import com.googlecode.android_scripting.jsonrpc.RpcReceiverManagerFactory;
     28 
     29 /**
     30  * FacadeService exposes SL4A's Facade methods through a {@link Service} so
     31  * they can be invoked from third-party apps.
     32  * <p>
     33  * Example binding code:<br>
     34  * {@code
     35  *   Intent intent = new Intent();
     36  *   intent.setPackage("com.googlecode.android_scripting");
     37  *   intent.setAction("com.googlecode.android_scripting.service.FacadeService.ACTION_BIND");
     38  *   sl4aService = bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
     39  * }
     40  * Example using the service:<br>
     41  * {@code
     42  *   Bundle sl4aBundle = new Bundle;
     43  *   bundle.putString{"sl4aMethod", "makeToast"};
     44  *   bundle.putString{"message", "Hello World!"};
     45  *   Message msg = Message.obtain(null, SL4A_ACTION);
     46  *   msg.setData(sl4aBundle);
     47  *   msg.replyTo = myReplyHandler; // Set a Messenger if you need the response
     48  *   mSl4aService.send(msg);
     49  * }
     50  * <p>
     51  * For more info on binding a {@link Service} using a {@link Messenger} please
     52  * refer to Android's public developer documentation.
     53  */
     54 public class FacadeService extends Service {
     55 
     56     private RpcReceiverManagerFactory rpcReceiverManagerFactory;
     57 
     58     @Override
     59     public IBinder onBind(Intent intent) {
     60         if (rpcReceiverManagerFactory == null) {
     61             rpcReceiverManagerFactory =
     62                     new FacadeManagerFactory(FacadeConfiguration.getSdkLevel(), this, null,
     63                             FacadeConfiguration.getFacadeClasses());
     64         }
     65         HandlerThread handlerThread = new HandlerThread("MessageHandlerThread");
     66         handlerThread.start();
     67         Messenger aMessenger = new Messenger(new MessageHandler(handlerThread,
     68                 rpcReceiverManagerFactory));
     69         return aMessenger.getBinder();
     70     }
     71 }
     72