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.os.Build;
31 import android.os.IBinder;
32 import android.service.quicksettings.TileService;
33 import android.support.v4.app.NotificationCompat;
34 import android.support.v4.content.ContextCompat;
35 import android.support.v4.content.LocalBroadcastManager;
37 import com.farmerbb.taskbar.BuildConfig;
38 import com.farmerbb.taskbar.activity.MainActivity;
39 import com.farmerbb.taskbar.R;
40 import com.farmerbb.taskbar.util.DependencyUtils;
41 import com.farmerbb.taskbar.util.IconCache;
42 import com.farmerbb.taskbar.util.U;
44 public class NotificationService extends Service {
46 private boolean isHidden = true;
49 public IBinder onBind(Intent intent) {
54 public int onStartCommand(Intent intent, int flags, int startId) {
55 if(intent != null && intent.getBooleanExtra("start_services", false)) {
56 startService(new Intent(this, TaskbarService.class));
57 startService(new Intent(this, StartMenuService.class));
58 startService(new Intent(this, DashboardService.class));
64 BroadcastReceiver userForegroundReceiver = new BroadcastReceiver() {
66 public void onReceive(Context context, Intent intent) {
67 startService(new Intent(context, TaskbarService.class));
68 startService(new Intent(context, StartMenuService.class));
69 startService(new Intent(context, DashboardService.class));
73 BroadcastReceiver userBackgroundReceiver = new BroadcastReceiver() {
75 public void onReceive(Context context, Intent intent) {
76 stopService(new Intent(context, TaskbarService.class));
77 stopService(new Intent(context, StartMenuService.class));
78 stopService(new Intent(context, DashboardService.class));
80 IconCache.getInstance(context).clearCache();
84 @TargetApi(Build.VERSION_CODES.M)
86 public void onCreate() {
89 SharedPreferences pref = U.getSharedPreferences(this);
90 if(pref.getBoolean("taskbar_active", false)) {
91 if(U.canDrawOverlays(this)) {
92 isHidden = U.getSharedPreferences(this).getBoolean("is_hidden", false);
94 Intent intent = new Intent(this, MainActivity.class);
95 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
97 Intent receiverIntent = new Intent("com.farmerbb.taskbar.SHOW_HIDE_TASKBAR");
98 receiverIntent.setPackage(BuildConfig.APPLICATION_ID);
100 Intent receiverIntent2 = new Intent("com.farmerbb.taskbar.QUIT");
101 receiverIntent2.setPackage(BuildConfig.APPLICATION_ID);
103 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
104 PendingIntent receiverPendingIntent = PendingIntent.getBroadcast(this, 0, receiverIntent, PendingIntent.FLAG_UPDATE_CURRENT);
105 PendingIntent receiverPendingIntent2 = PendingIntent.getBroadcast(this, 0, receiverIntent2, PendingIntent.FLAG_UPDATE_CURRENT);
107 String id = "taskbar_notification_channel";
109 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
110 CharSequence name = getString(R.string.app_name);
111 int importance = NotificationManager.IMPORTANCE_MIN;
113 NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
114 mNotificationManager.createNotificationChannel(new NotificationChannel(id, name, importance));
117 NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, id)
118 .setSmallIcon(pref.getString("start_button_image", U.getDefaultStartButtonImage(this)).equals("app_logo")
119 ? R.drawable.ic_system
120 : R.drawable.ic_allapps)
121 .setContentIntent(contentIntent)
122 .setContentTitle(getString(R.string.taskbar_is_active))
123 .setContentText(getString(R.string.click_to_open_settings))
124 .setColor(ContextCompat.getColor(this, R.color.colorPrimary))
125 .setPriority(Notification.PRIORITY_MIN)
129 String showHideLabel;
131 if(U.canEnableFreeform() && !U.isChromeOs(this)) {
132 String freeformLabel = getString(pref.getBoolean("freeform_hack", false) ? R.string.freeform_off : R.string.freeform_on);
134 Intent freeformIntent = new Intent("com.farmerbb.taskbar.TOGGLE_FREEFORM_MODE");
135 freeformIntent.setPackage(BuildConfig.APPLICATION_ID);
137 PendingIntent freeformPendingIntent = PendingIntent.getBroadcast(this, 0, freeformIntent, PendingIntent.FLAG_UPDATE_CURRENT);
139 mBuilder.addAction(0, freeformLabel, freeformPendingIntent);
141 showHideLabel = getString(isHidden ? R.string.action_show_alt : R.string.action_hide_alt);
143 showHideLabel = getString(isHidden ? R.string.action_show : R.string.action_hide);
145 mBuilder.addAction(0, showHideLabel, receiverPendingIntent)
146 .addAction(0, getString(R.string.action_quit), receiverPendingIntent2);
148 startForeground(8675309, mBuilder.build());
150 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.UPDATE_SWITCH"));
152 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
153 TileService.requestListeningState(this, new ComponentName(BuildConfig.APPLICATION_ID, QuickSettingsTileService.class.getName()));
155 DependencyUtils.requestTaskerQuery(this);
158 registerReceiver(userForegroundReceiver, new IntentFilter(Intent.ACTION_USER_FOREGROUND));
159 registerReceiver(userBackgroundReceiver, new IntentFilter(Intent.ACTION_USER_BACKGROUND));
162 pref.edit().putBoolean("taskbar_active", false).apply();
170 public void onDestroy() {
171 SharedPreferences pref = U.getSharedPreferences(this);
172 if(pref.getBoolean("is_restarting", false))
173 pref.edit().remove("is_restarting").apply();
175 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.UPDATE_SWITCH"));
177 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
178 TileService.requestListeningState(this, new ComponentName(BuildConfig.APPLICATION_ID, QuickSettingsTileService.class.getName()));
180 DependencyUtils.requestTaskerQuery(this);
182 if(!U.launcherIsDefault(this) || U.isChromeOs(this))
183 U.stopFreeformHack(this);
189 unregisterReceiver(userForegroundReceiver);
190 unregisterReceiver(userBackgroundReceiver);