From a0ffb9846bdbdeb7a29d83a8fe86749c86ef2c0f Mon Sep 17 00:00:00 2001 From: Oizaro <45825534+Oizaro@users.noreply.github.com> Date: Mon, 7 Sep 2020 16:02:01 +0200 Subject: [PATCH] Notification for API 21 and above + cleanup --- .../gms/common/ForegroundServiceContext.java | 33 +++++++----- .../java/org/microg/gms/gcm/McsService.java | 11 ++-- .../java/org/microg/gms/ui/Conditions.java | 52 ------------------- .../gms/ui/SettingsDashboardActivity.java | 1 - 4 files changed, 24 insertions(+), 73 deletions(-) delete mode 100755 play-services-core/src/main/java/org/microg/gms/ui/Conditions.java diff --git a/play-services-core/src/main/java/org/microg/gms/common/ForegroundServiceContext.java b/play-services-core/src/main/java/org/microg/gms/common/ForegroundServiceContext.java index d8e1fe3b..63257576 100644 --- a/play-services-core/src/main/java/org/microg/gms/common/ForegroundServiceContext.java +++ b/play-services-core/src/main/java/org/microg/gms/common/ForegroundServiceContext.java @@ -2,6 +2,8 @@ package org.microg.gms.common; import android.app.ActivityManager; import android.app.Notification; +import android.app.NotificationChannel; +import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.ComponentName; @@ -13,7 +15,7 @@ import android.os.PowerManager; import android.provider.Settings; import android.util.Log; -import androidx.annotation.RequiresApi; +import androidx.core.app.NotificationCompat; import com.mgoogle.android.gms.R; @@ -29,20 +31,20 @@ public class ForegroundServiceContext extends ContextWrapper { @Override public ComponentName startService(Intent service) { - if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !isIgnoringBatteryOptimizations() && !isAppOnForeground()) { + PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE); + + if (!powerManager.isPowerSaveMode() && !isAppOnForeground()) { Log.d(TAG, "Starting in foreground mode."); service.putExtra(EXTRA_FOREGROUND, true); - return super.startForegroundService(service); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + return super.startForegroundService(service); + } else { + return super.startService(service); + } } return super.startService(service); } - @RequiresApi(api = Build.VERSION_CODES.M) - private boolean isIgnoringBatteryOptimizations() { - PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE); - return powerManager.isIgnoringBatteryOptimizations(getPackageName()); - } - private boolean isAppOnForeground() { ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); List appProcesses = activityManager.getRunningAppProcesses(); @@ -70,12 +72,19 @@ public class ForegroundServiceContext extends ContextWrapper { mIntent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS); mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, mIntent, 0); - return new Notification.Builder(context) + + NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, "foreground-service") .setOngoing(true) .setContentIntent(pendingIntent) .setContentTitle(context.getResources().getString(R.string.notification_service_title)) .setContentText(context.getResources().getString(R.string.notification_service_content)) - .setSmallIcon(R.drawable.ic_foreground_notification) - .build(); + .setSmallIcon(R.drawable.ic_foreground_notification); + + NotificationManager notificationManager = + (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); + + notificationManager.notify(0, notificationBuilder.build()); + + return null; } } diff --git a/play-services-core/src/main/java/org/microg/gms/gcm/McsService.java b/play-services-core/src/main/java/org/microg/gms/gcm/McsService.java index 56c17d41..acbc1de3 100644 --- a/play-services-core/src/main/java/org/microg/gms/gcm/McsService.java +++ b/play-services-core/src/main/java/org/microg/gms/gcm/McsService.java @@ -18,6 +18,8 @@ package org.microg.gms.gcm; import android.app.AlarmManager; import android.app.Notification; +import android.app.NotificationChannel; +import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.ComponentName; @@ -38,6 +40,7 @@ import android.os.SystemClock; import android.os.UserHandle; import android.util.Log; +import androidx.core.app.NotificationCompat; import androidx.legacy.content.WakefulBroadcastReceiver; import com.mgoogle.android.gms.R; @@ -315,14 +318,6 @@ public class McsService extends Service implements Handler.Callback { return START_REDELIVER_INTENT; } - private Notification buildForegroundNotification() { - return new Notification.Builder(this) - .setCategory(Notification.CATEGORY_MESSAGE) - .setContentTitle(this.getResources().getString(R.string.notification_service_title)) - .setSmallIcon(android.R.drawable.stat_notify_sync) - .build(); - } - private void handleSendMessage(Intent intent) { String messageId = intent.getStringExtra(EXTRA_MESSAGE_ID); String collapseKey = intent.getStringExtra(EXTRA_COLLAPSE_KEY); diff --git a/play-services-core/src/main/java/org/microg/gms/ui/Conditions.java b/play-services-core/src/main/java/org/microg/gms/ui/Conditions.java deleted file mode 100755 index fa47e1be..00000000 --- a/play-services-core/src/main/java/org/microg/gms/ui/Conditions.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (C) 2013-2017 microG Project Team - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.microg.gms.ui; - -import android.content.Context; -import android.content.Intent; -import android.net.Uri; -import android.os.PowerManager; -import android.provider.Settings; - -import com.mgoogle.android.gms.R; - -import org.microg.gms.gcm.GcmPrefs; -import org.microg.tools.ui.Condition; - -import static android.os.Build.VERSION.SDK_INT; - -public class Conditions { - public static final Condition GCM_BATTERY_OPTIMIZATIONS = new Condition.Builder() - .title(R.string.cond_gcm_bat_title) - .summary(R.string.cond_gcm_bat_summary) - .evaluation(new Condition.Evaluation() { - @Override - public boolean isActive(Context context) { - if (SDK_INT < 23) return false; - if (!GcmPrefs.get(context).isEnabled()) return false; - PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); - return !pm.isIgnoringBatteryOptimizations(context.getPackageName()); - } - }) - .firstAction(R.string.cond_gcm_bat_action, v -> { - if (SDK_INT < 23) return; - Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); - intent.setData(Uri.parse("package:" + v.getContext().getPackageName())); - v.getContext().startActivity(intent); - }).build(); - -} diff --git a/play-services-core/src/main/java/org/microg/gms/ui/SettingsDashboardActivity.java b/play-services-core/src/main/java/org/microg/gms/ui/SettingsDashboardActivity.java index adc0681c..ed2fb218 100755 --- a/play-services-core/src/main/java/org/microg/gms/ui/SettingsDashboardActivity.java +++ b/play-services-core/src/main/java/org/microg/gms/ui/SettingsDashboardActivity.java @@ -27,7 +27,6 @@ public class SettingsDashboardActivity extends AbstractDashboardActivity { public SettingsDashboardActivity() { preferencesResource = R.xml.preferences_start; - addCondition(Conditions.GCM_BATTERY_OPTIMIZATIONS); } @Override