Use if instead of switch for resources

Converting to Soong will move some code from directly compiled
into the app to compiled into an Android library and then
shared between the app and the tests.  This will cause resource
IDs in the library to become non-final, which means they can
no longer be used in case statements.  Convert affect case
statements to if blocks.

Test: m RunSettingsRoboTests
Change-Id: I25742a374f06d3fa4decbfc0d223a350acc50881
This commit is contained in:
Colin Cross
2019-05-02 19:56:42 -07:00
parent 90cb5628bb
commit 807e861105
12 changed files with 297 additions and 367 deletions

View File

@@ -197,8 +197,7 @@ public class ToggleAccessibilityServicePreferenceFragment
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.permission_enable_allow_button:
if (view.getId() == R.id.permission_enable_allow_button) {
if (isFullDiskEncrypted()) {
String title = createConfirmCredentialReasonMessage();
Intent intent = ConfirmDeviceCredentialActivity.createIntent(title, null);
@@ -207,17 +206,13 @@ public class ToggleAccessibilityServicePreferenceFragment
} else {
handleConfirmServiceEnabled(true);
}
break;
case R.id.permission_enable_deny_button:
} else if (view.getId() == R.id.permission_enable_deny_button) {
handleConfirmServiceEnabled(false);
break;
case R.id.permission_disable_stop_button:
} else if (view.getId() == R.id.permission_disable_stop_button) {
handleConfirmServiceEnabled(false);
break;
case R.id.permission_disable_cancel_button:
} else if (view.getId() == R.id.permission_disable_cancel_button) {
handleConfirmServiceEnabled(true);
break;
default:
} else {
throw new IllegalArgumentException();
}
mDialog.dismiss();

View File

