Nick Chalko | 0816eb3 | 2018-11-09 11:49:26 -0800 | [diff] [blame] | 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; |
Nick Chalko | 0816eb3 | 2018-11-09 11:49:26 -0800 | [diff] [blame] | 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 | /** |
Nick Chalko | cc863e4 | 2018-11-16 12:04:49 -0800 | [diff] [blame] | 38 | * Parses the current state from the state file for {@code extconInfo}. |
Nick Chalko | 0816eb3 | 2018-11-09 11:49:26 -0800 | [diff] [blame] | 39 | * |
Nick Chalko | cc863e4 | 2018-11-16 12:04:49 -0800 | [diff] [blame] | 40 | * @param extconInfo the extconInfo to parse state for |
Nick Chalko | 0816eb3 | 2018-11-09 11:49:26 -0800 | [diff] [blame] | 41 | * @see #parseState(ExtconInfo, String) |
| 42 | * @see ExtconInfo#getStatePath() |
| 43 | */ |
Nick Chalko | cc863e4 | 2018-11-16 12:04:49 -0800 | [diff] [blame] | 44 | @Nullable |
| 45 | public S parseStateFromFile(ExtconInfo extconInfo) throws IOException { |
Nick Chalko | 0816eb3 | 2018-11-09 11:49:26 -0800 | [diff] [blame] | 46 | String statePath = extconInfo.getStatePath(); |
Nick Chalko | cc863e4 | 2018-11-16 12:04:49 -0800 | [diff] [blame] | 47 | return parseState( |
| 48 | extconInfo, |
| 49 | FileUtils.readTextFile(new File(statePath), 0, null).trim()); |
Nick Chalko | 0816eb3 | 2018-11-09 11:49:26 -0800 | [diff] [blame] | 50 | } |
| 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 | } |