Skip to content

Commit 4f5a5ae

Browse files
committed
Fix(core): Fix androdi 14 compatibility
1 parent d93244c commit 4f5a5ae

13 files changed

+334
-674
lines changed

.github/workflows/continuous-integration.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ jobs:
5757

5858
# Run Instrumentation Tests in Firebase Test Lab
5959
- name: Run Instrumentation Tests in Firebase Test Lab
60-
run: gcloud firebase test android run --type instrumentation --app ${{ env.module_app }}/build/outputs/apk/debug/${{ env.module_app }}-debug.apk --test ${{ env.module_app }}/build/outputs/apk/androidTest/debug/${{ env.module_app }}-debug-androidTest.apk --device model=oriole,version=31,locale=en,orientation=portrait
60+
run: gcloud firebase test android run --type instrumentation --app ${{ env.module_app }}/build/outputs/apk/debug/${{ env.module_app }}-debug.apk --test ${{ env.module_app }}/build/outputs/apk/androidTest/debug/${{ env.module_app }}-debug-androidTest.apk --device model=akita,version=34,locale=en,orientation=portrait
6161

6262

6363
##########################################

app/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
22
apply plugin: 'com.google.firebase.crashlytics'
33

44
android {
5-
compileSdkVersion 34
5+
compileSdk 34
66
useLibrary 'org.apache.http.legacy'
77

88
defaultConfig {

app/src/main/AndroidManifest.xml

+10-18
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:tools="http://schemas.android.com/tools"
4-
android:versionCode="39403"
5-
android:versionName="">
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:versionCode="39413"
5+
android:versionName="">
66

77
<!-- Permissions -->
8+
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
89
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
910
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
1011
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
@@ -30,9 +31,6 @@ android:versionName="">
3031
<!-- Allows changing locales -->
3132
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/>
3233

33-
<!-- Apps wanting to use foreground services, API 28 higher -->
34-
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
35-
3634
<application android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:largeHeap="true" android:name="org.glpi.inventory.agent.ui.InventoryAgentApp" android:requestLegacyExternalStorage="true" android:roundIcon="@drawable/ic_launcher_round" android:theme="@style/AppTheme" android:vmSafeMode="true">
3735

3836
<uses-library android:name="org.apache.http.legacy" android:required="false"/>
@@ -56,12 +54,6 @@ android:versionName="">
5654
<activity android:label="@string/app_name" android:name="org.glpi.inventory.agent.preference.GlobalParametersPreference"/>
5755
<activity android:label="@string/app_name" android:launchMode="standard" android:name="org.glpi.inventory.agent.ui.ActivityInventoryReport" android:theme="@style/NoActionBar"/>
5856

59-
<service android:description="@string/agent_description" android:enabled="true" android:exported="true" android:icon="@mipmap/ic_launcher" android:label="Inventory Agent" android:name="org.glpi.inventory.agent.service.InventoryService" tools:ignore="ExportedService">
60-
<intent-filter>
61-
<action android:name="org.glpi.inventory.service"/>
62-
</intent-filter>
63-
</service>
64-
6557
<receiver android:enabled="true" android:exported="true" android:name="org.glpi.inventory.agent.broadcast.BootStartAgent" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
6658
<intent-filter>
6759
<action android:name="android.intent.action.BOOT_COMPLETED"/>
@@ -72,11 +64,11 @@ android:versionName="">
7264
<action android:name="android.intent.action.ACTION_SHUTDOWN"/>
7365
</intent-filter>
7466
</receiver>
75-
<receiver android:exported="true" android:name="org.glpi.inventory.agent.broadcast.TimeAlarm">
76-
<intent-filter>
77-
<action android:name="org.glpi.inventory.agent.ALARM"/>
78-
</intent-filter>
79-
</receiver>
67+
68+
<service android:name=".broadcast.InventoryJobScheduler"
69+
android:enabled="true"
70+
android:exported="false"
71+
android:permission="android.permission.BIND_JOB_SERVICE"/>
8072

8173
<activity android:name="org.glpi.inventory.agent.ui.PermissionActivity"/>
8274
<activity android:name="org.glpi.inventory.agent.ui.ActivityListServers"/>
@@ -96,4 +88,4 @@ android:versionName="">
9688
<activity android:name="org.glpi.inventory.agent.ui.ActivityCategories"/>
9789
</application>
9890

99-
</manifest>
91+
</manifest>

app/src/main/java/org/glpi/inventory/agent/broadcast/BootStartAgent.java

+4-41
Original file line numberDiff line numberDiff line change
@@ -38,53 +38,16 @@
3838
import android.content.BroadcastReceiver;
3939
import android.content.Context;
4040
import android.content.Intent;
41-
import android.content.SharedPreferences;
42-
import android.os.Build;
43-
import android.preference.PreferenceManager;
44-
import android.widget.Toast;
45-
46-
import org.flyve.inventory.InventoryLog;
47-
import org.glpi.inventory.agent.service.InventoryService;
4841
import org.glpi.inventory.agent.ui.ActivityMain;
49-
import org.glpi.inventory.agent.utils.AgentLog;
5042

5143
public class BootStartAgent extends BroadcastReceiver {
5244

53-
TimeAlarm alarm = new TimeAlarm();
54-
55-
/**
56-
* It sets an alarm after the user has finished booting
57-
* @param context in which the receiver is running
58-
* @param intent being received
59-
*/
6045
@Override
6146
public void onReceive(Context context, Intent intent) {
62-
63-
String action = intent.getAction();
64-
if(action==null) {
65-
return;
47+
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
48+
Intent activityIntent = new Intent(context, ActivityMain.class);
49+
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
50+
context.startActivity(activityIntent);
6651
}
67-
68-
SharedPreferences customSharedPreference = PreferenceManager.getDefaultSharedPreferences(context);
69-
if (customSharedPreference.getBoolean("boot", false)) {
70-
try {
71-
72-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
73-
Intent myIntent = new Intent(context, InventoryService.class);
74-
context.startForegroundService(myIntent);
75-
} else {
76-
Intent myIntent = new Intent(context, ActivityMain.class);
77-
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
78-
context.startActivity(myIntent);
79-
}
80-
}catch(Exception ex) {
81-
Toast.makeText(context, ex.getMessage(), Toast.LENGTH_LONG).show();
82-
}
83-
}
84-
85-
if(customSharedPreference.getBoolean("autoStartInventory", false)){
86-
alarm.setAlarm(context);
87-
}
88-
8952
}
9053
}

app/src/main/java/org/glpi/inventory/agent/broadcast/TimeAlarm.java renamed to app/src/main/java/org/glpi/inventory/agent/broadcast/InventoryJobScheduler.java

+83-66
Original file line numberDiff line numberDiff line change
@@ -35,51 +35,71 @@
3535

3636
package org.glpi.inventory.agent.broadcast;
3737

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;
3942
import android.app.PendingIntent;
40-
import android.content.BroadcastReceiver;
43+
import android.app.job.JobParameters;
44+
import android.app.job.JobService;
4145
import android.content.Context;
4246
import android.content.Intent;
4347
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;
4651
import android.preference.PreferenceManager;
4752

48-
import androidx.annotation.RequiresApi;
53+
import androidx.core.app.NotificationCompat;
4954

5055
import org.flyve.inventory.InventoryTask;
5156
import org.glpi.inventory.agent.R;
5257
import org.glpi.inventory.agent.schema.ServerSchema;
58+
import org.glpi.inventory.agent.ui.ActivityMain;
5359
import org.glpi.inventory.agent.utils.AgentLog;
5460
import org.glpi.inventory.agent.utils.Helpers;
5561
import org.glpi.inventory.agent.utils.HttpInventory;
5662
import org.glpi.inventory.agent.utils.LocalPreferences;
5763

5864
import java.util.ArrayList;
65+
import java.util.Calendar;
5966

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";
6172

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-
*/
6773
@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());
6994

