Avoid add preferenc again if it already in the PreferenceGroup

Bug: 177406865
Bug: 185187729
Test: make SettingsRoboTests
Test: make SettingsGoogleRoboTests
Change-Id: I69f5033c0a07d846c340b9871e69a2cdbe0bb2aa
This commit is contained in:
ykhung
2021-04-23 01:07:09 +08:00
parent 09f6086bea
commit 5c23d65901
5 changed files with 76 additions and 27 deletions

View File

@@ -317,6 +317,7 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
}
int prefIndex = mAppListPrefGroup.getPreferenceCount();
for (BatteryDiffEntry entry : entries) {
boolean isAdded = false;
final String appLabel = entry.getAppLabel();
final Drawable appIcon = entry.getAppIcon();
if (TextUtils.isEmpty(appLabel) || appIcon == null) {
@@ -324,8 +325,13 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
continue;
}
final String prefKey = entry.mBatteryHistEntry.getKey();
PowerGaugePreference pref =
(PowerGaugePreference) mPreferenceCache.get(prefKey);
PowerGaugePreference pref = mAppListPrefGroup.findPreference(prefKey);
if (pref != null) {
isAdded = true;
Log.w(TAG, "preference should be removed for\n" + entry);
} else {
pref = (PowerGaugePreference) mPreferenceCache.get(prefKey);
}
// Creates new innstance if cached preference is not found.
if (pref == null) {
pref = new PowerGaugePreference(mPrefContext);
@@ -340,7 +346,9 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
// Sets the BatteryDiffEntry to preference for launching detailed page.
pref.setBatteryDiffEntry(entry);
setPreferenceSummary(pref, entry);
mAppListPrefGroup.addPreference(pref);
if (!isAdded) {
mAppListPrefGroup.addPreference(pref);
}
prefIndex++;
}
}

View File

@@ -97,8 +97,18 @@ public class BatteryChartView extends AppCompatImageView implements View.OnClick
public void setLevels(int[] levels) {
// We should provide trapezoid count + 1 data to draw all trapezoids.
mLevels = levels.length == mTrapezoidCount + 1 ? levels : null;
setClickable(mLevels != null);
setClickable(false);
invalidate();
if (mLevels == null) {
return;
}
// Sets the chart is clickable if there is at least one valid item in it.
for (int index = 0; index < mLevels.length; index++) {
if (mLevels[index] != 0) {
setClickable(true);
break;
}
}
}
/** Sets the selected group index to draw highlight effect. */

View File

@@ -71,6 +71,7 @@ public final class ConvertUtils {
private static String sZoneId;
private static SimpleDateFormat sSimpleDateFormat;
private static SimpleDateFormat sSimpleDateFormatForHour;
private ConvertUtils() {}
@@ -139,10 +140,27 @@ public final class ConvertUtils {
sZoneId = currentZoneId;
sSimpleDateFormat =
new SimpleDateFormat("MMM dd,yyyy HH:mm:ss", Locale.ENGLISH);
sSimpleDateFormatForHour = null;
}
return sSimpleDateFormat.format(new Date(timestamp));
}
/** Converts UTC timestamp to local time hour data. */
public static int utcToLocalTimeHour(long timestamp) {
final String currentZoneId = TimeZone.getDefault().getID();
if (!currentZoneId.equals(sZoneId) || sSimpleDateFormatForHour == null) {
sZoneId = currentZoneId;
sSimpleDateFormat = null;
sSimpleDateFormatForHour = new SimpleDateFormat("HH", Locale.ENGLISH);
}
try {
return Integer.parseInt(
sSimpleDateFormatForHour.format(new Date(timestamp)));
} catch (NumberFormatException e) {
return Integer.MIN_VALUE;
}
}
/** Gets indexed battery usage data for each corresponding time slot. */
public static Map<Integer, List<BatteryDiffEntry>> getIndexedUsageMap(
final Context context,