Expose the battery information through the BatteryModule class
The battery capacity is read through the Android base framework.
Change-Id: I17fb88fdc55c52ace5caa450af495cd36fc7e05f
diff --git a/Android.mk b/Android.mk
index 03e1f15..455895d 100644
--- a/Android.mk
+++ b/Android.mk
@@ -25,6 +25,8 @@
LOCAL_SRC_FILES := \
$(call all-subdir-java-files)
+LOCAL_JAVA_LIBRARIES := framework
+
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := com.fairphone.common
diff --git a/java/com/fairphone/common/modules/BatteryModule.java b/java/com/fairphone/common/modules/BatteryModule.java
new file mode 100644
index 0000000..e5c8cc4
--- /dev/null
+++ b/java/com/fairphone/common/modules/BatteryModule.java
@@ -0,0 +1,95 @@
+/*
+ * Copyright 2018 Fairphone B.V.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.fairphone.common.modules;
+
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.content.Context;
+import android.util.Log;
+
+import com.android.internal.os.PowerProfile;
+
+/**
+ * A Fairphone 2 battery (module).
+ */
+public class BatteryModule extends Module {
+
+ private enum Battery {
+
+ BAT01("FP2-BAT01", 2420),
+ BAT02("FP2-BAT02", 2440);
+
+ /** Battery pack version identifier. */
+ @NonNull
+ final String versionId;
+
+ /** Battery pack design capacity in milliampere-hour. */
+ final int capacity;
+
+ Battery(@NonNull String versionId, int capacity) {
+ this.versionId = versionId;
+ this.capacity = capacity;
+ }
+ }
+
+ private static final String TAG = "BatteryModule";
+
+ /** Installed battery design capacity in milliampere-hour. */
+ private final int mDesignCapacity;
+
+ /**
+ * @return A fresh instance holding the installed battery module information, or {@code null}
+ * if the battery was not recognised.
+ */
+ @Nullable
+ public static BatteryModule getModule(@NonNull Context context) {
+ Battery battery = null;
+
+ final int capacity = (int) new PowerProfile(context).getBatteryCapacity();
+
+ for (Battery temp : Battery.values()) {
+ if (capacity == temp.capacity) {
+ battery = temp;
+ break;
+ }
+ }
+
+ if (battery == null) {
+ Log.e(TAG, "Unknown battery capacity: " + capacity);
+ return null;
+ } else {
+ return new BatteryModule(battery);
+ }
+ }
+
+ /**
+ * Create a new instance holding the installed battery module information.
+ */
+ private BatteryModule(@NonNull Battery battery) {
+ super(battery.versionId);
+
+ mDesignCapacity = battery.capacity;
+ }
+
+ /**
+ * @return The installed battery design capacity in milliampere-hour.
+ */
+ public int getDesignCapacity() {
+ return mDesignCapacity;
+ }
+
+}
diff --git a/java/com/fairphone/common/modules/Module.java b/java/com/fairphone/common/modules/Module.java
index 71f8e11..7eb072f 100644
--- a/java/com/fairphone/common/modules/Module.java
+++ b/java/com/fairphone/common/modules/Module.java
@@ -20,8 +20,6 @@
/**
* A Fairphone 2 module.
- * <p>
- * A plain old Java object that holds information about a module.
*/
public abstract class Module {
@@ -29,16 +27,15 @@
@NonNull
private final String mVersionId;
- /* package */ Module(@NonNull String versionId) {
+ Module(@NonNull String versionId) {
mVersionId = versionId;
}
/**
- * @return The installed, non-translatable module version identifier or
- * <code>null</code>.
+ * @return The installed, non-translatable module version identifier or {@code null}.
*/
@NonNull
- public String getVersionId() {
+ public final String getVersionId() {
return mVersionId;
}