@@ -690,22 +690,18 @@ public class ManageApplications extends InstrumentedFragment
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int menuId = item.getItemId();
switch (item.getItemId()) {
case R.id.sort_order_alpha:
case R.id.sort_order_size:
int i = item.getItemId();
if (i == R.id.sort_order_alpha || i == R.id.sort_order_size) {
if (mApplications != null) {
mApplications.rebuild(menuId);
}
break;
case R.id.show_system:
case R.id.hide_system:
} else if (i == R.id.show_system || i == R.id.hide_system) {
mShowSystem = !mShowSystem;
mApplications.rebuild();
break;
case R.id.reset_app_preferences:
} else if (i == R.id.reset_app_preferences) {
mResetAppsHelper.buildResetDialog();
return true;
case R.id.advanced:
} else if (i == R.id.advanced) {
if (mListType == LIST_TYPE_NOTIFICATION) {
new SubSettingLauncher(getContext())
.setDestination(ConfigureNotificationSettings.class.getName())
@@ -719,8 +715,7 @@ public class ManageApplications extends InstrumentedFragment
startActivityForResult(intent, ADVANCED_SETTINGS);
}
return true;
default:
// Handle the home button
} else {// Handle the home button
return false;
}
updateOptionsMenu();
@@ -1133,8 +1128,7 @@ public class ManageApplications extends InstrumentedFragment
ApplicationsState.FILTER_DOWNLOADED_AND_LAUNCHER);
}
}
switch (mLastSortMode) {
case R.id.sort_order_size:
if (mLastSortMode == R.id.sort_order_size) {
switch (mWhichSize) {
case SIZE_INTERNAL:
comparatorObj = ApplicationsState.INTERNAL_SIZE_COMPARATOR;
@@ -1146,16 +1140,12 @@ public class ManageApplications extends InstrumentedFragment
comparatorObj = ApplicationsState.SIZE_COMPARATOR;
break;
}
break;
case R.id.sort_order_recent_notification:
} else if (mLastSortMode == R.id.sort_order_recent_notification) {
comparatorObj = AppStateNotificationBridge.RECENT_NOTIFICATION_COMPARATOR;
break;
case R.id.sort_order_frequent_notification:
} else if (mLastSortMode == R.id.sort_order_frequent_notification) {
comparatorObj = AppStateNotificationBridge.FREQUENCY_NOTIFICATION_COMPARATOR;
break;
default:
} else {
comparatorObj = ApplicationsState.ALPHA_COMPARATOR;
break;
}
filterObj = new CompoundFilter(filterObj, ApplicationsState.FILTER_NOT_HIDE);

View File

@@ -424,14 +424,14 @@ public class PrivateVolumeSettings extends SettingsPreferenceFragment {
public boolean onOptionsItemSelected(MenuItem item) {
final Context context = getActivity();
final Bundle args = new Bundle();
switch (item.getItemId()) {
case R.id.storage_rename:
int i = item.getItemId();
if (i == R.id.storage_rename) {
RenameFragment.show(this, mVolume);
return true;
case R.id.storage_mount:
} else if (i == R.id.storage_mount) {
new MountTask(context, mVolume).execute();
return true;
case R.id.storage_unmount:
} else if (i == R.id.storage_unmount) {
args.putString(VolumeInfo.EXTRA_VOLUME_ID, mVolume.getId());
new SubSettingLauncher(context)
.setDestination(PrivateVolumeUnmount.class.getCanonicalName())
@@ -440,7 +440,7 @@ public class PrivateVolumeSettings extends SettingsPreferenceFragment {
.setArguments(args)
.launch();
return true;
case R.id.storage_format:
} else if (i == R.id.storage_format) {
args.putString(VolumeInfo.EXTRA_VOLUME_ID, mVolume.getId());
new SubSettingLauncher(context)
.setDestination(PrivateVolumeFormat.class.getCanonicalName())
@@ -449,12 +449,12 @@ public class PrivateVolumeSettings extends SettingsPreferenceFragment {
.setArguments(args)
.launch();
return true;
case R.id.storage_migrate:
} else if (i == R.id.storage_migrate) {
final Intent intent = new Intent(context, StorageWizardMigrateConfirm.class);
intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, mVolume.getId());
startActivity(intent);
return true;
case R.id.storage_free:
} else if (i == R.id.storage_free) {
final Intent deletion_helper_intent =
new Intent(StorageManager.ACTION_MANAGE_STORAGE);
startActivity(deletion_helper_intent);
@@ -476,8 +476,7 @@ public class PrivateVolumeSettings extends SettingsPreferenceFragment {
itemTitleId = 0;
}
Intent intent = null;
switch (itemTitleId) {
case R.string.storage_detail_apps: {
if (itemTitleId == R.string.storage_detail_apps) {
Bundle args = new Bundle();
args.putString(ManageApplications.EXTRA_CLASSNAME,
StorageUseActivity.class.getName());
@@ -492,41 +491,28 @@ public class PrivateVolumeSettings extends SettingsPreferenceFragment {
.setTitleRes(R.string.apps_storage)
.setSourceMetricsCategory(getMetricsCategory())
.toIntent();
} break;
case R.string.storage_detail_images: {
} else if (itemTitleId == R.string.storage_detail_images) {
intent = getIntentForStorage(AUTHORITY_MEDIA, "images_root");
} break;
case R.string.storage_detail_videos: {
} else if (itemTitleId == R.string.storage_detail_videos) {
intent = getIntentForStorage(AUTHORITY_MEDIA, "videos_root");
} break;
case R.string.storage_detail_audio: {
} else if (itemTitleId == R.string.storage_detail_audio) {
intent = getIntentForStorage(AUTHORITY_MEDIA, "audio_root");
} break;
case R.string.storage_detail_system: {
} else if (itemTitleId == R.string.storage_detail_system) {
SystemInfoFragment.show(this);
return true;
}
case R.string.storage_detail_other: {
} else if (itemTitleId == R.string.storage_detail_other) {
OtherInfoFragment.show(this, mStorageManager.getBestVolumeDescription(mVolume),
mSharedVolume, userId);
return true;
}
case R.string.storage_detail_cached: {
} else if (itemTitleId == R.string.storage_detail_cached) {
ConfirmClearCacheFragment.show(this);
return true;
}
case R.string.storage_menu_explore: {
} else if (itemTitleId == R.string.storage_menu_explore) {
intent = mSharedVolume.buildBrowseIntent();
} break;
case 0: {
} else if (itemTitleId == 0) {
UserInfoFragment.show(this, pref.getTitle(), pref.getSummary());
return true;
}
}
if (intent != null) {
intent.putExtra(Intent.EXTRA_USER_ID, userId);
@@ -569,36 +555,32 @@ public class PrivateVolumeSettings extends SettingsPreferenceFragment {
} catch (NumberFormatException e) {
itemTitleId = 0;
}
switch (itemTitleId) {
case R.string.storage_detail_system: {
// Cannot display 'Other' until all known items are accounted for.
if (itemTitleId == R.string.storage_detail_system) {
updatePreference(item, mSystemSize);
accountedSize += mSystemSize;
if (LOGV) Log.v(TAG, "mSystemSize: " + mSystemSize
+ " accountedSize: " + accountedSize);
} break;
case R.string.storage_detail_apps: {
} else if (itemTitleId == R.string.storage_detail_apps) {
updatePreference(item, details.appsSize.get(userId));
accountedSize += details.appsSize.get(userId);
if (LOGV) Log.v(TAG, "appsSize: " + details.appsSize.get(userId)
+ " accountedSize: " + accountedSize);
} break;
case R.string.storage_detail_images: {
} else if (itemTitleId == R.string.storage_detail_images) {
final long imagesSize = totalValues(details, userId,
Environment.DIRECTORY_DCIM, Environment.DIRECTORY_PICTURES);
updatePreference(item, imagesSize);
accountedSize += imagesSize;
if (LOGV) Log.v(TAG, "imagesSize: " + imagesSize
+ " accountedSize: " + accountedSize);
} break;
case R.string.storage_detail_videos: {
} else if (itemTitleId == R.string.storage_detail_videos) {
final long videosSize = totalValues(details, userId,
Environment.DIRECTORY_MOVIES);
updatePreference(item, videosSize);
accountedSize += videosSize;
if (LOGV) Log.v(TAG, "videosSize: " + videosSize
+ " accountedSize: " + accountedSize);
} break;
case R.string.storage_detail_audio: {
} else if (itemTitleId == R.string.storage_detail_audio) {
final long audioSize = totalValues(details, userId,
Environment.DIRECTORY_MUSIC,
Environment.DIRECTORY_ALARMS, Environment.DIRECTORY_NOTIFICATIONS,
@@ -607,37 +589,30 @@ public class PrivateVolumeSettings extends SettingsPreferenceFragment {
accountedSize += audioSize;
if (LOGV) Log.v(TAG, "audioSize: " + audioSize
+ " accountedSize: " + accountedSize);
} break;
case R.string.storage_detail_other: {
} else if (itemTitleId == R.string.storage_detail_other) {
final long downloadsSize = totalValues(details, userId,
Environment.DIRECTORY_DOWNLOADS);
final long miscSize = details.miscSize.get(userId);
totalDownloadsSize += downloadsSize;
totalMiscSize += miscSize;
accountedSize += miscSize + downloadsSize;
if (LOGV)
Log.v(TAG, "miscSize for " + userId + ": " + miscSize + "(total: "
+ totalMiscSize + ") \ndownloadsSize: " + downloadsSize + "(total: "
+ totalDownloadsSize + ") accountedSize: " + accountedSize);
// Cannot display 'Other' until all known items are accounted for.
otherItem = item;
} break;
case R.string.storage_detail_cached: {
} else if (itemTitleId == R.string.storage_detail_cached) {
updatePreference(item, details.cacheSize);
accountedSize += details.cacheSize;
if (LOGV)
Log.v(TAG, "cacheSize: " + details.cacheSize + " accountedSize: "
+ accountedSize);
} break;
case 0: {
} else if (itemTitleId == 0) {
final long userSize = details.usersSize.get(userId);
updatePreference(item, userSize);
accountedSize += userSize;
if (LOGV) Log.v(TAG, "userSize: " + userSize
+ " accountedSize: " + accountedSize);
} break;
}
}
if (otherItem != null) {

View File

@@ -105,13 +105,11 @@ public class ContextualCardsAdapter extends RecyclerView.Adapter<RecyclerView.Vi
@Override
public int getSpanSize(int position) {
final int viewType = mContextualCards.get(position).getViewType();
switch (viewType) {
case ConditionContextualCardRenderer.VIEW_TYPE_HALF_WIDTH:
case SliceContextualCardRenderer.VIEW_TYPE_HALF_WIDTH:
if (viewType == ConditionContextualCardRenderer.VIEW_TYPE_HALF_WIDTH
|| viewType == SliceContextualCardRenderer.VIEW_TYPE_HALF_WIDTH) {
return HALF_WIDTH;
default:
return FULL_WIDTH;
}
return FULL_WIDTH;
}
});
}

View File

@@ -84,14 +84,12 @@ public class SliceContextualCardRenderer implements ContextualCardRenderer, Life
@Override
public RecyclerView.ViewHolder createViewHolder(View view, @LayoutRes int viewType) {
switch (viewType) {
case VIEW_TYPE_DEFERRED_SETUP:
if (viewType == VIEW_TYPE_DEFERRED_SETUP) {
return mDeferredSetupCardHelper.createViewHolder(view);
case VIEW_TYPE_HALF_WIDTH:
} else if (viewType == VIEW_TYPE_HALF_WIDTH) {
return mHalfCardHelper.createViewHolder(view);
default:
return mFullCardHelper.createViewHolder(view);
}
return mFullCardHelper.createViewHolder(view);
}
@Override
@@ -132,14 +130,11 @@ public class SliceContextualCardRenderer implements ContextualCardRenderer, Life
return;
}
switch (holder.getItemViewType()) {
case VIEW_TYPE_DEFERRED_SETUP:
if (holder.getItemViewType() == VIEW_TYPE_DEFERRED_SETUP) {
mDeferredSetupCardHelper.bindView(holder, card, slice);
break;
case VIEW_TYPE_HALF_WIDTH:
} else if (holder.getItemViewType() == VIEW_TYPE_HALF_WIDTH) {
mHalfCardHelper.bindView(holder, card, slice);
break;
default:
} else {
mFullCardHelper.bindView(holder, card, slice);
}
if (swipeBackground != null) {
@@ -147,14 +142,11 @@ public class SliceContextualCardRenderer implements ContextualCardRenderer, Life
}
});
switch (holder.getItemViewType()) {
case VIEW_TYPE_DEFERRED_SETUP:
// Deferred setup is never dismissible.
break;
case VIEW_TYPE_HALF_WIDTH:
if (holder.getItemViewType()
== VIEW_TYPE_DEFERRED_SETUP) {// Deferred setup is never dismissible.
} else if (holder.getItemViewType() == VIEW_TYPE_HALF_WIDTH) {
initDismissalActions(holder, card);
break;
default:
} else {
initDismissalActions(holder, card);
}

View File

@@ -52,10 +52,10 @@ public class SwipeDismissalDelegate extends ItemTouchHelper.Callback {
@Override
public int getMovementFlags(@NonNull RecyclerView recyclerView,
@NonNull RecyclerView.ViewHolder viewHolder) {
switch (viewHolder.getItemViewType()) {
case SliceContextualCardRenderer.VIEW_TYPE_FULL_WIDTH:
case SliceContextualCardRenderer.VIEW_TYPE_HALF_WIDTH:
// Here we are making sure the current displayed view is the initial view of
if (viewHolder.getItemViewType() == SliceContextualCardRenderer.VIEW_TYPE_FULL_WIDTH
|| viewHolder.getItemViewType()
== SliceContextualCardRenderer.VIEW_TYPE_HALF_WIDTH) {// Here we are making sure
// the current displayed view is the initial view of
// either slice full card or half card, and only allow swipe on these two types.
if (viewHolder.itemView.findViewById(R.id.dismissal_view).getVisibility()
== View.VISIBLE) {
@@ -64,9 +64,8 @@ public class SwipeDismissalDelegate extends ItemTouchHelper.Callback {
}
return makeMovementFlags(0 /*dragFlags*/,
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT /*swipeFlags*/);
default:
return 0;
}
return 0;
}
@Override

View File

@@ -117,38 +117,32 @@ public class LocaleLinearLayoutManager extends LinearLayoutManager {
final int position = this.getPosition(host);
boolean result = false;
switch (action) {
case R.id.action_drag_move_up:
if (action == R.id.action_drag_move_up) {
if (position > 0) {
mAdapter.onItemMove(position, position - 1);
result = true;
}
break;
case R.id.action_drag_move_down:
} else if (action == R.id.action_drag_move_down) {
if (position + 1 < itemCount) {
mAdapter.onItemMove(position, position + 1);
result = true;
}
break;
case R.id.action_drag_move_top:
} else if (action == R.id.action_drag_move_top) {
if (position != 0) {
mAdapter.onItemMove(position, 0);
result = true;
}
break;
case R.id.action_drag_move_bottom:
} else if (action == R.id.action_drag_move_bottom) {
if (position != itemCount - 1) {
mAdapter.onItemMove(position, itemCount - 1);
result = true;
}
break;
case R.id.action_drag_remove:
} else if (action == R.id.action_drag_remove) {
if (itemCount > 1) {
mAdapter.removeItem(position);
result = true;
}
break;
default:
} else {
return super.performAccessibilityActionForItem(recycler, state, host, action, args);
}

View File

@@ -207,16 +207,12 @@ public class PrivateDnsModeDialogPreference extends CustomDialogPreferenceCompat
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.private_dns_mode_off:
if (checkedId == R.id.private_dns_mode_off) {
mMode = PRIVATE_DNS_MODE_OFF;
break;
case R.id.private_dns_mode_opportunistic:
} else if (checkedId == R.id.private_dns_mode_opportunistic) {
mMode = PRIVATE_DNS_MODE_OPPORTUNISTIC;
break;
case R.id.private_dns_mode_provider:
} else if (checkedId == R.id.private_dns_mode_provider) {
mMode = PRIVATE_DNS_MODE_PROVIDER_HOSTNAME;
break;
}
updateDialogInfo();
}

View File

@@ -492,15 +492,11 @@ public class ConfirmLockPassword extends ConfirmDeviceCredentialBaseActivity {
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.next_button:
if (v.getId() == R.id.next_button) {
handleNext();
break;
case R.id.cancel_button:
} else if (v.getId() == R.id.cancel_button) {
getActivity().setResult(RESULT_CANCELED);
getActivity().finish();
break;
}
}

View File

@@ -1086,8 +1086,7 @@ public class UserSettings extends SettingsPreferenceFragment
public void onClick(View v) {
if (v.getTag() instanceof UserPreference) {
int userId = ((UserPreference) v.getTag()).getUserId();
switch (v.getId()) {
case UserPreference.DELETE_ID:
if (v.getId() == UserPreference.DELETE_ID) {
final EnforcedAdmin removeDisallowedAdmin =
RestrictedLockUtilsInternal.checkIfRestrictionEnforced(getContext(),
UserManager.DISALLOW_REMOVE_USER, UserHandle.myUserId());
@@ -1097,10 +1096,8 @@ public class UserSettings extends SettingsPreferenceFragment
} else {
onRemoveUserClicked(userId);
}
break;
case UserPreference.SETTINGS_ID:
} else if (v.getId() == UserPreference.SETTINGS_ID) {
onManageUserClicked(userId, false);
break;
}
}
}

View File

@@ -153,9 +153,8 @@ public class VpnSettings extends RestrictedSettingsFragment implements
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.vpn_create: {
// Generate a new key. Here we just use the current time.
if (item.getItemId() == R.id.vpn_create) {
long millis = System.currentTimeMillis();
while (mLegacyVpnPreferences.containsKey(Long.toHexString(millis))) {
++millis;
@@ -164,7 +163,6 @@ public class VpnSettings extends RestrictedSettingsFragment implements
ConfigDialogFragment.show(this, profile, true /* editing */, false /* exists */);
return true;
}
}
return super.onOptionsItemSelected(item);
}

View File

@@ -94,22 +94,22 @@ public class AddNetworkFragment extends InstrumentedFragment implements WifiConf
public void onClick(View view) {
String ssid = null;
switch (view.getId()) {
case SUBMIT_BUTTON_ID:
if (view.getId() == SUBMIT_BUTTON_ID) {
handleSubmitAction();
break;
case CANCEL_BUTTON_ID:
} else if (view.getId() == CANCEL_BUTTON_ID) {
handleCancelAction();
break;
case SSID_SCANNER_BUTTON_ID:
} else if (view.getId() == SSID_SCANNER_BUTTON_ID) {
final TextView ssidEditText = getView().findViewById(R.id.ssid);
ssid = ssidEditText.getText().toString();
// No break and flows to case PASSWORD_SCANNER_BUTTON_ID
case PASSWORD_SCANNER_BUTTON_ID:
// Launch QR code scanner to join a network.
startActivityForResult(WifiDppUtils.getEnrolleeQrCodeScannerIntent(ssid),
REQUEST_CODE_WIFI_DPP_ENROLLEE_QR_CODE_SCANNER);
break;
} else if (view.getId()
== PASSWORD_SCANNER_BUTTON_ID) {// Launch QR code scanner to join a network.
startActivityForResult(WifiDppUtils.getEnrolleeQrCodeScannerIntent(ssid),
REQUEST_CODE_WIFI_DPP_ENROLLEE_QR_CODE_SCANNER);
}
}