/*
 * Copyright 2012-2014 One Platform Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onepf.opfpush.gcm;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import org.onepf.opfpush.OPFPushLog;

/**
 * @author Kirill Rozov
 * @since 01.10.14.
 */
class GCMSettings {

    private static final String KEY_REGISTRATION_TOKEN = "registration_token";
    private static final String KEY_APP_VERSION = "app_version";

    private static final String PREFS_NAME = "org.onepf.openpush.gcm";

    public static final int NO_SAVED_APP_VERSION = -1;

    private SharedPreferences preferences;

    public GCMSettings(@NonNull final Context context) {
        preferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    }

    public String getRegistrationId() {
        return preferences.getString(KEY_REGISTRATION_TOKEN, null);
    }

    public void saveRegistrationId(@Nullable final String registrationId) {
        OPFPushLog.methodD(GCMSettings.class, "saveRegistrationId", "registrationId");

        if (registrationId == null) {
            preferences.edit().remove(KEY_REGISTRATION_TOKEN).apply();
        } else {
            preferences.edit().putString(KEY_REGISTRATION_TOKEN, registrationId).apply();
        }
    }

    public int getAppVersion() {
        return preferences.getInt(KEY_APP_VERSION, NO_SAVED_APP_VERSION);
    }

    public void saveAppVersion(final int appVersion) {
        OPFPushLog.methodD(GCMSettings.class, "saveAppVersion", appVersion);
        preferences.edit().putInt(KEY_APP_VERSION, appVersion).apply();
    }

    public void removeAppVersion() {
        OPFPushLog.methodD(GCMSettings.class, "removeAppVersion");
        preferences.edit().remove(KEY_APP_VERSION).apply();
    }

    public void reset() {
        OPFPushLog.methodD(GCMSettings.class, "reset");
        preferences.edit().clear().apply();
    }
}

@@ -674,50 +674,8 @@
 xt;%0A
-import android.content.SharedPreferences;%0A
 impo
@@ -794,16 +794,58 @@
 PushLog;
+%0Aimport org.onepf.opfutils.OPFPreferences;
 %0A%0A/**%0A *
@@ -866,16 +866,39 @@
 l Rozov%0A
+ * @author Roman Savin%0A
  * @sinc
@@ -913,16 +913,22 @@
 14.%0A */%0A
+final 
 class GC
@@ -989,21 +989,18 @@
 TRATION_
-TOKEN
+ID
  = %22regi
@@ -1012,13 +1012,10 @@
 ion_
-token
+id
 %22;%0A 
@@ -1115,41 +1115,23 @@
 ing 
-PREFS_NAME = %22org.onepf.openpush.
+GCM_POSTFIX = %22
 gcm%22
@@ -1201,22 +1201,19 @@
 private 
-Shared
+OPF
 Preferen
@@ -1313,69 +1313,47 @@
 s = 
-context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE
+new OPFPreferences(context, GCM_POSTFIX
 );%0A 
@@ -1456,19 +1456,10 @@
 ION_
-TOKEN, null
+ID
 );%0A 
@@ -1682,39 +1682,32 @@
     preferences.
-edit().
 remove(KEY_REGIS
@@ -1718,21 +1718,10 @@
 ION_
-TOKEN).apply(
+ID
 );%0A 
@@ -1764,24 +1764,11 @@
 ces.
-edit().putString
+put
 (KEY
@@ -1785,13 +1785,10 @@
 ION_
-TOKEN
+ID
 , re
@@ -1800,24 +1800,16 @@
 ationId)
-.apply()
 ;%0A      
@@ -2089,20 +2089,10 @@
 ces.
-edit().putIn
+pu
 t(KE
@@ -2117,24 +2117,16 @@
 Version)
-.apply()
 ;%0A    %7D%0A
@@ -2250,23 +2250,16 @@
 erences.
-edit().
 remove(K
@@ -2273,24 +2273,16 @@
 VERSION)
-.apply()
 ;%0A    %7D%0A
@@ -2388,28 +2388,13 @@
 ces.
-edit().clear().apply
+clear
 ();%0A

Use OPFPreferences in GCMSettings class.