Skip to content

Commit

Permalink
Fix(core): Fix androdi 14 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
stonebuzz committed Sep 24, 2024
1 parent 3853012 commit a2a4369
Show file tree
Hide file tree
Showing 13 changed files with 334 additions and 674 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:

# Run Instrumentation Tests in Firebase Test Lab
- name: Run Instrumentation Tests in Firebase Test Lab
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
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


##########################################
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
apply plugin: 'com.google.firebase.crashlytics'

android {
compileSdkVersion 34
compileSdk 34
useLibrary 'org.apache.http.legacy'

defaultConfig {
Expand Down
28 changes: 10 additions & 18 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:versionCode="39401"
android:versionName="">
xmlns:tools="http://schemas.android.com/tools"
android:versionCode="39413"
android:versionName="">

<!-- Permissions -->
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
Expand All @@ -30,9 +31,6 @@ android:versionName="">
<!-- Allows changing locales -->
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/>

<!-- Apps wanting to use foreground services, API 28 higher -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

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

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

<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">
<intent-filter>
<action android:name="org.glpi.inventory.service"/>
</intent-filter>
</service>

<receiver android:enabled="true" android:exported="true" android:name="org.glpi.inventory.agent.broadcast.BootStartAgent" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
Expand All @@ -72,11 +64,11 @@ android:versionName="">
<action android:name="android.intent.action.ACTION_SHUTDOWN"/>
</intent-filter>
</receiver>
<receiver android:exported="true" android:name="org.glpi.inventory.agent.broadcast.TimeAlarm">
<intent-filter>
<action android:name="org.glpi.inventory.agent.ALARM"/>
</intent-filter>
</receiver>

<service android:name=".broadcast.InventoryJobScheduler"
android:enabled="true"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE"/>

<activity android:name="org.glpi.inventory.agent.ui.PermissionActivity"/>
<activity android:name="org.glpi.inventory.agent.ui.ActivityListServers"/>
Expand All @@ -96,4 +88,4 @@ android:versionName="">
<activity android:name="org.glpi.inventory.agent.ui.ActivityCategories"/>
</application>

</manifest>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -38,53 +38,16 @@
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.preference.PreferenceManager;
import android.widget.Toast;

import org.flyve.inventory.InventoryLog;
import org.glpi.inventory.agent.service.InventoryService;
import org.glpi.inventory.agent.ui.ActivityMain;
import org.glpi.inventory.agent.utils.AgentLog;

public class BootStartAgent extends BroadcastReceiver {

TimeAlarm alarm = new TimeAlarm();

/**
* It sets an alarm after the user has finished booting
* @param context in which the receiver is running
* @param intent being received
*/
@Override
public void onReceive(Context context, Intent intent) {

String action = intent.getAction();
if(action==null) {
return;
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent activityIntent = new Intent(context, ActivityMain.class);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activityIntent);
}

SharedPreferences customSharedPreference = PreferenceManager.getDefaultSharedPreferences(context);
if (customSharedPreference.getBoolean("boot", false)) {
try {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Intent myIntent = new Intent(context, InventoryService.class);
context.startForegroundService(myIntent);
} else {
Intent myIntent = new Intent(context, ActivityMain.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}catch(Exception ex) {
Toast.makeText(context, ex.getMessage(), Toast.LENGTH_LONG).show();
}
}

if(customSharedPreference.getBoolean("autoStartInventory", false)){
alarm.setAlarm(context);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,51 +35,71 @@

package org.glpi.inventory.agent.broadcast;

import android.app.AlarmManager;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.app.job.JobParameters;
import android.app.job.JobService;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.PowerManager;
import android.graphics.Color;
import android.os.Handler;
import android.os.HandlerThread;
import android.preference.PreferenceManager;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

import org.flyve.inventory.InventoryTask;
import org.glpi.inventory.agent.R;
import org.glpi.inventory.agent.schema.ServerSchema;
import org.glpi.inventory.agent.ui.ActivityMain;
import org.glpi.inventory.agent.utils.AgentLog;
import org.glpi.inventory.agent.utils.Helpers;
import org.glpi.inventory.agent.utils.HttpInventory;
import org.glpi.inventory.agent.utils.LocalPreferences;

import java.util.ArrayList;
import java.util.Calendar;

public class TimeAlarm extends BroadcastReceiver {
@SuppressLint("SpecifyJobSchedulerIdRange")
public class InventoryJobScheduler extends JobService {

public static final int INVENTORY_JOB_ID = 4492015;
private static final String NOTIFICATION_CHANNEL_ID = "org.glpi.inventory.agent";

/**
* If the success XML is created, it sends the inventory
* @param context in which the receiver is running
* @param intent being received
*/
@Override
public void onReceive(final Context context, Intent intent) {
public boolean onStartJob(final JobParameters params) {
HandlerThread handlerThread = new HandlerThread("SomeOtherThread");
handlerThread.start();

Handler handler = new Handler(handlerThread.getLooper());
handler.post(new Runnable() {
@Override
public void run() {
AgentLog.d("GLPI-AGENT-JOBSCHEDULER : Run task" + Calendar.getInstance().getTime());
doInventory();
jobFinished(params, true);
}
});

return true;
}

private void doInventory() {
Context context = getApplicationContext();
AgentLog.d("GLPI-AGENT-JOBSCHEDULER : Launch inventory from JobScheduler " + Calendar.getInstance().getTime());

// check if is deactivated
// check if autoStartInventory is deactivated
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
Boolean val = sharedPreferences.getBoolean("autoStartInventory", false);
if (!val) {
AgentLog.d("The inventory will not be send, is deactivated");
if (!sharedPreferences.getBoolean("autoStartInventory", false)) {
AgentLog.d("GLPI-AGENT-JOBSCHEDULER : The inventory will not be send, is deactivated");
return;
}

AgentLog.d("Launch inventory from alarm");

PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
showPersistentNotification();

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

@Override
public void onTaskError(String error) {
AgentLog.d("GLPI-AGENT-JOBSCHEDULER : Inventory error");
Helpers.sendToNotificationBar(context.getApplicationContext(), context.getResources().getString(R.string.inventory_notification_fail));
AgentLog.e(error);
}
Expand All @@ -119,57 +141,52 @@ public void onTaskError(Throwable error) {
});
}
} else {
AgentLog.e(context.getResources().getString(R.string.inventory_no_server));
AgentLog.d("GLPI-AGENT-JOBSCHEDULER : " + context.getResources().getString(R.string.inventory_no_server));
}
}

wl.release();
@Override
public boolean onStopJob(final JobParameters params) {
AgentLog.d("GLPI-AGENT-JOBSCHEDULER : onStopJob() was called");
return true;
}

/**
* Schedules the alarm
* @param context
*/
@RequiresApi(api = Build.VERSION_CODES.M)
public void setAlarm(Context context) {

AgentLog.d("Set Alarm");

AlarmManager am =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, TimeAlarm.class);
i.setAction("org.glpi.inventory.agent.ALARM");
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_IMMUTABLE);

SharedPreferences customSharedPreference = PreferenceManager.getDefaultSharedPreferences(context);
String timeInventory = customSharedPreference.getString("timeInventory", "Week");

int time = 60 * 1000;

if (timeInventory.equals("Day")) {
time = 24 * 60 * 60 * 1000;
AgentLog.d("Alarm Daily");
} else if(timeInventory.equals("Week")) {
time = 7 * 24 * 60 * 60 * 1000;
AgentLog.d("Alarm Weekly");
} else if(timeInventory.equals("Month")) {
time = 30 * 24 * 60 * 60 * 1000;
AgentLog.d("Alarm Monthly");
}
private void showPersistentNotification() {
String channelName = getString(R.string.app_is_running);

try {
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), time, pi);
} catch (NullPointerException ex) {
AgentLog.e(ex.getMessage());
NotificationChannel chan = null;
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
manager.createNotificationChannel(chan);
}
}

/**
* Removes the alarm with a matching argument
* @param context
*/
public void cancelAlarm(Context context) {
Intent intent = new Intent(context, TimeAlarm.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
Intent appIntent = new Intent(this, ActivityMain.class);
appIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent appIntentRedirect = PendingIntent.getActivity(this, 0, appIntent, PendingIntent.FLAG_IMMUTABLE);

Intent notificationIntent = new Intent();
notificationIntent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
notificationIntent.putExtra("app_package", getPackageName());
notificationIntent.putExtra("app_uid", getApplicationInfo().uid);
PendingIntent notificationIntentRedirect = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);

Notification notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setOngoing(true)
.setSmallIcon(R.drawable.ic_stat)
.setContentTitle(getString(R.string.app_is_running))
.setContentText(getString(R.string.agent_description)) // Add a short text
.setCategory(Notification.CATEGORY_SERVICE)
.setStyle(new NotificationCompat.BigTextStyle().bigText(getString(R.string.app_is_running_extend)))
.setContentIntent(appIntentRedirect)
.addAction(R.drawable.ic_about, getString(R.string.disable_notification), notificationIntentRedirect)
.build();

int notificationId = 1;
manager.notify(notificationId, notification);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,12 @@ interface Presenter {
void showError(String message);

// Models
void doBindService(Activity activity);
void setupList(Activity activity, ListView lst);
void clickItem(final Activity activity, HomeSchema homeSchema);
List<HomeSchema> getListItems();
}

interface Model {
void doBindService(Activity activity);
void setupList(Activity activity, ListView lst);
void clickItem(final Activity activity, HomeSchema homeSchema);
List<HomeSchema> getListItems();
Expand Down
Loading

0 comments on commit a2a4369

Please sign in to comment.