From fdb935a3ff13ca38fb5e4f774bfc8506daece0a1 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 19 Jul 2018 11:06:36 +0200 Subject: [PATCH 1/9] First, improve code --- .../activity/NotificationPrivacyActivity.java | 168 ++++++++---------- 1 file changed, 77 insertions(+), 91 deletions(-) diff --git a/vector/src/main/java/im/vector/activity/NotificationPrivacyActivity.java b/vector/src/main/java/im/vector/activity/NotificationPrivacyActivity.java index 7470042f2e..4ec79eff2b 100644 --- a/vector/src/main/java/im/vector/activity/NotificationPrivacyActivity.java +++ b/vector/src/main/java/im/vector/activity/NotificationPrivacyActivity.java @@ -22,7 +22,6 @@ import android.net.Uri; import android.os.Build; import android.provider.Settings; -import android.support.v7.widget.Toolbar; import android.view.View; import android.widget.RadioButton; import android.widget.TextView; @@ -30,6 +29,7 @@ import org.jetbrains.annotations.NotNull; import butterknife.BindView; +import butterknife.OnClick; import im.vector.Matrix; import im.vector.R; import im.vector.gcm.GcmRegistrationManager; @@ -40,25 +40,20 @@ * The interest is to educate the user on the impacts of his choice of the type of notifications * on the privacy policy of his data. */ -public class NotificationPrivacyActivity extends RiotAppCompatActivity { +public class NotificationPrivacyActivity extends RiotAppCompatActivity { private static final String LOG_TAG = NotificationPrivacyActivity.class.getSimpleName(); + /* ========================================================================================== + * UI + * ========================================================================================== */ + @BindView(R.id.tv_apps_needs_permission) TextView tvNeedPermission; @BindView(R.id.tv_apps_no_permission) TextView tvNoPermission; - @BindView(R.id.rly_normal_notification_privacy) - View rlyNormalPrivacy; - - @BindView(R.id.rly_low_detail_notifications) - View rlyLowDetailNotifications; - - @BindView(R.id.rly_reduced_privacy_notifications) - View rlyReducedPrivacy; - @BindView(R.id.rb_normal_notification_privacy) RadioButton rbPrivacyNormal; @@ -68,18 +63,9 @@ public class NotificationPrivacyActivity extends RiotAppCompatActivity { @BindView(R.id.rb_notification_reduce_privacy) RadioButton rbPrivacyReduced; - @BindView(R.id.tv_normal_notification_privacy) - TextView tvPrivacyNormal; - - @BindView(R.id.tv_notification_low_detail) - TextView tvPrivacyLowDetail; - - @BindView(R.id.tv_notification_reduce_privacy) - TextView tvPrivacyReduced; - - public static Intent getIntent(final Context context) { - return new Intent(context, NotificationPrivacyActivity.class); - } + /* ========================================================================================== + * LifeCycle + * ========================================================================================== */ @NotNull @Override @@ -99,90 +85,88 @@ public int getTitleRes() { @Override public void initUiAndData() { - Toolbar toolbar = findViewById(R.id.toolbar); - if (toolbar != null) { - setSupportActionBar(toolbar); - if (getSupportActionBar() != null) { - getSupportActionBar().setDisplayShowHomeEnabled(true); - getSupportActionBar().setDisplayHomeAsUpEnabled(true); - } - } + configureToolbar(); // The permission request is only necessary for devices os versions greater than API 23 (M) - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { tvNeedPermission.setVisibility(View.VISIBLE); tvNoPermission.setVisibility(View.VISIBLE); - } else{ + } else { tvNeedPermission.setVisibility(View.GONE); tvNoPermission.setVisibility(View.GONE); } - - refreshNotificationPrivacy(); - - rlyNormalPrivacy.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - setNotificationPrivacy(NotificationPrivacyActivity.this, GcmRegistrationManager.NotificationPrivacy.NORMAL); - refreshNotificationPrivacy(); - } - }); - - rlyLowDetailNotifications.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - setNotificationPrivacy(NotificationPrivacyActivity.this, GcmRegistrationManager.NotificationPrivacy.LOW_DETAIL); - refreshNotificationPrivacy(); - } - }); - - rlyReducedPrivacy.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - setNotificationPrivacy(NotificationPrivacyActivity.this, GcmRegistrationManager.NotificationPrivacy.REDUCED); - refreshNotificationPrivacy(); - } - }); } @Override protected void onResume() { super.onResume(); + + refreshNotificationPrivacy(); + } + + /* ========================================================================================== + * UI Event + * ========================================================================================== */ + + @OnClick(R.id.rly_normal_notification_privacy) + void onNormalClick() { + updateNotificationPrivacy(GcmRegistrationManager.NotificationPrivacy.NORMAL); + } + + @OnClick(R.id.rly_low_detail_notifications) + void onLowDetailClick() { + updateNotificationPrivacy(GcmRegistrationManager.NotificationPrivacy.LOW_DETAIL); + } + + @OnClick(R.id.rly_reduced_privacy_notifications) + void onReducedPrivacyClick() { + updateNotificationPrivacy(GcmRegistrationManager.NotificationPrivacy.REDUCED); + } + + /* ========================================================================================== + * Private + * ========================================================================================== */ + + private void updateNotificationPrivacy(GcmRegistrationManager.NotificationPrivacy newNotificationPrivacy) { + setNotificationPrivacy(this, newNotificationPrivacy); refreshNotificationPrivacy(); } private void refreshNotificationPrivacy() { - GcmRegistrationManager gcmRegistrationManager = Matrix.getInstance(this).getSharedGCMRegistrationManager(); + GcmRegistrationManager.NotificationPrivacy notificationPrivacy = Matrix.getInstance(this) + .getSharedGCMRegistrationManager() + .getNotificationPrivacy(); - switch (gcmRegistrationManager.getNotificationPrivacy()) { - case REDUCED: - rbPrivacyNormal.setChecked(false); - rbPrivacyLowDetail.setChecked(false); - rbPrivacyReduced.setChecked(true); - break; - case LOW_DETAIL: - rbPrivacyNormal.setChecked(false); - rbPrivacyLowDetail.setChecked(true); - rbPrivacyReduced.setChecked(false); - break; - case NORMAL: - rbPrivacyNormal.setChecked(true); - rbPrivacyLowDetail.setChecked(false); - rbPrivacyReduced.setChecked(false); - break; - } + rbPrivacyNormal.setChecked(notificationPrivacy == GcmRegistrationManager.NotificationPrivacy.NORMAL); + rbPrivacyLowDetail.setChecked(notificationPrivacy == GcmRegistrationManager.NotificationPrivacy.LOW_DETAIL); + rbPrivacyReduced.setChecked(notificationPrivacy == GcmRegistrationManager.NotificationPrivacy.REDUCED); + } + + /* ========================================================================================== + * Public static + * ========================================================================================== */ + + /** + * Return an intent to start this Activity + * + * @param context Android context + * @return an intent to start this Activity + */ + public static Intent getIntent(final Context context) { + return new Intent(context, NotificationPrivacyActivity.class); } /** * Set the new notification privacy setting. * - * @param activity the activity from which to display the IgnoreBatteryOptimizations permission request dialog, if required + * @param activity the activity from which to display the IgnoreBatteryOptimizations permission request dialog, if required * @param notificationPrivacy the new setting */ - static public void setNotificationPrivacy(Activity activity, GcmRegistrationManager.NotificationPrivacy notificationPrivacy) { - GcmRegistrationManager gcmRegistrationManager = Matrix.getInstance(activity).getSharedGCMRegistrationManager(); - + public static void setNotificationPrivacy(Activity activity, GcmRegistrationManager.NotificationPrivacy notificationPrivacy) { // first, set the new privacy setting - gcmRegistrationManager.setNotificationPrivacy(notificationPrivacy); + Matrix.getInstance(activity) + .getSharedGCMRegistrationManager() + .setNotificationPrivacy(notificationPrivacy); // for the "NORMAL" privacy, the app needs to be able to run in background // this requires the IgnoreBatteryOptimizations permission from android M @@ -201,25 +185,27 @@ static public void setNotificationPrivacy(Activity activity, GcmRegistrationMana /** * Get the displayed i18ned string for a notification privacy setting. - * - * @param context + * + * @param context Android context * @param notificationPrivacy the setting to stringify * @return a string */ - static public String getNotificationPrivacyString(Context context, GcmRegistrationManager.NotificationPrivacy notificationPrivacy) { - String notificationPrivacyString = null; + public static String getNotificationPrivacyString(Context context, GcmRegistrationManager.NotificationPrivacy notificationPrivacy) { + int stringRes; switch (notificationPrivacy) { case REDUCED: - notificationPrivacyString = context.getString(R.string.settings_notification_privacy_reduced); + stringRes = R.string.settings_notification_privacy_reduced; break; case LOW_DETAIL: - notificationPrivacyString = context.getString(R.string.settings_notification_privacy_low_detail); + stringRes = R.string.settings_notification_privacy_low_detail; break; case NORMAL: - notificationPrivacyString = context.getString(R.string.settings_notification_privacy_normal); - break;} + default: + stringRes = R.string.settings_notification_privacy_normal; + break; + } - return notificationPrivacyString; + return context.getString(stringRes); } } From a2c54f19e2e2081f058e0df391c1cf89e7a6cfe1 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 19 Jul 2018 15:01:23 +0200 Subject: [PATCH 2/9] setStoredRegistrationState() now return void --- .../im/vector/gcm/GcmRegistrationManager.java | 270 +++++++++--------- 1 file changed, 132 insertions(+), 138 deletions(-) diff --git a/vector/src/main/java/im/vector/gcm/GcmRegistrationManager.java b/vector/src/main/java/im/vector/gcm/GcmRegistrationManager.java index 578262632a..85d612e43a 100755 --- a/vector/src/main/java/im/vector/gcm/GcmRegistrationManager.java +++ b/vector/src/main/java/im/vector/gcm/GcmRegistrationManager.java @@ -131,7 +131,7 @@ private enum RegistrationState { private final Context mContext; // the registration state - private RegistrationState mRegistrationState = RegistrationState.UNREGISTRATED; + private RegistrationState mRegistrationState; // defines the GCM registration token private String mRegistrationToken = null; @@ -246,7 +246,7 @@ private void onGCMUnregistred() { mRegistrationToken = null; // reset the registration state - mRegistrationState = setStoredRegistrationState(RegistrationState.UNREGISTRATED); + setAndStoreRegistrationState(RegistrationState.UNREGISTRATED); // try again checkRegistrations(); } @@ -332,7 +332,7 @@ private void registerToGCM(final GCMRegistrationListener gcmRegistrationListener return; } if (mRegistrationState == RegistrationState.UNREGISTRATED) { - mRegistrationState = setStoredRegistrationState(RegistrationState.GCM_REGISTRATING); + setAndStoreRegistrationState(RegistrationState.GCM_REGISTRATING); try { new AsyncTask() { @@ -349,8 +349,7 @@ protected String doInBackground(Void... voids) { @Override protected void onPostExecute(String pushKey) { - mRegistrationState - = setStoredRegistrationState(((pushKey != null) ? RegistrationState.GCM_REGISTRED : RegistrationState.UNREGISTRATED)); + setAndStoreRegistrationState(((pushKey != null) ? RegistrationState.GCM_REGISTRED : RegistrationState.UNREGISTRATED)); setStoredRegistrationToken(pushKey); // warn the listener @@ -386,9 +385,10 @@ protected void onPostExecute(String pushKey) { Log.e(LOG_TAG, "registerToGCM : onPusherRegistered/onPusherRegistrationFailed failed " + e2.getMessage(), e2); } } - mRegistrationState = setStoredRegistrationState(RegistrationState.UNREGISTRATED); + setAndStoreRegistrationState(RegistrationState.UNREGISTRATED); } } else if (mRegistrationState == RegistrationState.GCM_REGISTRATING) { + // Please wait gcmRegistrationListener.onGCMRegistrationFailed(); } else { gcmRegistrationListener.onGCMRegistered(); @@ -501,7 +501,7 @@ public void onThirdPartyUnregistered() { Log.d(LOG_TAG, "500 error : onThirdPartyUnregistered"); setStoredRegistrationToken(null); - mRegistrationState = setStoredRegistrationState(RegistrationState.UNREGISTRATED); + setAndStoreRegistrationState(RegistrationState.UNREGISTRATED); register(null); } @@ -510,7 +510,7 @@ public void onThirdPartyUnregistrationFailed() { Log.d(LOG_TAG, "500 error : onThirdPartyUnregistrationFailed"); setStoredRegistrationToken(null); - mRegistrationState = setStoredRegistrationState(RegistrationState.UNREGISTRATED); + setAndStoreRegistrationState(RegistrationState.UNREGISTRATED); register(null); } }); @@ -519,7 +519,7 @@ public void onThirdPartyUnregistrationFailed() { Log.d(LOG_TAG, "500 error : no GCM key"); setStoredRegistrationToken(null); - mRegistrationState = setStoredRegistrationState(RegistrationState.UNREGISTRATED); + setAndStoreRegistrationState(RegistrationState.UNREGISTRATED); register(null); } } @@ -587,7 +587,7 @@ private void registerToThirdPartyServer(final MXSession session, final boolean a // fallback to the GCM_REGISTRED state // thus, the client will try again to register with checkRegistrations. - mRegistrationState = setStoredRegistrationState(RegistrationState.GCM_REGISTRED); + setAndStoreRegistrationState(RegistrationState.GCM_REGISTRED); return; } @@ -599,69 +599,75 @@ private void registerToThirdPartyServer(final MXSession session, final boolean a // - or, if the app cannot run in background, the user does not want to send event content to GCM boolean eventIdOnlyPushes = isBackgroundSyncAllowed() || !isContentSendingAllowed(); - getPushersRestClient(session) - .addHttpPusher(mRegistrationToken, DEFAULT_PUSHER_APP_ID, computePushTag(session), - mPusherLang, mPusherAppName, mBasePusherDeviceName, - DEFAULT_PUSHER_URL, append, eventIdOnlyPushes, new ApiCallback() { - @Override - public void onSuccess(Void info) { - Log.d(LOG_TAG, "registerToThirdPartyServer succeeded"); - - if (null != listener) { - try { - listener.onThirdPartyRegistered(); - } catch (Exception e) { - Log.e(LOG_TAG, "onSessionRegistered failed " + e.getMessage(), e); - } - } + getPushersRestClient(session).addHttpPusher(mRegistrationToken, + DEFAULT_PUSHER_APP_ID, + computePushTag(session), + mPusherLang, + mPusherAppName, + mBasePusherDeviceName, + DEFAULT_PUSHER_URL, + append, + eventIdOnlyPushes, + new ApiCallback() { + @Override + public void onSuccess(Void info) { + Log.d(LOG_TAG, "registerToThirdPartyServer succeeded"); + + if (null != listener) { + try { + listener.onThirdPartyRegistered(); + } catch (Exception e) { + Log.e(LOG_TAG, "onSessionRegistered failed " + e.getMessage(), e); } + } + } - private void onError(final String message) { - Log.e(LOG_TAG, "registerToThirdPartyServer failed" + session.getMyUserId() + " (" + message + ")"); + private void onError(final String message) { + Log.e(LOG_TAG, "registerToThirdPartyServer failed" + session.getMyUserId() + " (" + message + ")"); - // fallback to the GCM_REGISTRED state - // thus, the client will try again to register with checkRegistrations. - mRegistrationState = setStoredRegistrationState(RegistrationState.GCM_REGISTRED); + // fallback to the GCM_REGISTRED state + // thus, the client will try again to register with checkRegistrations. + setAndStoreRegistrationState(RegistrationState.GCM_REGISTRED); - if (null != listener) { - try { - listener.onThirdPartyRegistrationFailed(); - } catch (Exception e) { - Log.e(LOG_TAG, "onThirdPartyRegistrationFailed failed " + e.getMessage(), e); - } - } + if (null != listener) { + try { + listener.onThirdPartyRegistrationFailed(); + } catch (Exception e) { + Log.e(LOG_TAG, "onThirdPartyRegistrationFailed failed " + e.getMessage(), e); } + } + } + @Override + public void onNetworkError(Exception e) { + Log.e(LOG_TAG, "registerToThirdPartyServer onNetworkError " + e.getMessage(), e); + new Handler(Looper.getMainLooper()).postDelayed(new Runnable() { @Override - public void onNetworkError(Exception e) { - Log.e(LOG_TAG, "registerToThirdPartyServer onNetworkError " + e.getMessage(), e); - new Handler(Looper.getMainLooper()).postDelayed(new Runnable() { - @Override - public void run() { - if (mRegistrationState == RegistrationState.SERVER_REGISTRATING) { - Log.e(LOG_TAG, "registerToThirdPartyServer onNetworkError -> retry"); - registerToThirdPartyServer(session, append, listener); - } - } - }, 30 * 1000); + public void run() { + if (mRegistrationState == RegistrationState.SERVER_REGISTRATING) { + Log.e(LOG_TAG, "registerToThirdPartyServer onNetworkError -> retry"); + registerToThirdPartyServer(session, append, listener); + } } + }, 30 * 1000); + } - @Override - public void onMatrixError(MatrixError e) { - Log.e(LOG_TAG, "registerToThirdPartyServer onMatrixError " + e.errcode); - onError(e.getMessage()); + @Override + public void onMatrixError(MatrixError e) { + Log.e(LOG_TAG, "registerToThirdPartyServer onMatrixError " + e.errcode); + onError(e.getMessage()); - if (MatrixError.UNKNOWN.equals(e.errcode)) { - manage500Error(); - } - } + if (MatrixError.UNKNOWN.equals(e.errcode)) { + manage500Error(); + } + } - @Override - public void onUnexpectedError(Exception e) { - Log.e(LOG_TAG, "registerToThirdPartyServer onUnexpectedError " + e.getMessage(), e); - onError(e.getMessage()); - } - }); + @Override + public void onUnexpectedError(Exception e) { + Log.e(LOG_TAG, "registerToThirdPartyServer onUnexpectedError " + e.getMessage(), e); + onError(e.getMessage()); + } + }); } /** @@ -728,7 +734,7 @@ public void onUnexpectedError(Exception e) { */ public void forceSessionsRegistration(final ThirdPartyRegistrationListener listener) { if ((mRegistrationState == RegistrationState.SERVER_REGISTERED) || (mRegistrationState == RegistrationState.GCM_REGISTRED)) { - mRegistrationState = setStoredRegistrationState(RegistrationState.GCM_REGISTRED); + setAndStoreRegistrationState(RegistrationState.GCM_REGISTRED); register(listener); } else { @@ -783,7 +789,7 @@ public void onGCMRegistrationFailed() { } else { // check if the notifications must be displayed if (useGCM() && areDeviceNotificationsAllowed() && !TextUtils.isEmpty(mRegistrationToken)) { - mRegistrationState = setStoredRegistrationState(RegistrationState.SERVER_REGISTRATING); + setAndStoreRegistrationState(RegistrationState.SERVER_REGISTRATING); registerToThirdPartyServer(new ArrayList<>(Matrix.getInstance(mContext).getSessions()), 0); } else { dispatchOnThirdPartyRegistrationFailed(); @@ -801,7 +807,7 @@ private void registerToThirdPartyServer(final ArrayList sessions, fin // reach this end of the list ? if (index >= sessions.size()) { Log.d(LOG_TAG, "registerSessions : all the sessions are registered"); - mRegistrationState = setStoredRegistrationState(RegistrationState.SERVER_REGISTERED); + setAndStoreRegistrationState(RegistrationState.SERVER_REGISTERED); dispatchOnThirdPartyRegistered(); // get the pushers list @@ -831,7 +837,7 @@ public void onThirdPartyRegistered() { public void onThirdPartyRegistrationFailed() { Log.d(LOG_TAG, "registerSessions : onSessionRegistrationFailed " + session.getMyUserId()); - mRegistrationState = setStoredRegistrationState(RegistrationState.GCM_REGISTRED); + setAndStoreRegistrationState(RegistrationState.GCM_REGISTRED); dispatchOnThirdPartyRegistrationFailed(); } @@ -861,7 +867,7 @@ public void unregister(final ThirdPartyRegistrationListener listener) { Log.e(LOG_TAG, "unregisterSessions : invalid state " + mRegistrationState); dispatchOnThirdPartyUnregistrationFailed(); } else { - mRegistrationState = setStoredRegistrationState(RegistrationState.SERVER_UNREGISTRATING); + setAndStoreRegistrationState(RegistrationState.SERVER_UNREGISTRATING); unregister(new ArrayList<>(Matrix.getInstance(mContext).getSessions()), 0); } } @@ -875,7 +881,7 @@ public void unregister(final ThirdPartyRegistrationListener listener) { private void unregister(final ArrayList sessions, final int index) { // reach this end of the list ? if (index >= sessions.size()) { - mRegistrationState = setStoredRegistrationState(RegistrationState.GCM_REGISTRED); + setAndStoreRegistrationState(RegistrationState.GCM_REGISTRED); // trigger a registration if the user disabled them while the unregistration was processing if (useGCM() && areDeviceNotificationsAllowed() && Matrix.hasValidSessions()) { @@ -906,7 +912,7 @@ public void onThirdPartyUnregistered() { @Override public void onThirdPartyUnregistrationFailed() { - mRegistrationState = setStoredRegistrationState(RegistrationState.SERVER_REGISTERED); + setAndStoreRegistrationState(RegistrationState.SERVER_REGISTERED); dispatchOnThirdPartyUnregistrationFailed(); } }); @@ -926,20 +932,13 @@ public void unregister(final MXSession session, final Pusher pusher, final ApiCa pusher.appDisplayName, pusher.deviceDisplayName, pusher.data.get("url"), - new ApiCallback() { + new SimpleApiCallback(callback) { @Override public void onSuccess(Void info) { mPushersRestClients.remove(session.getMyUserId()); refreshPushersList(new ArrayList<>(Matrix.getInstance(mContext).getSessions()), callback); } - @Override - public void onNetworkError(Exception e) { - if (null != callback) { - callback.onNetworkError(e); - } - } - @Override public void onMatrixError(MatrixError e) { if (e.mStatus == 404) { @@ -948,16 +947,8 @@ public void onMatrixError(MatrixError e) { onSuccess(null); return; } - if (null != callback) { - callback.onMatrixError(e); - } - } - @Override - public void onUnexpectedError(Exception e) { - if (null != callback) { - callback.onUnexpectedError(e); - } + super.onMatrixError(e); } }); } @@ -971,60 +962,64 @@ public void onUnexpectedError(Exception e) { public void unregister(final MXSession session, final ThirdPartyRegistrationListener listener) { Log.d(LOG_TAG, "unregister " + session.getMyUserId()); - getPushersRestClient(session) - .removeHttpPusher(mRegistrationToken, DEFAULT_PUSHER_APP_ID, computePushTag(session), - mPusherLang, mPusherAppName, mBasePusherDeviceName, - DEFAULT_PUSHER_URL, new ApiCallback() { - @Override - public void onSuccess(Void info) { - Log.d(LOG_TAG, "unregisterSession succeeded"); - - if (null != listener) { - try { - listener.onThirdPartyUnregistered(); - } catch (Exception e) { - Log.e(LOG_TAG, "unregister : onThirdPartyUnregistered " + e.getMessage(), e); - } - } - } + getPushersRestClient(session).removeHttpPusher(mRegistrationToken, + DEFAULT_PUSHER_APP_ID, + computePushTag(session), + mPusherLang, + mPusherAppName, + mBasePusherDeviceName, + DEFAULT_PUSHER_URL, + new ApiCallback() { + @Override + public void onSuccess(Void info) { + Log.d(LOG_TAG, "unregisterSession succeeded"); - private void onError(final String message) { - if (session.isAlive()) { - Log.e(LOG_TAG, "fail to unregister " + session.getMyUserId() + " (" + message + ")"); - - if (null != listener) { - try { - listener.onThirdPartyUnregistrationFailed(); - } catch (Exception e) { - Log.e(LOG_TAG, "unregister : onThirdPartyUnregistrationFailed " + e.getMessage(), e); - } - } - } + if (null != listener) { + try { + listener.onThirdPartyUnregistered(); + } catch (Exception e) { + Log.e(LOG_TAG, "unregister : onThirdPartyUnregistered " + e.getMessage(), e); } + } + } - @Override - public void onNetworkError(Exception e) { - Log.e(LOG_TAG, "unregisterSession onNetworkError " + e.getMessage(), e); - onError(e.getMessage()); - } + private void onError(final String message) { + if (session.isAlive()) { + Log.e(LOG_TAG, "fail to unregister " + session.getMyUserId() + " (" + message + ")"); - @Override - public void onMatrixError(MatrixError e) { - if (e.mStatus == 404) { - // httpPusher is not available on server side anymore so assume the removal was successful - onSuccess(null); - return; + if (null != listener) { + try { + listener.onThirdPartyUnregistrationFailed(); + } catch (Exception e) { + Log.e(LOG_TAG, "unregister : onThirdPartyUnregistrationFailed " + e.getMessage(), e); } - Log.e(LOG_TAG, "unregisterSession onMatrixError " + e.errcode); - onError(e.getMessage()); } + } + } - @Override - public void onUnexpectedError(Exception e) { - Log.e(LOG_TAG, "unregisterSession onUnexpectedError " + e.getMessage(), e); - onError(e.getMessage()); - } - }); + @Override + public void onNetworkError(Exception e) { + Log.e(LOG_TAG, "unregisterSession onNetworkError " + e.getMessage(), e); + onError(e.getMessage()); + } + + @Override + public void onMatrixError(MatrixError e) { + if (e.mStatus == 404) { + // httpPusher is not available on server side anymore so assume the removal was successful + onSuccess(null); + return; + } + Log.e(LOG_TAG, "unregisterSession onMatrixError " + e.errcode); + onError(e.getMessage()); + } + + @Override + public void onUnexpectedError(Exception e) { + Log.e(LOG_TAG, "unregisterSession onUnexpectedError " + e.getMessage(), e); + onError(e.getMessage()); + } + }); } //================================================================================ @@ -1395,9 +1390,10 @@ private RegistrationState getStoredRegistrationState() { * Update the stored registration state. * * @param state the new state - * @return the the nex state */ - private RegistrationState setStoredRegistrationState(RegistrationState state) { + private void setAndStoreRegistrationState(RegistrationState state) { + mRegistrationState = state; + // do not store the .ing state if ((RegistrationState.GCM_REGISTRATING != state) && (RegistrationState.SERVER_REGISTRATING != state) && @@ -1408,8 +1404,6 @@ private RegistrationState setStoredRegistrationState(RegistrationState state) { .putInt(PREFS_PUSHER_REGISTRATION_STATUS, state.ordinal()) .apply(); } - - return state; } /** @@ -1425,7 +1419,7 @@ public void clearGCMData(final boolean clearRegistrationToken, final ApiCallback protected Void doInBackground(Void... voids) { setStoredRegistrationToken(null); mRegistrationToken = null; - mRegistrationState = setStoredRegistrationState(RegistrationState.UNREGISTRATED); + setAndStoreRegistrationState(RegistrationState.UNREGISTRATED); if (clearRegistrationToken) { GCMHelper.clearRegistrationToken(); From e9091b10c55f939bee44ae6464f88656f8b10a13 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 19 Jul 2018 17:03:22 +0200 Subject: [PATCH 3/9] Rework GcmRegistrationManager for code clarity --- .../vector/gcm/MatrixGcmListenerService.java | 14 +- .../im/vector/activity/SplashActivity.java | 2 +- .../VectorSettingsPreferencesFragment.kt | 31 +- .../im/vector/gcm/GcmRegistrationManager.java | 532 +++++++----------- .../vector/services/EventStreamService.java | 2 +- 5 files changed, 227 insertions(+), 354 deletions(-) diff --git a/vector/src/app/java/im/vector/gcm/MatrixGcmListenerService.java b/vector/src/app/java/im/vector/gcm/MatrixGcmListenerService.java index f56a7a901b..4966fd9c82 100755 --- a/vector/src/app/java/im/vector/gcm/MatrixGcmListenerService.java +++ b/vector/src/app/java/im/vector/gcm/MatrixGcmListenerService.java @@ -18,6 +18,8 @@ package im.vector.gcm; +import android.support.annotation.Nullable; + import com.google.firebase.messaging.FirebaseMessagingService; import com.google.firebase.messaging.RemoteMessage; import com.google.gson.JsonParser; @@ -54,6 +56,7 @@ public class MatrixGcmListenerService extends FirebaseMessagingService { * @param data the GCM data * @return the event */ + @Nullable private Event parseEvent(Map data) { // accept only event with room id. if ((null == data) || !data.containsKey("room_id") || !data.containsKey("event_id")) { @@ -121,13 +124,14 @@ private void onMessageReceivedInternal(final Map data) { Event event = parseEvent(data); String roomName = data.get("room_name"); - if ((null == roomName) && (null != roomId)) { + if (null == roomName && null != roomId) { + // Try to get the room name from our store MXSession session = Matrix.getInstance(getApplicationContext()).getDefaultSession(); if ((null != session) && session.getDataHandler().getStore().isReady()) { Room room = session.getDataHandler().getStore().getRoom(roomId); if (null != room) { - roomName = VectorUtils.getRoomDisplayName(MatrixGcmListenerService.this, session, room); + roomName = VectorUtils.getRoomDisplayName(this, session, room); } } } @@ -142,7 +146,7 @@ private void onMessageReceivedInternal(final Map data) { // the first GCM event could have been triggered whereas the application is not yet launched. // so it is required to create the sessions and to start/resume event stream if (!mCheckLaunched && (null != Matrix.getInstance(getApplicationContext()).getDefaultSession())) { - CommonActivityUtils.startEventStreamService(MatrixGcmListenerService.this); + CommonActivityUtils.startEventStreamService(this); mCheckLaunched = true; } @@ -168,7 +172,7 @@ private void onMessageReceivedInternal(final Map data) { } } - CommonActivityUtils.catchupEventStream(MatrixGcmListenerService.this); + CommonActivityUtils.catchupEventStream(this); } catch (Exception e) { Log.d(LOG_TAG, "## onMessageReceivedInternal() failed : " + e.getMessage(), e); } @@ -181,6 +185,8 @@ private void onMessageReceivedInternal(final Map data) { */ @Override public void onMessageReceived(RemoteMessage message) { + Log.d(LOG_TAG, "## onMessageReceived() from FCM"); + final Map data = message.getData(); if (null == mUIHandler) { diff --git a/vector/src/main/java/im/vector/activity/SplashActivity.java b/vector/src/main/java/im/vector/activity/SplashActivity.java index 18a4732311..08a39eae86 100755 --- a/vector/src/main/java/im/vector/activity/SplashActivity.java +++ b/vector/src/main/java/im/vector/activity/SplashActivity.java @@ -266,7 +266,7 @@ public void onInitialSyncComplete(String toToken) { // trigger the GCM registration if required GcmRegistrationManager gcmRegistrationManager = Matrix.getInstance(getApplicationContext()).getSharedGCMRegistrationManager(); - if (!gcmRegistrationManager.isGCMRegistred()) { + if (!gcmRegistrationManager.isGcmRegistered()) { gcmRegistrationManager.checkRegistrations(); } else { gcmRegistrationManager.forceSessionsRegistration(null); diff --git a/vector/src/main/java/im/vector/fragments/VectorSettingsPreferencesFragment.kt b/vector/src/main/java/im/vector/fragments/VectorSettingsPreferencesFragment.kt index 2f537ab657..e6258e30f9 100755 --- a/vector/src/main/java/im/vector/fragments/VectorSettingsPreferencesFragment.kt +++ b/vector/src/main/java/im/vector/fragments/VectorSettingsPreferencesFragment.kt @@ -449,20 +449,11 @@ class VectorSettingsPreferencesFragment : PreferenceFragment(), SharedPreference Matrix.getInstance(activity)!!.sharedGCMRegistrationManager .forceSessionsRegistration(object : GcmRegistrationManager.ThirdPartyRegistrationListener { - - override fun onThirdPartyRegistered() { - hideLoadingView() - } - - override fun onThirdPartyRegistrationFailed() { - hideLoadingView() - } - - override fun onThirdPartyUnregistered() { + override fun onSuccess() { hideLoadingView() } - override fun onThirdPartyUnregistrationFailed() { + override fun onError() { hideLoadingView() } }) @@ -1092,7 +1083,7 @@ class VectorSettingsPreferencesFragment : PreferenceFragment(), SharedPreference // when using GCM // need to register on servers - if (isConnected && gcmMgr.useGCM() && (gcmMgr.isServerRegistred || gcmMgr.isServerUnRegistred)) { + if (isConnected && gcmMgr.useGCM() && (gcmMgr.isServerRegistered || gcmMgr.isServerUnRegistered)) { val listener = object : GcmRegistrationManager.ThirdPartyRegistrationListener { private fun onDone() { @@ -1104,27 +1095,19 @@ class VectorSettingsPreferencesFragment : PreferenceFragment(), SharedPreference } } - override fun onThirdPartyRegistered() { - onDone() - } - - override fun onThirdPartyRegistrationFailed() { - gcmMgr.setDeviceNotificationsAllowed(isAllowed) - onDone() - } - - override fun onThirdPartyUnregistered() { + override fun onSuccess() { onDone() } - override fun onThirdPartyUnregistrationFailed() { + override fun onError() { + // Set again the previous state gcmMgr.setDeviceNotificationsAllowed(isAllowed) onDone() } } displayLoadingView() - if (gcmMgr.isServerRegistred) { + if (gcmMgr.isServerRegistered) { gcmMgr.unregister(listener) } else { gcmMgr.register(listener) diff --git a/vector/src/main/java/im/vector/gcm/GcmRegistrationManager.java b/vector/src/main/java/im/vector/gcm/GcmRegistrationManager.java index 85d612e43a..027fada1b4 100755 --- a/vector/src/main/java/im/vector/gcm/GcmRegistrationManager.java +++ b/vector/src/main/java/im/vector/gcm/GcmRegistrationManager.java @@ -22,10 +22,11 @@ import android.content.SharedPreferences; import android.content.pm.PackageInfo; import android.net.Uri; -import android.os.AsyncTask; import android.os.Build; import android.os.Handler; import android.os.Looper; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.text.TextUtils; import org.matrix.androidsdk.HomeServerConnectionConfig; @@ -78,7 +79,7 @@ public final class GcmRegistrationManager { /** * GCM registration interface */ - public interface GCMRegistrationListener { + public interface GcmRegistrationListener { // GCM is properly registered. void onGCMRegistered(); @@ -87,20 +88,14 @@ public interface GCMRegistrationListener { } /** - * 3rd party server registration interface + * 3rd party server registration listener */ public interface ThirdPartyRegistrationListener { - // the third party server is registered - void onThirdPartyRegistered(); + // the third party server is successfully registered/unregistered + void onSuccess(); - // the third party server registration fails. - void onThirdPartyRegistrationFailed(); - - // the third party server is unregister - void onThirdPartyUnregistered(); - - // the third party server unregistration fails - void onThirdPartyUnregistrationFailed(); + // An error occurred + void onError(); } private String mPusherAppName = null; @@ -113,14 +108,32 @@ public interface ThirdPartyRegistrationListener { public List mPushersList = new ArrayList<>(); /** - * Registration steps + * Registration steps (Note: do not change the order of enum, cause their ordinal are store in the SharedPrefs) */ private enum RegistrationState { + /** + * GCM is not registered + */ UNREGISTRATED, + /** + * GCM registration is started + */ GCM_REGISTRATING, - GCM_REGISTRED, + /** + * GCM is registered, but token has not been sent to the Push server + */ + GCM_REGISTERED, + /** + * GCM token is currently sent to the Push server + */ SERVER_REGISTRATING, + /** + * GCM token has been sent to the Push server + */ SERVER_REGISTERED, + /** + * Push servers is currently un registering + */ SERVER_UNREGISTRATING, } @@ -134,7 +147,7 @@ private enum RegistrationState { private RegistrationState mRegistrationState; // defines the GCM registration token - private String mRegistrationToken = null; + private String mRegistrationToken; // 3 states : null not initialized (retrieved by flavor) private static Boolean mUseGCM; @@ -168,7 +181,7 @@ public void onNetworkConnectionUpdate(boolean isConnected) { // test if the server registration / unregistration should be done if (useGCM()) { // test if the user expect having notifications on his device but it was not yet done - if (areDeviceNotificationsAllowed() && (mRegistrationState == RegistrationState.GCM_REGISTRED)) { + if (areDeviceNotificationsAllowed() && (mRegistrationState == RegistrationState.GCM_REGISTERED)) { register(null); } else if (!areDeviceNotificationsAllowed() && (mRegistrationState == RegistrationState.SERVER_REGISTERED)) { unregister(null); @@ -233,14 +246,7 @@ public void checkRegistrations() { addSessionsRegistrationListener(new ThirdPartyRegistrationListener() { @Override - public void onThirdPartyRegistered() { - } - - @Override - public void onThirdPartyRegistrationFailed() { - } - - private void onGCMUnregistred() { + public void onSuccess() { Log.d(LOG_TAG, "resetGCMRegistration : remove the GCM registration token done"); clearOldStoredRegistrationToken(); mRegistrationToken = null; @@ -252,13 +258,9 @@ private void onGCMUnregistred() { } @Override - public void onThirdPartyUnregistered() { - onGCMUnregistred(); - } - - @Override - public void onThirdPartyUnregistrationFailed() { - onGCMUnregistred(); + public void onError() { + // Ignore any error + onSuccess(); } }); @@ -266,7 +268,7 @@ public void onThirdPartyUnregistrationFailed() { } else if (mRegistrationState == RegistrationState.UNREGISTRATED) { Log.d(LOG_TAG, "checkPusherRegistration : try to register to GCM server"); - registerToGCM(new GCMRegistrationListener() { + registerToGcm(new GcmRegistrationListener() { @Override public void onGCMRegistered() { Log.d(LOG_TAG, "checkRegistrations : reregistered"); @@ -278,10 +280,10 @@ public void onGCMRegistrationFailed() { Log.d(LOG_TAG, "checkRegistrations : onPusherRegistrationFailed"); } }); - } else if (mRegistrationState == RegistrationState.GCM_REGISTRED) { + } else if (mRegistrationState == RegistrationState.GCM_REGISTERED) { // register the 3rd party server // the server registration might have failed - // so ensure that it will be done when the application is debackgrounded. + // so ensure that it will be done when the application is de-backgrounded. if (useGCM() && areDeviceNotificationsAllowed()) { register(null); } @@ -299,11 +301,11 @@ public void onGCMRegistrationFailed() { * * @return the GCM registration token */ - private String getGCMRegistrationToken() { + private String getGcmRegistrationToken() { String registrationToken = getStoredRegistrationToken(); if (TextUtils.isEmpty(registrationToken)) { - Log.d(LOG_TAG, "## getGCMRegistrationToken() : undefined token -> getting a new one"); + Log.d(LOG_TAG, "## getGcmRegistrationToken() : undefined token -> getting a new one"); registrationToken = GCMHelper.getRegistrationToken(); } return registrationToken; @@ -314,78 +316,48 @@ private String getGCMRegistrationToken() { * * @param gcmRegistrationListener the events listener. */ - private void registerToGCM(final GCMRegistrationListener gcmRegistrationListener) { - Log.d(LOG_TAG, "registerToGCM with state " + mRegistrationState); + private void registerToGcm(final @NonNull GcmRegistrationListener gcmRegistrationListener) { + Log.d(LOG_TAG, "registerToGcm with state " + mRegistrationState); // do not use GCM if (!useGCM()) { Log.d(LOG_TAG, "registerPusher : GCM is disabled"); // warn the listener - if (null != gcmRegistrationListener) { - try { - gcmRegistrationListener.onGCMRegistrationFailed(); - } catch (Exception e) { - Log.e(LOG_TAG, "registerToGCM : onPusherRegistered/onPusherRegistrationFailed failed " + e.getMessage(), e); - } + try { + gcmRegistrationListener.onGCMRegistrationFailed(); + } catch (Exception e) { + Log.e(LOG_TAG, "registerToGcm : onGCMRegistrationFailed failed " + e.getMessage(), e); } return; } + if (mRegistrationState == RegistrationState.UNREGISTRATED) { setAndStoreRegistrationState(RegistrationState.GCM_REGISTRATING); - try { - new AsyncTask() { - @Override - protected String doInBackground(Void... voids) { - String registrationToken = getGCMRegistrationToken(); - - if (registrationToken != null) { - mRegistrationToken = registrationToken; - } - - return registrationToken; - } + String token = getGcmRegistrationToken(); - @Override - protected void onPostExecute(String pushKey) { - setAndStoreRegistrationState(((pushKey != null) ? RegistrationState.GCM_REGISTRED : RegistrationState.UNREGISTRATED)); - setStoredRegistrationToken(pushKey); + setAndStoreRegistrationState((token != null ? RegistrationState.GCM_REGISTERED : RegistrationState.UNREGISTRATED)); - // warn the listener - if (null != gcmRegistrationListener) { - try { - if (pushKey != null) { - gcmRegistrationListener.onGCMRegistered(); - } else { - // stay in GCM_REGISTRATING state - gcmRegistrationListener.onGCMRegistrationFailed(); - } - } catch (Exception e) { - Log.e(LOG_TAG, "registerToGCM : onPusherRegistered/onPusherRegistrationFailed failed " + e.getMessage(), e); - } - } + setAndStoreRegistrationToken(token); - if (mRegistrationState == RegistrationState.GCM_REGISTRED) { - // register the sessions to the 3rd party server - // this setting should be updated from the listener - if (useGCM()) { - register(null); - } - } - } - }.execute(); + // warn the listener + try { + if (mRegistrationToken != null) { + gcmRegistrationListener.onGCMRegistered(); + } else { + gcmRegistrationListener.onGCMRegistrationFailed(); + } } catch (Exception e) { - Log.e(LOG_TAG, "## registerToGCM() failed " + e.getMessage(), e); - // warn the listener - if (null != gcmRegistrationListener) { - try { - gcmRegistrationListener.onGCMRegistrationFailed(); - } catch (Exception e2) { - Log.e(LOG_TAG, "registerToGCM : onPusherRegistered/onPusherRegistrationFailed failed " + e2.getMessage(), e2); - } + Log.e(LOG_TAG, "registerToGcm : onGCMRegistered/onGCMRegistrationFailed failed " + e.getMessage(), e); + } + + if (mRegistrationState == RegistrationState.GCM_REGISTERED) { + // register the sessions to the 3rd party server + // this setting should be updated from the listener + if (useGCM()) { + register(null); } - setAndStoreRegistrationState(RegistrationState.UNREGISTRATED); } } else if (mRegistrationState == RegistrationState.GCM_REGISTRATING) { // Please wait @@ -405,32 +377,24 @@ public void resetGCMRegistration() { /** * Reset the GCM registration. * - * @param newToken the new registration token + * @param newToken the new registration token, to register again, or null */ - public void resetGCMRegistration(final String newToken) { + public void resetGCMRegistration(final @Nullable String newToken) { Log.d(LOG_TAG, "resetGCMRegistration"); if (RegistrationState.SERVER_REGISTERED == mRegistrationState) { - Log.d(LOG_TAG, "resetGCMRegistration : unregister before retrieving the new GCM key"); + Log.d(LOG_TAG, "resetGCMRegistration : unregister server before retrieving the new GCM key"); unregister(new ThirdPartyRegistrationListener() { @Override - public void onThirdPartyRegistered() { - } - - @Override - public void onThirdPartyRegistrationFailed() { - } - - @Override - public void onThirdPartyUnregistered() { - Log.d(LOG_TAG, "resetGCMRegistration : unregistration is done --> start the registration process"); + public void onSuccess() { + Log.d(LOG_TAG, "resetGCMRegistration : un-registration is done --> start the registration process"); resetGCMRegistration(newToken); } @Override - public void onThirdPartyUnregistrationFailed() { - Log.d(LOG_TAG, "resetGCMRegistration : unregistration failed."); + public void onError() { + Log.d(LOG_TAG, "resetGCMRegistration : un-registration failed."); } }); } else { @@ -480,7 +444,7 @@ private void manage500Error() { Timer relaunchTimer = new Timer(); - // wait 5 seconds before registering + // wait 30 seconds before registering relaunchTimer.schedule(new TimerTask() { @Override public void run() { @@ -489,27 +453,19 @@ public void run() { unregister(new ThirdPartyRegistrationListener() { @Override - public void onThirdPartyRegistered() { - } + public void onSuccess() { + Log.d(LOG_TAG, "500 error: unregister success"); - @Override - public void onThirdPartyRegistrationFailed() { - } - - @Override - public void onThirdPartyUnregistered() { - Log.d(LOG_TAG, "500 error : onThirdPartyUnregistered"); - - setStoredRegistrationToken(null); + setAndStoreRegistrationToken(null); setAndStoreRegistrationState(RegistrationState.UNREGISTRATED); register(null); } @Override - public void onThirdPartyUnregistrationFailed() { - Log.d(LOG_TAG, "500 error : onThirdPartyUnregistrationFailed"); + public void onError() { + Log.d(LOG_TAG, "500 error : unregister error"); - setStoredRegistrationToken(null); + setAndStoreRegistrationToken(null); setAndStoreRegistrationState(RegistrationState.UNREGISTRATED); register(null); } @@ -518,7 +474,7 @@ public void onThirdPartyUnregistrationFailed() { } else { Log.d(LOG_TAG, "500 error : no GCM key"); - setStoredRegistrationToken(null); + setAndStoreRegistrationToken(null); setAndStoreRegistrationState(RegistrationState.UNREGISTRATED); register(null); } @@ -528,35 +484,13 @@ public void onThirdPartyUnregistrationFailed() { /** - * Tells if the registration was done with event id only. - * - * @return true if the registration was done with event Id only + * Force sessions registration */ public void onAppResume() { if (mRegistrationState == RegistrationState.SERVER_REGISTERED) { Log.d(LOG_TAG, "## onAppResume() : force the GCM registration"); - forceSessionsRegistration(new ThirdPartyRegistrationListener() { - @Override - public void onThirdPartyRegistered() { - - } - - @Override - public void onThirdPartyRegistrationFailed() { - - } - - @Override - public void onThirdPartyUnregistered() { - - } - - @Override - public void onThirdPartyUnregistrationFailed() { - - } - }); + forceSessionsRegistration(null); } } @@ -566,7 +500,9 @@ public void onThirdPartyUnregistrationFailed() { * @param session the session to register. * @param listener the registration listener */ - private void registerToThirdPartyServer(final MXSession session, final boolean append, final ThirdPartyRegistrationListener listener) { + private void registerToThirdPartyServer(final MXSession session, + final boolean append, + @NonNull final ThirdPartyRegistrationListener listener) { // test if the push server registration is allowed if (!areDeviceNotificationsAllowed() || !useGCM() || !session.isAlive()) { if (!areDeviceNotificationsAllowed()) { @@ -577,17 +513,15 @@ private void registerToThirdPartyServer(final MXSession session, final boolean a Log.d(LOG_TAG, "registerPusher : GCM is disabled."); } - if (null != listener) { - try { - listener.onThirdPartyRegistrationFailed(); - } catch (Exception e) { - Log.e(LOG_TAG, "registerToThirdPartyServer failed " + e.getMessage(), e); - } + try { + listener.onError(); + } catch (Exception e) { + Log.e(LOG_TAG, "registerToThirdPartyServer failed " + e.getMessage(), e); } - // fallback to the GCM_REGISTRED state + // fallback to the GCM_REGISTERED state // thus, the client will try again to register with checkRegistrations. - setAndStoreRegistrationState(RegistrationState.GCM_REGISTRED); + setAndStoreRegistrationState(RegistrationState.GCM_REGISTERED); return; } @@ -613,28 +547,24 @@ private void registerToThirdPartyServer(final MXSession session, final boolean a public void onSuccess(Void info) { Log.d(LOG_TAG, "registerToThirdPartyServer succeeded"); - if (null != listener) { - try { - listener.onThirdPartyRegistered(); - } catch (Exception e) { - Log.e(LOG_TAG, "onSessionRegistered failed " + e.getMessage(), e); - } + try { + listener.onSuccess(); + } catch (Exception e) { + Log.e(LOG_TAG, "onSessionRegistered failed " + e.getMessage(), e); } } private void onError(final String message) { Log.e(LOG_TAG, "registerToThirdPartyServer failed" + session.getMyUserId() + " (" + message + ")"); - // fallback to the GCM_REGISTRED state + // fallback to the GCM_REGISTERED state // thus, the client will try again to register with checkRegistrations. - setAndStoreRegistrationState(RegistrationState.GCM_REGISTRED); + setAndStoreRegistrationState(RegistrationState.GCM_REGISTERED); - if (null != listener) { - try { - listener.onThirdPartyRegistrationFailed(); - } catch (Exception e) { - Log.e(LOG_TAG, "onThirdPartyRegistrationFailed failed " + e.getMessage(), e); - } + try { + listener.onError(); + } catch (Exception e) { + Log.e(LOG_TAG, "onThirdPartyRegistrationFailed failed " + e.getMessage(), e); } } @@ -674,10 +604,10 @@ public void onUnexpectedError(Exception e) { * Refresh the pushers list (i.e the devices which expect to have notification). * * @param sessions the sessions - * @param callback the callback; + * @param callback the callback */ public void refreshPushersList(List sessions, final ApiCallback callback) { - if ((null != sessions) && (sessions.size() > 0)) { + if (null != sessions && !sessions.isEmpty()) { getPushersRestClient(sessions.get(0)).getPushers(new ApiCallback() { @Override @@ -691,7 +621,7 @@ public void onSuccess(PushersResponse pushersResponse) { Pusher selfPusher = null; for (Pusher pusher : mPushersList) { - if (TextUtils.equals(pusher.pushkey, getGCMRegistrationToken())) { + if (TextUtils.equals(pusher.pushkey, getGcmRegistrationToken())) { selfPusher = pusher; break; } @@ -732,15 +662,16 @@ public void onUnexpectedError(Exception e) { * * @param listener the listener */ - public void forceSessionsRegistration(final ThirdPartyRegistrationListener listener) { - if ((mRegistrationState == RegistrationState.SERVER_REGISTERED) || (mRegistrationState == RegistrationState.GCM_REGISTRED)) { - setAndStoreRegistrationState(RegistrationState.GCM_REGISTRED); + public void forceSessionsRegistration(@Nullable ThirdPartyRegistrationListener listener) { + if (mRegistrationState == RegistrationState.SERVER_REGISTERED + || mRegistrationState == RegistrationState.GCM_REGISTERED) { + setAndStoreRegistrationState(RegistrationState.GCM_REGISTERED); register(listener); } else { if (null != listener) { try { - listener.onThirdPartyRegistrationFailed(); + listener.onError(); } catch (Exception e) { Log.e(LOG_TAG, "forceSessionsRegistration failed " + e.getMessage(), e); } @@ -753,47 +684,59 @@ public void forceSessionsRegistration(final ThirdPartyRegistrationListener liste * * @param listener the registration listener. */ - public void register(final ThirdPartyRegistrationListener listener) { + public void register(@Nullable final ThirdPartyRegistrationListener listener) { Log.d(LOG_TAG, "register with state " + mRegistrationState); addSessionsRegistrationListener(listener); - if (mRegistrationState == RegistrationState.GCM_REGISTRATING) { - // please wait - } else if (mRegistrationState == RegistrationState.SERVER_REGISTRATING) { - // please wait - } else if (mRegistrationState == RegistrationState.UNREGISTRATED) { - Log.d(LOG_TAG, "register unregistrated : try to register again"); + switch (mRegistrationState) { + case UNREGISTRATED: + Log.d(LOG_TAG, "register unregistrated : try to register again"); - // if the registration failed - // try to register again - registerToGCM(new GCMRegistrationListener() { - @Override - public void onGCMRegistered() { - Log.d(LOG_TAG, "GCM registration failed again : register on server side"); - register(listener); - } + // if the registration failed + // try to register again + registerToGcm(new GcmRegistrationListener() { + @Override + public void onGCMRegistered() { + Log.d(LOG_TAG, "GCM registration success: register on server side"); + register(listener); + } - @Override - public void onGCMRegistrationFailed() { - Log.d(LOG_TAG, "register unregistrated : GCM registration failed again"); - dispatchOnThirdPartyRegistrationFailed(); + @Override + public void onGCMRegistrationFailed() { + Log.d(LOG_TAG, "GCM registration failed"); + dispatchOnThirdPartyError(); + } + }); + break; + case GCM_REGISTRATING: + // please wait + break; + case GCM_REGISTERED: + // check if the notifications must be displayed + if (useGCM() + && areDeviceNotificationsAllowed() + && !TextUtils.isEmpty(mRegistrationToken)) { + setAndStoreRegistrationState(RegistrationState.SERVER_REGISTRATING); + registerToThirdPartyServer(new ArrayList<>(Matrix.getInstance(mContext).getSessions()), 0); + } else { + dispatchOnThirdPartyError(); } - }); - } else if (mRegistrationState == RegistrationState.SERVER_REGISTERED) { - Log.e(LOG_TAG, "register : already registred"); - dispatchOnThirdPartyRegistered(); - } else if (mRegistrationState != RegistrationState.GCM_REGISTRED) { - Log.e(LOG_TAG, "register : invalid state " + mRegistrationState); - dispatchOnThirdPartyRegistrationFailed(); - } else { - // check if the notifications must be displayed - if (useGCM() && areDeviceNotificationsAllowed() && !TextUtils.isEmpty(mRegistrationToken)) { - setAndStoreRegistrationState(RegistrationState.SERVER_REGISTRATING); - registerToThirdPartyServer(new ArrayList<>(Matrix.getInstance(mContext).getSessions()), 0); - } else { - dispatchOnThirdPartyRegistrationFailed(); - } + break; + case SERVER_REGISTRATING: + // please wait + break; + case SERVER_REGISTERED: + Log.e(LOG_TAG, "register : already registered"); + // disable the application start on device boot + PreferencesManager.setAutoStartOnBoot(mContext, false); + + dispatchOnThirdPartySuccess(); + break; + case SERVER_UNREGISTRATING: + Log.e(LOG_TAG, "register : invalid state " + mRegistrationState); + dispatchOnThirdPartyError(); + break; } } @@ -808,7 +751,11 @@ private void registerToThirdPartyServer(final ArrayList sessions, fin if (index >= sessions.size()) { Log.d(LOG_TAG, "registerSessions : all the sessions are registered"); setAndStoreRegistrationState(RegistrationState.SERVER_REGISTERED); - dispatchOnThirdPartyRegistered(); + + // disable the application start on device boot + PreferencesManager.setAutoStartOnBoot(mContext, false); + + dispatchOnThirdPartySuccess(); // get the pushers list refreshPushersList(sessions, null); @@ -826,27 +773,22 @@ private void registerToThirdPartyServer(final ArrayList sessions, fin final MXSession session = sessions.get(index); - registerToThirdPartyServer(session, (index > 0), new ThirdPartyRegistrationListener() { + registerToThirdPartyServer(session, index > 0, new ThirdPartyRegistrationListener() { @Override - public void onThirdPartyRegistered() { - Log.d(LOG_TAG, "registerSessions : session " + session.getMyUserId() + " is registred"); + public void onSuccess() { + Log.d(LOG_TAG, "registerSessions : session " + session.getMyUserId() + " is registered"); + + // Go on with the next index registerToThirdPartyServer(sessions, index + 1); } @Override - public void onThirdPartyRegistrationFailed() { + public void onError() { Log.d(LOG_TAG, "registerSessions : onSessionRegistrationFailed " + session.getMyUserId()); - setAndStoreRegistrationState(RegistrationState.GCM_REGISTRED); - dispatchOnThirdPartyRegistrationFailed(); - } - - @Override - public void onThirdPartyUnregistered() { - } - - @Override - public void onThirdPartyUnregistrationFailed() { + // fallback to the GCM_REGISTERED state + setAndStoreRegistrationState(RegistrationState.GCM_REGISTERED); + dispatchOnThirdPartyError(); } }); } @@ -865,7 +807,7 @@ public void unregister(final ThirdPartyRegistrationListener listener) { // please wait } else if (mRegistrationState != RegistrationState.SERVER_REGISTERED) { Log.e(LOG_TAG, "unregisterSessions : invalid state " + mRegistrationState); - dispatchOnThirdPartyUnregistrationFailed(); + dispatchOnThirdPartyError(); } else { setAndStoreRegistrationState(RegistrationState.SERVER_UNREGISTRATING); unregister(new ArrayList<>(Matrix.getInstance(mContext).getSessions()), 0); @@ -881,16 +823,16 @@ public void unregister(final ThirdPartyRegistrationListener listener) { private void unregister(final ArrayList sessions, final int index) { // reach this end of the list ? if (index >= sessions.size()) { - setAndStoreRegistrationState(RegistrationState.GCM_REGISTRED); + setAndStoreRegistrationState(RegistrationState.GCM_REGISTERED); - // trigger a registration if the user disabled them while the unregistration was processing + // trigger a registration if the user enabled them while the un-registration was processing if (useGCM() && areDeviceNotificationsAllowed() && Matrix.hasValidSessions()) { register(null); } else { CommonActivityUtils.onGcmUpdate(mContext); } - dispatchOnThirdPartyUnregistered(); + dispatchOnThirdPartySuccess(); return; } @@ -898,22 +840,14 @@ private void unregister(final ArrayList sessions, final int index) { unregister(session, new ThirdPartyRegistrationListener() { @Override - public void onThirdPartyRegistered() { - } - - @Override - public void onThirdPartyRegistrationFailed() { - } - - @Override - public void onThirdPartyUnregistered() { + public void onSuccess() { unregister(sessions, index + 1); } @Override - public void onThirdPartyUnregistrationFailed() { + public void onError() { setAndStoreRegistrationState(RegistrationState.SERVER_REGISTERED); - dispatchOnThirdPartyUnregistrationFailed(); + dispatchOnThirdPartyError(); } }); } @@ -959,7 +893,7 @@ public void onMatrixError(MatrixError e) { * @param session the session. * @param listener the listener */ - public void unregister(final MXSession session, final ThirdPartyRegistrationListener listener) { + public void unregister(final MXSession session, @Nullable final ThirdPartyRegistrationListener listener) { Log.d(LOG_TAG, "unregister " + session.getMyUserId()); getPushersRestClient(session).removeHttpPusher(mRegistrationToken, @@ -976,9 +910,9 @@ public void onSuccess(Void info) { if (null != listener) { try { - listener.onThirdPartyUnregistered(); + listener.onSuccess(); } catch (Exception e) { - Log.e(LOG_TAG, "unregister : onThirdPartyUnregistered " + e.getMessage(), e); + Log.e(LOG_TAG, "unregister : onSuccess() " + e.getMessage(), e); } } } @@ -989,9 +923,9 @@ private void onError(final String message) { if (null != listener) { try { - listener.onThirdPartyUnregistrationFailed(); + listener.onError(); } catch (Exception e) { - Log.e(LOG_TAG, "unregister : onThirdPartyUnregistrationFailed " + e.getMessage(), e); + Log.e(LOG_TAG, "unregister : onError() " + e.getMessage(), e); } } } @@ -1027,7 +961,7 @@ public void onUnexpectedError(Exception e) { //================================================================================ /** - * Tells if GCM has a push key. + * Tells if GCM has a push token. */ public boolean hasRegistrationToken() { return null != mRegistrationToken; @@ -1041,26 +975,26 @@ public String getCurrentRegistrationToken() { } /** - * Tell if GCM is registred i.e. ready to use + * Tell if GCM is registered i.e. ready to use */ - public boolean isGCMRegistred() { - return (mRegistrationState == RegistrationState.GCM_REGISTRED) + public boolean isGcmRegistered() { + return (mRegistrationState == RegistrationState.GCM_REGISTERED) || (mRegistrationState == RegistrationState.SERVER_REGISTRATING) || (mRegistrationState == RegistrationState.SERVER_REGISTERED); } /** - * Tells if the GCM is registrered on server + * Tells if the GCM is registered on server */ - public boolean isServerRegistred() { + public boolean isServerRegistered() { return mRegistrationState == RegistrationState.SERVER_REGISTERED; } /** - * Tells if the GCM is unregistrered on server + * Tells if the GCM is unregistered on server */ - public boolean isServerUnRegistred() { - return mRegistrationState == RegistrationState.GCM_REGISTRED; + public boolean isServerUnRegistered() { + return mRegistrationState == RegistrationState.GCM_REGISTERED; } //================================================================================ @@ -1254,7 +1188,7 @@ public void setBackgroundSyncAllowed(boolean isAllowed) { * @return true if the application can be restarted in background */ public boolean canStartAppInBackground() { - return isBackgroundSyncAllowed() || (null != getStoredRegistrationToken()); + return isBackgroundSyncAllowed() || null != getStoredRegistrationToken(); } /** @@ -1370,9 +1304,11 @@ private void clearOldStoredRegistrationToken() { * * @param registrationToken the registration token */ - private void setStoredRegistrationToken(String registrationToken) { + private void setAndStoreRegistrationToken(String registrationToken) { Log.d(LOG_TAG, "Saving registration token"); + mRegistrationToken = registrationToken; + getGcmSharedPreferences() .edit() .putString(PREFS_PUSHER_REGISTRATION_TOKEN_KEY_FCM, registrationToken) @@ -1412,35 +1348,20 @@ private void setAndStoreRegistrationState(RegistrationState state) { * @param clearRegistrationToken true to clear the provided GCM token * @param callback the asynchronous callback */ - public void clearGCMData(final boolean clearRegistrationToken, final ApiCallback callback) { + public void clearGCMData(boolean clearRegistrationToken, @NonNull ApiCallback callback) { try { - new AsyncTask() { - @Override - protected Void doInBackground(Void... voids) { - setStoredRegistrationToken(null); - mRegistrationToken = null; - setAndStoreRegistrationState(RegistrationState.UNREGISTRATED); + setAndStoreRegistrationToken(null); + setAndStoreRegistrationState(RegistrationState.UNREGISTRATED); - if (clearRegistrationToken) { - GCMHelper.clearRegistrationToken(); - } - return null; - } - - @Override - protected void onPostExecute(Void nothing) { - if (null != callback) { - callback.onSuccess(null); - } + if (clearRegistrationToken) { + GCMHelper.clearRegistrationToken(); + } - } - }.execute(); + callback.onSuccess(null); } catch (Exception e) { Log.e(LOG_TAG, "## clearGCMData failed " + e.getMessage(), e); - if (null != callback) { - callback.onUnexpectedError(e); - } + callback.onUnexpectedError(e); } } @@ -1455,7 +1376,7 @@ protected void onPostExecute(Void nothing) { */ private void addSessionsRegistrationListener(final ThirdPartyRegistrationListener listener) { synchronized (this) { - if ((null != listener) && (mThirdPartyRegistrationListeners.indexOf(listener) == -1)) { + if (null != listener && !mThirdPartyRegistrationListeners.contains(listener)) { mThirdPartyRegistrationListeners.add(listener); } } @@ -1464,16 +1385,13 @@ private void addSessionsRegistrationListener(final ThirdPartyRegistrationListene /** * Dispatch the onThirdPartyRegistered to the listeners. */ - private void dispatchOnThirdPartyRegistered() { - // disable the application start on device boot - PreferencesManager.setAutoStartOnBoot(mContext, false); - + private void dispatchOnThirdPartySuccess() { synchronized (this) { for (ThirdPartyRegistrationListener listener : mThirdPartyRegistrationListeners) { try { - listener.onThirdPartyRegistered(); + listener.onSuccess(); } catch (Exception e) { - Log.e(LOG_TAG, "onSessionsRegistered " + e.getMessage(), e); + Log.e(LOG_TAG, "dispatchOnThirdPartySuccess " + e.getMessage(), e); } } @@ -1484,47 +1402,13 @@ private void dispatchOnThirdPartyRegistered() { /** * Dispatch the onThirdPartyRegistrationFailed to the listeners. */ - private void dispatchOnThirdPartyRegistrationFailed() { - synchronized (this) { - for (ThirdPartyRegistrationListener listener : mThirdPartyRegistrationListeners) { - try { - listener.onThirdPartyRegistrationFailed(); - } catch (Exception e) { - Log.e(LOG_TAG, "onSessionsRegistrationFailed " + e.getMessage(), e); - } - } - - mThirdPartyRegistrationListeners.clear(); - } - } - - /** - * Dispatch the onThirdPartyUnregistered to the listeners. - */ - private void dispatchOnThirdPartyUnregistered() { - synchronized (this) { - for (ThirdPartyRegistrationListener listener : mThirdPartyRegistrationListeners) { - try { - listener.onThirdPartyUnregistered(); - } catch (Exception e) { - Log.e(LOG_TAG, "onSessionUnregistered " + e.getMessage(), e); - } - } - - mThirdPartyRegistrationListeners.clear(); - } - } - - /** - * Dispatch the onThirdPartyUnregistrationFailed to the listeners. - */ - private void dispatchOnThirdPartyUnregistrationFailed() { + private void dispatchOnThirdPartyError() { synchronized (this) { for (ThirdPartyRegistrationListener listener : mThirdPartyRegistrationListeners) { try { - listener.onThirdPartyUnregistrationFailed(); + listener.onError(); } catch (Exception e) { - Log.e(LOG_TAG, "dispatchOnThirdPartyUnregistrationFailed " + e.getMessage(), e); + Log.e(LOG_TAG, "dispatchOnThirdPartyError " + e.getMessage(), e); } } diff --git a/vector/src/main/java/im/vector/services/EventStreamService.java b/vector/src/main/java/im/vector/services/EventStreamService.java index 6214feede0..ff8ee53ee5 100755 --- a/vector/src/main/java/im/vector/services/EventStreamService.java +++ b/vector/src/main/java/im/vector/services/EventStreamService.java @@ -818,7 +818,7 @@ private boolean shouldDisplayListenForEventsNotification() { return (!mGcmRegistrationManager.useGCM() || // the GCM registration was not done TextUtils.isEmpty(mGcmRegistrationManager.getCurrentRegistrationToken()) - && !mGcmRegistrationManager.isServerRegistred()) + && !mGcmRegistrationManager.isServerRegistered()) && mGcmRegistrationManager.isBackgroundSyncAllowed() && mGcmRegistrationManager.areDeviceNotificationsAllowed(); } From 4855f002a9980170e6ee2aca44fa2a48e5333218 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 19 Jul 2018 17:15:47 +0200 Subject: [PATCH 4/9] Better check of nullity --- .../im/vector/activity/CommonActivityUtils.java | 2 +- .../java/im/vector/activity/LoginActivity.java | 2 +- .../im/vector/activity/VectorHomeActivity.java | 2 +- .../im/vector/services/EventStreamService.java | 16 ++++++++++++---- .../main/java/im/vector/util/CallsManager.java | 2 +- 5 files changed, 16 insertions(+), 8 deletions(-) diff --git a/vector/src/main/java/im/vector/activity/CommonActivityUtils.java b/vector/src/main/java/im/vector/activity/CommonActivityUtils.java index 9e0dfee9cc..5755f70a0c 100755 --- a/vector/src/main/java/im/vector/activity/CommonActivityUtils.java +++ b/vector/src/main/java/im/vector/activity/CommonActivityUtils.java @@ -623,7 +623,7 @@ public static void startEventStreamService(Context context) { } } - if (null != EventStreamService.getInstance()) { + if (EventStreamService.getInstance() != null) { EventStreamService.getInstance().refreshForegroundNotification(); } } diff --git a/vector/src/main/java/im/vector/activity/LoginActivity.java b/vector/src/main/java/im/vector/activity/LoginActivity.java index 6493984eab..8b949401f7 100644 --- a/vector/src/main/java/im/vector/activity/LoginActivity.java +++ b/vector/src/main/java/im/vector/activity/LoginActivity.java @@ -372,7 +372,7 @@ public void initUiAndData() { goToSplash(); } else { // detect if the application has already been started - if (null == EventStreamService.getInstance()) { + if (EventStreamService.getInstance() == null) { Log.d(LOG_TAG, "## onCreate(): goToSplash with credentials but there is no event stream service."); goToSplash(); } else { diff --git a/vector/src/main/java/im/vector/activity/VectorHomeActivity.java b/vector/src/main/java/im/vector/activity/VectorHomeActivity.java index 13566d5448..242ad62bcb 100644 --- a/vector/src/main/java/im/vector/activity/VectorHomeActivity.java +++ b/vector/src/main/java/im/vector/activity/VectorHomeActivity.java @@ -1817,7 +1817,7 @@ public void onDrawerClosed(View view) { } case R.id.sliding_menu_exit: { - if (null != EventStreamService.getInstance()) { + if (EventStreamService.getInstance() != null) { EventStreamService.getInstance().stopNow(); } runOnUiThread(new Runnable() { diff --git a/vector/src/main/java/im/vector/services/EventStreamService.java b/vector/src/main/java/im/vector/services/EventStreamService.java index ff8ee53ee5..9dca05108d 100755 --- a/vector/src/main/java/im/vector/services/EventStreamService.java +++ b/vector/src/main/java/im/vector/services/EventStreamService.java @@ -30,6 +30,7 @@ import android.os.IBinder; import android.os.PowerManager; import android.os.SystemClock; +import android.support.annotation.Nullable; import android.text.Spannable; import android.text.SpannableString; import android.text.TextUtils; @@ -85,6 +86,7 @@ public class EventStreamService extends Service { /** * static instance */ + @Nullable private static EventStreamService mActiveEventStreamService = null; /** @@ -197,6 +199,7 @@ public enum ForegroundNotificationState { /** * @return the event stream instance */ + @Nullable public static EventStreamService getInstance() { return mActiveEventStreamService; } @@ -543,7 +546,8 @@ private void setServiceState(StreamAction newState) { * @return true if the service is stopped. */ public static boolean isStopped() { - return (null == getInstance()) || (getInstance().mServiceState == StreamAction.STOP); + return getInstance() == null + || getInstance().mServiceState == StreamAction.STOP; } /** @@ -661,7 +665,7 @@ private void start() { Log.d(LOG_TAG, "## start : start the service"); // release previous instance - if ((null != mActiveEventStreamService) && (this != mActiveEventStreamService)) { + if (null != mActiveEventStreamService && this != mActiveEventStreamService) { mActiveEventStreamService.stop(); } @@ -1149,8 +1153,12 @@ public void run() { * @param senderDisplayName the sender display name * @param unreadMessagesCount the unread messages count */ - public static void onStaticNotifiedEvent(Context context, Event event, String roomName, String senderDisplayName, int unreadMessagesCount) { - if ((null != event) && !mBackgroundNotificationEventIds.contains(event.eventId)) { + public static void onStaticNotifiedEvent(Context context, + @Nullable Event event, + String roomName, + String senderDisplayName, + int unreadMessagesCount) { + if (null != event && !mBackgroundNotificationEventIds.contains(event.eventId)) { mBackgroundNotificationEventIds.add(event.eventId); String header = (TextUtils.isEmpty(roomName) ? "" : roomName + ": "); String text; diff --git a/vector/src/main/java/im/vector/util/CallsManager.java b/vector/src/main/java/im/vector/util/CallsManager.java index 5881df3a4a..9517b5d661 100755 --- a/vector/src/main/java/im/vector/util/CallsManager.java +++ b/vector/src/main/java/im/vector/util/CallsManager.java @@ -479,7 +479,7 @@ public void checkDeadCalls() { if ((null != mActiveCall) && !hasActiveCall) { Log.e(LOG_TAG, "## checkDeadCalls() : fix an infinite ringing"); - if (null != EventStreamService.getInstance()) { + if (EventStreamService.getInstance() != null) { EventStreamService.getInstance().hideCallNotifications(); } From 3ba7e4f587492e2dc3d4d99fec0b7db08da184aa Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 19 Jul 2018 18:06:28 +0200 Subject: [PATCH 5/9] Avoid crash --- .../main/java/im/vector/util/CallsManager.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/vector/src/main/java/im/vector/util/CallsManager.java b/vector/src/main/java/im/vector/util/CallsManager.java index 9517b5d661..62743d8398 100755 --- a/vector/src/main/java/im/vector/util/CallsManager.java +++ b/vector/src/main/java/im/vector/util/CallsManager.java @@ -232,8 +232,10 @@ public void run() { switch (state) { case IMXCall.CALL_STATE_CREATED: if (mActiveCall.isIncoming()) { - EventStreamService.getInstance().displayIncomingCallNotification(mActiveCall.getSession(), - mActiveCall.getRoom(), null, mActiveCall.getCallId(), null); + if (EventStreamService.getInstance() != null) { + EventStreamService.getInstance().displayIncomingCallNotification(mActiveCall.getSession(), + mActiveCall.getRoom(), null, mActiveCall.getCallId(), null); + } startRinging(); } break; @@ -248,8 +250,10 @@ public void run() { break; case IMXCall.CALL_STATE_CONNECTED: - EventStreamService.getInstance().displayCallInProgressNotification(mActiveCall.getSession(), - mActiveCall.getRoom(), mActiveCall.getCallId()); + if (EventStreamService.getInstance() != null) { + EventStreamService.getInstance().displayCallInProgressNotification(mActiveCall.getSession(), + mActiveCall.getRoom(), mActiveCall.getCallId()); + } mCallSoundsManager.stopSounds(); requestAudioFocus(); @@ -653,7 +657,9 @@ private void releaseCall(IMXCall call) { mCallView = null; mLocalVideoLayoutConfig = null; - EventStreamService.getInstance().hideCallNotifications(); + if (EventStreamService.getInstance() != null) { + EventStreamService.getInstance().hideCallNotifications(); + } } } } From e32eecdeadc6dfec0de2786970e30bd4276ebab2 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 19 Jul 2018 18:07:10 +0200 Subject: [PATCH 6/9] Clean up --- vector/src/main/java/im/vector/Matrix.java | 2 +- .../java/im/vector/services/EventStreamService.java | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/vector/src/main/java/im/vector/Matrix.java b/vector/src/main/java/im/vector/Matrix.java index 9c7fb61e0f..feff46ee0d 100755 --- a/vector/src/main/java/im/vector/Matrix.java +++ b/vector/src/main/java/im/vector/Matrix.java @@ -193,7 +193,7 @@ private Matrix(Context appContext) { * @return the shared instance */ public synchronized static Matrix getInstance(Context appContext) { - if ((instance == null) && (null != appContext)) { + if (instance == null && null != appContext) { instance = new Matrix(appContext); } return instance; diff --git a/vector/src/main/java/im/vector/services/EventStreamService.java b/vector/src/main/java/im/vector/services/EventStreamService.java index 9dca05108d..f37804dcb8 100755 --- a/vector/src/main/java/im/vector/services/EventStreamService.java +++ b/vector/src/main/java/im/vector/services/EventStreamService.java @@ -323,7 +323,7 @@ public void stopAccounts(List matrixIds) { public int onStartCommand(Intent intent, int flags, int startId) { // no intent : restarted by Android // EXTRA_AUTO_RESTART_ACTION : restarted by the service itself ( - if ((null == intent) || intent.hasExtra(EXTRA_AUTO_RESTART_ACTION)) { + if (null == intent || intent.hasExtra(EXTRA_AUTO_RESTART_ACTION)) { boolean restart = false; if (StreamAction.AUTO_RESTART == mServiceState) { @@ -884,7 +884,10 @@ private void setForegroundNotificationState(ForegroundNotificationState foregrou case NONE: // The foreground/ sticky notification can be removed NotificationUtils.INSTANCE.cancelNotificationForegroundService(this); - stopForeground(true); + + if (getInstance() != null) { + getInstance().stopForeground(true); + } break; case INITIAL_SYNCING: notification = NotificationUtils.INSTANCE.buildForegroundServiceNotification(this, R.string.notification_sync_in_progress); @@ -903,7 +906,9 @@ private void setForegroundNotificationState(ForegroundNotificationState foregrou if (notification != null) { // display the stick foreground notification - startForeground(NotificationUtils.NOTIFICATION_ID_FOREGROUND_SERVICE, notification); + if (getInstance() != null) { + getInstance().startForeground(NotificationUtils.NOTIFICATION_ID_FOREGROUND_SERVICE, notification); + } } } From 2fd0547776b479ecfce91a6f82e557f513a20fed Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 19 Jul 2018 18:08:28 +0200 Subject: [PATCH 7/9] Fix issue of notification not displayed when Riot is not started --- .../vector/gcm/MatrixGcmListenerService.java | 13 ++++++--- .../vector/services/EventStreamService.java | 27 +++++++++++++++++-- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/vector/src/app/java/im/vector/gcm/MatrixGcmListenerService.java b/vector/src/app/java/im/vector/gcm/MatrixGcmListenerService.java index 4966fd9c82..1587a694e5 100755 --- a/vector/src/app/java/im/vector/gcm/MatrixGcmListenerService.java +++ b/vector/src/app/java/im/vector/gcm/MatrixGcmListenerService.java @@ -145,23 +145,23 @@ private void onMessageReceivedInternal(final Map data) { // check if the application has been launched once // the first GCM event could have been triggered whereas the application is not yet launched. // so it is required to create the sessions and to start/resume event stream - if (!mCheckLaunched && (null != Matrix.getInstance(getApplicationContext()).getDefaultSession())) { + if (!mCheckLaunched && null != Matrix.getInstance(getApplicationContext()).getDefaultSession()) { CommonActivityUtils.startEventStreamService(this); mCheckLaunched = true; } // check if the event was not yet received // a previous catchup might have already retrieved the notified event - if ((null != eventId) && (null != roomId)) { + if (null != eventId && null != roomId) { try { Collection sessions = Matrix.getInstance(getApplicationContext()).getSessions(); - if ((null != sessions) && (sessions.size() > 0)) { + if (null != sessions && !sessions.isEmpty()) { for (MXSession session : sessions) { if (session.getDataHandler().getStore().isReady()) { if (null != session.getDataHandler().getStore().getEvent(eventId, roomId)) { Log.e(LOG_TAG, "## onMessageReceivedInternal() : ignore the event " + eventId - + " in room " + roomId + "because it is already known"); + + " in room " + roomId + " because it is already known"); return; } } @@ -187,6 +187,11 @@ private void onMessageReceivedInternal(final Map data) { public void onMessageReceived(RemoteMessage message) { Log.d(LOG_TAG, "## onMessageReceived() from FCM"); + // Ensure event stream service is started + if(EventStreamService.getInstance() == null) { + CommonActivityUtils.startEventStreamService(this); + } + final Map data = message.getData(); if (null == mUIHandler) { diff --git a/vector/src/main/java/im/vector/services/EventStreamService.java b/vector/src/main/java/im/vector/services/EventStreamService.java index f37804dcb8..ba463bd9e8 100755 --- a/vector/src/main/java/im/vector/services/EventStreamService.java +++ b/vector/src/main/java/im/vector/services/EventStreamService.java @@ -1210,13 +1210,36 @@ public static void onStaticNotifiedEvent(Context context, notifiedLine.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, header.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); mBackgroundNotificationStrings.add(0, notifiedLine); - getInstance().displayMessagesNotification(mBackgroundNotificationStrings, new BingRule(null, null, true, true, true)); + displayMessagesNotificationStatic(context, mBackgroundNotificationStrings, new BingRule(null, null, true, true, true)); } } else if (0 == unreadMessagesCount) { mBackgroundNotificationStrings.clear(); mLastBackgroundNotificationUnreadCount = 0; mLastBackgroundNotificationRoomId = null; - getInstance().displayMessagesNotification(null, null); + displayMessagesNotificationStatic(context, null, null); + } + } + + /** + * Display a list of messages in the messages notification, when the EventStreamService is not started. + * + * @param messages the messages list, null will hide the messages notification. + * @param rule the bing rule to use + */ + private static void displayMessagesNotificationStatic(Context context, List messages, BingRule rule) { + if (!Matrix.getInstance(context).getSharedGCMRegistrationManager().areDeviceNotificationsAllowed() + || null == messages + || messages.isEmpty()) { + NotificationUtils.INSTANCE.cancelNotificationMessage(context); + RoomsNotifications.deleteCachedRoomNotifications(VectorApp.getInstance()); + } else { + Notification notification = NotificationUtils.INSTANCE.buildMessagesListNotification(context, messages, rule); + + if (null != notification) { + NotificationUtils.INSTANCE.showNotificationMessage(context, notification); + } else { + NotificationUtils.INSTANCE.cancelNotificationMessage(context); + } } } From 9641d42ce7e1fd9fadf544a8d35d4d4e8aa08157 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 19 Jul 2018 19:06:18 +0200 Subject: [PATCH 8/9] format --- vector/src/app/java/im/vector/gcm/MatrixGcmListenerService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vector/src/app/java/im/vector/gcm/MatrixGcmListenerService.java b/vector/src/app/java/im/vector/gcm/MatrixGcmListenerService.java index 1587a694e5..6e1c8dc328 100755 --- a/vector/src/app/java/im/vector/gcm/MatrixGcmListenerService.java +++ b/vector/src/app/java/im/vector/gcm/MatrixGcmListenerService.java @@ -188,7 +188,7 @@ public void onMessageReceived(RemoteMessage message) { Log.d(LOG_TAG, "## onMessageReceived() from FCM"); // Ensure event stream service is started - if(EventStreamService.getInstance() == null) { + if (EventStreamService.getInstance() == null) { CommonActivityUtils.startEventStreamService(this); } From f347fc155c569551aa69cfbf6e344bdc0d79b411 Mon Sep 17 00:00:00 2001 From: ganfra Date: Wed, 25 Jul 2018 16:22:16 +0200 Subject: [PATCH 9/9] Notifications : update CHANGES.rst file --- CHANGES.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.rst b/CHANGES.rst index 9df2ea438d..b156ef9938 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -23,6 +23,7 @@ Bugfix: - Fix issue on verifying device, update the wording of the description message (#1067) - Messages with code blocks show other HTML as plain text (#2280) - Message with

was sometimes not properly formatted (#2275) + - Fix notification issue when Riot is not started (#2451) Translations: -