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 
persistsToDisknull35     fun persistsToDisk(bubbles: List<BubbleEntity>): Boolean {
36         if (DEBUG) Log.d(TAG, "persisting ${bubbles.size} bubbles")
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)
45                 if (DEBUG) Log.d(TAG, "persisted ${bubbles.size} bubbles")
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     }
54 
readFromDisknull55     fun readFromDisk(): List<BubbleEntity> {
56         synchronized(bubbleFile) {
57             if (!bubbleFile.exists()) return emptyList()
58             try { return bubbleFile.openRead().use(::readXml) } catch (e: Throwable) {
59                 Log.e(TAG, "Failed to open bubble file", e)
60             }
61             return emptyList()
62         }
63     }
64 }
65 
66 private const val TAG = "BubblePersistentRepository"
67 private const val DEBUG = false
68