summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Horvath <robhor@google.com>2023-05-24 13:07:03 +0000
committerRobert Horvath <robhor@google.com>2023-05-24 13:36:37 +0000
commit2faeb351cb38216496064d03251630c69f86f1dd (patch)
treeab54b7d8b47beea4ab7d7fd188d450045785649c
parentb7991ff0f0cc41608a637283a8cf6b95f604cda0 (diff)
downloadTvSettings-2faeb351cb38216496064d03251630c69f86f1dd.tar.gz
Replace EnergyModesService for ContentProvider
Remove the AIDL based Service, as when building client apps like SetupWraith we may not be able to use the platform AIDL compiler, meaning that we cannot use stable AIDLs and generated code may have incompatibilities. Bug: 277889148 Test: Manual Change-Id: I53973a099e1b846167ec55d557c0da78d53184ed
-rw-r--r--Settings/AndroidManifest.xml10
-rw-r--r--Settings/src/com/android/tv/settings/device/eco/EnergyModesContentProvider.java215
-rw-r--r--Settings/src/com/android/tv/settings/device/eco/EnergyModesService.java130
-rw-r--r--Settings/src/com/android/tv/settings/device/eco/IEnergyModesService.aidl62
4 files changed, 223 insertions, 194 deletions
diff --git a/Settings/AndroidManifest.xml b/Settings/AndroidManifest.xml
index 5e443d905..db2d4cec2 100644
--- a/Settings/AndroidManifest.xml
+++ b/Settings/AndroidManifest.xml
@@ -1307,8 +1307,14 @@
<service android:name=".device.eco.EnergyModesStatsLogJobService"
android:permission="android.permission.BIND_JOB_SERVICE" />
- <service android:name=".device.eco.EnergyModesService"
- android:exported="true" />
+ <provider
+ android:name=".device.eco.EnergyModesContentProvider"
+ android:authorities="com.android.tv.settings.device.eco.energymodes"
+ android:exported="true">
+ <intent-filter>
+ <action android:name="com.android.tv.settings.ENERGY_MODES_PROVIDER"/>
+ </intent-filter>
+ </provider>
<activity
android:name=".device.eco.EnergyModesActivity"
diff --git a/Settings/src/com/android/tv/settings/device/eco/EnergyModesContentProvider.java b/Settings/src/com/android/tv/settings/device/eco/EnergyModesContentProvider.java
new file mode 100644
index 000000000..18265cde6
--- /dev/null
+++ b/Settings/src/com/android/tv/settings/device/eco/EnergyModesContentProvider.java
@@ -0,0 +1,215 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * 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.android.tv.settings.device.eco;
+
+import static android.Manifest.permission.MANAGE_LOW_POWER_STANDBY;
+
+import android.content.ContentProvider;
+import android.content.ContentValues;
+import android.content.Context;
+import android.database.Cursor;
+import android.graphics.drawable.Icon;
+import android.net.Uri;
+import android.os.Binder;
+import android.os.Bundle;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+
+import com.android.tv.settings.device.eco.EnergyModesHelper.EnergyMode;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * ContentProvider that provides methods to query and set Energy Modes.
+ */
+public class EnergyModesContentProvider extends ContentProvider {
+ /** Method to get available energy modes, default mode, and selected mode */
+ private static final String METHOD_GET_ENERGY_MODES = "getEnergyModes";
+
+ /**
+ * Method to set the selected energy mode.
+ * Requires permission MANAGE_LOW_POWER_STANDBY.
+ */
+ private static final String METHOD_SET_ENERGY_MODE = "setEnergyMode";
+
+ /** Key for a String representing the identifier of the default mode (may be null). */
+ private static final String KEY_DEFAULT_MODE = "default_mode";
+
+ /** Key for a String representing the currently selected mode. */
+ private static final String KEY_SELECTED_MODE = "selected_mode";
+
+ /** Key for a List of Bundle representing the available energy modes. */
+ private static final String KEY_ENERGY_MODES = "energy_modes";
+
+ /** Key for a String representing the identifier for an energy mode. */
+ private static final String KEY_IDENTIFIER = "identifier";
+
+ /** Key for an Icon representing the icon of an energy mode. */
+ private static final String KEY_ICON = "icon";
+
+ /** Key for an int representing the color of an energy mode (in ARGB). */
+ private static final String KEY_COLOR = "color";
+
+ /** Key for a String representing the title of an energy mode. */
+ private static final String KEY_TITLE = "title";
+
+ /** Key for a String representing the subtitle of an energy mode. */
+ private static final String KEY_SUBTITLE = "subtitle";
+
+ /** Key for a String representing the description of an energy mode. */
+ private static final String KEY_DESCRIPTION = "description";
+
+ /** Key for a String representing a short description of an energy mode. */
+ private static final String KEY_SHORT_DESCRIPTION = "short_description";
+
+ /** Key for a String array representing the (human-friendly) features of an energy mode. */
+ private static final String KEY_FEATURES_LIST = "features_list";
+
+ /** Key for a String array representing the features of an energy mode. */
+ private static final String KEY_FEATURES = "features";
+
+ @Override
+ public boolean onCreate() {
+ return true;
+ }
+
+ @Nullable
+ @Override
+ public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection,
+ @Nullable String[] selectionArgs, @Nullable String sortOrder) {
+ throw new UnsupportedOperationException("query operation not supported currently.");
+ }
+
+ @Nullable
+ @Override
+ public String getType(@NonNull Uri uri) {
+ throw new UnsupportedOperationException("getType operation not supported currently.");
+ }
+
+ @Nullable
+ @Override
+ public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
+ throw new UnsupportedOperationException("insert operation not supported currently.");
+ }
+
+ @Override
+ public int delete(@NonNull Uri uri, @Nullable String selection,
+ @Nullable String[] selectionArgs) {
+ throw new UnsupportedOperationException("delete operation not supported currently.");
+ }
+
+ @Override
+ public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection,
+ @Nullable String[] selectionArgs) {
+ throw new UnsupportedOperationException("update operation not supported currently.");
+ }
+
+ @Override
+ public Bundle call(String method, String arg, Bundle extras) {
+ if (METHOD_GET_ENERGY_MODES.equals(method)) {
+ return getEnergyModesFromBinder();
+ } else if (METHOD_SET_ENERGY_MODE.equals(method)) {
+ return setEnergyModeFromBinder(arg);
+ }
+
+ throw new IllegalArgumentException("Unknown method name");
+ }
+
+ private Bundle getEnergyModesFromBinder() {
+ final long ident = Binder.clearCallingIdentity();
+ try {
+ return getEnergyModes();
+ } finally {
+ Binder.restoreCallingIdentity(ident);
+ }
+ }
+
+ private Bundle getEnergyModes() {
+ EnergyModesHelper energyModesHelper = new EnergyModesHelper(getContext());
+
+ final EnergyMode defaultMode = energyModesHelper.getDefaultEnergyMode();
+ final EnergyMode currentMode = energyModesHelper.updateEnergyMode();
+
+ Bundle bundle = new Bundle();
+ bundle.putString(KEY_DEFAULT_MODE, getModeIdentifier(defaultMode));
+ bundle.putString(KEY_SELECTED_MODE, getModeIdentifier(currentMode));
+ bundle.putParcelableList(KEY_ENERGY_MODES, getModes(energyModesHelper));
+ return bundle;
+ }
+
+ private Bundle setEnergyModeFromBinder(String identifier) {
+ getContext().enforceCallingOrSelfPermission(MANAGE_LOW_POWER_STANDBY, null);
+ final long ident = Binder.clearCallingIdentity();
+ try {
+ EnergyModesHelper energyModesHelper = new EnergyModesHelper(getContext());
+ EnergyMode energyMode = energyModesHelper.getEnergyMode(/* identifier= */ identifier);
+ if (energyMode == null) {
+ throw new IllegalArgumentException("Unknown energy mode: " + identifier);
+ }
+
+ energyModesHelper.setEnergyMode(energyMode);
+ } finally {
+ Binder.restoreCallingIdentity(ident);
+ }
+
+ return null;
+ }
+
+ @Nullable
+ private String getModeIdentifier(@Nullable EnergyMode mode) {
+ if (mode == null) {
+ return null;
+ }
+
+ return getContext().getString(mode.identifierRes);
+ }
+
+ @NonNull
+ private List<Bundle> getModes(@NonNull EnergyModesHelper helper) {
+ final List<Bundle> result = new ArrayList<>();
+ final List<EnergyMode> energyModes = helper.getEnergyModes();
+
+ for (EnergyMode mode : energyModes) {
+ result.add(convertEnergyModeToBundle(helper, mode));
+ }
+
+ return result;
+ }
+
+ @NonNull
+ private Bundle convertEnergyModeToBundle(
+ @NonNull EnergyModesHelper helper, @NonNull EnergyMode mode) {
+ Context context = getContext();
+ Bundle bundle = new Bundle();
+
+ bundle.putString(KEY_IDENTIFIER, getModeIdentifier(mode));
+ bundle.putParcelable(KEY_ICON, Icon.createWithResource(context, mode.iconRes));
+ bundle.putInt(KEY_COLOR, context.getColor(mode.colorRes));
+ bundle.putString(KEY_TITLE, context.getString(mode.titleRes));
+ bundle.putString(KEY_SUBTITLE, context.getString(mode.subtitleRes));
+ bundle.putString(KEY_DESCRIPTION, context.getString(mode.infoTextRes));
+ bundle.putString(KEY_SHORT_DESCRIPTION, context.getString(mode.infoTextRes));
+ bundle.putStringArray(KEY_FEATURES_LIST,
+ context.getResources().getStringArray(mode.featuresRes));
+ bundle.putStringArray(KEY_FEATURES,
+ helper.getAllowedFeatures(mode).toArray(new String[0]));
+
+ return bundle;
+ }
+}
diff --git a/Settings/src/com/android/tv/settings/device/eco/EnergyModesService.java b/Settings/src/com/android/tv/settings/device/eco/EnergyModesService.java
deleted file mode 100644
index 7f089a51a..000000000
--- a/Settings/src/com/android/tv/settings/device/eco/EnergyModesService.java
+++ /dev/null
@@ -1,130 +0,0 @@
-/*
- * Copyright (C) 2023 The Android Open Source Project
- *
- * 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.android.tv.settings.device.eco;
-
-import static android.Manifest.permission.MANAGE_LOW_POWER_STANDBY;
-
-import android.annotation.EnforcePermission;
-import android.app.Service;
-import android.content.Context;
-import android.content.Intent;
-import android.graphics.drawable.Icon;
-import android.os.Binder;
-import android.os.Bundle;
-import android.os.IBinder;
-
-import com.android.tv.settings.device.eco.EnergyModesHelper.EnergyMode;
-
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * Service that provides methods to query and set Energy Modes.
- */
-public class EnergyModesService extends Service {
-
- private BinderService mService;
-
- @Override
- public void onCreate() {
- super.onCreate();
- mService = new BinderService(getApplication());
- }
-
- @Override
- public IBinder onBind(Intent intent) {
- return mService;
- }
-
- private class BinderService extends IEnergyModesService.Stub {
- private final Context mContext;
- private final EnergyModesHelper mHelper;
-
- BinderService(Context context) {
- mContext = context;
- mHelper = new EnergyModesHelper(context);
- }
-
- @Override
- public List<Bundle> getModes() {
- final long ident = Binder.clearCallingIdentity();
- try {
- final List<Bundle> result = new ArrayList<>();
- final List<EnergyMode> energyModes = mHelper.getEnergyModes();
- final EnergyMode currentMode = mHelper.updateEnergyMode();
-
- for (EnergyMode mode : energyModes) {
- result.add(convertToBundle(mode, mode == currentMode));
- }
-
- return result;
- } finally {
- Binder.restoreCallingIdentity(ident);
- }
- }
-
- private Bundle convertToBundle(EnergyMode mode, boolean selected) {
- Bundle bundle = new Bundle();
-
- bundle.putBoolean(KEY_SELECTED, selected);
- bundle.putString(KEY_IDENTIFIER, getString(mode.identifierRes));
- bundle.putParcelable(KEY_ICON, Icon.createWithResource(mContext, mode.iconRes));
- bundle.putInt(KEY_COLOR, getColor(mode.colorRes));
- bundle.putString(KEY_TITLE, getString(mode.titleRes));
- bundle.putString(KEY_SUBTITLE, getString(mode.subtitleRes));
- bundle.putString(KEY_DESCRIPTION, getString(mode.infoTextRes));
- bundle.putString(KEY_SHORT_DESCRIPTION, getString(mode.infoTextRes));
- bundle.putStringArray(KEY_FEATURES_LIST,
- getResources().getStringArray(mode.featuresRes));
- bundle.putStringArray(KEY_FEATURES,
- mHelper.getAllowedFeatures(mode).toArray(new String[0]));
-
- return bundle;
- }
-
- @EnforcePermission(MANAGE_LOW_POWER_STANDBY)
- @Override
- public void setMode(String identifier) {
- super.setMode_enforcePermission();
-
- final long ident = Binder.clearCallingIdentity();
- try {
- final EnergyMode mode = mHelper.getEnergyMode(identifier);
- if (mode == null) {
- throw new IllegalArgumentException("Unknown energy mode: " + identifier);
- }
- mHelper.setEnergyMode(mode);
- } finally {
- Binder.restoreCallingIdentity(ident);
- }
- }
-
- @Override
- public String getDefaultMode() {
- final long ident = Binder.clearCallingIdentity();
- try {
- final EnergyMode mode = mHelper.getDefaultEnergyMode();
- if (mode == null) {
- return null;
- }
- return getString(mode.identifierRes);
- } finally {
- Binder.restoreCallingIdentity(ident);
- }
- }
- }
-}
diff --git a/Settings/src/com/android/tv/settings/device/eco/IEnergyModesService.aidl b/Settings/src/com/android/tv/settings/device/eco/IEnergyModesService.aidl
deleted file mode 100644
index 6f5c9269b..000000000
--- a/Settings/src/com/android/tv/settings/device/eco/IEnergyModesService.aidl
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
- * Copyright (C) 2023 The Android Open Source Project
- *
- * 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.android.tv.settings.device.eco;
-
-import android.os.Bundle;
-
-/** {@hide} */
-interface IEnergyModesService {
- /** Key for a boolean representing whether the energy mode is currently selected. */
- const String KEY_SELECTED = "selected";
-
- /** Key for a String representing the identifier for an energy mode. */
- const String KEY_IDENTIFIER = "identifier";
-
- /** Key for an Icon representing the icon of an energy mode. */
- const String KEY_ICON = "icon";
-
- /** Key for an int representing the color of an energy mode (in ARGB). */
- const String KEY_COLOR = "color";
-
- /** Key for a String representing the title of an energy mode. */
- const String KEY_TITLE = "title";
-
- /** Key for a String representing the subtitle of an energy mode. */
- const String KEY_SUBTITLE = "subtitle";
-
- /** Key for a String representing the description of an energy mode. */
- const String KEY_DESCRIPTION = "description";
-
- /** Key for a String representing a short description of an energy mode. */
- const String KEY_SHORT_DESCRIPTION = "short_description";
-
- /** Key for a String array representing the (human-friendly) features of an energy mode. */
- const String KEY_FEATURES_LIST = "features_list";
-
- /** Key for a String array representing the features of an energy mode. */
- const String KEY_FEATURES = "features";
-
- /** Returns the list of available energy modes */
- List<Bundle> getModes();
-
- /** Sets the selected energy mode by the given identifier */
- @EnforcePermission("MANAGE_LOW_POWER_STANDBY")
- void setMode(String identifier);
-
- /** Returns the identifier of the default energy mode */
- String getDefaultMode();
-}