1 /*
2  * Copyright (C) 2017 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 package com.android.launcher3.util;
17 
18 import static com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR;
19 
20 import android.app.Activity;
21 import android.content.Context;
22 import android.os.Handler;
23 import android.os.IBinder;
24 import android.os.Message;
25 import android.view.inputmethod.InputMethodManager;
26 
27 /**
28  * Utility class for offloading some class from UI thread
29  */
30 public class UiThreadHelper {
31 
32     private static Handler sHandler;
33 
34     private static final int MSG_HIDE_KEYBOARD = 1;
35     private static final int MSG_SET_ORIENTATION = 2;
36     private static final int MSG_RUN_COMMAND = 3;
37 
getHandler(Context context)38     private static Handler getHandler(Context context) {
39         if (sHandler == null) {
40             sHandler = new Handler(UI_HELPER_EXECUTOR.getLooper(),
41                     new UiCallbacks(context.getApplicationContext()));
42         }
43         return sHandler;
44     }
45 
hideKeyboardAsync(Context context, IBinder token)46     public static void hideKeyboardAsync(Context context, IBinder token) {
47         Message.obtain(getHandler(context), MSG_HIDE_KEYBOARD, token).sendToTarget();
48     }
49 
setOrientationAsync(Activity activity, int orientation)50     public static void setOrientationAsync(Activity activity, int orientation) {
51         Message.obtain(getHandler(activity), MSG_SET_ORIENTATION, orientation, 0, activity)
52                 .sendToTarget();
53     }
54 
runAsyncCommand(Context context, AsyncCommand command, int arg1, int arg2)55     public static void runAsyncCommand(Context context, AsyncCommand command, int arg1, int arg2) {
56         Message.obtain(getHandler(context), MSG_RUN_COMMAND, arg1, arg2, command).sendToTarget();
57     }
58 
59     private static class UiCallbacks implements Handler.Callback {
60 
61         private final InputMethodManager mIMM;
62 
UiCallbacks(Context context)63         UiCallbacks(Context context) {
64             mIMM = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
65         }
66 
67         @Override
handleMessage(Message message)68         public boolean handleMessage(Message message) {
69             switch (message.what) {
70                 case MSG_HIDE_KEYBOARD:
71                     mIMM.hideSoftInputFromWindow((IBinder) message.obj, 0);
72                     return true;
73                 case MSG_SET_ORIENTATION:
74                     ((Activity) message.obj).setRequestedOrientation(message.arg1);
75                     return true;
76                 case MSG_RUN_COMMAND:
77                     ((AsyncCommand) message.obj).execute(message.arg1, message.arg2);
78                     return true;
79             }
80             return false;
81         }
82     }
83 
84     public interface AsyncCommand {
85 
execute(int arg1, int arg2)86         void execute(int arg1, int arg2);
87     }
88 }
89