Skip to content

Commit

Permalink
Fix(core): Fix crash on start up for Android 14
Browse files Browse the repository at this point in the history
  • Loading branch information
stonebuzz committed Sep 19, 2024
1 parent aa9b55f commit d1a6e1a
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 30 deletions.
13 changes: 12 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ android:versionName="">
<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_TYPE_DATA_SYNC" android:minSdkVersion="34"/>
<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,7 +59,15 @@ 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">
<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"
android:foregroundServiceType="dataSync">
<intent-filter>
<action android:name="org.glpi.inventory.service"/>
</intent-filter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -129,8 +130,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();
Expand All @@ -139,7 +139,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);
Expand All @@ -153,7 +153,13 @@ 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 {
startForeground(2, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC);
}

}

class TimeDisplayTimerTask extends TimerTask {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -319,6 +317,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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@
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;
import android.widget.RelativeLayout;

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;

Expand All @@ -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
Expand Down Expand Up @@ -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);
}

}


Expand All @@ -133,7 +139,7 @@ public void showServer(ArrayList<String> model) {

@Override
protected void onDestroy() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
unregisterReceiver(mMessageReceiver);
super.onDestroy();
}
}
37 changes: 24 additions & 13 deletions app/src/main/java/org/glpi/inventory/agent/ui/ActivityMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,19 @@
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;
import androidx.core.app.ActivityCompat;
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;
Expand All @@ -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;
Expand Down Expand Up @@ -143,7 +135,12 @@ protected void onStart() {
}
};

registerReceiver(appRestrictionChange, restrictionsFilter);
if (Build.VERSION.SDK_INT >= 34 && getApplicationInfo().targetSdkVersion >= 34) {
registerReceiver(appRestrictionChange, restrictionsFilter, RECEIVER_NOT_EXPORTED);
} else {
registerReceiver(appRestrictionChange, restrictionsFilter);
}

}

@Override
Expand Down Expand Up @@ -270,7 +267,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();
}

Expand All @@ -280,6 +283,7 @@ protected void onPause() {
unregisterReceiver(broadcastReceiver);
}

@RequiresApi(api = 34)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -290,6 +294,7 @@ protected void onCreate(Bundle savedInstanceState) {
new String[]{
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.CAMERA,
Manifest.permission.FOREGROUND_SERVICE_DATA_SYNC,
},
1);

Expand Down Expand Up @@ -342,8 +347,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);
Expand Down Expand Up @@ -439,7 +450,7 @@ private void loadCategories() {
protected void onDestroy() {
super.onDestroy();
sharedPreferences.unregisterOnSharedPreferenceChangeListener(this);
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
unregisterReceiver(mMessageReceiver);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
&& grantResults[2] == PackageManager.PERMISSION_GRANTED) {
presenter.permissionSuccess();
} else {
presenter.showError(getString(R.string.permission_error_result));
Expand Down

0 comments on commit d1a6e1a

Please sign in to comment.