Added compatibility for Android Oreo notifications system

This commit is contained in:
Juan Miguel Boyero Corral 2017-10-25 11:00:01 +02:00
parent 8293bc1d06
commit a2c4511a99
9 changed files with 85 additions and 157 deletions

View File

@ -13,7 +13,7 @@
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -10,7 +10,7 @@ ext {
android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
buildToolsVersion "26.0.2"
dexOptions {
maxProcessCount=2

View File

@ -26,7 +26,9 @@ import android.content.Context;
import android.graphics.BitmapFactory;
import android.support.v4.app.NotificationCompat;
import es.ugr.swad.swadroid.preferences.Preferences;
import es.ugr.swad.swadroid.utils.NotificationUtils;
import static es.ugr.swad.swadroid.utils.NotificationUtils.SWADROID_CHANNEL_ID;
/**
* Class for create notification alerts.
@ -35,12 +37,12 @@ import es.ugr.swad.swadroid.preferences.Preferences;
*/
public class AlertNotificationFactory {
public static NotificationCompat.Builder createAlertNotificationBuilder(Context context, String contentTitle, String contentText,
String ticker, PendingIntent pendingIntent, int smallIcon, int largeIcon, boolean alertSignals,
String ticker, PendingIntent pendingIntent, int smallIcon, int largeIcon,
boolean autocancel, boolean ongoing, boolean onlyAlertOnce) {
int flags = 0;
NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(context)
NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(context, SWADROID_CHANNEL_ID)
.setAutoCancel(autocancel)
.setSmallIcon(smallIcon)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), largeIcon))
@ -58,21 +60,9 @@ public class AlertNotificationFactory {
}
//Add sound, vibration and lights
if(alertSignals) {
if(Preferences.isNotifSoundEnabled()) {
flags |= Notification.DEFAULT_SOUND;
} else {
notifBuilder.setSound(null);
}
if(Preferences.isNotifVibrateEnabled()) {
flags |= Notification.DEFAULT_VIBRATE;
}
if(Preferences.isNotifLightsEnabled()) {
flags |= Notification.DEFAULT_LIGHTS;
}
}
flags |= Notification.DEFAULT_SOUND;
flags |= Notification.DEFAULT_VIBRATE;
flags |= Notification.DEFAULT_LIGHTS;
notifBuilder.setDefaults(flags);
@ -80,17 +70,16 @@ public class AlertNotificationFactory {
}
public static NotificationCompat.Builder createProgressNotificationBuilder(Context context, String contentTitle, String contentText,
String ticker, PendingIntent pendingIntent, int smallIcon, int largeIcon, boolean alertSignals,
String ticker, PendingIntent pendingIntent, int smallIcon, int largeIcon,
boolean autocancel, boolean ongoing, boolean onlyAlertOnce, int maxProgress, int progress, boolean indeterminate) {
NotificationCompat.Builder notifBuilder = createAlertNotificationBuilder(context,
NotificationCompat.Builder notifBuilder = createAlertNotificationBuilder(context,
contentTitle,
contentText,
ticker,
pendingIntent,
smallIcon,
largeIcon,
alertSignals,
largeIcon,
autocancel,
ongoing,
onlyAlertOnce);
@ -101,7 +90,7 @@ public class AlertNotificationFactory {
}
public static Notification createAlertNotification(Context context, String contentTitle, String contentText,
String ticker, PendingIntent pendingIntent, int smallIcon, int largeIcon, boolean alertSignals,
String ticker, PendingIntent pendingIntent, int smallIcon, int largeIcon,
boolean autocancel, boolean ongoing, boolean onlyAlertOnce) {
NotificationCompat.Builder notifBuilder = createAlertNotificationBuilder(context,
@ -110,8 +99,7 @@ public class AlertNotificationFactory {
ticker,
pendingIntent,
smallIcon,
largeIcon,
alertSignals,
largeIcon,
autocancel,
ongoing,
onlyAlertOnce);
@ -121,7 +109,7 @@ public class AlertNotificationFactory {
}
public static Notification createProgressNotification(Context context, String contentTitle, String contentText,
String ticker, PendingIntent pendingIntent, int smallIcon, int largeIcon, boolean alertSignals,
String ticker, PendingIntent pendingIntent, int smallIcon, int largeIcon,
boolean autocancel, boolean ongoing, boolean onlyAlertOnce, int maxProgress, int progress, boolean indeterminate) {
NotificationCompat.Builder notifBuilder = createProgressNotificationBuilder(context,
@ -130,8 +118,7 @@ public class AlertNotificationFactory {
ticker,
pendingIntent,
smallIcon,
largeIcon,
alertSignals,
largeIcon,
autocancel,
ongoing,
onlyAlertOnce,
@ -143,10 +130,17 @@ public class AlertNotificationFactory {
return notifBuilder.build();
}
public static void showAlertNotification(Context context, Notification notif, int notifId) {
//Obtain a reference to the notification service
NotificationManager notifManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
public static void showAlertNotification(Context context, Notification notif, int notifId) {
NotificationManager notifManager;
//Obtain a reference to the notification service
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationUtils mNotificationUtils = new NotificationUtils(context);
notifManager = mNotificationUtils.getManager();
} else {
notifManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}
//Send alert
notifManager.notify(notifId, notif);

View File

@ -509,7 +509,6 @@ public class Notifications extends Module implements
R.drawable.ic_launcher_swadroid_notif,
R.drawable.ic_launcher_swadroid,
true,
true,
false,
false);

View File

@ -478,7 +478,6 @@ public class NotificationsSyncAdapterService extends Service {
R.drawable.ic_launcher_swadroid_notif,
R.drawable.ic_launcher_swadroid,
true,
true,
false,
false);

View File

@ -138,18 +138,6 @@ public class Preferences {
* Last synchronization time preference name
*/
public static final String LASTSYNCTIMEPREF = "lastSyncTimeLimit";
/**
* Notifications sound enable preference name
*/
public static final String NOTIFSOUNDENABLEPREF = "prefNotifSoundEnable";
/**
* Notifications vibrate enable preference name
*/
public static final String NOTIFVIBRATEENABLEPREF = "prefNotifVibrateEnable";
/**
* Notifications lights enable preference name
*/
public static final String NOTIFLIGHTSENABLEPREF = "prefNotifLightsEnable";
/**
* Changelog preference name
*/
@ -368,68 +356,6 @@ public class Preferences {
editor.commit();
}
/**
* Checks if the sound is enabled for notification alerts
*
* @return true if the sound is enabled for notification alerts
* false otherwise
*/
public static boolean isNotifSoundEnabled() {
return prefs.getBoolean(NOTIFSOUNDENABLEPREF, true);
}
/**
* Enables or disables the sound for notification alerts
*
* @param notifSoundEnabled true if the sound is enabled for notification alerts
* false otherwise
*/
public static void setNotifSoundEnabled(boolean notifSoundEnabled) {
editor = editor.putBoolean(NOTIFSOUNDENABLEPREF, notifSoundEnabled);
editor.commit();
}
/**
* Checks if the vibration is enabled for notification alerts
*
* @return true if the vibration is enabled for notification alerts
* false otherwise
*/
public static boolean isNotifVibrateEnabled() {
return prefs.getBoolean(NOTIFVIBRATEENABLEPREF, true);
}
/**
* Enables or disables the vibration for notification alerts
*
* @param notifVibrateEnabled the notifVibrateEnabled to set
*/
public static void setNotifVibrateEnabled(boolean notifVibrateEnabled) {
editor = editor.putBoolean(NOTIFVIBRATEENABLEPREF, notifVibrateEnabled);
editor.commit();
}
/**
* Checks if the lights are enabled for notification alerts
*
* @return true if the lights are enabled for notification alerts
* false otherwise
*/
public static boolean isNotifLightsEnabled() {
return prefs.getBoolean(NOTIFLIGHTSENABLEPREF, true);
}
/**
* Enables or disables the lights for notification alerts
*
* @param notifLightsEnabled true if the lights are enabled for notification alerts
* false otherwise
*/
public static void setNotifLightsEnabled(boolean notifLightsEnabled) {
editor = editor.putBoolean(NOTIFLIGHTSENABLEPREF, notifLightsEnabled);
editor.commit();
}
/**
* Gets the login data
*

View File

@ -33,7 +33,10 @@ import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.security.NoSuchAlgorithmException;
@ -49,6 +52,7 @@ import es.ugr.swad.swadroid.modules.login.Login;
import es.ugr.swad.swadroid.modules.login.LoginActivity;
import es.ugr.swad.swadroid.sync.SyncUtils;
import es.ugr.swad.swadroid.utils.Crypto;
import es.ugr.swad.swadroid.utils.NotificationUtils;
import es.ugr.swad.swadroid.utils.Utils;
/**
@ -113,18 +117,6 @@ public class PreferencesActivity extends PreferenceActivity implements OnPrefere
* Synchronization enable preference
*/
private static CheckBoxPreference syncEnablePref;
/**
* Notifications sound enable preference
*/
private static CheckBoxPreference notifSoundEnablePref;
/**
* Notifications vibrate enable preference
*/
private static CheckBoxPreference notifVibrateEnablePref;
/**
* Notifications lights enable preference
*/
private static CheckBoxPreference notifLightsEnablePref;
/**
* Application debuggable flag
*/
@ -201,9 +193,6 @@ public class PreferencesActivity extends PreferenceActivity implements OnPrefere
privacyPolicyPref = findPreference(Preferences.PRIVACYPOLICYPREF);
syncTimePref = findPreference(Preferences.SYNCTIMEPREF);
syncEnablePref = (CheckBoxPreference) findPreference(Preferences.SYNCENABLEPREF);
notifSoundEnablePref = (CheckBoxPreference) findPreference(Preferences.NOTIFSOUNDENABLEPREF);
notifVibrateEnablePref = (CheckBoxPreference) findPreference(Preferences.NOTIFVIBRATEENABLEPREF);
notifLightsEnablePref = (CheckBoxPreference) findPreference(Preferences.NOTIFLIGHTSENABLEPREF);
ratePref.setOnPreferenceChangeListener(this);
twitterPref.setOnPreferenceChangeListener(this);
@ -215,9 +204,6 @@ public class PreferencesActivity extends PreferenceActivity implements OnPrefere
privacyPolicyPref.setOnPreferenceChangeListener(this);
syncEnablePref.setOnPreferenceChangeListener(this);
syncTimePref.setOnPreferenceChangeListener(this);
notifSoundEnablePref.setOnPreferenceChangeListener(this);
notifVibrateEnablePref.setOnPreferenceChangeListener(this);
notifLightsEnablePref.setOnPreferenceChangeListener(this);
logOutPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@ -416,18 +402,6 @@ public class PreferencesActivity extends PreferenceActivity implements OnPrefere
syncTimePref.setSummary(prefSyncTimeEntry);
syncPrefsChanged = true;
} else if(Preferences.NOTIFSOUNDENABLEPREF.equals(key)) {
boolean notifSoundEnabled = (Boolean) newValue;
Preferences.setNotifSoundEnabled(notifSoundEnabled);
notifSoundEnablePref.setChecked(notifSoundEnabled);
} else if(Preferences.NOTIFVIBRATEENABLEPREF.equals(key)) {
boolean notifVibrateEnabled = (Boolean) newValue;
Preferences.setNotifVibrateEnabled(notifVibrateEnabled);
notifVibrateEnablePref.setChecked(notifVibrateEnabled);
} else if(Preferences.NOTIFLIGHTSENABLEPREF.equals(key)) {
boolean notifLightsEnabled = (Boolean) newValue;
Preferences.setNotifLightsEnabled(notifLightsEnabled);
notifLightsEnablePref.setChecked(notifLightsEnabled);
}
return returnValue;

View File

@ -0,0 +1,53 @@
package es.ugr.swad.swadroid.utils;
/**
* Class for manage Android Oreo notifications
*
* @author Juan Miguel Boyero Corral <juanmi1982@gmail.com>
*/
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.Color;
import android.os.Build;
@TargetApi(Build.VERSION_CODES.O)
public class NotificationUtils extends ContextWrapper {
private NotificationManager mManager;
public static final String SWADROID_CHANNEL_ID = "es.ugr.swad.swadroid.SWADROID";
public static final String SWADROID_CHANNEL_NAME = "SWADROID CHANNEL";
public NotificationUtils(Context base) {
super(base);
createChannels();
}
public void createChannels() {
// create SWADroid channel
NotificationChannel androidChannel = new NotificationChannel(SWADROID_CHANNEL_ID,
SWADROID_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
// Sets whether notifications posted to this channel should display notification lights
androidChannel.enableLights(true);
// Sets whether notification posted to this channel should vibrate.
androidChannel.enableVibration(true);
// Sets the notification light color for notifications posted to this channel
androidChannel.setLightColor(Color.GREEN);
// Sets whether notifications posted to this channel appear on the lockscreen or not
androidChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
getManager().createNotificationChannel(androidChannel);
}
public NotificationManager getManager() {
if (mManager == null) {
mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
return mManager;
}
}

View File

@ -25,23 +25,6 @@
android:summary="@string/prefSyncTimeSummary"
android:title="@string/prefSyncTimeTitle" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/prefCatNotifTitle" >
<CheckBoxPreference
android:defaultValue="true"
android:key="@string/prefNotifSoundKey"
android:summary="@string/prefNotifSoundSummary"
android:title="@string/prefNotifSoundTitle" />
<CheckBoxPreference
android:defaultValue="true"
android:key="@string/prefNotifVibrateKey"
android:summary="@string/prefNotifVibrateSummary"
android:title="@string/prefNotifVibrateTitle" />
<CheckBoxPreference
android:defaultValue="true"
android:key="@string/prefNotifLightsKey"
android:summary="@string/prefNotifLightsSummary"
android:title="@string/prefNotifLightsTitle" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/about_preferences" >
<Preference
android:defaultValue=""