OSDN Git Service

Merge TaskbarIntent and SharedPreferenceConstant into single Constants class
[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
21 import com.farmerbb.taskbar.activity.SecondaryHomeActivity;
22 import com.farmerbb.taskbar.util.LauncherHelper;
23 import com.farmerbb.taskbar.util.U;
24
25 import static com.farmerbb.taskbar.util.Constants.PREF_TASKBAR_ACTIVE;
26
27 public abstract class UIController {
28     protected Context context;
29
30     public UIController(Context context) {
31         this.context = context;
32     }
33
34     abstract void onCreateHost(UIHost host);
35     abstract void onRecreateHost(UIHost host);
36     abstract void onDestroyHost(UIHost host);
37
38     protected void init(Context context, UIHost host, Runnable runnable) {
39         SharedPreferences pref = U.getSharedPreferences(context);
40         LauncherHelper helper = LauncherHelper.getInstance();
41
42         boolean shouldProceed;
43         if(helper.isOnSecondaryHomeScreen())
44             shouldProceed = host instanceof SecondaryHomeActivity;
45         else
46             shouldProceed = true;
47
48         if(shouldProceed && (pref.getBoolean(PREF_TASKBAR_ACTIVE, false)
49                 || helper.isOnHomeScreen())) {
50             if(U.canDrawOverlays(context))
51                 runnable.run();
52             else {
53                 pref.edit().putBoolean(PREF_TASKBAR_ACTIVE, false).apply();
54                 host.terminate();
55             }
56         } else
57             host.terminate();
58     }
59 }