Notification for API 21 and above + cleanup

This commit is contained in:
Oizaro 2020-09-07 16:02:01 +02:00
parent aee60e4d91
commit a0ffb9846b
4 changed files with 24 additions and 73 deletions

View File

@ -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<ActivityManager.RunningAppProcessInfo> 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;
}
}

View File

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

View File

@ -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();
}

View File

@ -27,7 +27,6 @@ public class SettingsDashboardActivity extends AbstractDashboardActivity {
public SettingsDashboardActivity() {
preferencesResource = R.xml.preferences_start;
addCondition(Conditions.GCM_BATTERY_OPTIMIZATIONS);
}
@Override