PlayerAdapter.java revision 3328f87260d3b7be4fcf0c22cdc2dda5d60ab77c
1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.example.android.leanback;
18
19import android.os.Handler;
20
21class PlayerAdapter extends
22        android.support.v17.leanback.media.PlayerAdapter {
23
24    static final int FAUX_DURATION = 33 * 1000;
25
26    private boolean mIsPlaying;
27    private long mStartTime;
28    private long mStartPosition = 0;
29    private Handler mHandler = new Handler();
30    private final Runnable mUpdateProgressRunnable = new Runnable() {
31        @Override
32        public void run() {
33            getCallback().onCurrentPositionChanged(com.example.android.leanback.PlayerAdapter.this);
34            mHandler.postDelayed(this, 16);
35        }
36    };
37
38    @Override
39    public boolean isPlaying() {
40        return mIsPlaying;
41    }
42
43    @Override
44    public long getDuration() {
45        return FAUX_DURATION;
46    }
47
48    @Override
49    public void seekTo(long position) {
50        mStartPosition = position;
51        mStartTime = System.currentTimeMillis();
52        getCallback().onCurrentPositionChanged(com.example.android.leanback.PlayerAdapter.this);
53    }
54
55    @Override
56    public long getCurrentPosition() {
57        int speed;
58        if (!mIsPlaying) {
59            speed = 0;
60        } else {
61            speed = 1;
62        }
63        long position = mStartPosition + (System.currentTimeMillis() - mStartTime) * speed;
64        if (position > getDuration()) {
65            position = getDuration();
66            onPlaybackComplete(true);
67        } else if (position < 0) {
68            position = 0;
69            onPlaybackComplete(false);
70        }
71        return (int) position;
72    }
73
74    void onPlaybackComplete(final boolean ended) {
75        mHandler.post(new Runnable() {
76            @Override
77            public void run() {
78                mStartPosition = 0;
79                mIsPlaying = false;
80                getCallback().onPlayStateChanged(com.example.android.leanback.PlayerAdapter.this);
81                if (ended) {
82                    getCallback().onPlayCompleted(com.example.android.leanback.PlayerAdapter.this);
83                }
84            }
85        });
86    }
87
88    @Override
89    public void play() {
90        if (mIsPlaying) {
91            return;
92        }
93        mStartPosition = getCurrentPosition();
94        mIsPlaying = true;
95        mStartTime = System.currentTimeMillis();
96        getCallback().onPlayStateChanged(com.example.android.leanback.PlayerAdapter.this);
97    }
98
99    @Override
100    public void pause() {
101        if (!mIsPlaying) {
102            return;
103        }
104        mStartPosition = getCurrentPosition();
105        mIsPlaying = false;
106        getCallback().onPlayStateChanged(com.example.android.leanback.PlayerAdapter.this);
107    }
108
109    @Override
110    public void setProgressUpdatingEnabled(boolean enable) {
111        mHandler.removeCallbacks(mUpdateProgressRunnable);
112        if (enable) {
113            mUpdateProgressRunnable.run();
114        }
115    }
116}
117