35
35
36
36
package org .glpi .inventory .agent .broadcast ;
37
37
38
- import android .app .AlarmManager ;
38
+ import android .annotation .SuppressLint ;
39
+ import android .app .Notification ;
40
+ import android .app .NotificationChannel ;
41
+ import android .app .NotificationManager ;
39
42
import android .app .PendingIntent ;
40
- import android .content .BroadcastReceiver ;
43
+ import android .app .job .JobParameters ;
44
+ import android .app .job .JobService ;
41
45
import android .content .Context ;
42
46
import android .content .Intent ;
43
47
import android .content .SharedPreferences ;
44
- import android .os .Build ;
45
- import android .os .PowerManager ;
48
+ import android .graphics .Color ;
49
+ import android .os .Handler ;
50
+ import android .os .HandlerThread ;
46
51
import android .preference .PreferenceManager ;
47
52
48
- import androidx .annotation . RequiresApi ;
53
+ import androidx .core . app . NotificationCompat ;
49
54
50
55
import org .flyve .inventory .InventoryTask ;
51
56
import org .glpi .inventory .agent .R ;
52
57
import org .glpi .inventory .agent .schema .ServerSchema ;
58
+ import org .glpi .inventory .agent .ui .ActivityMain ;
53
59
import org .glpi .inventory .agent .utils .AgentLog ;
54
60
import org .glpi .inventory .agent .utils .Helpers ;
55
61
import org .glpi .inventory .agent .utils .HttpInventory ;
56
62
import org .glpi .inventory .agent .utils .LocalPreferences ;
57
63
58
64
import java .util .ArrayList ;
65
+ import java .util .Calendar ;
59
66
60
- public class TimeAlarm extends BroadcastReceiver {
67
+ @ SuppressLint ("SpecifyJobSchedulerIdRange" )
68
+ public class InventoryJobScheduler extends JobService {
69
+
70
+ public static final int INVENTORY_JOB_ID = 4492015 ;
71
+ private static final String NOTIFICATION_CHANNEL_ID = "org.glpi.inventory.agent" ;
61
72
62
- /**
63
- * If the success XML is created, it sends the inventory
64
- * @param context in which the receiver is running
65
- * @param intent being received
66
- */
67
73
@ Override
68
- public void onReceive (final Context context , Intent intent ) {
74
+ public boolean onStartJob (final JobParameters params ) {
75
+ HandlerThread handlerThread = new HandlerThread ("SomeOtherThread" );
76
+ handlerThread .start ();
77
+
78
+ Handler handler = new Handler (handlerThread .getLooper ());
79
+ handler .post (new Runnable () {
80
+ @ Override
81
+ public void run () {
82
+ AgentLog .d ("GLPI-AGENT-JOBSCHEDULER : Run task" + Calendar .getInstance ().getTime ());
83
+ doInventory ();
84
+ jobFinished (params , true );
85
+ }
86
+ });
87
+
88
+ return true ;
89
+ }
90
+
91
+ private void doInventory () {
92
+ Context context = getApplicationContext ();
93
+ AgentLog .d ("GLPI-AGENT-JOBSCHEDULER : Launch inventory from JobScheduler " + Calendar .getInstance ().getTime ());
69
94
70
- // check if is deactivated
95
+ // check if autoStartInventory is deactivated
71
96
SharedPreferences sharedPreferences = PreferenceManager .getDefaultSharedPreferences (context );
72
- Boolean val = sharedPreferences .getBoolean ("autoStartInventory" , false );
73
- if (!val ) {
74
- AgentLog .d ("The inventory will not be send, is deactivated" );
97
+ if (!sharedPreferences .getBoolean ("autoStartInventory" , false )) {
98
+ AgentLog .d ("GLPI-AGENT-JOBSCHEDULER : The inventory will not be send, is deactivated" );
75
99
return ;
76
100
}
77
101
78
- AgentLog .d ("Launch inventory from alarm" );
79
-
80
- PowerManager pm = (PowerManager ) context .getSystemService (Context .POWER_SERVICE );
81
- PowerManager .WakeLock wl = pm .newWakeLock (PowerManager .PARTIAL_WAKE_LOCK , "" );
82
- wl .acquire ();
102
+ showPersistentNotification ();
83
103
84
104
final InventoryTask inventory = new InventoryTask (context .getApplicationContext (), Helpers .getAgentDescription (context ), true );
85
105
final HttpInventory httpInventory = new HttpInventory (context .getApplicationContext ());
@@ -99,12 +119,14 @@ public void onTaskSuccess(String data) {
99
119
httpInventory .sendInventory (data , model , new HttpInventory .OnTaskCompleted () {
100
120
@ Override
101
121
public void onTaskSuccess (String data ) {
122
+ AgentLog .d ("GLPI-AGENT-JOBSCHEDULER : Inventory Success" );
102
123
Helpers .sendToNotificationBar (context .getApplicationContext (), context .getResources ().getString (R .string .inventory_notification_sent ));
103
124
//Helpers.sendAnonymousData(context.getApplicationContext(), inventory);
104
125
}
105
126
106
127
@ Override
107
128
public void onTaskError (String error ) {
129
+ AgentLog .d ("GLPI-AGENT-JOBSCHEDULER : Inventory error" );
108
130
Helpers .sendToNotificationBar (context .getApplicationContext (), context .getResources ().getString (R .string .inventory_notification_fail ));
109
131
AgentLog .e (error );
110
132
}
@@ -119,57 +141,52 @@ public void onTaskError(Throwable error) {
119
141
});
120
142
}
121
143
} else {
122
- AgentLog .e ( context .getResources ().getString (R .string .inventory_no_server ));
144
+ AgentLog .d ( "GLPI-AGENT-JOBSCHEDULER : " + context .getResources ().getString (R .string .inventory_no_server ));
123
145
}
146
+ }
124
147
125
- wl .release ();
148
+ @ Override
149
+ public boolean onStopJob (final JobParameters params ) {
150
+ AgentLog .d ("GLPI-AGENT-JOBSCHEDULER : onStopJob() was called" );
151
+ return true ;
126
152
}
127
153
128
- /**
129
- * Schedules the alarm
130
- * @param context
131
- */
132
- @ RequiresApi (api = Build .VERSION_CODES .M )
133
- public void setAlarm (Context context ) {
134
-
135
- AgentLog .d ("Set Alarm" );
136
-
137
- AlarmManager am =(AlarmManager )context .getSystemService (Context .ALARM_SERVICE );
138
- Intent i = new Intent (context , TimeAlarm .class );
139
- i .setAction ("org.glpi.inventory.agent.ALARM" );
140
- PendingIntent pi = PendingIntent .getBroadcast (context , 0 , i , PendingIntent .FLAG_IMMUTABLE );
141
-
142
- SharedPreferences customSharedPreference = PreferenceManager .getDefaultSharedPreferences (context );
143
- String timeInventory = customSharedPreference .getString ("timeInventory" , "Week" );
144
-
145
- int time = 60 * 1000 ;
146
-
147
- if (timeInventory .equals ("Day" )) {
148
- time = 24 * 60 * 60 * 1000 ;
149
- AgentLog .d ("Alarm Daily" );
150
- } else if (timeInventory .equals ("Week" )) {
151
- time = 7 * 24 * 60 * 60 * 1000 ;
152
- AgentLog .d ("Alarm Weekly" );
153
- } else if (timeInventory .equals ("Month" )) {
154
- time = 30 * 24 * 60 * 60 * 1000 ;
155
- AgentLog .d ("Alarm Monthly" );
156
- }
154
+ private void showPersistentNotification () {
155
+ String channelName = getString (R .string .app_is_running );
157
156
158
- try {
159
- am .setRepeating (AlarmManager .RTC_WAKEUP , System .currentTimeMillis (), time , pi );
160
- } catch (NullPointerException ex ) {
161
- AgentLog .e (ex .getMessage ());
157
+ NotificationChannel chan = null ;
158
+ NotificationManager manager = (NotificationManager ) getSystemService (Context .NOTIFICATION_SERVICE );
159
+
160
+ if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .O ) {
161
+ chan = new NotificationChannel (NOTIFICATION_CHANNEL_ID , channelName , NotificationManager .IMPORTANCE_NONE );
162
+ chan .setLightColor (Color .BLUE );
163
+ chan .setLockscreenVisibility (Notification .VISIBILITY_PRIVATE );
164
+ manager .createNotificationChannel (chan );
162
165
}
163
- }
164
166
165
- /**
166
- * Removes the alarm with a matching argument
167
- * @param context
168
- */
169
- public void cancelAlarm (Context context ) {
170
- Intent intent = new Intent (context , TimeAlarm .class );
171
- PendingIntent sender = PendingIntent .getBroadcast (context , 0 , intent , 0 );
172
- AlarmManager alarmManager = (AlarmManager ) context .getSystemService (Context .ALARM_SERVICE );
173
- alarmManager .cancel (sender );
167
+ Intent appIntent = new Intent (this , ActivityMain .class );
168
+ appIntent .setFlags (Intent .FLAG_ACTIVITY_CLEAR_TOP | Intent .FLAG_ACTIVITY_SINGLE_TOP );
169
+ PendingIntent appIntentRedirect = PendingIntent .getActivity (this , 0 , appIntent , PendingIntent .FLAG_IMMUTABLE );
170
+
171
+ Intent notificationIntent = new Intent ();
172
+ notificationIntent .setAction ("android.settings.APP_NOTIFICATION_SETTINGS" );
173
+ notificationIntent .putExtra ("app_package" , getPackageName ());
174
+ notificationIntent .putExtra ("app_uid" , getApplicationInfo ().uid );
175
+ PendingIntent notificationIntentRedirect = PendingIntent .getActivity (this , 0 , notificationIntent , PendingIntent .FLAG_IMMUTABLE );
176
+
177
+ Notification notification = new NotificationCompat .Builder (this , NOTIFICATION_CHANNEL_ID )
178
+ .setOngoing (true )
179
+ .setSmallIcon (R .drawable .ic_stat )
180
+ .setContentTitle (getString (R .string .app_is_running ))
181
+ .setContentText (getString (R .string .agent_description )) // Add a short text
182
+ .setCategory (Notification .CATEGORY_SERVICE )
183
+ .setStyle (new NotificationCompat .BigTextStyle ().bigText (getString (R .string .app_is_running_extend )))
184
+ .setContentIntent (appIntentRedirect )
185
+ .addAction (R .drawable .ic_about , getString (R .string .disable_notification ), notificationIntentRedirect )
186
+ .build ();
187
+
188
+ int notificationId = 1 ;
189
+ manager .notify (notificationId , notification );
174
190
}
191
+
175
192
}
0 commit comments