From 6656619c8c651820071087a391c30c3537b82242 Mon Sep 17 00:00:00 2001 From: stonebuzz Date: Thu, 19 Sep 2024 10:57:22 +0200 Subject: [PATCH] Fix(core): Fix crash on start up for Android 14 --- app/src/main/AndroidManifest.xml | 24 +++++++++++- .../inventory/agent/core/home/HomeModel.java | 1 + .../GlobalParametersPreference.java | 2 - .../InventoryParametersPreference.java | 4 +- .../agent/service/InventoryService.java | 15 ++++++-- .../agent/ui/ActivityDetailServer.java | 6 +-- .../agent/ui/ActivityListServers.java | 12 ++++-- .../glpi/inventory/agent/ui/ActivityMain.java | 38 ++++++++++++------- .../agent/ui/PermissionActivity.java | 3 +- 9 files changed, 75 insertions(+), 30 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index eaaae5d1a..bd2697689 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -20,6 +20,18 @@ android:versionName=""> + + + + + + + + + + + + @@ -31,6 +43,9 @@ android:versionName=""> + + + @@ -56,7 +71,14 @@ android:versionName=""> - + diff --git a/app/src/main/java/org/glpi/inventory/agent/core/home/HomeModel.java b/app/src/main/java/org/glpi/inventory/agent/core/home/HomeModel.java index b4b89e3e8..c305a257b 100644 --- a/app/src/main/java/org/glpi/inventory/agent/core/home/HomeModel.java +++ b/app/src/main/java/org/glpi/inventory/agent/core/home/HomeModel.java @@ -37,6 +37,7 @@ import android.app.Activity; import android.app.ActivityManager; +import android.app.Service; import android.content.ComponentName; import android.content.Context; import android.content.Intent; diff --git a/app/src/main/java/org/glpi/inventory/agent/preference/GlobalParametersPreference.java b/app/src/main/java/org/glpi/inventory/agent/preference/GlobalParametersPreference.java index 106f9b1fd..c6aa9f199 100644 --- a/app/src/main/java/org/glpi/inventory/agent/preference/GlobalParametersPreference.java +++ b/app/src/main/java/org/glpi/inventory/agent/preference/GlobalParametersPreference.java @@ -35,7 +35,6 @@ package org.glpi.inventory.agent.preference; -import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceActivity; @@ -45,7 +44,6 @@ import android.widget.LinearLayout; import androidx.appcompat.widget.Toolbar; -import androidx.localbroadcastmanager.content.LocalBroadcastManager; import org.glpi.inventory.agent.R; import org.glpi.inventory.agent.utils.AgentLog; diff --git a/app/src/main/java/org/glpi/inventory/agent/preference/InventoryParametersPreference.java b/app/src/main/java/org/glpi/inventory/agent/preference/InventoryParametersPreference.java index 72f771084..e5d693c18 100644 --- a/app/src/main/java/org/glpi/inventory/agent/preference/InventoryParametersPreference.java +++ b/app/src/main/java/org/glpi/inventory/agent/preference/InventoryParametersPreference.java @@ -45,7 +45,6 @@ import android.widget.LinearLayout; import androidx.appcompat.widget.Toolbar; -import androidx.localbroadcastmanager.content.LocalBroadcastManager; import org.glpi.inventory.agent.R; import org.glpi.inventory.agent.utils.AgentLog; @@ -75,8 +74,7 @@ public void onClick(View v) { public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) { if ("timeInventory".equals(s)) { - Intent intent = new Intent("timeAlarmChanged"); - LocalBroadcastManager.getInstance(this).sendBroadcast(intent); + sendBroadcast(new Intent("timeAlarmChanged")); AgentLog.d("Preference "+ s +" changed -> " + sharedPreferences.getString(s, "Week")); } diff --git a/app/src/main/java/org/glpi/inventory/agent/service/InventoryService.java b/app/src/main/java/org/glpi/inventory/agent/service/InventoryService.java index c7e1e223a..237111080 100644 --- a/app/src/main/java/org/glpi/inventory/agent/service/InventoryService.java +++ b/app/src/main/java/org/glpi/inventory/agent/service/InventoryService.java @@ -43,6 +43,7 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.content.pm.ServiceInfo; import android.graphics.Color; import android.os.Build; import android.os.Handler; @@ -52,6 +53,8 @@ import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import androidx.core.app.NotificationCompat; +import androidx.core.app.ServiceCompat; + import android.util.Log; import android.util.Xml; @@ -129,8 +132,7 @@ private void startMyOwnForeground(){ //create intent to redirect user to app on click Intent appIntent = new Intent(getApplicationContext(), ActivityMain.class); appIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); - PendingIntent appIntentRedirect = PendingIntent.getActivity(getApplicationContext(), 0, appIntent, PendingIntent.FLAG_MUTABLE); - + PendingIntent appIntentRedirect = PendingIntent.getActivity(getApplicationContext(), 0, appIntent, PendingIntent.FLAG_IMMUTABLE); //create inent to invite user to disable notification Intent notificationIntent = new Intent(); @@ -139,7 +141,7 @@ private void startMyOwnForeground(){ notificationIntent.putExtra("app_uid", getApplicationInfo().uid); notificationIntent.putExtra("android.provider.extra.APP_PACKAGE", getPackageName()); PendingIntent notificationIntentRedirect = PendingIntent.getActivity(getApplicationContext(), 0, - notificationIntent, PendingIntent.FLAG_MUTABLE); + notificationIntent, PendingIntent.FLAG_IMMUTABLE); //create notification NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID); @@ -153,7 +155,12 @@ private void startMyOwnForeground(){ .setContentIntent(appIntentRedirect) .addAction(R.drawable.ic_about, getApplicationContext().getResources().getString(R.string.disable_notification), notificationIntentRedirect) .build(); - startForeground(2, notification); + + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { + startForeground(2, notification); + } else { + ServiceCompat.startForeground(this, 2,notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC | ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE | ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA); + } } class TimeDisplayTimerTask extends TimerTask { diff --git a/app/src/main/java/org/glpi/inventory/agent/ui/ActivityDetailServer.java b/app/src/main/java/org/glpi/inventory/agent/ui/ActivityDetailServer.java index f9c997cd2..f21339af2 100644 --- a/app/src/main/java/org/glpi/inventory/agent/ui/ActivityDetailServer.java +++ b/app/src/main/java/org/glpi/inventory/agent/ui/ActivityDetailServer.java @@ -50,11 +50,9 @@ import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; -import androidx.localbroadcastmanager.content.LocalBroadcastManager; import com.google.android.material.floatingactionbutton.FloatingActionButton; -import org.flyve.inventory.InventoryLog; import org.glpi.inventory.agent.R; import org.glpi.inventory.agent.core.detailserver.DetailServer; import org.glpi.inventory.agent.core.detailserver.DetailServerPresenter; @@ -256,6 +254,7 @@ public void onClick(View v) { serverInfo.add(editSerial.getText().toString()); if (serverName == null) { presenter.saveServer(serverInfo, getApplicationContext()); + sendBroadcast(new Intent("reload-servers")); //manage automatic inventory if(extra_Data != null){ try { @@ -281,6 +280,7 @@ public void onClick(View v) { } } else { presenter.updateServer(serverInfo, serverName, getApplicationContext()); + sendBroadcast(new Intent("reload-servers")); } break; case R.id.deleteServer: @@ -319,6 +319,6 @@ public void modelServer(ServerSchema model) { @Override public void onBackPressed() { super.onBackPressed(); - LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("reload-servers")); + sendBroadcast(new Intent("reload-servers")); } } diff --git a/app/src/main/java/org/glpi/inventory/agent/ui/ActivityListServers.java b/app/src/main/java/org/glpi/inventory/agent/ui/ActivityListServers.java index 0aa3e3497..3554428e8 100644 --- a/app/src/main/java/org/glpi/inventory/agent/ui/ActivityListServers.java +++ b/app/src/main/java/org/glpi/inventory/agent/ui/ActivityListServers.java @@ -39,6 +39,7 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.os.Build; import android.os.Bundle; import android.view.View; import android.widget.Button; @@ -46,7 +47,6 @@ import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; -import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -70,6 +70,7 @@ public void onReceive(Context context, Intent intent) { } }; + /** * Called when the activity is starting, inflates the activity's UI * @param savedInstanceState if the activity is re-initialized, it contains the data it most recently supplied @@ -106,7 +107,12 @@ public void onClick(View v) { presenter.loadServers(this); IntentFilter filter = new IntentFilter("reload-servers"); - LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, filter); + if (Build.VERSION.SDK_INT >= 34 && getApplicationInfo().targetSdkVersion >= 34) { + registerReceiver(mMessageReceiver, filter, RECEIVER_NOT_EXPORTED); + } else { + registerReceiver(mMessageReceiver, filter); + } + } @@ -133,7 +139,7 @@ public void showServer(ArrayList model) { @Override protected void onDestroy() { - LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver); + unregisterReceiver(mMessageReceiver); super.onDestroy(); } } diff --git a/app/src/main/java/org/glpi/inventory/agent/ui/ActivityMain.java b/app/src/main/java/org/glpi/inventory/agent/ui/ActivityMain.java index a78c87e3d..623991df5 100644 --- a/app/src/main/java/org/glpi/inventory/agent/ui/ActivityMain.java +++ b/app/src/main/java/org/glpi/inventory/agent/ui/ActivityMain.java @@ -45,10 +45,10 @@ import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; -import android.os.Environment; import android.os.Parcelable; import android.preference.PreferenceManager; +import androidx.annotation.RequiresApi; import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; @@ -56,11 +56,8 @@ import androidx.drawerlayout.widget.DrawerLayout; import androidx.enterprise.feedback.KeyedAppState; import androidx.enterprise.feedback.KeyedAppStatesReporter; -import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; -import androidx.localbroadcastmanager.content.LocalBroadcastManager; -import android.provider.Settings; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; @@ -70,19 +67,14 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton; -import org.flyve.inventory.InventoryTask; import org.glpi.inventory.agent.R; -import org.glpi.inventory.agent.core.detailserver.DetailServer; -import org.glpi.inventory.agent.core.detailserver.DetailServerPresenter; import org.glpi.inventory.agent.core.main.Main; import org.glpi.inventory.agent.core.main.MainPresenter; import org.glpi.inventory.agent.preference.GlobalParametersPreference; import org.glpi.inventory.agent.preference.InventoryParametersPreference; -import org.glpi.inventory.agent.schema.ServerSchema; import org.glpi.inventory.agent.service.InventoryService; 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 org.glpi.inventory.agent.utils.LocalStorage; import org.json.JSONException; @@ -143,7 +135,14 @@ protected void onStart() { } }; - registerReceiver(appRestrictionChange, restrictionsFilter); + if (Build.VERSION.SDK_INT >= 34 && getApplicationInfo().targetSdkVersion >= 34) { + registerReceiver(appRestrictionChange, restrictionsFilter, RECEIVER_NOT_EXPORTED); + registerReceiver(mMessageReceiver, restrictionsFilter, RECEIVER_NOT_EXPORTED); + } else { + registerReceiver(appRestrictionChange, restrictionsFilter); + registerReceiver(mMessageReceiver, restrictionsFilter); + } + } @Override @@ -270,7 +269,13 @@ private void resolveRestrictions() { @Override protected void onResume() { super.onResume(); - registerReceiver(broadcastReceiver,new IntentFilter(InventoryService.TIMER_RECEIVER)); + + if (Build.VERSION.SDK_INT >= 34 && getApplicationInfo().targetSdkVersion >= 34) { + registerReceiver(broadcastReceiver,new IntentFilter(InventoryService.TIMER_RECEIVER), RECEIVER_NOT_EXPORTED); + } else { + registerReceiver(broadcastReceiver,new IntentFilter(InventoryService.TIMER_RECEIVER)); + } + resolveRestrictions(); } @@ -290,6 +295,7 @@ protected void onCreate(Bundle savedInstanceState) { new String[]{ Manifest.permission.READ_PHONE_STATE, Manifest.permission.CAMERA, + Manifest.permission.FOREGROUND_SERVICE_DATA_SYNC }, 1); @@ -342,8 +348,14 @@ public void onItemClick(AdapterView parent, View view, int position, long id) drawerLayout.setDrawerListener(mDrawerToggle); mDrawerToggle.syncState(); + + IntentFilter timeAlarmChanged = new IntentFilter("timeAlarmChanged"); - LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, timeAlarmChanged); + if (Build.VERSION.SDK_INT >= 34 && getApplicationInfo().targetSdkVersion >= 34) { + registerReceiver(appRestrictionChange, timeAlarmChanged, RECEIVER_NOT_EXPORTED); + } else { + registerReceiver(appRestrictionChange, timeAlarmChanged); + } //FloatActionButton mainFab = findViewById(R.id.fab); @@ -439,7 +451,7 @@ private void loadCategories() { protected void onDestroy() { super.onDestroy(); sharedPreferences.unregisterOnSharedPreferenceChangeListener(this); - LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver); + unregisterReceiver(mMessageReceiver); } @Override diff --git a/app/src/main/java/org/glpi/inventory/agent/ui/PermissionActivity.java b/app/src/main/java/org/glpi/inventory/agent/ui/PermissionActivity.java index a33f26328..b054c80b5 100644 --- a/app/src/main/java/org/glpi/inventory/agent/ui/PermissionActivity.java +++ b/app/src/main/java/org/glpi/inventory/agent/ui/PermissionActivity.java @@ -80,7 +80,8 @@ public void onRequestPermissionsResult(int requestCode, String permissions[], in // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED - && grantResults[1] == PackageManager.PERMISSION_GRANTED) { + && grantResults[1] == PackageManager.PERMISSION_GRANTED + ) { presenter.permissionSuccess(); } else { presenter.showError(getString(R.string.permission_error_result));