Home | History | Annotate | Download | only in backup
      1 /*
      2  * Copyright (C) 2015 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.server.backup;
     18 
     19 import android.app.INotificationManager;
     20 import android.app.backup.BlobBackupHelper;
     21 import android.content.Context;
     22 import android.os.ServiceManager;
     23 import android.os.UserHandle;
     24 import android.util.Log;
     25 import android.util.Slog;
     26 
     27 public class NotificationBackupHelper extends BlobBackupHelper {
     28     static final String TAG = "NotifBackupHelper";   // must be < 23 chars
     29     static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
     30 
     31     // Current version of the blob schema
     32     static final int BLOB_VERSION = 1;
     33 
     34     // Key under which the payload blob is stored
     35     static final String KEY_NOTIFICATIONS = "notifications";
     36 
     37     public NotificationBackupHelper(Context context) {
     38         super(BLOB_VERSION, KEY_NOTIFICATIONS);
     39         // context is currently unused
     40     }
     41 
     42     @Override
     43     protected byte[] getBackupPayload(String key) {
     44         byte[] newPayload = null;
     45         if (KEY_NOTIFICATIONS.equals(key)) {
     46             try {
     47                 INotificationManager nm = INotificationManager.Stub.asInterface(
     48                         ServiceManager.getService("notification"));
     49                 newPayload = nm.getBackupPayload(UserHandle.USER_OWNER);
     50             } catch (Exception e) {
     51                 // Treat as no data
     52                 Slog.e(TAG, "Couldn't communicate with notification manager");
     53                 newPayload = null;
     54             }
     55         }
     56         return newPayload;
     57     }
     58 
     59     @Override
     60     protected void applyRestoredPayload(String key, byte[] payload) {
     61         if (DEBUG) {
     62             Slog.v(TAG, "Got restore of " + key);
     63         }
     64 
     65         if (KEY_NOTIFICATIONS.equals(key)) {
     66             try {
     67                 INotificationManager nm = INotificationManager.Stub.asInterface(
     68                         ServiceManager.getService("notification"));
     69                 nm.applyRestore(payload, UserHandle.USER_OWNER);
     70             } catch (Exception e) {
     71                 Slog.e(TAG, "Couldn't communicate with notification manager");
     72             }
     73         }
     74     }
     75 
     76 }
     77