Move push token preference to AppPreferences

Signed-off-by: Chris Narkiewicz <hello@ezaquarii.com>
This commit is contained in:
Chris Narkiewicz 2019-03-11 15:13:35 +00:00
parent 31d7639d3b
commit 293dae52d2
8 changed files with 35 additions and 16 deletions

View file

@ -34,7 +34,7 @@ public final class PushUtils {
private PushUtils() {
}
public static void pushRegistrationToServer() {
public static void pushRegistrationToServer(final String pushToken) {
// do nothing
}

View file

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

View file

@ -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() {

View file

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

View file

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

View file

@ -2566,7 +2566,7 @@ public class FileDisplayActivity extends HookActivity
if (!preferences.isKeysReInitEnabled()) {
PushUtils.reinitKeys();
} else {
PushUtils.pushRegistrationToServer();
PushUtils.pushRegistrationToServer(preferences.getPushToken());
}
}

View file

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

View file

@ -34,7 +34,7 @@ public final class PushUtils {
private PushUtils() {
}
public static void pushRegistrationToServer() {
public static void pushRegistrationToServer(final String pushToken) {
// do nothing
}