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.android.internal.inputmethod;
18 
19 import android.view.WindowManager;
20 import android.view.WindowManager.LayoutParams.SoftInputModeFlags;
21 
22 import java.util.StringJoiner;
23 
24 /**
25  * Provides useful methods for debugging.
26  */
27 public final class InputMethodDebug {
28     /**
29      * Not intended to be instantiated.
30      */
InputMethodDebug()31     private InputMethodDebug() {
32     }
33 
34     /**
35      * Converts {@link StartInputReason} to {@link String} for debug logging.
36      *
37      * @param reason integer constant for {@link StartInputReason}.
38      * @return {@link String} message corresponds for the given {@code reason}.
39      */
startInputReasonToString(@tartInputReason int reason)40     public static String startInputReasonToString(@StartInputReason int reason) {
41         switch (reason) {
42             case StartInputReason.UNSPECIFIED:
43                 return "UNSPECIFIED";
44             case StartInputReason.WINDOW_FOCUS_GAIN:
45                 return "WINDOW_FOCUS_GAIN";
46             case StartInputReason.WINDOW_FOCUS_GAIN_REPORT_ONLY:
47                 return "WINDOW_FOCUS_GAIN_REPORT_ONLY";
48             case StartInputReason.APP_CALLED_RESTART_INPUT_API:
49                 return "APP_CALLED_RESTART_INPUT_API";
50             case StartInputReason.CHECK_FOCUS:
51                 return "CHECK_FOCUS";
52             case StartInputReason.BOUND_TO_IMMS:
53                 return "BOUND_TO_IMMS";
54             case StartInputReason.UNBOUND_FROM_IMMS:
55                 return "UNBOUND_FROM_IMMS";
56             case StartInputReason.ACTIVATED_BY_IMMS:
57                 return "ACTIVATED_BY_IMMS";
58             case StartInputReason.DEACTIVATED_BY_IMMS:
59                 return "DEACTIVATED_BY_IMMS";
60             case StartInputReason.SESSION_CREATED_BY_IME:
61                 return "SESSION_CREATED_BY_IME";
62             default:
63                 return "Unknown=" + reason;
64         }
65     }
66 
67     /**
68      * Converts {@link UnbindReason} to {@link String} for debug logging.
69      *
70      * @param reason integer constant for {@link UnbindReason}.
71      * @return {@link String} message corresponds for the given {@code reason}.
72      */
unbindReasonToString(@nbindReason int reason)73     public static String unbindReasonToString(@UnbindReason int reason) {
74         switch (reason) {
75             case UnbindReason.UNSPECIFIED:
76                 return "UNSPECIFIED";
77             case UnbindReason.SWITCH_CLIENT:
78                 return "SWITCH_CLIENT";
79             case UnbindReason.SWITCH_IME:
80                 return "SWITCH_IME";
81             case UnbindReason.DISCONNECT_IME:
82                 return "DISCONNECT_IME";
83             case UnbindReason.NO_IME:
84                 return "NO_IME";
85             case UnbindReason.SWITCH_IME_FAILED:
86                 return "SWITCH_IME_FAILED";
87             case UnbindReason.SWITCH_USER:
88                 return "SWITCH_USER";
89             default:
90                 return "Unknown=" + reason;
91         }
92     }
93 
94     /**
95      * Converts {@link SoftInputModeFlags} to {@link String} for debug logging.
96      *
97      * @param softInputMode integer constant for {@link SoftInputModeFlags}.
98      * @return {@link String} message corresponds for the given {@code softInputMode}.
99      */
softInputModeToString(@oftInputModeFlags int softInputMode)100     public static String softInputModeToString(@SoftInputModeFlags int softInputMode) {
101         final StringJoiner joiner = new StringJoiner("|");
102         final int state = softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE;
103         final int adjust = softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST;
104         final boolean isForwardNav =
105                 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0;
106 
107         switch (state) {
108             case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
109                 joiner.add("STATE_UNSPECIFIED");
110                 break;
111             case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
112                 joiner.add("STATE_UNCHANGED");
113                 break;
114             case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
115                 joiner.add("STATE_HIDDEN");
116                 break;
117             case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
118                 joiner.add("STATE_ALWAYS_HIDDEN");
119                 break;
120             case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
121                 joiner.add("STATE_VISIBLE");
122                 break;
123             case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
124                 joiner.add("STATE_ALWAYS_VISIBLE");
125                 break;
126             default:
127                 joiner.add("STATE_UNKNOWN(" + state + ")");
128                 break;
129         }
130 
131         switch (adjust) {
132             case WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED:
133                 joiner.add("ADJUST_UNSPECIFIED");
134                 break;
135             case WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE:
136                 joiner.add("ADJUST_RESIZE");
137                 break;
138             case WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN:
139                 joiner.add("ADJUST_PAN");
140                 break;
141             case WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING:
142                 joiner.add("ADJUST_NOTHING");
143                 break;
144             default:
145                 joiner.add("ADJUST_UNKNOWN(" + adjust + ")");
146                 break;
147         }
148 
149         if (isForwardNav) {
150             // This is a special bit that is set by the system only during the window navigation.
151             joiner.add("IS_FORWARD_NAVIGATION");
152         }
153 
154         return joiner.setEmptyValue("(none)").toString();
155     }
156 
157     /**
158      * Converts {@link StartInputFlags} to {@link String} for debug logging.
159      *
160      * @param startInputFlags integer constant for {@link StartInputFlags}.
161      * @return {@link String} message corresponds for the given {@code startInputFlags}.
162      */
startInputFlagsToString(@tartInputFlags int startInputFlags)163     public static String startInputFlagsToString(@StartInputFlags int startInputFlags) {
164         final StringJoiner joiner = new StringJoiner("|");
165         if ((startInputFlags & StartInputFlags.VIEW_HAS_FOCUS) != 0) {
166             joiner.add("VIEW_HAS_FOCUS");
167         }
168         if ((startInputFlags & StartInputFlags.IS_TEXT_EDITOR) != 0) {
169             joiner.add("IS_TEXT_EDITOR");
170         }
171         if ((startInputFlags & StartInputFlags.FIRST_WINDOW_FOCUS_GAIN) != 0) {
172             joiner.add("FIRST_WINDOW_FOCUS_GAIN");
173         }
174         if ((startInputFlags & StartInputFlags.INITIAL_CONNECTION) != 0) {
175             joiner.add("INITIAL_CONNECTION");
176         }
177 
178         return joiner.setEmptyValue("(none)").toString();
179     }
180 }
181