| /* |
| * Copyright (C) 2015 The Android Open Source Project |
| * |
| * 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 com.android.deskclock.data; |
| |
| import android.content.SharedPreferences; |
| |
| /** |
| * This class encapsulates the transfer of data between widget objects and their permanent storage |
| * in {@link SharedPreferences}. |
| */ |
| final class WidgetDAO { |
| |
| /** Suffix for a key to a preference that stores the instance count for a given widget type. */ |
| private static final String WIDGET_COUNT = "_widget_count"; |
| |
| private WidgetDAO() {} |
| |
| /** |
| * @param widgetProviderClass indicates the type of widget being counted |
| * @param count the number of widgets of the given type |
| * @return the delta between the new count and the old count |
| */ |
| static int updateWidgetCount(SharedPreferences prefs, Class widgetProviderClass, int count) { |
| final String key = widgetProviderClass.getSimpleName() + WIDGET_COUNT; |
| final int oldCount = prefs.getInt(key, 0); |
| if (count == 0) { |
| prefs.edit().remove(key).apply(); |
| } else { |
| prefs.edit().putInt(key, count).apply(); |
| } |
| return count - oldCount; |
| } |
| } |