CMParts: Move resource utility methods into a util class
Other settings can also make use of these methods.
Change-Id: Iab31a8a43b62c7700b499537ccb854f9a5768608
diff --git a/src/org/cyanogenmod/cmparts/livedisplay/LiveDisplay.java b/src/org/cyanogenmod/cmparts/livedisplay/LiveDisplay.java
index 6cca88e..bf6ae60 100644
--- a/src/org/cyanogenmod/cmparts/livedisplay/LiveDisplay.java
+++ b/src/org/cyanogenmod/cmparts/livedisplay/LiveDisplay.java
@@ -34,6 +34,7 @@
import org.cyanogenmod.cmparts.search.BaseSearchIndexProvider;
import org.cyanogenmod.cmparts.search.SearchIndexableRaw;
import org.cyanogenmod.cmparts.search.Searchable;
+import org.cyanogenmod.cmparts.utils.ResourceUtils;
import java.util.Collections;
import java.util.List;
@@ -227,29 +228,6 @@
SettingsHelper.get(getActivity()).stopWatching(this);
}
- private static String getStringForResourceName(Resources res,
- String resourceName, String defaultValue) {
- int resId = res.getIdentifier(resourceName, "string", "org.cyanogenmod.cmparts");
- if (resId <= 0) {
- Log.e(TAG, "No resource found for " + resourceName);
- return defaultValue;
- } else {
- return res.getString(resId);
- }
- }
-
- private static String getLocalizedProfileName(Resources res, String profileName) {
- String name = profileName.toLowerCase().replace(" ", "_");
- String nameRes = String.format(COLOR_PROFILE_TITLE, name);
- return getStringForResourceName(res, nameRes, profileName);
- }
-
- private static String getLocalizedProfileSummary(Resources res, String profileName) {
- String name = profileName.toLowerCase().replace(" ", "_");
- String summaryRes = String.format(COLOR_PROFILE_SUMMARY, name);
- return getStringForResourceName(res, summaryRes, null);
- }
-
private boolean updateDisplayModes() {
final DisplayMode[] modes = mHardware.getDisplayModes();
if (modes == null || modes.length == 0) {
@@ -264,10 +242,12 @@
mColorProfileSummaries = new String[modes.length];
for (int i = 0; i < modes.length; i++) {
values[i] = String.valueOf(modes[i].id);
- entries[i] = getLocalizedProfileName(getResources(), modes[i].name);
+ entries[i] = ResourceUtils.getLocalizedString(
+ getResources(), modes[i].name, COLOR_PROFILE_TITLE);
// Populate summary
- String summary = getLocalizedProfileSummary(getResources(), modes[i].name);
+ String summary = ResourceUtils.getLocalizedString(
+ getResources(), modes[i].name, COLOR_PROFILE_SUMMARY);
if (summary != null) {
summary = String.format("%s - %s", entries[i], summary);
}
@@ -403,7 +383,8 @@
DisplayMode[] modes = CMHardwareManager.getInstance(context).getDisplayModes();
if (modes != null && modes.length > 0) {
for (DisplayMode mode : modes) {
- result.add(getLocalizedProfileName(context.getResources(), mode.name));
+ result.add(ResourceUtils.getLocalizedString(
+ context.getResources(), mode.name, COLOR_PROFILE_TITLE));
}
}
}
diff --git a/src/org/cyanogenmod/cmparts/utils/ResourceUtils.java b/src/org/cyanogenmod/cmparts/utils/ResourceUtils.java
new file mode 100644
index 0000000..85ea311
--- /dev/null
+++ b/src/org/cyanogenmod/cmparts/utils/ResourceUtils.java
@@ -0,0 +1,44 @@
+/**
+ * Copyright (C) 2016 The CyanogenMod 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 org.cyanogenmod.cmparts.utils;
+
+import android.content.res.Resources;
+import android.util.Log;
+
+public class ResourceUtils {
+
+ private static final String TAG = ResourceUtils.class.getSimpleName();
+
+ public static String getLocalizedString(final Resources res,
+ final String stringName,
+ final String stringFormat) {
+ final String name = stringName.toLowerCase().replace(" ", "_");
+ final String nameRes = String.format(stringFormat, name);
+ return getStringForResourceName(res, nameRes, stringName);
+ }
+
+ public static String getStringForResourceName(final Resources res,
+ final String resourceName,
+ final String defaultValue) {
+ final int resId = res.getIdentifier(resourceName, "string", "org.cyanogenmod.cmparts");
+ if (resId <= 0) {
+ Log.e(TAG, "No resource found for " + resourceName);
+ return defaultValue;
+ } else {
+ return res.getString(resId);
+ }
+ }
+}