TransportControlView.java revision 01d96bd2afdcff866e0e1010eb7c88e101d0f6fa
16b05d58018c2806459c121e507c005639b74aee9Jim Miller/*
26b05d58018c2806459c121e507c005639b74aee9Jim Miller * Copyright (C) 2011 The Android Open Source Project
36b05d58018c2806459c121e507c005639b74aee9Jim Miller *
46b05d58018c2806459c121e507c005639b74aee9Jim Miller * Licensed under the Apache License, Version 2.0 (the "License");
56b05d58018c2806459c121e507c005639b74aee9Jim Miller * you may not use this file except in compliance with the License.
66b05d58018c2806459c121e507c005639b74aee9Jim Miller * You may obtain a copy of the License at
76b05d58018c2806459c121e507c005639b74aee9Jim Miller *
86b05d58018c2806459c121e507c005639b74aee9Jim Miller *      http://www.apache.org/licenses/LICENSE-2.0
96b05d58018c2806459c121e507c005639b74aee9Jim Miller *
106b05d58018c2806459c121e507c005639b74aee9Jim Miller * Unless required by applicable law or agreed to in writing, software
116b05d58018c2806459c121e507c005639b74aee9Jim Miller * distributed under the License is distributed on an "AS IS" BASIS,
126b05d58018c2806459c121e507c005639b74aee9Jim Miller * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136b05d58018c2806459c121e507c005639b74aee9Jim Miller * See the License for the specific language governing permissions and
146b05d58018c2806459c121e507c005639b74aee9Jim Miller * limitations under the License.
156b05d58018c2806459c121e507c005639b74aee9Jim Miller */
166b05d58018c2806459c121e507c005639b74aee9Jim Miller
176b05d58018c2806459c121e507c005639b74aee9Jim Millerpackage com.android.internal.widget;
186b05d58018c2806459c121e507c005639b74aee9Jim Miller
191c18828d20807342d37000746b18a3c1696f3b2eJim Millerimport java.lang.ref.WeakReference;
201c18828d20807342d37000746b18a3c1696f3b2eJim Miller
211c18828d20807342d37000746b18a3c1696f3b2eJim Millerimport com.android.internal.widget.LockScreenWidgetCallback;
221c18828d20807342d37000746b18a3c1696f3b2eJim Millerimport com.android.internal.widget.LockScreenWidgetInterface;
236b05d58018c2806459c121e507c005639b74aee9Jim Miller
24f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Triviimport android.app.PendingIntent;
25f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Triviimport android.app.PendingIntent.CanceledException;
261c18828d20807342d37000746b18a3c1696f3b2eJim Millerimport android.content.ComponentName;
276b05d58018c2806459c121e507c005639b74aee9Jim Millerimport android.content.Context;
281c18828d20807342d37000746b18a3c1696f3b2eJim Millerimport android.content.Intent;
291c18828d20807342d37000746b18a3c1696f3b2eJim Millerimport android.graphics.Bitmap;
306b05d58018c2806459c121e507c005639b74aee9Jim Millerimport android.media.AudioManager;
311c18828d20807342d37000746b18a3c1696f3b2eJim Millerimport android.media.MediaMetadataRetriever;
321c18828d20807342d37000746b18a3c1696f3b2eJim Millerimport android.media.RemoteControlClient;
331c18828d20807342d37000746b18a3c1696f3b2eJim Millerimport android.media.IRemoteControlDisplay;
341c18828d20807342d37000746b18a3c1696f3b2eJim Millerimport android.os.Bundle;
356b05d58018c2806459c121e507c005639b74aee9Jim Millerimport android.os.Handler;
366b05d58018c2806459c121e507c005639b74aee9Jim Millerimport android.os.Message;
371c18828d20807342d37000746b18a3c1696f3b2eJim Millerimport android.os.RemoteException;
381c18828d20807342d37000746b18a3c1696f3b2eJim Millerimport android.text.Spannable;
391c18828d20807342d37000746b18a3c1696f3b2eJim Millerimport android.text.TextUtils;
401c18828d20807342d37000746b18a3c1696f3b2eJim Millerimport android.text.style.ForegroundColorSpan;
416b05d58018c2806459c121e507c005639b74aee9Jim Millerimport android.util.AttributeSet;
421c18828d20807342d37000746b18a3c1696f3b2eJim Millerimport android.util.Log;
431c18828d20807342d37000746b18a3c1696f3b2eJim Millerimport android.view.KeyEvent;
446b05d58018c2806459c121e507c005639b74aee9Jim Millerimport android.view.View;
456b05d58018c2806459c121e507c005639b74aee9Jim Millerimport android.view.View.OnClickListener;
461c18828d20807342d37000746b18a3c1696f3b2eJim Millerimport android.widget.FrameLayout;
471c18828d20807342d37000746b18a3c1696f3b2eJim Millerimport android.widget.ImageView;
481c18828d20807342d37000746b18a3c1696f3b2eJim Millerimport android.widget.TextView;
496b05d58018c2806459c121e507c005639b74aee9Jim Miller
506b05d58018c2806459c121e507c005639b74aee9Jim Miller
511c18828d20807342d37000746b18a3c1696f3b2eJim Millerimport com.android.internal.R;
521c18828d20807342d37000746b18a3c1696f3b2eJim Miller
531c18828d20807342d37000746b18a3c1696f3b2eJim Millerpublic class TransportControlView extends FrameLayout implements OnClickListener,
541c18828d20807342d37000746b18a3c1696f3b2eJim Miller        LockScreenWidgetInterface {
551c18828d20807342d37000746b18a3c1696f3b2eJim Miller
561c18828d20807342d37000746b18a3c1696f3b2eJim Miller    private static final int MSG_UPDATE_STATE = 100;
571c18828d20807342d37000746b18a3c1696f3b2eJim Miller    private static final int MSG_SET_METADATA = 101;
581c18828d20807342d37000746b18a3c1696f3b2eJim Miller    private static final int MSG_SET_TRANSPORT_CONTROLS = 102;
591c18828d20807342d37000746b18a3c1696f3b2eJim Miller    private static final int MSG_SET_ARTWORK = 103;
601c18828d20807342d37000746b18a3c1696f3b2eJim Miller    private static final int MSG_SET_GENERATION_ID = 104;
611c18828d20807342d37000746b18a3c1696f3b2eJim Miller    private static final int MAXDIM = 512;
621c18828d20807342d37000746b18a3c1696f3b2eJim Miller    protected static final boolean DEBUG = true;
631c18828d20807342d37000746b18a3c1696f3b2eJim Miller    protected static final String TAG = "TransportControlView";
641c18828d20807342d37000746b18a3c1696f3b2eJim Miller
651c18828d20807342d37000746b18a3c1696f3b2eJim Miller    private ImageView mAlbumArt;
661c18828d20807342d37000746b18a3c1696f3b2eJim Miller    private TextView mTrackTitle;
671c18828d20807342d37000746b18a3c1696f3b2eJim Miller    private ImageView mBtnPrev;
681c18828d20807342d37000746b18a3c1696f3b2eJim Miller    private ImageView mBtnPlay;
691c18828d20807342d37000746b18a3c1696f3b2eJim Miller    private ImageView mBtnNext;
701c18828d20807342d37000746b18a3c1696f3b2eJim Miller    private int mClientGeneration;
711c18828d20807342d37000746b18a3c1696f3b2eJim Miller    private Metadata mMetadata = new Metadata();
721c18828d20807342d37000746b18a3c1696f3b2eJim Miller    private boolean mAttached;
73f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi    private PendingIntent mClientIntent;
741c18828d20807342d37000746b18a3c1696f3b2eJim Miller    private int mTransportControlFlags;
751c18828d20807342d37000746b18a3c1696f3b2eJim Miller    private int mPlayState;
761c18828d20807342d37000746b18a3c1696f3b2eJim Miller    private AudioManager mAudioManager;
771c18828d20807342d37000746b18a3c1696f3b2eJim Miller    private LockScreenWidgetCallback mWidgetCallbacks;
781c18828d20807342d37000746b18a3c1696f3b2eJim Miller    private IRemoteControlDisplayWeak mIRCD;
791c18828d20807342d37000746b18a3c1696f3b2eJim Miller
801c18828d20807342d37000746b18a3c1696f3b2eJim Miller    /**
811c18828d20807342d37000746b18a3c1696f3b2eJim Miller     * The metadata which should be populated into the view once we've been attached
821c18828d20807342d37000746b18a3c1696f3b2eJim Miller     */
831c18828d20807342d37000746b18a3c1696f3b2eJim Miller    private Bundle mPopulateMetadataWhenAttached = null;
841c18828d20807342d37000746b18a3c1696f3b2eJim Miller
851c18828d20807342d37000746b18a3c1696f3b2eJim Miller    // This handler is required to ensure messages from IRCD are handled in sequence and on
861c18828d20807342d37000746b18a3c1696f3b2eJim Miller    // the UI thread.
876b05d58018c2806459c121e507c005639b74aee9Jim Miller    private Handler mHandler = new Handler() {
881c18828d20807342d37000746b18a3c1696f3b2eJim Miller        @Override
896b05d58018c2806459c121e507c005639b74aee9Jim Miller        public void handleMessage(Message msg) {
901c18828d20807342d37000746b18a3c1696f3b2eJim Miller            switch (msg.what) {
911c18828d20807342d37000746b18a3c1696f3b2eJim Miller            case MSG_UPDATE_STATE:
921c18828d20807342d37000746b18a3c1696f3b2eJim Miller                if (mClientGeneration == msg.arg1) updatePlayPauseState(msg.arg2);
931c18828d20807342d37000746b18a3c1696f3b2eJim Miller                break;
941c18828d20807342d37000746b18a3c1696f3b2eJim Miller
951c18828d20807342d37000746b18a3c1696f3b2eJim Miller            case MSG_SET_METADATA:
961c18828d20807342d37000746b18a3c1696f3b2eJim Miller                if (mClientGeneration == msg.arg1) updateMetadata((Bundle) msg.obj);
971c18828d20807342d37000746b18a3c1696f3b2eJim Miller                break;
981c18828d20807342d37000746b18a3c1696f3b2eJim Miller
991c18828d20807342d37000746b18a3c1696f3b2eJim Miller            case MSG_SET_TRANSPORT_CONTROLS:
1001c18828d20807342d37000746b18a3c1696f3b2eJim Miller                if (mClientGeneration == msg.arg1) updateTransportControls(msg.arg2);
1011c18828d20807342d37000746b18a3c1696f3b2eJim Miller                break;
1021c18828d20807342d37000746b18a3c1696f3b2eJim Miller
1031c18828d20807342d37000746b18a3c1696f3b2eJim Miller            case MSG_SET_ARTWORK:
1041c18828d20807342d37000746b18a3c1696f3b2eJim Miller                if (mClientGeneration == msg.arg1) {
10501d96bd2afdcff866e0e1010eb7c88e101d0f6faJean-Michel Trivi                    if (mMetadata.bitmap != null) {
10601d96bd2afdcff866e0e1010eb7c88e101d0f6faJean-Michel Trivi                        mMetadata.bitmap.recycle();
10701d96bd2afdcff866e0e1010eb7c88e101d0f6faJean-Michel Trivi                    }
1081c18828d20807342d37000746b18a3c1696f3b2eJim Miller                    mMetadata.bitmap = (Bitmap) msg.obj;
1091c18828d20807342d37000746b18a3c1696f3b2eJim Miller                    mAlbumArt.setImageBitmap(mMetadata.bitmap);
1101c18828d20807342d37000746b18a3c1696f3b2eJim Miller                }
1111c18828d20807342d37000746b18a3c1696f3b2eJim Miller                break;
1121c18828d20807342d37000746b18a3c1696f3b2eJim Miller
1131c18828d20807342d37000746b18a3c1696f3b2eJim Miller            case MSG_SET_GENERATION_ID:
1141c18828d20807342d37000746b18a3c1696f3b2eJim Miller                if (mWidgetCallbacks != null) {
1151c18828d20807342d37000746b18a3c1696f3b2eJim Miller                    boolean clearing = msg.arg2 != 0;
1161c18828d20807342d37000746b18a3c1696f3b2eJim Miller                    if (DEBUG) Log.v(TAG, "New genId = " + msg.arg1 + ", clearing = " + clearing);
1171c18828d20807342d37000746b18a3c1696f3b2eJim Miller                    if (!clearing) {
1181c18828d20807342d37000746b18a3c1696f3b2eJim Miller                        mWidgetCallbacks.requestShow(TransportControlView.this);
1191c18828d20807342d37000746b18a3c1696f3b2eJim Miller                    } else {
1201c18828d20807342d37000746b18a3c1696f3b2eJim Miller                        mWidgetCallbacks.requestHide(TransportControlView.this);
1211c18828d20807342d37000746b18a3c1696f3b2eJim Miller                    }
1221c18828d20807342d37000746b18a3c1696f3b2eJim Miller                }
1231c18828d20807342d37000746b18a3c1696f3b2eJim Miller                mClientGeneration = msg.arg1;
124f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi                mClientIntent = (PendingIntent) msg.obj;
1251c18828d20807342d37000746b18a3c1696f3b2eJim Miller                break;
1261c18828d20807342d37000746b18a3c1696f3b2eJim Miller
1276b05d58018c2806459c121e507c005639b74aee9Jim Miller            }
1286b05d58018c2806459c121e507c005639b74aee9Jim Miller        }
1296b05d58018c2806459c121e507c005639b74aee9Jim Miller    };
1306b05d58018c2806459c121e507c005639b74aee9Jim Miller
1311c18828d20807342d37000746b18a3c1696f3b2eJim Miller    /**
1321c18828d20807342d37000746b18a3c1696f3b2eJim Miller     * This class is required to have weak linkage to the current TransportControlView
1331c18828d20807342d37000746b18a3c1696f3b2eJim Miller     * because the remote process can hold a strong reference to this binder object and
1341c18828d20807342d37000746b18a3c1696f3b2eJim Miller     * we can't predict when it will be GC'd in the remote process. Without this code, it
1351c18828d20807342d37000746b18a3c1696f3b2eJim Miller     * would allow a heavyweight object to be held on this side of the binder when there's
1361c18828d20807342d37000746b18a3c1696f3b2eJim Miller     * no requirement to run a GC on the other side.
1371c18828d20807342d37000746b18a3c1696f3b2eJim Miller     */
1381c18828d20807342d37000746b18a3c1696f3b2eJim Miller    private static class IRemoteControlDisplayWeak extends IRemoteControlDisplay.Stub {
1391c18828d20807342d37000746b18a3c1696f3b2eJim Miller        private WeakReference<Handler> mLocalHandler;
1401c18828d20807342d37000746b18a3c1696f3b2eJim Miller
1411c18828d20807342d37000746b18a3c1696f3b2eJim Miller        IRemoteControlDisplayWeak(Handler handler) {
1421c18828d20807342d37000746b18a3c1696f3b2eJim Miller            mLocalHandler = new WeakReference<Handler>(handler);
1431c18828d20807342d37000746b18a3c1696f3b2eJim Miller        }
1441c18828d20807342d37000746b18a3c1696f3b2eJim Miller
1451c18828d20807342d37000746b18a3c1696f3b2eJim Miller        public void setPlaybackState(int generationId, int state) {
1461c18828d20807342d37000746b18a3c1696f3b2eJim Miller            Handler handler = mLocalHandler.get();
1471c18828d20807342d37000746b18a3c1696f3b2eJim Miller            if (handler != null) {
1481c18828d20807342d37000746b18a3c1696f3b2eJim Miller                handler.obtainMessage(MSG_UPDATE_STATE, generationId, state).sendToTarget();
1496b05d58018c2806459c121e507c005639b74aee9Jim Miller            }
1501c18828d20807342d37000746b18a3c1696f3b2eJim Miller        }
1516b05d58018c2806459c121e507c005639b74aee9Jim Miller
1521c18828d20807342d37000746b18a3c1696f3b2eJim Miller        public void setMetadata(int generationId, Bundle metadata) {
1531c18828d20807342d37000746b18a3c1696f3b2eJim Miller            Handler handler = mLocalHandler.get();
1541c18828d20807342d37000746b18a3c1696f3b2eJim Miller            if (handler != null) {
1551c18828d20807342d37000746b18a3c1696f3b2eJim Miller                handler.obtainMessage(MSG_SET_METADATA, generationId, 0, metadata).sendToTarget();
1561c18828d20807342d37000746b18a3c1696f3b2eJim Miller            }
1571c18828d20807342d37000746b18a3c1696f3b2eJim Miller        }
1581c18828d20807342d37000746b18a3c1696f3b2eJim Miller
1591c18828d20807342d37000746b18a3c1696f3b2eJim Miller        public void setTransportControlFlags(int generationId, int flags) {
1601c18828d20807342d37000746b18a3c1696f3b2eJim Miller            Handler handler = mLocalHandler.get();
1611c18828d20807342d37000746b18a3c1696f3b2eJim Miller            if (handler != null) {
1621c18828d20807342d37000746b18a3c1696f3b2eJim Miller                handler.obtainMessage(MSG_SET_TRANSPORT_CONTROLS, generationId, flags)
1631c18828d20807342d37000746b18a3c1696f3b2eJim Miller                        .sendToTarget();
1641c18828d20807342d37000746b18a3c1696f3b2eJim Miller            }
1651c18828d20807342d37000746b18a3c1696f3b2eJim Miller        }
1661c18828d20807342d37000746b18a3c1696f3b2eJim Miller
1671c18828d20807342d37000746b18a3c1696f3b2eJim Miller        public void setArtwork(int generationId, Bitmap bitmap) {
1681c18828d20807342d37000746b18a3c1696f3b2eJim Miller            Handler handler = mLocalHandler.get();
1691c18828d20807342d37000746b18a3c1696f3b2eJim Miller            if (handler != null) {
1701c18828d20807342d37000746b18a3c1696f3b2eJim Miller                handler.obtainMessage(MSG_SET_ARTWORK, generationId, 0, bitmap).sendToTarget();
1711c18828d20807342d37000746b18a3c1696f3b2eJim Miller            }
1721c18828d20807342d37000746b18a3c1696f3b2eJim Miller        }
1731c18828d20807342d37000746b18a3c1696f3b2eJim Miller
1741c18828d20807342d37000746b18a3c1696f3b2eJim Miller        public void setAllMetadata(int generationId, Bundle metadata, Bitmap bitmap) {
1751c18828d20807342d37000746b18a3c1696f3b2eJim Miller            Handler handler = mLocalHandler.get();
1761c18828d20807342d37000746b18a3c1696f3b2eJim Miller            if (handler != null) {
1771c18828d20807342d37000746b18a3c1696f3b2eJim Miller                handler.obtainMessage(MSG_SET_METADATA, generationId, 0, metadata).sendToTarget();
1781c18828d20807342d37000746b18a3c1696f3b2eJim Miller                handler.obtainMessage(MSG_SET_ARTWORK, generationId, 0, bitmap).sendToTarget();
1791c18828d20807342d37000746b18a3c1696f3b2eJim Miller            }
1801c18828d20807342d37000746b18a3c1696f3b2eJim Miller        }
1811c18828d20807342d37000746b18a3c1696f3b2eJim Miller
182f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi        public void setCurrentClientId(int clientGeneration, PendingIntent mediaIntent,
1831c18828d20807342d37000746b18a3c1696f3b2eJim Miller                boolean clearing) throws RemoteException {
1841c18828d20807342d37000746b18a3c1696f3b2eJim Miller            Handler handler = mLocalHandler.get();
1851c18828d20807342d37000746b18a3c1696f3b2eJim Miller            if (handler != null) {
1861c18828d20807342d37000746b18a3c1696f3b2eJim Miller                handler.obtainMessage(MSG_SET_GENERATION_ID,
187f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi                    clientGeneration, (clearing ? 1 : 0), mediaIntent).sendToTarget();
1881c18828d20807342d37000746b18a3c1696f3b2eJim Miller            }
1891c18828d20807342d37000746b18a3c1696f3b2eJim Miller        }
1901c18828d20807342d37000746b18a3c1696f3b2eJim Miller    };
1916b05d58018c2806459c121e507c005639b74aee9Jim Miller
1926b05d58018c2806459c121e507c005639b74aee9Jim Miller    public TransportControlView(Context context, AttributeSet attrs) {
1936b05d58018c2806459c121e507c005639b74aee9Jim Miller        super(context, attrs);
1941c18828d20807342d37000746b18a3c1696f3b2eJim Miller        Log.v(TAG, "Create TCV " + this);
1951c18828d20807342d37000746b18a3c1696f3b2eJim Miller        mAudioManager = new AudioManager(mContext);
1961c18828d20807342d37000746b18a3c1696f3b2eJim Miller        mIRCD = new IRemoteControlDisplayWeak(mHandler);
1976b05d58018c2806459c121e507c005639b74aee9Jim Miller    }
1986b05d58018c2806459c121e507c005639b74aee9Jim Miller
1991c18828d20807342d37000746b18a3c1696f3b2eJim Miller    private void updateTransportControls(int transportControlFlags) {
2001c18828d20807342d37000746b18a3c1696f3b2eJim Miller        mTransportControlFlags = transportControlFlags;
2016b05d58018c2806459c121e507c005639b74aee9Jim Miller    }
2026b05d58018c2806459c121e507c005639b74aee9Jim Miller
2031c18828d20807342d37000746b18a3c1696f3b2eJim Miller    @Override
2041c18828d20807342d37000746b18a3c1696f3b2eJim Miller    public void onFinishInflate() {
2051c18828d20807342d37000746b18a3c1696f3b2eJim Miller        super.onFinishInflate();
2061c18828d20807342d37000746b18a3c1696f3b2eJim Miller        mTrackTitle = (TextView) findViewById(R.id.title);
2071c18828d20807342d37000746b18a3c1696f3b2eJim Miller        mTrackTitle.setSelected(true); // enable marquee
2081c18828d20807342d37000746b18a3c1696f3b2eJim Miller        mAlbumArt = (ImageView) findViewById(R.id.albumart);
2091c18828d20807342d37000746b18a3c1696f3b2eJim Miller        mBtnPrev = (ImageView) findViewById(R.id.btn_prev);
2101c18828d20807342d37000746b18a3c1696f3b2eJim Miller        mBtnPlay = (ImageView) findViewById(R.id.btn_play);
2111c18828d20807342d37000746b18a3c1696f3b2eJim Miller        mBtnNext = (ImageView) findViewById(R.id.btn_next);
2121c18828d20807342d37000746b18a3c1696f3b2eJim Miller        final View buttons[] = { mBtnPrev, mBtnPlay, mBtnNext };
2131c18828d20807342d37000746b18a3c1696f3b2eJim Miller        for (View view : buttons) {
2141c18828d20807342d37000746b18a3c1696f3b2eJim Miller            view.setOnClickListener(this);
2151c18828d20807342d37000746b18a3c1696f3b2eJim Miller        }
2166b05d58018c2806459c121e507c005639b74aee9Jim Miller    }
2176b05d58018c2806459c121e507c005639b74aee9Jim Miller
2186b05d58018c2806459c121e507c005639b74aee9Jim Miller    @Override
2191c18828d20807342d37000746b18a3c1696f3b2eJim Miller    public void onAttachedToWindow() {
2201c18828d20807342d37000746b18a3c1696f3b2eJim Miller        super.onAttachedToWindow();
2211c18828d20807342d37000746b18a3c1696f3b2eJim Miller        if (mPopulateMetadataWhenAttached != null) {
2221c18828d20807342d37000746b18a3c1696f3b2eJim Miller            updateMetadata(mPopulateMetadataWhenAttached);
2231c18828d20807342d37000746b18a3c1696f3b2eJim Miller            mPopulateMetadataWhenAttached = null;
2241c18828d20807342d37000746b18a3c1696f3b2eJim Miller        }
2251c18828d20807342d37000746b18a3c1696f3b2eJim Miller        if (!mAttached) {
2261c18828d20807342d37000746b18a3c1696f3b2eJim Miller            if (DEBUG) Log.v(TAG, "Registering TCV " + this);
2271c18828d20807342d37000746b18a3c1696f3b2eJim Miller            mAudioManager.registerRemoteControlDisplay(mIRCD);
2281c18828d20807342d37000746b18a3c1696f3b2eJim Miller        }
2291c18828d20807342d37000746b18a3c1696f3b2eJim Miller        mAttached = true;
2301c18828d20807342d37000746b18a3c1696f3b2eJim Miller    }
2311c18828d20807342d37000746b18a3c1696f3b2eJim Miller
2321c18828d20807342d37000746b18a3c1696f3b2eJim Miller    @Override
2331c18828d20807342d37000746b18a3c1696f3b2eJim Miller    public void onDetachedFromWindow() {
2341c18828d20807342d37000746b18a3c1696f3b2eJim Miller        super.onDetachedFromWindow();
2351c18828d20807342d37000746b18a3c1696f3b2eJim Miller        if (mAttached) {
2361c18828d20807342d37000746b18a3c1696f3b2eJim Miller            if (DEBUG) Log.v(TAG, "Unregistering TCV " + this);
2371c18828d20807342d37000746b18a3c1696f3b2eJim Miller            mAudioManager.unregisterRemoteControlDisplay(mIRCD);
2381c18828d20807342d37000746b18a3c1696f3b2eJim Miller        }
2391c18828d20807342d37000746b18a3c1696f3b2eJim Miller        mAttached = false;
2401c18828d20807342d37000746b18a3c1696f3b2eJim Miller    }
2411c18828d20807342d37000746b18a3c1696f3b2eJim Miller
2421c18828d20807342d37000746b18a3c1696f3b2eJim Miller    @Override
2431c18828d20807342d37000746b18a3c1696f3b2eJim Miller    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
2441c18828d20807342d37000746b18a3c1696f3b2eJim Miller        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
2451c18828d20807342d37000746b18a3c1696f3b2eJim Miller        int dim = Math.min(MAXDIM, Math.max(getWidth(), getHeight()));
2461c18828d20807342d37000746b18a3c1696f3b2eJim Miller//        Log.v(TAG, "setting max bitmap size: " + dim + "x" + dim);
2471c18828d20807342d37000746b18a3c1696f3b2eJim Miller//        mAudioManager.remoteControlDisplayUsesBitmapSize(mIRCD, dim, dim);
2481c18828d20807342d37000746b18a3c1696f3b2eJim Miller    }
2491c18828d20807342d37000746b18a3c1696f3b2eJim Miller
2501c18828d20807342d37000746b18a3c1696f3b2eJim Miller    class Metadata {
2511c18828d20807342d37000746b18a3c1696f3b2eJim Miller        private String artist;
2521c18828d20807342d37000746b18a3c1696f3b2eJim Miller        private String trackTitle;
2531c18828d20807342d37000746b18a3c1696f3b2eJim Miller        private String albumTitle;
2541c18828d20807342d37000746b18a3c1696f3b2eJim Miller        private Bitmap bitmap;
2551c18828d20807342d37000746b18a3c1696f3b2eJim Miller
2561c18828d20807342d37000746b18a3c1696f3b2eJim Miller        public String toString() {
2571c18828d20807342d37000746b18a3c1696f3b2eJim Miller            return "Metadata[artist=" + artist + " trackTitle=" + trackTitle + " albumTitle=" + albumTitle + "]";
2581c18828d20807342d37000746b18a3c1696f3b2eJim Miller        }
2591c18828d20807342d37000746b18a3c1696f3b2eJim Miller    }
2601c18828d20807342d37000746b18a3c1696f3b2eJim Miller
2611c18828d20807342d37000746b18a3c1696f3b2eJim Miller    private String getMdString(Bundle data, int id) {
2621c18828d20807342d37000746b18a3c1696f3b2eJim Miller        return data.getString(Integer.toString(id));
2631c18828d20807342d37000746b18a3c1696f3b2eJim Miller    }
2641c18828d20807342d37000746b18a3c1696f3b2eJim Miller
2651c18828d20807342d37000746b18a3c1696f3b2eJim Miller    private void updateMetadata(Bundle data) {
2661c18828d20807342d37000746b18a3c1696f3b2eJim Miller        if (mAttached) {
2671c18828d20807342d37000746b18a3c1696f3b2eJim Miller            mMetadata.artist = getMdString(data, MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST);
2681c18828d20807342d37000746b18a3c1696f3b2eJim Miller            mMetadata.trackTitle = getMdString(data, MediaMetadataRetriever.METADATA_KEY_TITLE);
2691c18828d20807342d37000746b18a3c1696f3b2eJim Miller            mMetadata.albumTitle = getMdString(data, MediaMetadataRetriever.METADATA_KEY_ALBUM);
2701c18828d20807342d37000746b18a3c1696f3b2eJim Miller            populateMetadata();
2711c18828d20807342d37000746b18a3c1696f3b2eJim Miller        } else {
2721c18828d20807342d37000746b18a3c1696f3b2eJim Miller            mPopulateMetadataWhenAttached = data;
2731c18828d20807342d37000746b18a3c1696f3b2eJim Miller        }
2741c18828d20807342d37000746b18a3c1696f3b2eJim Miller    }
2751c18828d20807342d37000746b18a3c1696f3b2eJim Miller
2761c18828d20807342d37000746b18a3c1696f3b2eJim Miller    /**
2771c18828d20807342d37000746b18a3c1696f3b2eJim Miller     * Populates the given metadata into the view
2781c18828d20807342d37000746b18a3c1696f3b2eJim Miller     */
2791c18828d20807342d37000746b18a3c1696f3b2eJim Miller    private void populateMetadata() {
2801c18828d20807342d37000746b18a3c1696f3b2eJim Miller        StringBuilder sb = new StringBuilder();
2811c18828d20807342d37000746b18a3c1696f3b2eJim Miller        int trackTitleLength = 0;
2821c18828d20807342d37000746b18a3c1696f3b2eJim Miller        if (!TextUtils.isEmpty(mMetadata.trackTitle)) {
2831c18828d20807342d37000746b18a3c1696f3b2eJim Miller            sb.append(mMetadata.trackTitle);
2841c18828d20807342d37000746b18a3c1696f3b2eJim Miller            trackTitleLength = mMetadata.trackTitle.length();
2851c18828d20807342d37000746b18a3c1696f3b2eJim Miller        }
2861c18828d20807342d37000746b18a3c1696f3b2eJim Miller        if (!TextUtils.isEmpty(mMetadata.artist)) {
2871c18828d20807342d37000746b18a3c1696f3b2eJim Miller            if (sb.length() != 0) {
2881c18828d20807342d37000746b18a3c1696f3b2eJim Miller                sb.append(" - ");
2891c18828d20807342d37000746b18a3c1696f3b2eJim Miller            }
2901c18828d20807342d37000746b18a3c1696f3b2eJim Miller            sb.append(mMetadata.artist);
2911c18828d20807342d37000746b18a3c1696f3b2eJim Miller        }
2921c18828d20807342d37000746b18a3c1696f3b2eJim Miller        if (!TextUtils.isEmpty(mMetadata.albumTitle)) {
2931c18828d20807342d37000746b18a3c1696f3b2eJim Miller            if (sb.length() != 0) {
2941c18828d20807342d37000746b18a3c1696f3b2eJim Miller                sb.append(" - ");
2956b05d58018c2806459c121e507c005639b74aee9Jim Miller            }
2961c18828d20807342d37000746b18a3c1696f3b2eJim Miller            sb.append(mMetadata.albumTitle);
2971c18828d20807342d37000746b18a3c1696f3b2eJim Miller        }
2981c18828d20807342d37000746b18a3c1696f3b2eJim Miller        mTrackTitle.setText(sb.toString(), TextView.BufferType.SPANNABLE);
2991c18828d20807342d37000746b18a3c1696f3b2eJim Miller        Spannable str = (Spannable) mTrackTitle.getText();
3001c18828d20807342d37000746b18a3c1696f3b2eJim Miller        if (trackTitleLength != 0) {
3011c18828d20807342d37000746b18a3c1696f3b2eJim Miller            str.setSpan(new ForegroundColorSpan(0xffffffff), 0, trackTitleLength,
3021c18828d20807342d37000746b18a3c1696f3b2eJim Miller                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
3031c18828d20807342d37000746b18a3c1696f3b2eJim Miller            trackTitleLength++;
3041c18828d20807342d37000746b18a3c1696f3b2eJim Miller        }
3051c18828d20807342d37000746b18a3c1696f3b2eJim Miller        if (sb.length() > trackTitleLength) {
3061c18828d20807342d37000746b18a3c1696f3b2eJim Miller            str.setSpan(new ForegroundColorSpan(0x7fffffff), trackTitleLength, sb.length(),
3071c18828d20807342d37000746b18a3c1696f3b2eJim Miller                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
3086b05d58018c2806459c121e507c005639b74aee9Jim Miller        }
3091c18828d20807342d37000746b18a3c1696f3b2eJim Miller
3101c18828d20807342d37000746b18a3c1696f3b2eJim Miller        mAlbumArt.setImageBitmap(mMetadata.bitmap);
3111c18828d20807342d37000746b18a3c1696f3b2eJim Miller        final int flags = mTransportControlFlags;
3121c18828d20807342d37000746b18a3c1696f3b2eJim Miller        setVisibilityBasedOnFlag(mBtnPrev, flags, RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS);
3131c18828d20807342d37000746b18a3c1696f3b2eJim Miller        setVisibilityBasedOnFlag(mBtnNext, flags, RemoteControlClient.FLAG_KEY_MEDIA_NEXT);
3141c18828d20807342d37000746b18a3c1696f3b2eJim Miller        setVisibilityBasedOnFlag(mBtnPrev, flags,
3151c18828d20807342d37000746b18a3c1696f3b2eJim Miller                RemoteControlClient.FLAG_KEY_MEDIA_PLAY
3161c18828d20807342d37000746b18a3c1696f3b2eJim Miller                | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE
3171c18828d20807342d37000746b18a3c1696f3b2eJim Miller                | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE
3181c18828d20807342d37000746b18a3c1696f3b2eJim Miller                | RemoteControlClient.FLAG_KEY_MEDIA_STOP);
3191c18828d20807342d37000746b18a3c1696f3b2eJim Miller
3201c18828d20807342d37000746b18a3c1696f3b2eJim Miller        updatePlayPauseState(mPlayState);
3216b05d58018c2806459c121e507c005639b74aee9Jim Miller    }
3226b05d58018c2806459c121e507c005639b74aee9Jim Miller
3231c18828d20807342d37000746b18a3c1696f3b2eJim Miller    private static void setVisibilityBasedOnFlag(View view, int flags, int flag) {
3241c18828d20807342d37000746b18a3c1696f3b2eJim Miller        if ((flags & flag) != 0) {
3251c18828d20807342d37000746b18a3c1696f3b2eJim Miller            view.setVisibility(View.VISIBLE);
3261c18828d20807342d37000746b18a3c1696f3b2eJim Miller        } else {
3271c18828d20807342d37000746b18a3c1696f3b2eJim Miller            view.setVisibility(View.GONE);
3281c18828d20807342d37000746b18a3c1696f3b2eJim Miller        }
3291c18828d20807342d37000746b18a3c1696f3b2eJim Miller    }
3301c18828d20807342d37000746b18a3c1696f3b2eJim Miller
3311c18828d20807342d37000746b18a3c1696f3b2eJim Miller    private void updatePlayPauseState(int state) {
3321c18828d20807342d37000746b18a3c1696f3b2eJim Miller        if (DEBUG) Log.v(TAG,
3331c18828d20807342d37000746b18a3c1696f3b2eJim Miller                "updatePlayPauseState(), old=" + mPlayState + ", state=" + state);
3341c18828d20807342d37000746b18a3c1696f3b2eJim Miller        if (state == mPlayState) {
3351c18828d20807342d37000746b18a3c1696f3b2eJim Miller            return;
3361c18828d20807342d37000746b18a3c1696f3b2eJim Miller        }
3371c18828d20807342d37000746b18a3c1696f3b2eJim Miller        switch (state) {
3381c18828d20807342d37000746b18a3c1696f3b2eJim Miller            case RemoteControlClient.PLAYSTATE_PLAYING:
3391c18828d20807342d37000746b18a3c1696f3b2eJim Miller                mBtnPlay.setImageResource(com.android.internal.R.drawable.ic_media_pause);
3406b05d58018c2806459c121e507c005639b74aee9Jim Miller                break;
3416b05d58018c2806459c121e507c005639b74aee9Jim Miller
3421c18828d20807342d37000746b18a3c1696f3b2eJim Miller            case RemoteControlClient.PLAYSTATE_BUFFERING:
3431c18828d20807342d37000746b18a3c1696f3b2eJim Miller                mBtnPlay.setImageResource(com.android.internal.R.drawable.ic_media_stop);
3446b05d58018c2806459c121e507c005639b74aee9Jim Miller                break;
3456b05d58018c2806459c121e507c005639b74aee9Jim Miller
3461c18828d20807342d37000746b18a3c1696f3b2eJim Miller            case RemoteControlClient.PLAYSTATE_PAUSED:
3471c18828d20807342d37000746b18a3c1696f3b2eJim Miller            default:
3481c18828d20807342d37000746b18a3c1696f3b2eJim Miller                mBtnPlay.setImageResource(com.android.internal.R.drawable.ic_media_play);
3496b05d58018c2806459c121e507c005639b74aee9Jim Miller                break;
3506b05d58018c2806459c121e507c005639b74aee9Jim Miller        }
3511c18828d20807342d37000746b18a3c1696f3b2eJim Miller        mPlayState = state;
3521c18828d20807342d37000746b18a3c1696f3b2eJim Miller    }
3531c18828d20807342d37000746b18a3c1696f3b2eJim Miller
3541c18828d20807342d37000746b18a3c1696f3b2eJim Miller    public void onClick(View v) {
3551c18828d20807342d37000746b18a3c1696f3b2eJim Miller        int keyCode = -1;
3561c18828d20807342d37000746b18a3c1696f3b2eJim Miller        if (v == mBtnPrev) {
3571c18828d20807342d37000746b18a3c1696f3b2eJim Miller            keyCode = KeyEvent.KEYCODE_MEDIA_PREVIOUS;
3581c18828d20807342d37000746b18a3c1696f3b2eJim Miller        } else if (v == mBtnNext) {
3591c18828d20807342d37000746b18a3c1696f3b2eJim Miller            keyCode = KeyEvent.KEYCODE_MEDIA_NEXT;
3601c18828d20807342d37000746b18a3c1696f3b2eJim Miller        } else if (v == mBtnPlay) {
3611c18828d20807342d37000746b18a3c1696f3b2eJim Miller            keyCode = KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE;
3621c18828d20807342d37000746b18a3c1696f3b2eJim Miller
3631c18828d20807342d37000746b18a3c1696f3b2eJim Miller        }
3641c18828d20807342d37000746b18a3c1696f3b2eJim Miller        if (keyCode != -1) {
3651c18828d20807342d37000746b18a3c1696f3b2eJim Miller            sendMediaButtonClick(keyCode);
3661c18828d20807342d37000746b18a3c1696f3b2eJim Miller            if (mWidgetCallbacks != null) {
3671c18828d20807342d37000746b18a3c1696f3b2eJim Miller                mWidgetCallbacks.userActivity(this);
3681c18828d20807342d37000746b18a3c1696f3b2eJim Miller            }
3696b05d58018c2806459c121e507c005639b74aee9Jim Miller        }
3706b05d58018c2806459c121e507c005639b74aee9Jim Miller    }
3716b05d58018c2806459c121e507c005639b74aee9Jim Miller
3721c18828d20807342d37000746b18a3c1696f3b2eJim Miller    private void sendMediaButtonClick(int keyCode) {
373f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi        // use the registered PendingIntent that will be processed by the registered
374f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi        //    media button event receiver, which is the component of mClientIntent
3751c18828d20807342d37000746b18a3c1696f3b2eJim Miller        KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
3761c18828d20807342d37000746b18a3c1696f3b2eJim Miller        Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
3771c18828d20807342d37000746b18a3c1696f3b2eJim Miller        intent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
378f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi        try {
379f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi            mClientIntent.send(getContext(), 0, intent);
380f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi        } catch (CanceledException e) {
381f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi            Log.e(TAG, "Error sending intent for media button down: "+e);
382f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi            e.printStackTrace();
383f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi        }
3841c18828d20807342d37000746b18a3c1696f3b2eJim Miller
3851c18828d20807342d37000746b18a3c1696f3b2eJim Miller        keyEvent = new KeyEvent(KeyEvent.ACTION_UP, keyCode);
3861c18828d20807342d37000746b18a3c1696f3b2eJim Miller        intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
3871c18828d20807342d37000746b18a3c1696f3b2eJim Miller        intent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
388f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi        try {
389f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi            mClientIntent.send(getContext(), 0, intent);
390f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi        } catch (CanceledException e) {
391f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi            Log.e(TAG, "Error sending intent for media button up: "+e);
392f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi            e.printStackTrace();
393f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi        }
3941c18828d20807342d37000746b18a3c1696f3b2eJim Miller    }
3951c18828d20807342d37000746b18a3c1696f3b2eJim Miller
3961c18828d20807342d37000746b18a3c1696f3b2eJim Miller    public void setCallback(LockScreenWidgetCallback callback) {
3971c18828d20807342d37000746b18a3c1696f3b2eJim Miller        mWidgetCallbacks = callback;
3981c18828d20807342d37000746b18a3c1696f3b2eJim Miller    }
3991c18828d20807342d37000746b18a3c1696f3b2eJim Miller
400054340d0a3f242efeaf898cca38625bdcb3b4b5aJeff Sharkey    public boolean providesClock() {
401054340d0a3f242efeaf898cca38625bdcb3b4b5aJeff Sharkey        return false;
402054340d0a3f242efeaf898cca38625bdcb3b4b5aJeff Sharkey    }
403054340d0a3f242efeaf898cca38625bdcb3b4b5aJeff Sharkey
4046b05d58018c2806459c121e507c005639b74aee9Jim Miller}
405