Panagiotis Triantafyllou

steps for android 13 and up fixes

......@@ -16,6 +16,7 @@
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_HEALTH" />
<uses-permission android:name="android.permission.HIGH_SAMPLING_RATE_SENSORS" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<application android:largeHeap="true">
<!-- <meta-data-->
......
package ly.warp.sdk.activities;
import android.app.AlertDialog;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
......@@ -37,6 +42,7 @@ public class BaseFragmentActivity extends FragmentActivity implements Navigation
private BottomNavigationView mBottomNavigationView;
private Fragment mFragmentToSet = null;
private static final int REQUEST_NOTIFICATION_PERMISSION = 5000;
// ===========================================================
// Methods for/from SuperClass/Interfaces
......@@ -52,6 +58,12 @@ public class BaseFragmentActivity extends FragmentActivity implements Navigation
WarplyManager.getUserCouponsWithCouponsets(mUserCouponsReceiver);
WarplyManager.getCampaigns(mCampaignsCallback);
WarplyManager.getUnifiedCouponsDeals(mUnifiedCallback);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (!hasNotificationPermission()) {
requestNotificationPermission();
}
}
}
@Override
......@@ -103,6 +115,18 @@ public class BaseFragmentActivity extends FragmentActivity implements Navigation
super.attachBaseContext(ViewPumpContextWrapper.wrap(newBase));
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_NOTIFICATION_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(BaseFragmentActivity.this, "Notifications Permission Granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(BaseFragmentActivity.this, "Notifications Permission Denied", Toast.LENGTH_SHORT).show();
}
}
}
// ===========================================================
// Methods
// ===========================================================
......@@ -114,6 +138,24 @@ public class BaseFragmentActivity extends FragmentActivity implements Navigation
mBottomNavigationView.setSelectedItemId(R.id.menu_home);
}
private boolean hasNotificationPermission() {
return ContextCompat.checkSelfPermission(this, android.Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED;
}
private void requestNotificationPermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.POST_NOTIFICATIONS)) {
new AlertDialog.Builder(this)
.setTitle("Notification Permission Needed")
.setMessage("This app needs the Notification permission to keep you updated.")
.setPositiveButton("OK", (dialog, which) -> ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.POST_NOTIFICATIONS}, REQUEST_NOTIFICATION_PERMISSION))
.setNegativeButton("Cancel", (dialog, which) -> dialog.dismiss())
.create()
.show();
} else {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.POST_NOTIFICATIONS}, REQUEST_NOTIFICATION_PERMISSION);
}
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
......
......@@ -14,6 +14,7 @@ import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import androidx.annotation.Nullable;
......@@ -86,12 +87,10 @@ public class WarplyHealthService extends Service implements SensorEventListener
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
// registerStepSensor();
Warply.getInitializer(getApplicationContext()).init();
WarplyManager.getPacingDetails(new PacingDetailsRequest(), mPacingCallback);
}
// @RequiresApi(api = Build.VERSION_CODES.O)
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
......@@ -103,45 +102,59 @@ public class WarplyHealthService extends Service implements SensorEventListener
b.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS);
b.setChannelId(STEPS_CHANNEL_ID);
b.setContentTitle(getString(R.string.cos_steps_for_good_notification_title));
// b.setContentText(getString(R.string.cos_steps_for_good_notification_subtitle));
b.setStyle(new NotificationCompat.BigTextStyle().bigText(getString(R.string.cos_steps_for_good_notification_subtitle)));
b.setContentIntent(pbIntent);
b.setAutoCancel(true);
b.setAutoCancel(false); //it was true
b.setOngoing(true);
// b.setCategory(Notification.CATEGORY_SERVICE);
b.setPriority(NotificationCompat.PRIORITY_HIGH);
b.setSmallIcon(R.drawable.ic_launcher);
if (notificationManager != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
NotificationChannel notificationChannel = new NotificationChannel(STEPS_CHANNEL_ID, "steps_notification_channel", NotificationManager.IMPORTANCE_DEFAULT);
NotificationChannel notificationChannel = new NotificationChannel(STEPS_CHANNEL_ID, "steps_notification_channel", NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(notificationChannel);
Notification notification_build = b.setChannelId(STEPS_CHANNEL_ID).build();
notification_build.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
this.startForeground(2001, notification_build, ServiceInfo.FOREGROUND_SERVICE_TYPE_HEALTH);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(STEPS_CHANNEL_ID, "steps_notification_channel", NotificationManager.IMPORTANCE_DEFAULT);
NotificationChannel notificationChannel = new NotificationChannel(STEPS_CHANNEL_ID, "steps_notification_channel", NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(notificationChannel);
Notification notification_build = b.setChannelId(STEPS_CHANNEL_ID).build();
notification_build.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
this.startForeground(2001, notification_build);
} else {
Notification notification_build = b.build();
notification_build.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
this.startForeground(2001, notification_build);
}
}
new Thread(
() -> {
while (true) {
sendSteps();
try {
Thread.sleep(900000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
).start();
/** Old Implementation */
// new Thread(
// () -> {
// while (true) {
// sendSteps();
// try {
// Thread.sleep(900000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// }
// ).start();
/** New Implementation */
// Use Handler instead of Thread for periodic tasks
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
sendSteps();
handler.postDelayed(this, 900000);
}
};
handler.post(runnable);
// return super.onStartCommand(intent, flags, startId);
return Service.START_STICKY;
}
......@@ -158,22 +171,6 @@ public class WarplyHealthService extends Service implements SensorEventListener
sendSteps();
}
// @Override
// public void onTaskRemoved(Intent rootIntent) {
// Intent restartServiceIntent = new Intent(getApplicationContext(), WarplyHealthService.class);
// restartServiceIntent.setPackage(getPackageName());
//
// PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 2002, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
// AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
// alarmService.set(
// AlarmManager.ELAPSED_REALTIME,
// SystemClock.elapsedRealtime() + 1000,
// restartServicePendingIntent
// );
//
// super.onTaskRemoved(rootIntent);
// }
@Nullable
@Override
public IBinder onBind(Intent intent) {
......