1 /* 2 * Copyright (C) 2018 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.example.android.multiclientinputmethod; 18 19 import android.app.Service; 20 import android.content.Intent; 21 import android.inputmethodservice.MultiClientInputMethodServiceDelegate; 22 import android.os.IBinder; 23 import android.util.Log; 24 25 /** 26 * A {@link Service} that implements multi-client IME protocol. 27 */ 28 public final class MultiClientInputMethod extends Service { 29 private static final String TAG = "MultiClientInputMethod"; 30 private static final boolean DEBUG = false; 31 32 // last client that had active InputConnection. 33 int mLastClientId = MultiClientInputMethodServiceDelegate.INVALID_CLIENT_ID; 34 SoftInputWindowManager mSoftInputWindowManager; 35 MultiClientInputMethodServiceDelegate mDelegate; 36 37 @Override onCreate()38 public void onCreate() { 39 if (DEBUG) { 40 Log.v(TAG, "onCreate"); 41 } 42 mDelegate = MultiClientInputMethodServiceDelegate.create(this, 43 new MultiClientInputMethodServiceDelegate.ServiceCallback() { 44 @Override 45 public void initialized() { 46 if (DEBUG) { 47 Log.i(TAG, "initialized"); 48 } 49 } 50 51 @Override 52 public void addClient(int clientId, int uid, int pid, 53 int selfReportedDisplayId) { 54 final ClientCallbackImpl callback = new ClientCallbackImpl( 55 MultiClientInputMethod.this, mDelegate, 56 mSoftInputWindowManager, clientId, uid, pid, selfReportedDisplayId); 57 if (DEBUG) { 58 Log.v(TAG, "addClient clientId=" + clientId + " uid=" + uid 59 + " pid=" + pid + " displayId=" + selfReportedDisplayId); 60 } 61 mDelegate.acceptClient(clientId, callback, callback.getDispatcherState(), 62 callback.getLooper()); 63 } 64 65 @Override 66 public void removeClient(int clientId) { 67 if (DEBUG) { 68 Log.v(TAG, "removeClient clientId=" + clientId); 69 } 70 } 71 }); 72 mSoftInputWindowManager = new SoftInputWindowManager(this, mDelegate); 73 } 74 75 @Override onBind(Intent intent)76 public IBinder onBind(Intent intent) { 77 if (DEBUG) { 78 Log.v(TAG, "onBind intent=" + intent); 79 } 80 return mDelegate.onBind(intent); 81 } 82 83 @Override onUnbind(Intent intent)84 public boolean onUnbind(Intent intent) { 85 if (DEBUG) { 86 Log.v(TAG, "onUnbind intent=" + intent); 87 } 88 return mDelegate.onUnbind(intent); 89 } 90 91 @Override onDestroy()92 public void onDestroy() { 93 if (DEBUG) { 94 Log.v(TAG, "onDestroy"); 95 } 96 mDelegate.onDestroy(); 97 } 98 } 99