OSDN Git Service

09ee50cbb71a1d656077338c4a5c8666af268fbe
[android-x86/packages-apps-Taskbar.git] / app / src / main / java / com / farmerbb / taskbar / service / NotificationService.java
1 /* Copyright 2016 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.service;
17
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;
36
37 import androidx.core.app.NotificationCompat;
38 import androidx.core.content.ContextCompat;
39 import androidx.localbroadcastmanager.content.LocalBroadcastManager;
40
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;
49
50 public class NotificationService extends Service {
51
52     private boolean isHidden = true;
53     private boolean desktopModeStarted = false;
54
55     @Override
56     public IBinder onBind(Intent intent) {
57         return null;
58     }
59
60     @Override
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));
66         }
67
68         return START_STICKY;
69     }
70
71     BroadcastReceiver userForegroundReceiver = new BroadcastReceiver() {
72         @Override
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));
77         }
78     };
79
80     BroadcastReceiver userBackgroundReceiver = new BroadcastReceiver() {
81         @Override
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));
86
87             IconCache.getInstance(context).clearCache();
88         }
89     };
90
91     DisplayManager.DisplayListener listener = new DisplayManager.DisplayListener() {
92         @Override
93         public void onDisplayAdded(int displayId) {
94             startDesktopMode(displayId, true);
95         }
96
97         @Override
98         public void onDisplayChanged(int displayId) {}
99
100         @Override
101         public void onDisplayRemoved(int displayId) {
102             stopDesktopMode();
103         }
104     };
105
106     @TargetApi(Build.VERSION_CODES.M)
107     @Override
108     public void onCreate() {
109         super.onCreate();
110
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);
115
116                 Intent intent = new Intent(this, MainActivity.class);
117                 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
118
119                 Intent receiverIntent = new Intent("com.farmerbb.taskbar.SHOW_HIDE_TASKBAR");
120                 receiverIntent.setPackage(getPackageName());
121
122                 Intent receiverIntent2 = new Intent("com.farmerbb.taskbar.QUIT");
123                 receiverIntent2.setPackage(getPackageName());
124
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);
128
129                 String id = "taskbar_notification_channel";
130
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;
134
135                     NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
136                     mNotificationManager.createNotificationChannel(new NotificationChannel(id, name, importance));
137                 }
138
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)
148                         .setShowWhen(false)
149                         .setOngoing(true);
150
151                 String showHideLabel;
152
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);
155
156                     Intent freeformIntent = new Intent("com.farmerbb.taskbar.TOGGLE_FREEFORM_MODE");
157                     freeformIntent.setPackage(getPackageName());
158
159                     PendingIntent freeformPendingIntent = PendingIntent.getBroadcast(this, 0, freeformIntent, PendingIntent.FLAG_UPDATE_CURRENT);
160
161                     mBuilder.addAction(0, freeformLabel, freeformPendingIntent);
162
163                     showHideLabel = getString(isHidden ? R.string.tb_action_show_alt : R.string.tb_action_hide_alt);
164                 } else
165                     showHideLabel = getString(isHidden ? R.string.tb_action_show : R.string.tb_action_hide);
166
167                 mBuilder.addAction(0, showHideLabel, receiverPendingIntent)
168                         .addAction(0, getString(R.string.tb_action_quit), receiverPendingIntent2);
169
170                 startForeground(8675309, mBuilder.build());
171
172                 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.UPDATE_SWITCH"));
173
174                 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
175                     TileService.requestListeningState(this, new ComponentName(getPackageName(), QuickSettingsTileService.class.getName()));
176
177                 DependencyUtils.requestTaskerQuery(this);
178
179                 if(!isHidden) {
180                     registerReceiver(userForegroundReceiver, new IntentFilter(Intent.ACTION_USER_FOREGROUND));
181                     registerReceiver(userBackgroundReceiver, new IntentFilter(Intent.ACTION_USER_BACKGROUND));
182                 }
183
184                 if(U.shouldStartDesktopMode(this)) {
185                     startDesktopMode(U.getExternalDisplayID(this), false);
186
187                     DisplayManager manager = (DisplayManager) getSystemService(DISPLAY_SERVICE);
188                     manager.registerDisplayListener(listener, null);
189                 }
190             } else {
191                 pref.edit().putBoolean("taskbar_active", false).apply();
192
193                 stopSelf();
194             }
195         } else stopSelf();
196     }
197
198     @Override
199     public void onDestroy() {
200         SharedPreferences pref = U.getSharedPreferences(this);
201         if(pref.getBoolean("is_restarting", false))
202             pref.edit().remove("is_restarting").apply();
203         else {
204             LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.UPDATE_SWITCH"));
205
206             if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
207                 TileService.requestListeningState(this, new ComponentName(getPackageName(), QuickSettingsTileService.class.getName()));
208
209             DependencyUtils.requestTaskerQuery(this);
210
211             if(!U.launcherIsDefault(this) || U.isChromeOs(this))
212                 U.stopFreeformHack(this);
213         }
214
215         super.onDestroy();
216
217         if(!isHidden) {
218             unregisterReceiver(userForegroundReceiver);
219             unregisterReceiver(userBackgroundReceiver);
220         }
221
222         if(desktopModeStarted) {
223             DisplayManager manager = (DisplayManager) getSystemService(DISPLAY_SERVICE);
224             manager.unregisterDisplayListener(listener);
225
226             stopDesktopMode();
227         }
228     }
229
230     private void startDesktopMode(int displayId, boolean shouldDelay) {
231         LauncherHelper helper = LauncherHelper.getInstance();
232         if(displayId == Display.DEFAULT_DISPLAY || helper.isOnSecondaryHomeScreen()) return;
233
234         Runnable desktopModeLaunch = () -> {
235             helper.setOnSecondaryHomeScreen(true, displayId);
236
237             Intent intent = new Intent(this, SecondaryHomeActivity.class);
238             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
239
240             startActivity(intent, U.getActivityOptions(this, ApplicationType.GAME, null).toBundle());
241         };
242
243         if(shouldDelay)
244             new Handler().postDelayed(desktopModeLaunch, 500);
245         else
246             desktopModeLaunch.run();
247
248         desktopModeStarted = true;
249     }
250
251     private void stopDesktopMode() {
252         LocalBroadcastManager.getInstance(NotificationService.this).sendBroadcast(new Intent("com.farmerbb.taskbar.KILL_HOME_ACTIVITY"));
253         desktopModeStarted = false;
254     }
255 }