Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(core): Fix crash on start up for Android 14 #406

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ android:versionName="">
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>

<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
<uses-permission android:name="android.permission.NFC"/>

<uses-permission android:name="android.permission.TRANSMIT_IR"/>
<uses-permission android:name="android.permission.UWB_RANGING"/>


<!-- Allows for QR COde scanner -->
<uses-permission android:name="android.permission.VIBRATE"/>

Expand All @@ -31,6 +43,9 @@ 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_DATA_SYNC" android:minSdkVersion="34"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CAMERA" android:minSdkVersion="34"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE" 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">
Expand All @@ -56,7 +71,14 @@ 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="false"
android:icon="@mipmap/ic_launcher"
android:label="Inventory Agent"
android:name="org.glpi.inventory.agent.service.InventoryService"
android:foregroundServiceType="dataSync|connectedDevice|camera">
<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 @@ -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;
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 All @@ -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;

Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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 {
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 @@ -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 {
Expand All @@ -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:
Expand Down Expand Up @@ -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"));
}
}
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();
}
}
38 changes: 25 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,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
Expand Down Expand Up @@ -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();
}

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

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -439,7 +451,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
) {
presenter.permissionSuccess();
} else {
presenter.showError(getString(R.string.permission_error_result));
Expand Down
Loading