1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.imsserviceentitlement.fcm; 18 19 import android.content.Context; 20 import android.content.SharedPreferences; 21 import android.content.SharedPreferences.OnSharedPreferenceChangeListener; 22 import android.text.TextUtils; 23 import android.util.Log; 24 25 import androidx.annotation.WorkerThread; 26 27 /** Stores FCM token. */ 28 public final class FcmTokenStore { 29 private static final String TAG = "IMSSE-FcmTokenStore"; 30 31 private static final String FCM_TOKEN_FILE = "FCM_TOKEN"; 32 private static final String FCM_TOKEN_KEY = "FCM_TOKEN_SUB_"; 33 FcmTokenStore()34 private FcmTokenStore() {} 35 36 /** Returns FCM token or empty string if not available. */ getToken(Context context, int subId)37 public static String getToken(Context context, int subId) { 38 return getFcmTokenFile(context).getString(FCM_TOKEN_KEY + subId, ""); 39 } 40 41 /** Returns {@code true} if FCM token available. */ hasToken(Context context, int subId)42 public static boolean hasToken(Context context, int subId) { 43 return !TextUtils.isEmpty(getToken(context, subId)); 44 } 45 46 /** Saves the FCM token into data store. */ 47 @WorkerThread setToken(Context context, int subId, String token)48 public static boolean setToken(Context context, int subId, String token) { 49 if (!TextUtils.isEmpty(token)) { 50 return getFcmTokenFile(context) 51 .edit() 52 .putString(FCM_TOKEN_KEY + subId, token) 53 .commit(); 54 } else { 55 return getFcmTokenFile(context) 56 .edit() 57 .remove(FCM_TOKEN_KEY + subId) 58 .commit(); 59 } 60 } 61 62 /** Registers a listener for FCM token update. */ registerTokenUpdateListener( Context context, OnSharedPreferenceChangeListener listener)63 public static void registerTokenUpdateListener( 64 Context context, OnSharedPreferenceChangeListener listener) { 65 Log.d(TAG, "registerTokenUpdateListener"); 66 // Since FCM_TOKEN_FILE only contains one item FCM_TOKEN_KEY, a change to FCM_TOKEN_FILE 67 // means a change to FCM_TOKEN_KEY. The listener can ignore its arguments. 68 getFcmTokenFile(context).registerOnSharedPreferenceChangeListener(listener); 69 } 70 71 /** Unregisters a listener for FCM token update. */ unregisterTokenUpdateListener( Context context, OnSharedPreferenceChangeListener listener)72 public static void unregisterTokenUpdateListener( 73 Context context, OnSharedPreferenceChangeListener listener) { 74 Log.d(TAG, "unregisterTokenUpdateListener"); 75 // Since FCM_TOKEN_FILE only contains one item FCM_TOKEN_KEY, a change to FCM_TOKEN_FILE 76 // means a change to FCM_TOKEN_KEY. The listener can ignore its arguments. 77 getFcmTokenFile(context).unregisterOnSharedPreferenceChangeListener(listener); 78 } 79 getFcmTokenFile(Context context)80 private static SharedPreferences getFcmTokenFile(Context context) { 81 return context.getSharedPreferences(FCM_TOKEN_FILE, Context.MODE_PRIVATE); 82 } 83 } 84