OSDN Git Service

746aafb7953f3d7fbc7319a351a3f3bc9cc2637b
[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.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;
36
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;
43
44 public class NotificationService extends Service {
45
46     private boolean isHidden = true;
47
48     @Override
49     public IBinder onBind(Intent intent) {
50         return null;
51     }
52
53     @Override
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));
59         }
60
61         return START_STICKY;
62     }
63
64     BroadcastReceiver userForegroundReceiver = new BroadcastReceiver() {
65         @Override
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));
70         }
71     };
72
73     BroadcastReceiver userBackgroundReceiver = new BroadcastReceiver() {
74         @Override
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));
79
80             IconCache.getInstance(context).clearCache();
81         }
82     };
83
84     @TargetApi(Build.VERSION_CODES.M)
85     @Override
86     public void onCreate() {
87         super.onCreate();
88
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);
93
94                 Intent intent = new Intent(this, MainActivity.class);
95                 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
96
97                 Intent receiverIntent = new Intent("com.farmerbb.taskbar.SHOW_HIDE_TASKBAR");
98                 receiverIntent.setPackage(BuildConfig.APPLICATION_ID);
99
100                 Intent receiverIntent2 = new Intent("com.farmerbb.taskbar.QUIT");
101                 receiverIntent2.setPackage(BuildConfig.APPLICATION_ID);
102
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);
106
107                 String id = "taskbar_notification_channel";
108
109                 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
110                     CharSequence name = getString(R.string.app_name);
111                     int importance = NotificationManager.IMPORTANCE_MIN;
112
113                     NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
114                     mNotificationManager.createNotificationChannel(new NotificationChannel(id, name, importance));
115                 }
116
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)
126                         .setShowWhen(false)
127                         .setOngoing(true);
128
129                 String showHideLabel;
130
131                 if(U.canEnableFreeform() && !U.isChromeOs(this)) {
132                     String freeformLabel = getString(pref.getBoolean("freeform_hack", false) ? R.string.freeform_off : R.string.freeform_on);
133
134                     Intent freeformIntent = new Intent("com.farmerbb.taskbar.TOGGLE_FREEFORM_MODE");
135                     freeformIntent.setPackage(BuildConfig.APPLICATION_ID);
136
137                     PendingIntent freeformPendingIntent = PendingIntent.getBroadcast(this, 0, freeformIntent, PendingIntent.FLAG_UPDATE_CURRENT);
138
139                     mBuilder.addAction(0, freeformLabel, freeformPendingIntent);
140
141                     showHideLabel = getString(isHidden ? R.string.action_show_alt : R.string.action_hide_alt);
142                 } else
143                     showHideLabel = getString(isHidden ? R.string.action_show : R.string.action_hide);
144
145                 mBuilder.addAction(0, showHideLabel, receiverPendingIntent)
146                         .addAction(0, getString(R.string.action_quit), receiverPendingIntent2);
147
148                 startForeground(8675309, mBuilder.build());
149
150                 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.UPDATE_SWITCH"));
151
152                 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
153                     TileService.requestListeningState(this, new ComponentName(BuildConfig.APPLICATION_ID, QuickSettingsTileService.class.getName()));
154
155                 DependencyUtils.requestTaskerQuery(this);
156
157                 if(!isHidden) {
158                     registerReceiver(userForegroundReceiver, new IntentFilter(Intent.ACTION_USER_FOREGROUND));
159                     registerReceiver(userBackgroundReceiver, new IntentFilter(Intent.ACTION_USER_BACKGROUND));
160                 }
161             } else {
162                 pref.edit().putBoolean("taskbar_active", false).apply();
163
164                 stopSelf();
165             }
166         } else stopSelf();
167     }
168
169     @Override
170     public void onDestroy() {
171         SharedPreferences pref = U.getSharedPreferences(this);
172         if(pref.getBoolean("is_restarting", false))
173             pref.edit().remove("is_restarting").apply();
174         else {
175             LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.UPDATE_SWITCH"));
176
177             if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
178                 TileService.requestListeningState(this, new ComponentName(BuildConfig.APPLICATION_ID, QuickSettingsTileService.class.getName()));
179
180             DependencyUtils.requestTaskerQuery(this);
181
182             if(!U.launcherIsDefault(this) || U.isChromeOs(this))
183                 U.stopFreeformHack(this);
184         }
185
186         super.onDestroy();
187
188         if(!isHidden) {
189             unregisterReceiver(userForegroundReceiver);
190             unregisterReceiver(userBackgroundReceiver);
191         }
192     }
193 }