1 /*
2  * Copyright (C) 2022 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 android.view.inputmethod.cts.inprocime;
18 
19 import android.inputmethodservice.InputMethodService;
20 
21 import androidx.annotation.AnyThread;
22 import androidx.annotation.MainThread;
23 import androidx.annotation.Nullable;
24 
25 import java.util.concurrent.atomic.AtomicReference;
26 
27 public final class InProcIme extends InputMethodService {
28 
29     private static AtomicReference<InProcIme> sInstance = new AtomicReference<>();
30 
31     @MainThread
32     @Override
onCreate()33     public void onCreate() {
34         super.onCreate();
35         sInstance.compareAndSet(null, this);
36     }
37 
38     @MainThread
39     @Override
onDestroy()40     public void onDestroy() {
41         super.onDestroy();
42         sInstance.compareAndSet(this, null);
43         mOnUpdateSelectionListener = null;
44     }
45 
46     @AnyThread
getInstance()47     public static InProcIme getInstance() {
48         return sInstance.get();
49     }
50 
51     @FunctionalInterface
52     public interface OnUpdateSelectionListener {
onUpdateSelection(int oldSelStart, int oldSelEnd, int newSelStart, int newSelEnd, int candidatesStart, int candidatesEnd)53         void onUpdateSelection(int oldSelStart, int oldSelEnd, int newSelStart, int newSelEnd,
54                 int candidatesStart, int candidatesEnd);
55     }
56 
57     @Nullable
58     private OnUpdateSelectionListener mOnUpdateSelectionListener = null;
59 
60     @MainThread
setOnUpdateSelectionListener(@ullable OnUpdateSelectionListener listener)61     public void setOnUpdateSelectionListener(@Nullable OnUpdateSelectionListener listener) {
62         mOnUpdateSelectionListener = listener;
63     }
64 
65     @MainThread
66     @Override
onUpdateSelection(int oldSelStart, int oldSelEnd, int newSelStart, int newSelEnd, int candidatesStart, int candidatesEnd)67     public void onUpdateSelection(int oldSelStart, int oldSelEnd, int newSelStart, int newSelEnd,
68             int candidatesStart, int candidatesEnd) {
69         super.onUpdateSelection(oldSelStart, oldSelEnd, newSelStart, newSelEnd, candidatesStart,
70                 candidatesEnd);
71         if (mOnUpdateSelectionListener != null) {
72             mOnUpdateSelectionListener.onUpdateSelection(oldSelStart, oldSelEnd, newSelStart,
73                     newSelEnd, candidatesStart, candidatesEnd);
74         }
75     }
76 }
77