OSDN Git Service

Expose IME fix in desktop mode settings
[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.provider.Settings;
21 import android.view.View;
22
23 import com.farmerbb.taskbar.BuildConfig;
24 import com.farmerbb.taskbar.R;
25 import com.farmerbb.taskbar.activity.SecondaryHomeActivity;
26 import com.farmerbb.taskbar.helper.LauncherHelper;
27 import com.farmerbb.taskbar.util.CompatUtils;
28 import com.farmerbb.taskbar.util.TaskbarPosition;
29 import com.farmerbb.taskbar.util.U;
30
31 import static com.farmerbb.taskbar.util.Constants.*;
32
33 public abstract class UIController {
34     protected Context context;
35     private boolean prevImeVisibility;
36
37     public UIController(Context context) {
38         this.context = context;
39     }
40
41     abstract void onCreateHost(UIHost host);
42     abstract void onRecreateHost(UIHost host);
43     abstract void onDestroyHost(UIHost host);
44
45     protected void init(Context context, UIHost host, Runnable runnable) {
46         SharedPreferences pref = U.getSharedPreferences(context);
47         LauncherHelper helper = LauncherHelper.getInstance();
48
49         boolean shouldProceed;
50         if(helper.isOnSecondaryHomeScreen(context))
51             shouldProceed = host instanceof SecondaryHomeActivity;
52         else
53             shouldProceed = true;
54
55         if(shouldProceed && (pref.getBoolean(PREF_TASKBAR_ACTIVE, false)
56                 || helper.isOnHomeScreen(context))) {
57             if(U.canDrawOverlays(context))
58                 runnable.run();
59             else {
60                 pref.edit().putBoolean(PREF_TASKBAR_ACTIVE, false).apply();
61                 host.terminate();
62             }
63         } else
64             host.terminate();
65     }
66
67     protected int getBottomMargin(Context context, UIHost host) {
68         return host instanceof SecondaryHomeActivity
69                 && !U.isShowHideNavbarSupported()
70                 && TaskbarPosition.isBottom(context) ? U.getNavbarHeight(context) : -1;
71     }
72
73     protected void applyMarginFix(UIHost host, View layout, ViewParams params) {
74         if(U.getCurrentApiVersion() <= 29.0 || !(host instanceof SecondaryHomeActivity))
75             return;
76
77         layout.setOnApplyWindowInsetsListener((v, insets) -> {
78             boolean isImeVisible = CompatUtils.isImeVisible(v);
79             if(isImeVisible != prevImeVisibility) {
80                 prevImeVisibility = isImeVisible;
81
82                 ViewParams newParams = isImeVisible ? params.updateBottomMargin(0) : params;
83                 host.updateViewLayout(layout, newParams);
84
85                 SharedPreferences pref = U.getSharedPreferences(context);
86                 if(isImeFixDisabled() && !pref.getBoolean(PREF_DESKTOP_MODE_IME_FIX, false)) {
87                     pref.edit().putBoolean(PREF_DESKTOP_MODE_IME_FIX, true).apply();
88                     U.showToastLong(context, R.string.tb_desktop_mode_ime_fix_toast);
89                 }
90             }
91
92             return insets;
93         });
94     }
95
96     protected boolean isImeFixDisabled() {
97         String ime = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
98         return !ime.startsWith(BuildConfig.BASE_APPLICATION_ID) && !ime.startsWith("com.farmerbb.secondscreen");
99     }
100 }