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.disapproveime;
18 
19 import android.inputmethodservice.InputMethodService;
20 import android.view.View;
21 import android.widget.LinearLayout;
22 
23 /**
24  * This IME simulates deprecated or inapplicable operations for test.
25  */
26 public final class DisapproveInputMethodService extends InputMethodService {
27 
28     /**
29      * The callback for {@link DisapproveInputMethodService} test verification.
30      */
31     public static DisapproveImeCallback mDisapproveImeCallback;
32 
33     @Override
onCreateInputView()34     public View onCreateInputView() {
35         return new LinearLayout(this);
36     }
37 
38     @Override
onCreate()39     public void onCreate() {
40         boolean hasLinkageError = false;
41         try {
42             super.onCreate();
43         } catch (Throwable e) {
44             if (e instanceof LinkageError) {
45                 hasLinkageError = true;
46             }
47         }
48         if (mDisapproveImeCallback != null) {
49             mDisapproveImeCallback.onCreate(hasLinkageError);
50         }
51     }
52 
53     @Override
onDestroy()54     public void onDestroy() {
55         // no-op
56     }
57 
58     /**
59      * Override the deprecated {@link InputMethodService#onCreateInputMethodSessionInterface}
60      * for test.
61      */
62     @Override
onCreateInputMethodSessionInterface()63     public AbstractInputMethodSessionImpl onCreateInputMethodSessionInterface() {
64         return new MockInputMethodSessionImpl();
65     }
66 
67     private class MockInputMethodSessionImpl extends InputMethodSessionImpl {
68         // no-op
69     }
70 
71     /**
72      * Set {@link DisapproveImeCallback} callback.
73      */
setCallback(DisapproveImeCallback callback)74     public static void setCallback(DisapproveImeCallback callback) {
75         mDisapproveImeCallback = callback;
76     }
77 
78     /**
79      * Defines a callback for {@link DisapproveInputMethodService} test.
80      */
81     public interface DisapproveImeCallback {
onCreate(boolean hasLinkageError)82         void onCreate(boolean hasLinkageError);
83     }
84 }
85