158a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor/*
258a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor * Copyright (C) 2011 The Android Open Source Project
358a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor *
458a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor * Licensed under the Apache License, Version 2.0 (the "License");
558a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor * you may not use this file except in compliance with the License.
658a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor * You may obtain a copy of the License at
758a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor *
858a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor *      http://www.apache.org/licenses/LICENSE-2.0
958a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor *
1058a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor * Unless required by applicable law or agreed to in writing, software
1158a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor * distributed under the License is distributed on an "AS IS" BASIS,
1258a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1358a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor * See the License for the specific language governing permissions and
1458a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor * limitations under the License.
1558a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor */
1658a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor
1758a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylorpackage com.android.basicsmsreceiver;
1858a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor
1958a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylorimport android.app.Activity;
2058a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylorimport android.app.Application;
2158a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylorimport android.content.SharedPreferences;
2258a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylorimport android.util.Log;
2358a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor
2458a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylorpublic class BasicSmsReceiverApp extends Application {
2558a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor    public static final String LOG_TAG = "BasicSmsReceiverApp";
2658a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor    public static final String SMS_LITE_PREFS_KEY = "sms_lite_prefs";
2758a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor    public static final String PREF_KEY_NOTIFICATION_ID = "notification_id";
2858a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor
2958a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor    static BasicSmsReceiverApp gBasicSmsReceiverApp;
3058a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor
3158a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor    @Override
3258a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor    public void onCreate() {
3358a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor        super.onCreate();
3458a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor
3558a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor        gBasicSmsReceiverApp = this;
3658a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor    }
3758a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor
3858a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor    public static BasicSmsReceiverApp getBasicSmsReceiverApp() {
3958a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor        return gBasicSmsReceiverApp;
4058a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor    }
4158a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor
4258a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor    // Each incoming sms gets its own notification. We have to use a new unique notification id
4358a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor    // for each one.
4458a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor    public int getNextNotificationId() {
4558a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor        SharedPreferences prefs = getSharedPreferences(SMS_LITE_PREFS_KEY,
4658a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor                Activity.MODE_PRIVATE);
4758a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor        int notificationId = prefs.getInt(PREF_KEY_NOTIFICATION_ID, 0);
4858a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor        ++notificationId;
4958a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor        if (notificationId > 32765) {
5058a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor            notificationId = 1;     // wrap around before it gets dangerous
5158a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor        }
5258a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor
5358a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor        // Save the updated notificationId in SharedPreferences
5458a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor        SharedPreferences.Editor editor = prefs.edit();
5558a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor        editor.putInt(PREF_KEY_NOTIFICATION_ID, notificationId);
5658a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor        editor.apply();
5758a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor
5858a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor        Log.d(LOG_TAG, "getNextNotificationId: " + notificationId);
5958a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor
6058a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor        return notificationId;
6158a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor    }
6258a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor}
63