70-
// check if is deactivated
95+
// check if autoStartInventory is deactivated
7196
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");
7599
return;
76100
}
77101

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();
83103

84104
final InventoryTask inventory = new InventoryTask(context.getApplicationContext(), Helpers.getAgentDescription(context), true);
85105
final HttpInventory httpInventory = new HttpInventory(context.getApplicationContext());
@@ -99,12 +119,14 @@ public void onTaskSuccess(String data) {
99119
httpInventory.sendInventory(data, model, new HttpInventory.OnTaskCompleted() {
100120
@Override
101121
public void onTaskSuccess(String data) {
122+
AgentLog.d("GLPI-AGENT-JOBSCHEDULER : Inventory Success");
102123
Helpers.sendToNotificationBar(context.getApplicationContext(), context.getResources().getString(R.string.inventory_notification_sent));
103124
//Helpers.sendAnonymousData(context.getApplicationContext(), inventory);
104125
}
105126

106127
@Override
107128
public void onTaskError(String error) {
129+
AgentLog.d("GLPI-AGENT-JOBSCHEDULER : Inventory error");
108130
Helpers.sendToNotificationBar(context.getApplicationContext(), context.getResources().getString(R.string.inventory_notification_fail));
109131
AgentLog.e(error);
110132
}
@@ -119,57 +141,52 @@ public void onTaskError(Throwable error) {
119141
});
120142
}
121143
} 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));
123145
}
146+
}
124147

125-
wl.release();
148+
@Override
149+
public boolean onStopJob(final JobParameters params) {
150+
AgentLog.d("GLPI-AGENT-JOBSCHEDULER : onStopJob() was called");
151+
return true;
126152
}
127153

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);
157156

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);
162165
}
163-
}
164166

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);
174190
}
191+
175192
}

app/src/main/java/org/glpi/inventory/agent/core/home/Home.java

-2
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,12 @@ interface Presenter {
5454
void showError(String message);
5555

5656
// Models
57-
void doBindService(Activity activity);
5857
void setupList(Activity activity, ListView lst);
5958
void clickItem(final Activity activity, HomeSchema homeSchema);
6059
List<HomeSchema> getListItems();
6160
}
6261

6362
interface Model {
64-
void doBindService(Activity activity);
6563
void setupList(Activity activity, ListView lst);
6664
void clickItem(final Activity activity, HomeSchema homeSchema);
6765
List<HomeSchema> getListItems();

0 commit comments

Comments
 (0)