Pinyao Ting | ee191b1 | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | package com.android.systemui.bubbles.storage |
| 17 | |
| 18 | import android.content.Context |
| 19 | import android.util.AtomicFile |
| 20 | import android.util.Log |
| 21 | import java.io.File |
| 22 | import java.io.FileOutputStream |
| 23 | import java.io.IOException |
| 24 | import javax.inject.Inject |
| 25 | import javax.inject.Singleton |
| 26 | |
| 27 | @Singleton |
| 28 | class BubblePersistentRepository @Inject constructor( |
| 29 | context: Context |
| 30 | ) { |
| 31 | |
| 32 | private val bubbleFile: AtomicFile = AtomicFile(File(context.filesDir, |
| 33 | "overflow_bubbles.xml"), "overflow-bubbles") |
| 34 | |
Pinyao Ting | d4c1ba9 | 2020-05-04 20:29:56 -0700 | [diff] [blame] | 35 | fun persistsToDisk(bubbles: List<BubbleEntity>): Boolean { |
Pinyao Ting | 76ed7ba | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 36 | if (DEBUG) Log.d(TAG, "persisting ${bubbles.size} bubbles") |
Pinyao Ting | ee191b1 | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 37 | synchronized(bubbleFile) { |
| 38 | val stream: FileOutputStream = try { bubbleFile.startWrite() } catch (e: IOException) { |
| 39 | Log.e(TAG, "Failed to save bubble file", e) |
| 40 | return false |
| 41 | } |
| 42 | try { |
| 43 | writeXml(stream, bubbles) |
| 44 | bubbleFile.finishWrite(stream) |
Pinyao Ting | 76ed7ba | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 45 | if (DEBUG) Log.d(TAG, "persisted ${bubbles.size} bubbles") |
Pinyao Ting | ee191b1 | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 46 | return true |
| 47 | } catch (e: Exception) { |
| 48 | Log.e(TAG, "Failed to save bubble file, restoring backup", e) |
| 49 | bubbleFile.failWrite(stream) |
| 50 | } |
| 51 | } |
| 52 | return false |
| 53 | } |
Pinyao Ting | 76ed7ba | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 54 | |
Pinyao Ting | d4c1ba9 | 2020-05-04 20:29:56 -0700 | [diff] [blame] | 55 | fun readFromDisk(): List<BubbleEntity> { |
Pinyao Ting | 76ed7ba | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 56 | synchronized(bubbleFile) { |
| 57 | try { return bubbleFile.openRead().use(::readXml) } catch (e: Throwable) { |
| 58 | Log.e(TAG, "Failed to open bubble file", e) |
| 59 | } |
| 60 | return emptyList() |
| 61 | } |
| 62 | } |
Pinyao Ting | ee191b1 | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | private const val TAG = "BubblePersistentRepository" |
Pinyao Ting | 76ed7ba | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 66 | private const val DEBUG = false |