1 /*
2  * Copyright (C) 2021 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.apphibernation;
18 
19 
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.util.Slog;
23 import android.util.proto.ProtoInputStream;
24 import android.util.proto.ProtoOutputStream;
25 
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.List;
29 
30 /**
31  * Reads and writes protos for {@link GlobalLevelState} hiberation states.
32  */
33 final class GlobalLevelHibernationProto implements ProtoReadWriter<List<GlobalLevelState>> {
34     private static final String TAG = "GlobalLevelHibernationProtoReadWriter";
35 
36     @Override
writeToProto(@onNull ProtoOutputStream stream, @NonNull List<GlobalLevelState> data)37     public void writeToProto(@NonNull ProtoOutputStream stream,
38             @NonNull List<GlobalLevelState> data) {
39         for (int i = 0, size = data.size(); i < size; i++) {
40             long token = stream.start(GlobalLevelHibernationStatesProto.HIBERNATION_STATE);
41             GlobalLevelState state = data.get(i);
42             stream.write(GlobalLevelHibernationStateProto.PACKAGE_NAME, state.packageName);
43             stream.write(GlobalLevelHibernationStateProto.HIBERNATED, state.hibernated);
44             stream.write(GlobalLevelHibernationStateProto.SAVED_BYTE, state.savedByte);
45             stream.end(token);
46         }
47     }
48 
49     @Override
readFromProto(@onNull ProtoInputStream stream)50     public @Nullable List<GlobalLevelState> readFromProto(@NonNull ProtoInputStream stream)
51             throws IOException {
52         List<GlobalLevelState> list = new ArrayList<>();
53         while (stream.nextField() != ProtoInputStream.NO_MORE_FIELDS) {
54             if (stream.getFieldNumber()
55                     != (int) GlobalLevelHibernationStatesProto.HIBERNATION_STATE) {
56                 continue;
57             }
58             GlobalLevelState state = new GlobalLevelState();
59             long token = stream.start(GlobalLevelHibernationStatesProto.HIBERNATION_STATE);
60             while (stream.nextField() != ProtoInputStream.NO_MORE_FIELDS) {
61                 switch (stream.getFieldNumber()) {
62                     case (int) GlobalLevelHibernationStateProto.PACKAGE_NAME:
63                         state.packageName =
64                                 stream.readString(GlobalLevelHibernationStateProto.PACKAGE_NAME);
65                         break;
66                     case (int) GlobalLevelHibernationStateProto.HIBERNATED:
67                         state.hibernated =
68                                 stream.readBoolean(GlobalLevelHibernationStateProto.HIBERNATED);
69                         break;
70                     case (int) GlobalLevelHibernationStateProto.SAVED_BYTE:
71                         state.savedByte =
72                                 stream.readLong(GlobalLevelHibernationStateProto.SAVED_BYTE);
73                         break;
74                     default:
75                         Slog.w(TAG, "Undefined field in proto: " + stream.getFieldNumber());
76                 }
77             }
78             stream.end(token);
79             list.add(state);
80         }
81         return list;
82     }
83 }
84