Add title and icon in output switcher panel header
-title shows artist -subtitle shows album -add test cases Bug: 147776885 Test: make -j42 RunSettingsRoboTests Change-Id: Ib33e5550e668d8cc5d70051ea2e7dd74d61c767a
This commit is contained in:
@@ -22,9 +22,22 @@ import static com.android.settings.slices.CustomSliceRegistry.MEDIA_OUTPUT_SLICE
|
||||
import android.app.settings.SettingsEnums;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.media.MediaMetadata;
|
||||
import android.media.session.MediaController;
|
||||
import android.media.session.MediaSessionManager;
|
||||
import android.net.Uri;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.core.graphics.drawable.IconCompat;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.Utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -38,9 +51,14 @@ import java.util.List;
|
||||
*/
|
||||
public class MediaOutputPanel implements PanelContent {
|
||||
|
||||
private static final String TAG = "MediaOutputPanel";
|
||||
|
||||
private final Context mContext;
|
||||
private final String mPackageName;
|
||||
|
||||
private MediaSessionManager mMediaSessionManager;
|
||||
private MediaController mMediaController;
|
||||
|
||||
public static MediaOutputPanel create(Context context, String packageName) {
|
||||
return new MediaOutputPanel(context, packageName);
|
||||
}
|
||||
@@ -48,13 +66,78 @@ public class MediaOutputPanel implements PanelContent {
|
||||
private MediaOutputPanel(Context context, String packageName) {
|
||||
mContext = context.getApplicationContext();
|
||||
mPackageName = packageName;
|
||||
if (mPackageName != null) {
|
||||
mMediaSessionManager = mContext.getSystemService(MediaSessionManager.class);
|
||||
for (MediaController controller : mMediaSessionManager.getActiveSessions(null)) {
|
||||
if (TextUtils.equals(controller.getPackageName(), mPackageName)) {
|
||||
mMediaController = controller;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (mMediaController == null) {
|
||||
Log.e(TAG, "Unable to find " + mPackageName + " media controller");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getTitle() {
|
||||
if (mMediaController != null) {
|
||||
final MediaMetadata metadata = mMediaController.getMetadata();
|
||||
if (metadata != null) {
|
||||
return metadata.getString(MediaMetadata.METADATA_KEY_ARTIST);
|
||||
}
|
||||
}
|
||||
return mContext.getText(R.string.media_volume_title);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getSubTitle() {
|
||||
if (mMediaController != null) {
|
||||
final MediaMetadata metadata = mMediaController.getMetadata();
|
||||
if (metadata != null) {
|
||||
return metadata.getString(MediaMetadata.METADATA_KEY_ALBUM);
|
||||
}
|
||||
}
|
||||
return mContext.getText(R.string.media_output_panel_title);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IconCompat getIcon() {
|
||||
if (mMediaController == null) {
|
||||
return IconCompat.createWithResource(mContext, R.drawable.ic_media_stream).setTint(
|
||||
Utils.getColorAccentDefaultColor(mContext));
|
||||
}
|
||||
final MediaMetadata metadata = mMediaController.getMetadata();
|
||||
if (metadata != null) {
|
||||
final Bitmap bitmap = metadata.getDescription().getIconBitmap();
|
||||
if (bitmap != null) {
|
||||
return IconCompat.createWithBitmap(bitmap);
|
||||
}
|
||||
}
|
||||
Log.d(TAG, "Media meta data does not contain icon information");
|
||||
return getPackageIcon();
|
||||
}
|
||||
|
||||
private IconCompat getPackageIcon() {
|
||||
try {
|
||||
final Drawable drawable = mContext.getPackageManager().getApplicationIcon(mPackageName);
|
||||
if (drawable instanceof BitmapDrawable) {
|
||||
return IconCompat.createWithBitmap(((BitmapDrawable) drawable).getBitmap());
|
||||
}
|
||||
final Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
|
||||
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
|
||||
final Canvas canvas = new Canvas(bitmap);
|
||||
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
|
||||
drawable.draw(canvas);
|
||||
|
||||
return IconCompat.createWithBitmap(bitmap);
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
Log.e(TAG, "Package is not found. Unable to get package icon.");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Uri> getSlices() {
|
||||
final List<Uri> uris = new ArrayList<>();
|
||||
|
@@ -19,6 +19,8 @@ package com.android.settings.panel;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
|
||||
import androidx.core.graphics.drawable.IconCompat;
|
||||
|
||||
import com.android.settingslib.core.instrumentation.Instrumentable;
|
||||
|
||||
import java.util.List;
|
||||
@@ -28,13 +30,11 @@ import java.util.List;
|
||||
*/
|
||||
public interface PanelContent extends Instrumentable {
|
||||
|
||||
int ICON_UNAVAILABLE = -1;
|
||||
|
||||
/**
|
||||
* @return a icon resource for the title of the Panel.
|
||||
* @return a icon for the title of the Panel.
|
||||
*/
|
||||
default int getIcon() {
|
||||
return ICON_UNAVAILABLE;
|
||||
default IconCompat getIcon() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -38,6 +38,7 @@ import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.core.graphics.drawable.IconCompat;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
import androidx.lifecycle.LiveData;
|
||||
@@ -185,20 +186,19 @@ public class PanelFragment extends Fragment {
|
||||
mMetricsProvider = FeatureFactory.getFactory(activity).getMetricsFeatureProvider();
|
||||
|
||||
mPanelSlices.setLayoutManager(new LinearLayoutManager((activity)));
|
||||
|
||||
// Add predraw listener to remove the animation and while we wait for Slices to load.
|
||||
mLayoutView.getViewTreeObserver().addOnPreDrawListener(mOnPreDrawListener);
|
||||
|
||||
// Start loading Slices. When finished, the Panel will animate in.
|
||||
loadAllSlices();
|
||||
|
||||
final int iconRes = mPanel.getIcon();
|
||||
if (iconRes == PanelContent.ICON_UNAVAILABLE) {
|
||||
final IconCompat icon = mPanel.getIcon();
|
||||
if (icon == null) {
|
||||
mTitleView.setText(mPanel.getTitle());
|
||||
} else {
|
||||
mTitleView.setVisibility(View.GONE);
|
||||
mPanelHeader.setVisibility(View.VISIBLE);
|
||||
mTitleIcon.setImageResource(iconRes);
|
||||
mTitleIcon.setImageIcon(icon.toIcon(getContext()));
|
||||
mHeaderTitle.setText(mPanel.getTitle());
|
||||
mHeaderSubtitle.setText(mPanel.getSubTitle());
|
||||
}
|
||||
|
Reference in New Issue
Block a user