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.hardware.display.DisplayManager; 22 import android.hardware.display.DisplayManager.DisplayListener; 23 import android.inputmethodservice.MultiClientInputMethodServiceDelegate; 24 import android.os.IBinder; 25 import android.util.Log; 26 import android.util.SparseIntArray; 27 28 /** 29 * A {@link Service} that implements multi-client IME protocol. 30 */ 31 public final class MultiClientInputMethod extends Service implements DisplayListener { 32 private static final String TAG = "MultiClientInputMethod"; 33 private static final boolean DEBUG = false; 34 35 // last client that had active InputConnection for a given displayId. 36 final SparseIntArray mDisplayToLastClientId = new SparseIntArray(); 37 SoftInputWindowManager mSoftInputWindowManager; 38 MultiClientInputMethodServiceDelegate mDelegate; 39 40 private DisplayManager mDisplayManager; 41 42 @Override onCreate()43 public void onCreate() { 44 if (DEBUG) { 45 Log.v(TAG, "onCreate"); 46 } 47 mDelegate = MultiClientInputMethodServiceDelegate.create(this, 48 new MultiClientInputMethodServiceDelegate.ServiceCallback() { 49 @Override 50 public void initialized() { 51 if (DEBUG) { 52 Log.i(TAG, "initialized"); 53 } 54 } 55 56 @Override 57 public void addClient(int clientId, int uid, int pid, 58 int selfReportedDisplayId) { 59 final ClientCallbackImpl callback = new ClientCallbackImpl( 60 MultiClientInputMethod.this, mDelegate, 61 mSoftInputWindowManager, clientId, uid, pid, selfReportedDisplayId); 62 if (DEBUG) { 63 Log.v(TAG, "addClient clientId=" + clientId + " uid=" + uid 64 + " pid=" + pid + " displayId=" + selfReportedDisplayId); 65 } 66 mDelegate.acceptClient(clientId, callback, callback.getDispatcherState(), 67 callback.getLooper()); 68 } 69 70 @Override 71 public void removeClient(int clientId) { 72 if (DEBUG) { 73 Log.v(TAG, "removeClient clientId=" + clientId); 74 } 75 } 76 }); 77 mSoftInputWindowManager = new SoftInputWindowManager(this, mDelegate); 78 } 79 80 @Override onDisplayAdded(int displayId)81 public void onDisplayAdded(int displayId) { 82 } 83 84 @Override onDisplayRemoved(int displayId)85 public void onDisplayRemoved(int displayId) { 86 mDisplayToLastClientId.delete(displayId); 87 } 88 89 @Override onDisplayChanged(int displayId)90 public void onDisplayChanged(int displayId) { 91 } 92 93 @Override onBind(Intent intent)94 public IBinder onBind(Intent intent) { 95 if (DEBUG) { 96 Log.v(TAG, "onBind intent=" + intent); 97 } 98 mDisplayManager = getApplicationContext().getSystemService(DisplayManager.class); 99 mDisplayManager.registerDisplayListener(this, getMainThreadHandler()); 100 return mDelegate.onBind(intent); 101 } 102 103 @Override onUnbind(Intent intent)104 public boolean onUnbind(Intent intent) { 105 if (DEBUG) { 106 Log.v(TAG, "onUnbind intent=" + intent); 107 } 108 if (mDisplayManager != null) { 109 mDisplayManager.unregisterDisplayListener(this); 110 } 111 return mDelegate.onUnbind(intent); 112 } 113 114 @Override onDestroy()115 public void onDestroy() { 116 if (DEBUG) { 117 Log.v(TAG, "onDestroy"); 118 } 119 mDelegate.onDestroy(); 120 } 121 } 122