Ritesh Reddy | 8a6ce2c | 2015-12-17 17:03:54 +0000 | [diff] [blame] | 1 | package com.android.server.backup; |
| 2 | |
| 3 | |
| 4 | import android.app.backup.BlobBackupHelper; |
Ritesh Reddy | 8a6ce2c | 2015-12-17 17:03:54 +0000 | [diff] [blame] | 5 | import android.app.usage.UsageStatsManagerInternal; |
| 6 | import android.content.Context; |
Ritesh Reddy | 8a6ce2c | 2015-12-17 17:03:54 +0000 | [diff] [blame] | 7 | import android.os.UserHandle; |
| 8 | import android.util.Log; |
| 9 | |
| 10 | import com.android.server.LocalServices; |
| 11 | |
| 12 | import java.io.ByteArrayInputStream; |
| 13 | import java.io.ByteArrayOutputStream; |
| 14 | import java.io.DataInputStream; |
| 15 | import java.io.DataOutputStream; |
| 16 | import java.io.IOException; |
| 17 | |
| 18 | public class UsageStatsBackupHelper extends BlobBackupHelper { |
| 19 | static final String TAG = "UsgStatsBackupHelper"; // must be < 23 chars |
| 20 | static final boolean DEBUG = false; |
| 21 | |
| 22 | // Current version of the blob schema |
| 23 | static final int BLOB_VERSION = 1; |
| 24 | |
| 25 | // Key under which the payload blob is stored |
Ritesh Reddy | e35ae5c | 2016-02-03 17:06:57 +0000 | [diff] [blame] | 26 | // same as UsageStatsDatabase.KEY_USAGE_STATS |
Ritesh Reddy | 8a6ce2c | 2015-12-17 17:03:54 +0000 | [diff] [blame] | 27 | static final String KEY_USAGE_STATS = "usage_stats"; |
| 28 | |
| 29 | public UsageStatsBackupHelper(Context context) { |
| 30 | super(BLOB_VERSION, KEY_USAGE_STATS); |
| 31 | } |
| 32 | |
| 33 | @Override |
| 34 | protected byte[] getBackupPayload(String key) { |
Ritesh Reddy | e35ae5c | 2016-02-03 17:06:57 +0000 | [diff] [blame] | 35 | if (KEY_USAGE_STATS.equals(key)) { |
| 36 | UsageStatsManagerInternal localUsageStatsManager = |
| 37 | LocalServices.getService(UsageStatsManagerInternal.class); |
Ritesh Reddy | 8a6ce2c | 2015-12-17 17:03:54 +0000 | [diff] [blame] | 38 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 39 | DataOutputStream out = new DataOutputStream(baos); |
Ritesh Reddy | e35ae5c | 2016-02-03 17:06:57 +0000 | [diff] [blame] | 40 | try { |
Ritesh Reddy | 8a6ce2c | 2015-12-17 17:03:54 +0000 | [diff] [blame] | 41 | out.writeInt(UserHandle.USER_SYSTEM); |
| 42 | out.write(localUsageStatsManager.getBackupPayload(UserHandle.USER_SYSTEM, key)); |
Ritesh Reddy | e35ae5c | 2016-02-03 17:06:57 +0000 | [diff] [blame] | 43 | } catch (IOException ioe) { |
Ritesh Reddy | 8a6ce2c | 2015-12-17 17:03:54 +0000 | [diff] [blame] | 44 | if (DEBUG) Log.e(TAG, "Failed to backup Usage Stats", ioe); |
| 45 | baos.reset(); |
| 46 | } |
| 47 | return baos.toByteArray(); |
| 48 | } |
| 49 | return null; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | @Override |
| 54 | protected void applyRestoredPayload(String key, byte[] payload) { |
| 55 | if (KEY_USAGE_STATS.equals(key)) { |
Ritesh Reddy | e35ae5c | 2016-02-03 17:06:57 +0000 | [diff] [blame] | 56 | UsageStatsManagerInternal localUsageStatsManager = |
| 57 | LocalServices.getService(UsageStatsManagerInternal.class); |
Ritesh Reddy | 8a6ce2c | 2015-12-17 17:03:54 +0000 | [diff] [blame] | 58 | DataInputStream in = new DataInputStream(new ByteArrayInputStream(payload)); |
Ritesh Reddy | e35ae5c | 2016-02-03 17:06:57 +0000 | [diff] [blame] | 59 | try { |
Ritesh Reddy | 8a6ce2c | 2015-12-17 17:03:54 +0000 | [diff] [blame] | 60 | int user = in.readInt(); |
| 61 | byte[] restoreData = new byte[payload.length - 4]; |
Ritesh Reddy | e35ae5c | 2016-02-03 17:06:57 +0000 | [diff] [blame] | 62 | in.read(restoreData, 0, restoreData.length); |
Ritesh Reddy | 8a6ce2c | 2015-12-17 17:03:54 +0000 | [diff] [blame] | 63 | localUsageStatsManager.applyRestoredPayload(user, key, restoreData); |
Ritesh Reddy | e35ae5c | 2016-02-03 17:06:57 +0000 | [diff] [blame] | 64 | } catch (IOException ioe) { |
Ritesh Reddy | 8a6ce2c | 2015-12-17 17:03:54 +0000 | [diff] [blame] | 65 | if (DEBUG) Log.e(TAG, "Failed to restore Usage Stats", ioe); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | } |