blob: 92f30ff501124d3ae7ed52a16b5b12cb6f9a567f [file] [log] [blame]
Nick Chalko0816eb32018-11-09 11:49:26 -08001/*
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 */
16package com.android.server;
17
18import android.annotation.NonNull;
19import android.annotation.Nullable;
20import android.os.FileUtils;
21import android.util.Slog;
22
23import java.io.File;
Nick Chalko0816eb32018-11-09 11:49:26 -080024import 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 */
33public abstract class ExtconStateObserver<S> extends ExtconUEventObserver {
34 private static final String TAG = "ExtconStateObserver";
35 private static final boolean LOG = false;
36
37 /**
Nick Chalkocc863e42018-11-16 12:04:49 -080038 * Parses the current state from the state file for {@code extconInfo}.
Nick Chalko0816eb32018-11-09 11:49:26 -080039 *
Nick Chalkocc863e42018-11-16 12:04:49 -080040 * @param extconInfo the extconInfo to parse state for
Nick Chalko0816eb32018-11-09 11:49:26 -080041 * @see #parseState(ExtconInfo, String)
42 * @see ExtconInfo#getStatePath()
43 */
Nick Chalkocc863e42018-11-16 12:04:49 -080044 @Nullable
45 public S parseStateFromFile(ExtconInfo extconInfo) throws IOException {
Nick Chalko0816eb32018-11-09 11:49:26 -080046 String statePath = extconInfo.getStatePath();
Nick Chalkocc863e42018-11-16 12:04:49 -080047 return parseState(
48 extconInfo,
49 FileUtils.readTextFile(new File(statePath), 0, null).trim());
Nick Chalko0816eb32018-11-09 11:49:26 -080050 }
51
52 @Override
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 */
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
81 public abstract S parseState(ExtconInfo extconInfo, String state);
82}