diff --git a/android/app/src/main/java/com/mattermost/rnbeta/CustomPushNotification.java b/android/app/src/main/java/com/mattermost/rnbeta/CustomPushNotification.java index 625a4f769..368daa971 100644 --- a/android/app/src/main/java/com/mattermost/rnbeta/CustomPushNotification.java +++ b/android/app/src/main/java/com/mattermost/rnbeta/CustomPushNotification.java @@ -120,7 +120,7 @@ public class CustomPushNotification extends PushNotification { protected Notification.Builder getNotificationBuilder(PendingIntent intent) { final Resources res = mContext.getResources(); String packageName = mContext.getPackageName(); - NotificationPreferencesModule notificationPreferences = NotificationPreferencesModule.getInstance(); + NotificationPreferences notificationPreferences = NotificationPreferences.getInstance(mContext); // First, get a builder initialized with defaults from the core class. final Notification.Builder notification = new Notification.Builder(mContext); diff --git a/android/app/src/main/java/com/mattermost/rnbeta/NotificationPreferences.java b/android/app/src/main/java/com/mattermost/rnbeta/NotificationPreferences.java new file mode 100644 index 000000000..4bfd1f9c0 --- /dev/null +++ b/android/app/src/main/java/com/mattermost/rnbeta/NotificationPreferences.java @@ -0,0 +1,57 @@ +package com.mattermost.rnbeta; + +import android.content.Context; +import android.content.SharedPreferences; + +public class NotificationPreferences { + private static NotificationPreferences instance; + + public final String SHARED_NAME = "NotificationPreferences"; + public final String SOUND_PREF = "NotificationSound"; + public final String VIBRATE_PREF = "NotificationVibrate"; + public final String BLINK_PREF = "NotificationLights"; + + private SharedPreferences mSharedPreferences; + + private NotificationPreferences(Context context) { + mSharedPreferences = context.getSharedPreferences(SHARED_NAME, Context.MODE_PRIVATE); + } + + public static NotificationPreferences getInstance(Context context) { + if (instance == null) { + instance = new NotificationPreferences(context); + } + + return instance; + } + + public String getNotificationSound() { + return mSharedPreferences.getString(SOUND_PREF, null); + } + + public boolean getShouldVibrate() { + return mSharedPreferences.getBoolean(VIBRATE_PREF, true); + } + + public boolean getShouldBlink() { + return mSharedPreferences.getBoolean(BLINK_PREF, false); + } + + public void setNotificationSound(String soundUri) { + SharedPreferences.Editor editor = mSharedPreferences.edit(); + editor.putString(SOUND_PREF, soundUri); + editor.commit(); + } + + public void setShouldVibrate(boolean vibrate) { + SharedPreferences.Editor editor = mSharedPreferences.edit(); + editor.putBoolean(VIBRATE_PREF, vibrate); + editor.commit(); + } + + public void setShouldBlink(boolean blink) { + SharedPreferences.Editor editor = mSharedPreferences.edit(); + editor.putBoolean(BLINK_PREF, blink); + editor.commit(); + } +} diff --git a/android/app/src/main/java/com/mattermost/rnbeta/NotificationPreferencesModule.java b/android/app/src/main/java/com/mattermost/rnbeta/NotificationPreferencesModule.java index dbb42afc3..160b9c478 100644 --- a/android/app/src/main/java/com/mattermost/rnbeta/NotificationPreferencesModule.java +++ b/android/app/src/main/java/com/mattermost/rnbeta/NotificationPreferencesModule.java @@ -2,7 +2,6 @@ package com.mattermost.rnbeta; import android.app.Application; import android.content.Context; -import android.content.SharedPreferences; import android.database.Cursor; import android.media.Ringtone; import android.media.RingtoneManager; @@ -22,18 +21,13 @@ import com.facebook.react.bridge.WritableMap; public class NotificationPreferencesModule extends ReactContextBaseJavaModule { private static NotificationPreferencesModule instance; private final MainApplication mApplication; - private SharedPreferences mSharedPreferences; - - private final String SHARED_NAME = "NotificationPreferences"; - private final String SOUND_PREF = "NotificationSound"; - private final String VIBRATE_PREF = "NotificationVibrate"; - private final String BLINK_PREF = "NotificationLights"; + private NotificationPreferences mNotificationPreference; private NotificationPreferencesModule(MainApplication application, ReactApplicationContext reactContext) { super(reactContext); mApplication = application; Context context = mApplication.getApplicationContext(); - mSharedPreferences = context.getSharedPreferences(SHARED_NAME, Context.MODE_PRIVATE); + mNotificationPreference = NotificationPreferences.getInstance(context); } public static NotificationPreferencesModule getInstance(MainApplication application, ReactApplicationContext reactContext) { @@ -76,9 +70,9 @@ public class NotificationPreferencesModule extends ReactContextBaseJavaModule { Uri defaultUri = RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_NOTIFICATION); result.putString("defaultUri", Uri.decode(defaultUri.toString())); - result.putString("selectedUri", getNotificationSound()); - result.putBoolean("shouldVibrate", getShouldVibrate()); - result.putBoolean("shouldBlink", getShouldBlink()); + result.putString("selectedUri", mNotificationPreference.getNotificationSound()); + result.putBoolean("shouldVibrate", mNotificationPreference.getShouldVibrate()); + result.putBoolean("shouldBlink", mNotificationPreference.getShouldBlink()); result.putArray("sounds", sounds); promise.resolve(result); @@ -97,34 +91,16 @@ public class NotificationPreferencesModule extends ReactContextBaseJavaModule { @ReactMethod public void setNotificationSound(String soundUri) { - SharedPreferences.Editor editor = mSharedPreferences.edit(); - editor.putString(SOUND_PREF, soundUri); - editor.commit(); + mNotificationPreference.setNotificationSound(soundUri); } @ReactMethod public void setShouldVibrate(boolean vibrate) { - SharedPreferences.Editor editor = mSharedPreferences.edit(); - editor.putBoolean(VIBRATE_PREF, vibrate); - editor.commit(); + mNotificationPreference.setShouldVibrate(vibrate); } @ReactMethod - public void setShouldBlink(boolean vibrate) { - SharedPreferences.Editor editor = mSharedPreferences.edit(); - editor.putBoolean(BLINK_PREF, vibrate); - editor.commit(); - } - - public String getNotificationSound() { - return mSharedPreferences.getString(SOUND_PREF, null); - } - - public boolean getShouldVibrate() { - return mSharedPreferences.getBoolean(VIBRATE_PREF, true); - } - - public boolean getShouldBlink() { - return mSharedPreferences.getBoolean(BLINK_PREF, false); + public void setShouldBlink(boolean blink) { + mNotificationPreference.setShouldBlink(blink); } } diff --git a/fastlane/Fastfile b/fastlane/Fastfile index 67f4f3068..f381bacf6 100644 --- a/fastlane/Fastfile +++ b/fastlane/Fastfile @@ -423,6 +423,12 @@ platform :android do new_string: 'package com.mattermost.rn;' ) + find_replace_string( + path_to_file: './android/app/src/main/java/com/mattermost/rn/NotificationPreferences.java', + old_string: 'package com.mattermost.rnbeta;', + new_string: 'package com.mattermost.rn;' + ) + find_replace_string( path_to_file: './android/app/src/main/java/com/mattermost/rn/MattermostManagedModule.java', old_string: 'package com.mattermost.rnbeta;',