Home | History | Annotate | Download | only in cellbroadcastreceiver
      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 
     17 package com.android.cellbroadcastreceiver;
     18 
     19 import android.app.Application;
     20 import android.content.SharedPreferences;
     21 import android.util.Log;
     22 import android.preference.PreferenceManager;
     23 
     24 import static com.android.cellbroadcastreceiver.CellBroadcastReceiver.DBG;
     25 
     26 /**
     27  * The application class loads the default preferences at first start,
     28  * and remembers the time of the most recently received broadcast.
     29  */
     30 public class CellBroadcastReceiverApp extends Application {
     31     public static final String LOG_TAG = "CellBroadcastReceiverApp";
     32     public static final String PREF_KEY_NOTIFICATION_ID = "notification_id";
     33 
     34     static CellBroadcastReceiverApp gCellBroadcastReceiverApp;
     35 
     36     @Override
     37     public void onCreate() {
     38         super.onCreate();
     39         PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
     40         gCellBroadcastReceiverApp = this;
     41     }
     42 
     43     public static CellBroadcastReceiverApp getCellBroadcastReceiverApp() {
     44         return gCellBroadcastReceiverApp;
     45     }
     46 
     47     // Each incoming CB gets its own notification. We have to use a new unique notification id
     48     // for each one.
     49     public int getNextNotificationId() {
     50         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
     51         int notificationId = prefs.getInt(PREF_KEY_NOTIFICATION_ID, 0);
     52         ++notificationId;
     53         if (notificationId > 32765) {
     54             notificationId = 1;     // wrap around before it gets dangerous
     55         }
     56 
     57         // Save the updated notificationId in SharedPreferences
     58         SharedPreferences.Editor editor = prefs.edit();
     59         editor.putInt(PREF_KEY_NOTIFICATION_ID, notificationId);
     60         editor.apply();
     61 
     62         if (DBG) Log.d(LOG_TAG, "getNextNotificationId: " + notificationId);
     63 
     64         return notificationId;
     65     }
     66 }
     67