Format battery java code

After this CL, we can follow Guideline:go/hc-mainline-dev#format-code to keep java format consistent.

Test: manual
Bug: 304439460
Change-Id: I5bb77f81b0bd9be618e34942eaaee8296bc42796
This commit is contained in:
Jun Lan
2023-11-02 18:55:52 +08:00
parent da70c86bda
commit cca804e1ed
127 changed files with 4343 additions and 3581 deletions

View File

@@ -58,7 +58,7 @@ import java.util.Set;
/** Controller for battery usage breakdown preference group. */
public class BatteryUsageBreakdownController extends BasePreferenceController
implements LifecycleObserver, OnResume, OnDestroy {
implements LifecycleObserver, OnResume, OnDestroy {
private static final String TAG = "BatteryUsageBreakdownController";
private static final String ROOT_PREFERENCE_KEY = "battery_usage_breakdown";
private static final String FOOTER_PREFERENCE_KEY = "battery_usage_footer";
@@ -74,45 +74,33 @@ public class BatteryUsageBreakdownController extends BasePreferenceController
private final MetricsFeatureProvider mMetricsFeatureProvider;
private final Handler mHandler = new Handler(Looper.getMainLooper());
@VisibleForTesting
final Map<String, Preference> mPreferenceCache = new ArrayMap<>();
@VisibleForTesting final Map<String, Preference> mPreferenceCache = new ArrayMap<>();
private int mSpinnerPosition;
private String mSlotTimestamp;
@VisibleForTesting
Context mPrefContext;
@VisibleForTesting
PreferenceCategory mRootPreference;
@VisibleForTesting
SpinnerPreference mSpinnerPreference;
@VisibleForTesting
PreferenceGroup mAppListPreferenceGroup;
@VisibleForTesting
FooterPreference mFooterPreference;
@VisibleForTesting
BatteryDiffData mBatteryDiffData;
@VisibleForTesting
String mPercentLessThanThresholdText;
@VisibleForTesting
boolean mIsHighlightSlot;
@VisibleForTesting
String mAnomalyEventId;
@VisibleForTesting
String mAnomalyEntryKey;
@VisibleForTesting
String mAnomalyHintString;
@VisibleForTesting
String mAnomalyHintPrefKey;
@VisibleForTesting Context mPrefContext;
@VisibleForTesting PreferenceCategory mRootPreference;
@VisibleForTesting SpinnerPreference mSpinnerPreference;
@VisibleForTesting PreferenceGroup mAppListPreferenceGroup;
@VisibleForTesting FooterPreference mFooterPreference;
@VisibleForTesting BatteryDiffData mBatteryDiffData;
@VisibleForTesting String mPercentLessThanThresholdText;
@VisibleForTesting boolean mIsHighlightSlot;
@VisibleForTesting String mAnomalyEventId;
@VisibleForTesting String mAnomalyEntryKey;
@VisibleForTesting String mAnomalyHintString;
@VisibleForTesting String mAnomalyHintPrefKey;
public BatteryUsageBreakdownController(
Context context, Lifecycle lifecycle, SettingsActivity activity,
Context context,
Lifecycle lifecycle,
SettingsActivity activity,
InstrumentedPreferenceFragment fragment) {
super(context, ROOT_PREFERENCE_KEY);
mActivity = activity;
mFragment = fragment;
mMetricsFeatureProvider =
FeatureFactory.getFeatureFactory().getMetricsFeatureProvider();
mMetricsFeatureProvider = FeatureFactory.getFeatureFactory().getMetricsFeatureProvider();
if (lifecycle != null) {
lifecycle.addObserver(this);
}
@@ -133,7 +121,7 @@ public class BatteryUsageBreakdownController extends BasePreferenceController
@Override
public void onDestroy() {
mHandler.removeCallbacksAndMessages(/*token=*/ null);
mHandler.removeCallbacksAndMessages(/* token= */ null);
mPreferenceCache.clear();
mAppListPreferenceGroup.removeAll();
}
@@ -149,14 +137,17 @@ public class BatteryUsageBreakdownController extends BasePreferenceController
}
private boolean isAnomalyBatteryDiffEntry(BatteryDiffEntry entry) {
return mIsHighlightSlot && mAnomalyEntryKey != null
return mIsHighlightSlot
&& mAnomalyEntryKey != null
&& mAnomalyEntryKey.equals(entry.getKey());
}
private String getActionKey(BatteryDiffEntry entry) {
final String actionKey = TextUtils.isEmpty(entry.getPackageName())
? PACKAGE_NAME_NONE : entry.getPackageName();
return !isAnomalyBatteryDiffEntry(entry) ? actionKey : actionKey + "|" + mAnomalyEventId;
final String actionKey =
TextUtils.isEmpty(entry.getPackageName())
? PACKAGE_NAME_NONE
: entry.getPackageName();
return !isAnomalyBatteryDiffEntry(entry) ? actionKey : actionKey + "|" + mAnomalyEventId;
}
@Override
@@ -174,15 +165,24 @@ public class BatteryUsageBreakdownController extends BasePreferenceController
/* pageId */ SettingsEnums.OPEN_BATTERY_USAGE,
getActionKey(diffEntry),
(int) Math.round(diffEntry.getPercentage()));
Log.d(TAG, String.format("handleClick() label=%s key=%s package=%s",
diffEntry.getAppLabel(), diffEntry.getKey(), diffEntry.getPackageName()));
final String anomalyHintPrefKey = isAnomalyBatteryDiffEntry(diffEntry)
? mAnomalyHintPrefKey : null;
final String anomalyHintText = isAnomalyBatteryDiffEntry(diffEntry)
? mAnomalyHintString : null;
AdvancedPowerUsageDetail.startBatteryDetailPage(mActivity, mFragment.getMetricsCategory(),
diffEntry, powerPref.getPercentage(), mSlotTimestamp,
/*showTimeInformation=*/ true, anomalyHintPrefKey, anomalyHintText);
Log.d(
TAG,
String.format(
"handleClick() label=%s key=%s package=%s",
diffEntry.getAppLabel(), diffEntry.getKey(), diffEntry.getPackageName()));
final String anomalyHintPrefKey =
isAnomalyBatteryDiffEntry(diffEntry) ? mAnomalyHintPrefKey : null;
final String anomalyHintText =
isAnomalyBatteryDiffEntry(diffEntry) ? mAnomalyHintString : null;
AdvancedPowerUsageDetail.startBatteryDetailPage(
mActivity,
mFragment.getMetricsCategory(),
diffEntry,
powerPref.getPercentage(),
mSlotTimestamp,
/* showTimeInformation= */ true,
anomalyHintPrefKey,
anomalyHintText);
return true;
}
@@ -194,15 +194,16 @@ public class BatteryUsageBreakdownController extends BasePreferenceController
mSpinnerPreference = screen.findPreference(SPINNER_PREFERENCE_KEY);
mAppListPreferenceGroup = screen.findPreference(APP_LIST_PREFERENCE_KEY);
mFooterPreference = screen.findPreference(FOOTER_PREFERENCE_KEY);
mPercentLessThanThresholdText = mPrefContext.getString(
R.string.battery_usage_less_than_percent,
Utils.formatPercentage(BatteryDiffData.SMALL_PERCENTAGE_THRESHOLD, false));
mPercentLessThanThresholdText =
mPrefContext.getString(
R.string.battery_usage_less_than_percent,
Utils.formatPercentage(BatteryDiffData.SMALL_PERCENTAGE_THRESHOLD, false));
mAppListPreferenceGroup.setOrderingAsAdded(false);
mSpinnerPreference.initializeSpinner(
new String[]{
mPrefContext.getString(R.string.battery_usage_spinner_view_by_apps),
mPrefContext.getString(R.string.battery_usage_spinner_view_by_systems)
new String[] {
mPrefContext.getString(R.string.battery_usage_spinner_view_by_apps),
mPrefContext.getString(R.string.battery_usage_spinner_view_by_systems)
},
new AdapterView.OnItemSelectedListener() {
@Override
@@ -210,35 +211,38 @@ public class BatteryUsageBreakdownController extends BasePreferenceController
AdapterView<?> parent, View view, int position, long id) {
if (mSpinnerPosition != position) {
mSpinnerPosition = position;
mHandler.post(() -> {
removeAndCacheAllUnusedPreferences();
addAllPreferences();
mMetricsFeatureProvider.action(
mPrefContext,
SettingsEnums.ACTION_BATTERY_USAGE_SPINNER,
mSpinnerPosition);
});
mHandler.post(
() -> {
removeAndCacheAllUnusedPreferences();
addAllPreferences();
mMetricsFeatureProvider.action(
mPrefContext,
SettingsEnums.ACTION_BATTERY_USAGE_SPINNER,
mSpinnerPosition);
});
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
public void onNothingSelected(AdapterView<?> parent) {}
});
}
/**
* Updates UI when the battery usage is updated.
* @param slotUsageData The battery usage diff data for the selected slot. This is used in
* the app list.
*
* @param slotUsageData The battery usage diff data for the selected slot. This is used in the
* app list.
* @param slotTimestamp The selected slot timestamp information. This is used in the battery
* usage breakdown category.
* @param isAllUsageDataEmpty Whether all the battery usage data is null or empty. This is
* used when showing the footer.
* usage breakdown category.
* @param isAllUsageDataEmpty Whether all the battery usage data is null or empty. This is used
* when showing the footer.
*/
void handleBatteryUsageUpdated(
BatteryDiffData slotUsageData, String slotTimestamp,
boolean isAllUsageDataEmpty, boolean isHighlightSlot,
BatteryDiffData slotUsageData,
String slotTimestamp,
boolean isAllUsageDataEmpty,
boolean isHighlightSlot,
Optional<AnomalyEventWrapper> optionalAnomalyEventWrapper) {
mBatteryDiffData = slotUsageData;
mSlotTimestamp = slotTimestamp;
@@ -247,14 +251,15 @@ public class BatteryUsageBreakdownController extends BasePreferenceController
if (optionalAnomalyEventWrapper != null) {
final AnomalyEventWrapper anomalyEventWrapper =
optionalAnomalyEventWrapper.orElse(null);
mAnomalyEventId = anomalyEventWrapper != null
? anomalyEventWrapper.getEventId() : null;
mAnomalyEntryKey = anomalyEventWrapper != null
? anomalyEventWrapper.getAnomalyEntryKey() : null;
mAnomalyHintString = anomalyEventWrapper != null
? anomalyEventWrapper.getAnomalyHintString() : null;
mAnomalyHintPrefKey = anomalyEventWrapper != null
? anomalyEventWrapper.getAnomalyHintPrefKey() : null;
mAnomalyEventId = anomalyEventWrapper != null ? anomalyEventWrapper.getEventId() : null;
mAnomalyEntryKey =
anomalyEventWrapper != null ? anomalyEventWrapper.getAnomalyEntryKey() : null;
mAnomalyHintString =
anomalyEventWrapper != null ? anomalyEventWrapper.getAnomalyHintString() : null;
mAnomalyHintPrefKey =
anomalyEventWrapper != null
? anomalyEventWrapper.getAnomalyHintPrefKey()
: null;
}
showCategoryTitle(slotTimestamp);
@@ -263,35 +268,39 @@ public class BatteryUsageBreakdownController extends BasePreferenceController
}
private void showCategoryTitle(String slotTimestamp) {
mRootPreference.setTitle(slotTimestamp == null
? mPrefContext.getString(
R.string.battery_usage_breakdown_title_since_last_full_charge)
: mPrefContext.getString(
R.string.battery_usage_breakdown_title_for_slot, slotTimestamp));
mRootPreference.setTitle(
slotTimestamp == null
? mPrefContext.getString(
R.string.battery_usage_breakdown_title_since_last_full_charge)
: mPrefContext.getString(
R.string.battery_usage_breakdown_title_for_slot, slotTimestamp));
mRootPreference.setVisible(true);
}
private void showFooterPreference(boolean isAllBatteryUsageEmpty) {
mFooterPreference.setTitle(mPrefContext.getString(
isAllBatteryUsageEmpty
? R.string.battery_usage_screen_footer_empty
: R.string.battery_usage_screen_footer));
mFooterPreference.setTitle(
mPrefContext.getString(
isAllBatteryUsageEmpty
? R.string.battery_usage_screen_footer_empty
: R.string.battery_usage_screen_footer));
mFooterPreference.setVisible(true);
}
private void showSpinnerAndAppList() {
if (mBatteryDiffData == null) {
mHandler.post(() -> {
removeAndCacheAllUnusedPreferences();
});
mHandler.post(
() -> {
removeAndCacheAllUnusedPreferences();
});
return;
}
mSpinnerPreference.setVisible(true);
mAppListPreferenceGroup.setVisible(true);
mHandler.post(() -> {
removeAndCacheAllUnusedPreferences();
addAllPreferences();
});
mHandler.post(
() -> {
removeAndCacheAllUnusedPreferences();
addAllPreferences();
});
}
private List<BatteryDiffEntry> getBatteryDiffEntries() {
@@ -348,8 +357,11 @@ public class BatteryUsageBreakdownController extends BasePreferenceController
}
prefIndex++;
}
Log.d(TAG, String.format("addAllPreferences() is finished in %d/ms",
(System.currentTimeMillis() - start)));
Log.d(
TAG,
String.format(
"addAllPreferences() is finished in %d/ms",
(System.currentTimeMillis() - start)));
}
@VisibleForTesting
@@ -372,8 +384,7 @@ public class BatteryUsageBreakdownController extends BasePreferenceController
}
@VisibleForTesting
void setPreferencePercentage(
PowerGaugePreference preference, BatteryDiffEntry entry) {
void setPreferencePercentage(PowerGaugePreference preference, BatteryDiffEntry entry) {
preference.setPercentage(
entry.getPercentage() < BatteryDiffData.SMALL_PERCENTAGE_THRESHOLD
? mPercentLessThanThresholdText
@@ -383,11 +394,13 @@ public class BatteryUsageBreakdownController extends BasePreferenceController
}
@VisibleForTesting
void setPreferenceSummary(
PowerGaugePreference preference, BatteryDiffEntry entry) {
void setPreferenceSummary(PowerGaugePreference preference, BatteryDiffEntry entry) {
preference.setSummary(
BatteryUtils.buildBatteryUsageTimeSummary(mPrefContext, entry.isSystemEntry(),
entry.mForegroundUsageTimeInMs, entry.mBackgroundUsageTimeInMs,
BatteryUtils.buildBatteryUsageTimeSummary(
mPrefContext,
entry.isSystemEntry(),
entry.mForegroundUsageTimeInMs,
entry.mBackgroundUsageTimeInMs,
entry.mScreenOnTimeInMs));
}
}