OSDN Git Service

Move isImeVisible() to CompatUtils
[android-x86/packages-apps-Taskbar.git] / app / src / main / java / com / farmerbb / taskbar / ui / UIController.java
1 /* Copyright 2019 Braden Farmer
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 package com.farmerbb.taskbar.ui;
17
18 import android.content.Context;
19 import android.content.SharedPreferences;
20 import android.view.View;
21
22 import com.farmerbb.taskbar.activity.SecondaryHomeActivity;
23 import com.farmerbb.taskbar.helper.LauncherHelper;
24 import com.farmerbb.taskbar.util.CompatUtils;
25 import com.farmerbb.taskbar.util.TaskbarPosition;
26 import com.farmerbb.taskbar.util.U;
27
28 import static com.farmerbb.taskbar.util.Constants.*;
29
30 public abstract class UIController {
31     protected Context context;
32     private boolean prevImeVisibility;
33
34     public UIController(Context context) {
35         this.context = context;
36     }
37
38     abstract void onCreateHost(UIHost host);
39     abstract void onRecreateHost(UIHost host);
40     abstract void onDestroyHost(UIHost host);
41
42     protected void init(Context context, UIHost host, Runnable runnable) {
43         SharedPreferences pref = U.getSharedPreferences(context);
44         LauncherHelper helper = LauncherHelper.getInstance();
45
46         boolean shouldProceed;
47         if(helper.isOnSecondaryHomeScreen(context))
48             shouldProceed = host instanceof SecondaryHomeActivity;
49         else
50             shouldProceed = true;
51
52         if(shouldProceed && (pref.getBoolean(PREF_TASKBAR_ACTIVE, false)
53                 || helper.isOnHomeScreen(context))) {
54             if(U.canDrawOverlays(context))
55                 runnable.run();
56             else {
57                 pref.edit().putBoolean(PREF_TASKBAR_ACTIVE, false).apply();
58                 host.terminate();
59             }
60         } else
61             host.terminate();
62     }
63
64     protected int getBottomMargin(Context context, UIHost host) {
65         return host instanceof SecondaryHomeActivity
66                 && !U.isShowHideNavbarSupported()
67                 && TaskbarPosition.isBottom(context) ? U.getNavbarHeight(context) : -1;
68     }
69
70     protected void applyMarginFix(UIHost host, View layout, ViewParams params) {
71         if(U.getCurrentApiVersion() <= 29.0) return;
72
73         layout.setOnApplyWindowInsetsListener((v, insets) -> {
74             boolean isImeVisible = CompatUtils.isImeVisible(v);
75             if(isImeVisible != prevImeVisibility) {
76                 prevImeVisibility = isImeVisible;
77
78                 ViewParams newParams = isImeVisible ? params.noBottomMargin() : params;
79                 host.updateViewLayout(layout, newParams);
80             }
81
82             return insets;
83         });
84     }
85 }