• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.view;
18 
19 import android.annotation.IntDef;
20 import android.view.WindowManager.LayoutParams;
21 import android.view.WindowManager.LayoutParams.SoftInputModeFlags;
22 
23 import java.lang.annotation.Retention;
24 
25 import static java.lang.annotation.RetentionPolicy.SOURCE;
26 
27 public final class InputMethodClient {
28     public static final int START_INPUT_REASON_UNSPECIFIED = 0;
29     public static final int START_INPUT_REASON_WINDOW_FOCUS_GAIN = 1;
30     public static final int START_INPUT_REASON_WINDOW_FOCUS_GAIN_REPORT_ONLY = 2;
31     public static final int START_INPUT_REASON_APP_CALLED_RESTART_INPUT_API = 3;
32     public static final int START_INPUT_REASON_CHECK_FOCUS = 4;
33     public static final int START_INPUT_REASON_BOUND_TO_IMMS = 5;
34     public static final int START_INPUT_REASON_UNBOUND_FROM_IMMS = 6;
35     public static final int START_INPUT_REASON_ACTIVATED_BY_IMMS = 7;
36     public static final int START_INPUT_REASON_DEACTIVATED_BY_IMMS = 8;
37     public static final int START_INPUT_REASON_SESSION_CREATED_BY_IME = 9;
38 
39     @Retention(SOURCE)
40     @IntDef({START_INPUT_REASON_UNSPECIFIED, START_INPUT_REASON_WINDOW_FOCUS_GAIN,
41             START_INPUT_REASON_WINDOW_FOCUS_GAIN_REPORT_ONLY,
42             START_INPUT_REASON_APP_CALLED_RESTART_INPUT_API, START_INPUT_REASON_CHECK_FOCUS,
43             START_INPUT_REASON_BOUND_TO_IMMS, START_INPUT_REASON_ACTIVATED_BY_IMMS,
44             START_INPUT_REASON_DEACTIVATED_BY_IMMS, START_INPUT_REASON_SESSION_CREATED_BY_IME})
45     public @interface StartInputReason {}
46 
getStartInputReason(@tartInputReason final int reason)47     public static String getStartInputReason(@StartInputReason final int reason) {
48         switch (reason) {
49             case START_INPUT_REASON_UNSPECIFIED:
50                 return "UNSPECIFIED";
51             case START_INPUT_REASON_WINDOW_FOCUS_GAIN:
52                 return "WINDOW_FOCUS_GAIN";
53             case START_INPUT_REASON_WINDOW_FOCUS_GAIN_REPORT_ONLY:
54                 return "WINDOW_FOCUS_GAIN_REPORT_ONLY";
55             case START_INPUT_REASON_APP_CALLED_RESTART_INPUT_API:
56                 return "APP_CALLED_RESTART_INPUT_API";
57             case START_INPUT_REASON_CHECK_FOCUS:
58                 return "CHECK_FOCUS";
59             case START_INPUT_REASON_BOUND_TO_IMMS:
60                 return "BOUND_TO_IMMS";
61             case START_INPUT_REASON_UNBOUND_FROM_IMMS:
62                 return "UNBOUND_FROM_IMMS";
63             case START_INPUT_REASON_ACTIVATED_BY_IMMS:
64                 return "ACTIVATED_BY_IMMS";
65             case START_INPUT_REASON_DEACTIVATED_BY_IMMS:
66                 return "DEACTIVATED_BY_IMMS";
67             case START_INPUT_REASON_SESSION_CREATED_BY_IME:
68                 return "SESSION_CREATED_BY_IME";
69             default:
70                 return "Unknown=" + reason;
71         }
72     }
73 
74     public static final int UNBIND_REASON_UNSPECIFIED = 0;
75     public static final int UNBIND_REASON_SWITCH_CLIENT = 1;
76     public static final int UNBIND_REASON_SWITCH_IME = 2;
77     public static final int UNBIND_REASON_DISCONNECT_IME = 3;
78     public static final int UNBIND_REASON_NO_IME = 4;
79     public static final int UNBIND_REASON_SWITCH_IME_FAILED = 5;
80     public static final int UNBIND_REASON_SWITCH_USER = 6;
81 
82     @Retention(SOURCE)
83     @IntDef({UNBIND_REASON_UNSPECIFIED, UNBIND_REASON_SWITCH_CLIENT, UNBIND_REASON_SWITCH_IME,
84             UNBIND_REASON_DISCONNECT_IME, UNBIND_REASON_NO_IME, UNBIND_REASON_SWITCH_IME_FAILED,
85             UNBIND_REASON_SWITCH_USER})
86     public @interface UnbindReason {}
87 
getUnbindReason(@nbindReason final int reason)88     public static String getUnbindReason(@UnbindReason final int reason) {
89         switch (reason) {
90             case UNBIND_REASON_UNSPECIFIED:
91                 return "UNSPECIFIED";
92             case UNBIND_REASON_SWITCH_CLIENT:
93                 return "SWITCH_CLIENT";
94             case UNBIND_REASON_SWITCH_IME:
95                 return "SWITCH_IME";
96             case UNBIND_REASON_DISCONNECT_IME:
97                 return "DISCONNECT_IME";
98             case UNBIND_REASON_NO_IME:
99                 return "NO_IME";
100             case UNBIND_REASON_SWITCH_IME_FAILED:
101                 return "SWITCH_IME_FAILED";
102             case UNBIND_REASON_SWITCH_USER:
103                 return "SWITCH_USER";
104             default:
105                 return "Unknown=" + reason;
106         }
107     }
108 
softInputModeToString(@oftInputModeFlags final int softInputMode)109     public static String softInputModeToString(@SoftInputModeFlags final int softInputMode) {
110         final StringBuilder sb = new StringBuilder();
111         final int state = softInputMode & LayoutParams.SOFT_INPUT_MASK_STATE;
112         final int adjust = softInputMode & LayoutParams.SOFT_INPUT_MASK_ADJUST;
113         final boolean isForwardNav =
114                 (softInputMode & LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0;
115 
116         switch (state) {
117             case LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
118                 sb.append("STATE_UNSPECIFIED");
119                 break;
120             case LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
121                 sb.append("STATE_UNCHANGED");
122                 break;
123             case LayoutParams.SOFT_INPUT_STATE_HIDDEN:
124                 sb.append("STATE_HIDDEN");
125                 break;
126             case LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
127                 sb.append("STATE_ALWAYS_HIDDEN");
128                 break;
129             case LayoutParams.SOFT_INPUT_STATE_VISIBLE:
130                 sb.append("STATE_VISIBLE");
131                 break;
132             case LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
133                 sb.append("STATE_ALWAYS_VISIBLE");
134                 break;
135             default:
136                 sb.append("STATE_UNKNOWN(");
137                 sb.append(state);
138                 sb.append(")");
139                 break;
140         }
141 
142         switch (adjust) {
143             case LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED:
144                 sb.append("|ADJUST_UNSPECIFIED");
145                 break;
146             case LayoutParams.SOFT_INPUT_ADJUST_RESIZE:
147                 sb.append("|ADJUST_RESIZE");
148                 break;
149             case LayoutParams.SOFT_INPUT_ADJUST_PAN:
150                 sb.append("|ADJUST_PAN");
151                 break;
152             case LayoutParams.SOFT_INPUT_ADJUST_NOTHING:
153                 sb.append("|ADJUST_NOTHING");
154                 break;
155             default:
156                 sb.append("|ADJUST_UNKNOWN(");
157                 sb.append(adjust);
158                 sb.append(")");
159                 break;
160         }
161 
162         if (isForwardNav) {
163             // This is a special bit that is set by the system only during the window navigation.
164             sb.append("|IS_FORWARD_NAVIGATION");
165         }
166 
167         return sb.toString();
168     }
169 }
170