1 /* <lambda>null2 * Copyright (C) 2024 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.car.docklib.data 18 19 import android.content.ComponentName 20 import android.os.Build 21 import android.util.Log 22 import com.android.car.docklib.DockItemProto.DockAppItemListMessage 23 import com.android.car.docklib.DockItemProto.DockAppItemMessage 24 import java.io.File 25 26 /** 27 * Proto file controller to read and write to store dock data 28 * @param dataFile a file that the current user has read and write permission 29 */ 30 class DockProtoDataController(dataFile: File) { 31 companion object { 32 private const val TAG = "DockProtoDataController" 33 private val DEBUG = Build.isDebuggable() 34 const val FILE_NAME = "dock_item_data" 35 } 36 private val dataSource = DockProtoDataSource(dataFile) 37 38 /** 39 * Load data from storage file 40 * @return mapping of a position to the component pinned to that position, 41 * or an empty mapping if the storage file doesn't exist 42 */ 43 fun loadFromFile(): Map<Int, ComponentName>? { 44 if (DEBUG) Log.d(TAG, "Loading dock from file $dataSource") 45 return dataSource.readFromFile()?.let { dockAppItemListMessage -> 46 val items = HashMap<Int, ComponentName>() 47 dockAppItemListMessage.dockAppItemMessageList.forEach { 48 val componentName = ComponentName(it.packageName, it.className) 49 items[it.relativePosition] = componentName 50 } 51 if (DEBUG) Log.d(TAG, "Loaded dock from file $dataSource") 52 items 53 } 54 } 55 56 /** 57 * Create and write pinned dock items to file 58 * @param pinnedDockItems mapping of position to the component pinned to that position 59 */ 60 fun savePinnedItemsToFile(pinnedDockItems: Map<Int, ComponentName>) { 61 if (DEBUG) Log.d(TAG, "Save dock to file $dataSource") 62 val data = DockAppItemListMessage.newBuilder() 63 pinnedDockItems.forEach() { 64 data.addDockAppItemMessage( 65 DockAppItemMessage.newBuilder() 66 .setPackageName(it.value.packageName) 67 .setClassName(it.value.className) 68 .setRelativePosition(it.key) 69 .build() 70 ) 71 } 72 dataSource.writeToFile(data.build()) 73 } 74 } 75