1/*
2 * Copyright (C) 2011 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
17package com.android.basicsmsreceiver;
18
19import android.app.Activity;
20import android.app.Application;
21import android.content.SharedPreferences;
22import android.util.Log;
23
24public class BasicSmsReceiverApp extends Application {
25    public static final String LOG_TAG = "BasicSmsReceiverApp";
26    public static final String SMS_LITE_PREFS_KEY = "sms_lite_prefs";
27    public static final String PREF_KEY_NOTIFICATION_ID = "notification_id";
28
29    static BasicSmsReceiverApp gBasicSmsReceiverApp;
30
31    @Override
32    public void onCreate() {
33        super.onCreate();
34
35        gBasicSmsReceiverApp = this;
36    }
37
38    public static BasicSmsReceiverApp getBasicSmsReceiverApp() {
39        return gBasicSmsReceiverApp;
40    }
41
42    // Each incoming sms gets its own notification. We have to use a new unique notification id
43    // for each one.
44    public int getNextNotificationId() {
45        SharedPreferences prefs = getSharedPreferences(SMS_LITE_PREFS_KEY,
46                Activity.MODE_PRIVATE);
47        int notificationId = prefs.getInt(PREF_KEY_NOTIFICATION_ID, 0);
48        ++notificationId;
49        if (notificationId > 32765) {
50            notificationId = 1;     // wrap around before it gets dangerous
51        }
52
53        // Save the updated notificationId in SharedPreferences
54        SharedPreferences.Editor editor = prefs.edit();
55        editor.putInt(PREF_KEY_NOTIFICATION_ID, notificationId);
56        editor.apply();
57
58        Log.d(LOG_TAG, "getNextNotificationId: " + notificationId);
59
60        return notificationId;
61    }
62}
63