diff options
-rw-r--r-- | core/java/com/android/internal/app/MediaRouteChooserContentManager.java | 56 | ||||
-rw-r--r-- | core/java/com/android/internal/app/MediaRouteChooserDialog.java | 37 |
2 files changed, 69 insertions, 24 deletions
diff --git a/core/java/com/android/internal/app/MediaRouteChooserContentManager.java b/core/java/com/android/internal/app/MediaRouteChooserContentManager.java new file mode 100644 index 000000000000..09c6f5e6caaa --- /dev/null +++ b/core/java/com/android/internal/app/MediaRouteChooserContentManager.java @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2025 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.internal.app; + +import android.content.Context; +import android.view.Gravity; +import android.view.View; +import android.widget.LinearLayout; +import android.widget.ListView; + +import com.android.internal.R; + +public class MediaRouteChooserContentManager { + Context mContext; + + private final boolean mShowProgressBarWhenEmpty; + + public MediaRouteChooserContentManager(Context context, boolean showProgressBarWhenEmpty) { + mContext = context; + mShowProgressBarWhenEmpty = showProgressBarWhenEmpty; + } + + /** + * Starts binding all the views (list view, empty view, etc.) using the + * given container view. + */ + public void bindViews(View containerView) { + View emptyView = containerView.findViewById(android.R.id.empty); + ListView listView = containerView.findViewById(R.id.media_route_list); + listView.setEmptyView(emptyView); + + if (!mShowProgressBarWhenEmpty) { + containerView.findViewById(R.id.media_route_progress_bar).setVisibility(View.GONE); + + // Center the empty view when the progress bar is not shown. + LinearLayout.LayoutParams params = + (LinearLayout.LayoutParams) emptyView.getLayoutParams(); + params.gravity = Gravity.CENTER; + emptyView.setLayoutParams(params); + } + } +} diff --git a/core/java/com/android/internal/app/MediaRouteChooserDialog.java b/core/java/com/android/internal/app/MediaRouteChooserDialog.java index 23d966fdbc0d..5030a143ea94 100644 --- a/core/java/com/android/internal/app/MediaRouteChooserDialog.java +++ b/core/java/com/android/internal/app/MediaRouteChooserDialog.java @@ -23,14 +23,12 @@ import android.media.MediaRouter.RouteInfo; import android.os.Bundle; import android.text.TextUtils; import android.util.TypedValue; -import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; -import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; @@ -52,15 +50,15 @@ import java.util.Comparator; public class MediaRouteChooserDialog extends AlertDialog { private final MediaRouter mRouter; private final MediaRouterCallback mCallback; - private final boolean mShowProgressBarWhenEmpty; private int mRouteTypes; private View.OnClickListener mExtendedSettingsClickListener; private RouteAdapter mAdapter; - private ListView mListView; private Button mExtendedSettingsButton; private boolean mAttachedToWindow; + private final MediaRouteChooserContentManager mContentManager; + public MediaRouteChooserDialog(Context context, int theme) { this(context, theme, true); } @@ -70,7 +68,7 @@ public class MediaRouteChooserDialog extends AlertDialog { mRouter = (MediaRouter) context.getSystemService(Context.MEDIA_ROUTER_SERVICE); mCallback = new MediaRouterCallback(); - mShowProgressBarWhenEmpty = showProgressBarWhenEmpty; + mContentManager = new MediaRouteChooserContentManager(context, showProgressBarWhenEmpty); } /** @@ -128,8 +126,9 @@ public class MediaRouteChooserDialog extends AlertDialog { @Override protected void onCreate(Bundle savedInstanceState) { // Note: setView must be called before super.onCreate(). - setView(LayoutInflater.from(getContext()).inflate(R.layout.media_route_chooser_dialog, - null)); + View containerView = LayoutInflater.from(getContext()).inflate( + R.layout.media_route_chooser_dialog, null); + setView(containerView); setTitle(mRouteTypes == MediaRouter.ROUTE_TYPE_REMOTE_DISPLAY ? R.string.media_route_chooser_title_for_remote_display @@ -140,25 +139,15 @@ public class MediaRouteChooserDialog extends AlertDialog { super.onCreate(savedInstanceState); - View emptyView = findViewById(android.R.id.empty); mAdapter = new RouteAdapter(getContext()); - mListView = (ListView) findViewById(R.id.media_route_list); - mListView.setAdapter(mAdapter); - mListView.setOnItemClickListener(mAdapter); - mListView.setEmptyView(emptyView); + ListView listView = findViewById(R.id.media_route_list); + listView.setAdapter(mAdapter); + listView.setOnItemClickListener(mAdapter); - mExtendedSettingsButton = (Button) findViewById(R.id.media_route_extended_settings_button); + mExtendedSettingsButton = findViewById(R.id.media_route_extended_settings_button); updateExtendedSettingsButton(); - if (!mShowProgressBarWhenEmpty) { - findViewById(R.id.media_route_progress_bar).setVisibility(View.GONE); - - // Center the empty view when the progress bar is not shown. - LinearLayout.LayoutParams params = - (LinearLayout.LayoutParams) emptyView.getLayoutParams(); - params.gravity = Gravity.CENTER; - emptyView.setLayoutParams(params); - } + mContentManager.bindViews(containerView); } private void updateExtendedSettingsButton() { @@ -240,8 +229,8 @@ public class MediaRouteChooserDialog extends AlertDialog { view = mInflater.inflate(R.layout.media_route_list_item, parent, false); } MediaRouter.RouteInfo route = getItem(position); - TextView text1 = (TextView)view.findViewById(android.R.id.text1); - TextView text2 = (TextView)view.findViewById(android.R.id.text2); + TextView text1 = view.findViewById(android.R.id.text1); + TextView text2 = view.findViewById(android.R.id.text2); text1.setText(route.getName()); CharSequence description = route.getDescription(); if (TextUtils.isEmpty(description)) { |