diff --git a/src/generic/java/com/owncloud/android/utils/PushUtils.java b/src/generic/java/com/owncloud/android/utils/PushUtils.java index dfa0d1947b..6ab8e27d97 100644 --- a/src/generic/java/com/owncloud/android/utils/PushUtils.java +++ b/src/generic/java/com/owncloud/android/utils/PushUtils.java @@ -34,7 +34,7 @@ public final class PushUtils { private PushUtils() { } - public static void pushRegistrationToServer() { + public static void pushRegistrationToServer(final String pushToken) { // do nothing } diff --git a/src/gplay/java/com/owncloud/android/services/firebase/NCFirebaseInstanceIDService.java b/src/gplay/java/com/owncloud/android/services/firebase/NCFirebaseInstanceIDService.java index 71549674e3..fa4ea31014 100644 --- a/src/gplay/java/com/owncloud/android/services/firebase/NCFirebaseInstanceIDService.java +++ b/src/gplay/java/com/owncloud/android/services/firebase/NCFirebaseInstanceIDService.java @@ -23,19 +23,27 @@ import android.text.TextUtils; import com.google.firebase.iid.FirebaseInstanceId; import com.google.firebase.iid.FirebaseInstanceIdService; +import com.nextcloud.client.preferences.AppPreferences; import com.nextcloud.client.preferences.PreferenceManager; -import com.owncloud.android.MainApp; import com.owncloud.android.R; import com.owncloud.android.utils.PushUtils; public class NCFirebaseInstanceIDService extends FirebaseInstanceIdService { + private AppPreferences preferences; + + @Override + public void onCreate() { + super.onCreate(); + preferences = PreferenceManager.fromContext(this); + } + @Override public void onTokenRefresh() { //You can implement this method to store the token on your server if (!TextUtils.isEmpty(getResources().getString(R.string.push_server_url))) { - PreferenceManager.setPushToken(MainApp.getAppContext(), FirebaseInstanceId.getInstance().getToken()); - PushUtils.pushRegistrationToServer(); + preferences.setPushToken(FirebaseInstanceId.getInstance().getToken()); + PushUtils.pushRegistrationToServer(preferences.getPushToken()); } } } diff --git a/src/gplay/java/com/owncloud/android/utils/PushUtils.java b/src/gplay/java/com/owncloud/android/utils/PushUtils.java index c3e001ce74..88fa64435d 100644 --- a/src/gplay/java/com/owncloud/android/utils/PushUtils.java +++ b/src/gplay/java/com/owncloud/android/utils/PushUtils.java @@ -29,6 +29,7 @@ import android.util.Base64; import android.util.Log; import com.google.gson.Gson; +import com.nextcloud.client.preferences.AppPreferences; import com.nextcloud.client.preferences.PreferenceManager; import com.owncloud.android.MainApp; import com.owncloud.android.R; @@ -193,8 +194,7 @@ public final class PushUtils { } } - public static void pushRegistrationToServer() { - String token = PreferenceManager.getPushToken(MainApp.getAppContext()); + public static void pushRegistrationToServer(final String token) { arbitraryDataProvider = new ArbitraryDataProvider(MainApp.getAppContext().getContentResolver()); if (!TextUtils.isEmpty(MainApp.getAppContext().getResources().getString(R.string.push_server_url)) && @@ -373,8 +373,10 @@ public final class PushUtils { FileUtils.deleteQuietly(privateKeyFile); FileUtils.deleteQuietly(publicKeyFile); - pushRegistrationToServer(); - PreferenceManager.fromContext(context).setKeysReInitEnabled(); + AppPreferences preferences = PreferenceManager.fromContext(context); + String pushToken = preferences.getPushToken(); + pushRegistrationToServer(pushToken); + preferences.setKeysReInitEnabled(); } private static void migratePushKeys() { diff --git a/src/main/java/com/nextcloud/client/preferences/AppPreferences.java b/src/main/java/com/nextcloud/client/preferences/AppPreferences.java index 01a7a15ccc..94b715a92b 100644 --- a/src/main/java/com/nextcloud/client/preferences/AppPreferences.java +++ b/src/main/java/com/nextcloud/client/preferences/AppPreferences.java @@ -24,9 +24,13 @@ import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.utils.FileSortOrder; public interface AppPreferences { + void setKeysReInitEnabled(); boolean isKeysReInitEnabled(); + void setPushToken(String pushToken); + String getPushToken(); + boolean instantPictureUploadEnabled(); boolean instantVideoUploadEnabled(); diff --git a/src/main/java/com/nextcloud/client/preferences/PreferenceManager.java b/src/main/java/com/nextcloud/client/preferences/PreferenceManager.java index 39294af9ae..56cb58e689 100644 --- a/src/main/java/com/nextcloud/client/preferences/PreferenceManager.java +++ b/src/main/java/com/nextcloud/client/preferences/PreferenceManager.java @@ -88,12 +88,14 @@ public final class PreferenceManager implements AppPreferences { return preferences.getBoolean(PREF__KEYS_REINIT, false); } - public static void setPushToken(Context context, String pushToken) { - saveStringPreferenceNow(context, PREF__PUSH_TOKEN, pushToken); + @Override + public void setPushToken(String pushToken) { + preferences.edit().putString(PREF__PUSH_TOKEN, pushToken).apply(); } - public static String getPushToken(Context context) { - return getDefaultSharedPreferences(context).getString(PREF__PUSH_TOKEN, ""); + @Override + public String getPushToken() { + return preferences.getString(PREF__PUSH_TOKEN, ""); } @Override diff --git a/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java b/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java index 977718e859..5b23f9a32f 100644 --- a/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java +++ b/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java @@ -2566,7 +2566,7 @@ public class FileDisplayActivity extends HookActivity if (!preferences.isKeysReInitEnabled()) { PushUtils.reinitKeys(); } else { - PushUtils.pushRegistrationToServer(); + PushUtils.pushRegistrationToServer(preferences.getPushToken()); } } diff --git a/src/main/java/com/owncloud/android/ui/activity/UserInfoActivity.java b/src/main/java/com/owncloud/android/ui/activity/UserInfoActivity.java index 9a70b75ac5..873c3526fc 100644 --- a/src/main/java/com/owncloud/android/ui/activity/UserInfoActivity.java +++ b/src/main/java/com/owncloud/android/ui/activity/UserInfoActivity.java @@ -51,6 +51,8 @@ import com.bumptech.glide.Glide; import com.bumptech.glide.request.animation.GlideAnimation; import com.bumptech.glide.request.target.SimpleTarget; import com.google.gson.Gson; +import com.nextcloud.client.preferences.AppPreferences; +import com.nextcloud.client.preferences.PreferenceManager; import com.owncloud.android.R; import com.owncloud.android.authentication.AccountUtils; import com.owncloud.android.datamodel.ArbitraryDataProvider; @@ -112,6 +114,7 @@ public class UserInfoActivity extends FileActivity { @BindString(R.string.user_information_retrieval_error) protected String sorryMessage; + private AppPreferences preferences; private float mCurrentAccountAvatarRadiusDimension; private Unbinder unbinder; @@ -123,7 +126,7 @@ public class UserInfoActivity extends FileActivity { public void onCreate(Bundle savedInstanceState) { Log_OC.v(TAG, "onCreate() start"); super.onCreate(savedInstanceState); - + preferences = PreferenceManager.fromContext(this); Bundle bundle = getIntent().getExtras(); account = Parcels.unwrap(bundle.getParcelable(KEY_ACCOUNT)); @@ -439,7 +442,7 @@ public class UserInfoActivity extends FileActivity { @Subscribe(threadMode = ThreadMode.BACKGROUND) public void onMessageEvent(TokenPushEvent event) { - PushUtils.pushRegistrationToServer(); + PushUtils.pushRegistrationToServer(preferences.getPushToken()); } diff --git a/src/versionDev/java/com/owncloud/android/utils/PushUtils.java b/src/versionDev/java/com/owncloud/android/utils/PushUtils.java index 8941f41493..b6072ce38b 100644 --- a/src/versionDev/java/com/owncloud/android/utils/PushUtils.java +++ b/src/versionDev/java/com/owncloud/android/utils/PushUtils.java @@ -34,7 +34,7 @@ public final class PushUtils { private PushUtils() { } - public static void pushRegistrationToServer() { + public static void pushRegistrationToServer(final String pushToken) { // do nothing }