1 /*
2  * Copyright (C) 2018 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.server;
17 
18 import android.annotation.NonNull;
19 import android.annotation.Nullable;
20 import android.os.FileUtils;
21 import android.util.Slog;
22 
23 import java.io.File;
24 import java.io.IOException;
25 
26 /**
27  * A specialized ExtconUEventObserver that on receiving a {@link UEvent} calls {@link
28  * #updateState(ExtconInfo, String, S)} with the value of{@link #parseState(ExtconInfo, String)}.
29  *
30  * @param <S> the type of state to parse and update
31  * @hide
32  */
33 public abstract class ExtconStateObserver<S> extends ExtconUEventObserver {
34     private static final String TAG = "ExtconStateObserver";
35     private static final boolean LOG = false;
36 
37     /**
38      * Parses the current state from the state file for {@code extconInfo}.
39      *
40      * @param extconInfo the extconInfo to parse state for
41      * @see #parseState(ExtconInfo, String)
42      * @see ExtconInfo#getStatePath()
43      */
44     @Nullable
parseStateFromFile(ExtconInfo extconInfo)45     public S parseStateFromFile(ExtconInfo extconInfo) throws IOException {
46         String statePath = extconInfo.getStatePath();
47         return parseState(
48                 extconInfo,
49                 FileUtils.readTextFile(new File(statePath), 0, null).trim());
50     }
51 
52     @Override
onUEvent(ExtconInfo extconInfo, UEvent event)53     public void onUEvent(ExtconInfo extconInfo, UEvent event) {
54         if (LOG) Slog.d(TAG, extconInfo.getName() + " UEVENT: " + event);
55         String name = event.get("NAME");
56         S state = parseState(extconInfo, event.get("STATE"));
57         if (state != null) {
58             updateState(extconInfo, name, state);
59         }
60     }
61 
62     /**
63      * Subclasses of ExtconStateObserver should override this method update state for {@code
64      * exconInfo} from an {@code UEvent}.
65      *
66      * @param extconInfo the external connection
67      * @param eventName the {@code NAME} of the {@code UEvent}
68      * @param state the{@code STATE} as parsed by {@link #parseState(ExtconInfo, String)}.
69      */
updateState(ExtconInfo extconInfo, String eventName, @NonNull S state)70     public abstract void updateState(ExtconInfo extconInfo, String eventName, @NonNull S state);
71 
72     /**
73      * Subclasses of ExtconStateObserver should override this method to parse the {@code STATE} from
74      * an UEvent.
75      *
76      * @param extconInfo that matches the {@code DEVPATH} of {@code event}
77      * @param state the {@code STATE} from a {@code UEvent}.
78      * @return the parsed state. Return null if the state can not be parsed.
79      */
80     @Nullable
parseState(ExtconInfo extconInfo, String state)81     public abstract S parseState(ExtconInfo extconInfo, String state);
82 }
83