1 /* Copyright 2016 Braden Farmer
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.farmerbb.taskbar.service;
18 import android.annotation.TargetApi;
19 import android.app.Notification;
20 import android.app.NotificationChannel;
21 import android.app.NotificationManager;
22 import android.app.PendingIntent;
23 import android.app.Service;
24 import android.content.BroadcastReceiver;
25 import android.content.ComponentName;
26 import android.content.Context;
27 import android.content.Intent;
28 import android.content.IntentFilter;
29 import android.content.SharedPreferences;
30 import android.hardware.display.DisplayManager;
31 import android.os.Build;
32 import android.os.Handler;
33 import android.os.IBinder;
34 import android.service.quicksettings.TileService;
35 import android.view.Display;
37 import androidx.core.app.NotificationCompat;
38 import androidx.core.content.ContextCompat;
39 import androidx.localbroadcastmanager.content.LocalBroadcastManager;
41 import com.farmerbb.taskbar.activity.MainActivity;
42 import com.farmerbb.taskbar.activity.SecondaryHomeActivity;
43 import com.farmerbb.taskbar.R;
44 import com.farmerbb.taskbar.util.ApplicationType;
45 import com.farmerbb.taskbar.util.DependencyUtils;
46 import com.farmerbb.taskbar.util.IconCache;
47 import com.farmerbb.taskbar.util.LauncherHelper;
48 import com.farmerbb.taskbar.util.U;
50 public class NotificationService extends Service {
52 private boolean isHidden = true;
53 private boolean desktopModeStarted = false;
56 public IBinder onBind(Intent intent) {
61 public int onStartCommand(Intent intent, int flags, int startId) {
62 if(intent != null && intent.getBooleanExtra("start_services", false)) {
63 startService(new Intent(this, TaskbarService.class));
64 startService(new Intent(this, StartMenuService.class));
65 startService(new Intent(this, DashboardService.class));
71 BroadcastReceiver userForegroundReceiver = new BroadcastReceiver() {
73 public void onReceive(Context context, Intent intent) {
74 startService(new Intent(context, TaskbarService.class));
75 startService(new Intent(context, StartMenuService.class));
76 startService(new Intent(context, DashboardService.class));
80 BroadcastReceiver userBackgroundReceiver = new BroadcastReceiver() {
82 public void onReceive(Context context, Intent intent) {
83 stopService(new Intent(context, TaskbarService.class));
84 stopService(new Intent(context, StartMenuService.class));
85 stopService(new Intent(context, DashboardService.class));
87 IconCache.getInstance(context).clearCache();
91 DisplayManager.DisplayListener listener = new DisplayManager.DisplayListener() {
93 public void onDisplayAdded(int displayId) {
94 startDesktopMode(displayId, true);
98 public void onDisplayChanged(int displayId) {}
101 public void onDisplayRemoved(int displayId) {
106 @TargetApi(Build.VERSION_CODES.M)
108 public void onCreate() {
111 SharedPreferences pref = U.getSharedPreferences(this);
112 if(pref.getBoolean("taskbar_active", false)) {
113 if(U.canDrawOverlays(this)) {
114 isHidden = U.getSharedPreferences(this).getBoolean("is_hidden", false);
116 Intent intent = new Intent(this, MainActivity.class);
117 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
119 Intent receiverIntent = new Intent("com.farmerbb.taskbar.SHOW_HIDE_TASKBAR");
120 receiverIntent.setPackage(getPackageName());
122 Intent receiverIntent2 = new Intent("com.farmerbb.taskbar.QUIT");
123 receiverIntent2.setPackage(getPackageName());
125 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
126 PendingIntent receiverPendingIntent = PendingIntent.getBroadcast(this, 0, receiverIntent, PendingIntent.FLAG_UPDATE_CURRENT);
127 PendingIntent receiverPendingIntent2 = PendingIntent.getBroadcast(this, 0, receiverIntent2, PendingIntent.FLAG_UPDATE_CURRENT);
129 String id = "taskbar_notification_channel";
131 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
132 CharSequence name = getString(R.string.tb_app_name);
133 int importance = NotificationManager.IMPORTANCE_MIN;
135 NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
136 mNotificationManager.createNotificationChannel(new NotificationChannel(id, name, importance));
139 NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, id)
140 .setSmallIcon(pref.getString("start_button_image", U.getDefaultStartButtonImage(this)).equals("app_logo")
141 ? R.drawable.tb_system
142 : R.drawable.tb_allapps)
143 .setContentIntent(contentIntent)
144 .setContentTitle(getString(R.string.tb_taskbar_is_active))
145 .setContentText(getString(R.string.tb_click_to_open_settings))
146 .setColor(ContextCompat.getColor(this, R.color.tb_colorPrimary))
147 .setPriority(Notification.PRIORITY_MIN)
151 String showHideLabel;
153 if(U.canEnableFreeform() && !U.isChromeOs(this)) {
154 String freeformLabel = getString(pref.getBoolean("freeform_hack", false) ? R.string.tb_freeform_off : R.string.tb_freeform_on);
156 Intent freeformIntent = new Intent("com.farmerbb.taskbar.TOGGLE_FREEFORM_MODE");
157 freeformIntent.setPackage(getPackageName());
159 PendingIntent freeformPendingIntent = PendingIntent.getBroadcast(this, 0, freeformIntent, PendingIntent.FLAG_UPDATE_CURRENT);
161 mBuilder.addAction(0, freeformLabel, freeformPendingIntent);
163 showHideLabel = getString(isHidden ? R.string.tb_action_show_alt : R.string.tb_action_hide_alt);
165 showHideLabel = getString(isHidden ? R.string.tb_action_show : R.string.tb_action_hide);
167 mBuilder.addAction(0, showHideLabel, receiverPendingIntent)
168 .addAction(0, getString(R.string.tb_action_quit), receiverPendingIntent2);
170 startForeground(8675309, mBuilder.build());
172 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.UPDATE_SWITCH"));
174 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
175 TileService.requestListeningState(this, new ComponentName(getPackageName(), QuickSettingsTileService.class.getName()));
177 DependencyUtils.requestTaskerQuery(this);
180 registerReceiver(userForegroundReceiver, new IntentFilter(Intent.ACTION_USER_FOREGROUND));
181 registerReceiver(userBackgroundReceiver, new IntentFilter(Intent.ACTION_USER_BACKGROUND));
184 if(U.shouldStartDesktopMode(this)) {
185 startDesktopMode(U.getExternalDisplayID(this), false);
187 DisplayManager manager = (DisplayManager) getSystemService(DISPLAY_SERVICE);
188 manager.registerDisplayListener(listener, null);
191 pref.edit().putBoolean("taskbar_active", false).apply();
199 public void onDestroy() {
200 SharedPreferences pref = U.getSharedPreferences(this);
201 if(pref.getBoolean("is_restarting", false))
202 pref.edit().remove("is_restarting").apply();
204 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.UPDATE_SWITCH"));
206 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
207 TileService.requestListeningState(this, new ComponentName(getPackageName(), QuickSettingsTileService.class.getName()));
209 DependencyUtils.requestTaskerQuery(this);
211 if(!U.launcherIsDefault(this) || U.isChromeOs(this))
212 U.stopFreeformHack(this);
218 unregisterReceiver(userForegroundReceiver);
219 unregisterReceiver(userBackgroundReceiver);
222 if(desktopModeStarted) {
223 DisplayManager manager = (DisplayManager) getSystemService(DISPLAY_SERVICE);
224 manager.unregisterDisplayListener(listener);
230 private void startDesktopMode(int displayId, boolean shouldDelay) {
231 LauncherHelper helper = LauncherHelper.getInstance();
232 if(displayId == Display.DEFAULT_DISPLAY || helper.isOnSecondaryHomeScreen()) return;
234 Runnable desktopModeLaunch = () -> {
235 helper.setOnSecondaryHomeScreen(true, displayId);
237 Intent intent = new Intent(this, SecondaryHomeActivity.class);
238 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
240 startActivity(intent, U.getActivityOptions(this, ApplicationType.GAME, null).toBundle());
244 new Handler().postDelayed(desktopModeLaunch, 500);
246 desktopModeLaunch.run();
248 desktopModeStarted = true;
251 private void stopDesktopMode() {
252 LocalBroadcastManager.getInstance(NotificationService.this).sendBroadcast(new Intent("com.farmerbb.taskbar.KILL_HOME_ACTIVITY"));
253 desktopModeStarted = false;