PlayerService.java revision 42ea7eecd149161ed192d3029f0d77d1d08a4aa5
1/*
2 * Copyright (C) 2014 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 */
16package com.android.onemedia;
17
18import android.app.Service;
19import android.content.Intent;
20import android.media.session.MediaSessionToken;
21import android.media.session.PlaybackState;
22import android.os.Bundle;
23import android.os.IBinder;
24import android.os.RemoteException;
25import android.util.Log;
26
27import com.android.onemedia.playback.IRequestCallback;
28import com.android.onemedia.playback.RequestUtils;
29
30import java.util.ArrayList;
31
32public class PlayerService extends Service {
33    private static final String TAG = "PlayerService";
34
35    private PlayerBinder mBinder;
36    private PlayerSession mSession;
37    private Intent mIntent;
38    private boolean mStarted = false;
39
40    private ArrayList<IPlayerCallback> mCbs = new ArrayList<IPlayerCallback>();
41
42    @Override
43    public void onCreate() {
44        Log.d(TAG, "onCreate");
45        mIntent = onCreateServiceIntent();
46        if (mSession == null) {
47            mSession = onCreatePlayerController();
48            mSession.createSession();
49            mSession.setListener(mPlayerListener);
50        }
51    }
52
53    @Override
54    public IBinder onBind(Intent intent) {
55        if (mBinder == null) {
56            mBinder = new PlayerBinder();
57        }
58        return mBinder;
59    }
60
61    @Override
62    public int onStartCommand(Intent intent, int flags, int startId) {
63        Log.d(TAG, "onStartCommand");
64        return START_STICKY;
65    }
66
67    @Override
68    public void onDestroy() {
69        Log.d(TAG, "onDestroy");
70        mSession.onDestroy();
71        mSession = null;
72    }
73
74    public void onPlaybackStarted() {
75        if (!mStarted) {
76            Log.d(TAG, "Starting self");
77            startService(onCreateServiceIntent());
78            mStarted = true;
79        }
80    }
81
82    public void onPlaybackEnded() {
83        if (mStarted) {
84            Log.d(TAG, "Stopping self");
85            stopSelf();
86            mStarted = false;
87        }
88    }
89
90    protected Intent onCreateServiceIntent() {
91        return new Intent(this, PlayerService.class).setPackage(getBasePackageName());
92    }
93
94    protected PlayerSession onCreatePlayerController() {
95        return new PlayerSession(this);
96    }
97
98    protected ArrayList<String> getAllowedPackages() {
99        return null;
100    }
101
102    private final PlayerSession.Listener mPlayerListener = new PlayerSession.Listener() {
103        @Override
104        public void onPlayStateChanged(PlaybackState state) {
105            switch (state.getState()) {
106                case PlaybackState.PLAYSTATE_PLAYING:
107                    onPlaybackStarted();
108                    break;
109                case PlaybackState.PLAYSTATE_STOPPED:
110                case PlaybackState.PLAYSTATE_ERROR:
111                    onPlaybackEnded();
112                    break;
113            }
114        }
115    };
116
117    public class PlayerBinder extends IPlayerService.Stub {
118        @Override
119        public void sendRequest(String action, Bundle params, IRequestCallback cb) {
120            if (RequestUtils.ACTION_SET_CONTENT.equals(action)) {
121                mSession.setContent(params);
122            } else if (RequestUtils.ACTION_SET_NEXT_CONTENT.equals(action)) {
123                mSession.setNextContent(params);
124            }
125        }
126
127        @Override
128        public void registerCallback(final IPlayerCallback cb) throws RemoteException {
129            if (!mCbs.contains(cb)) {
130                mCbs.add(cb);
131                cb.asBinder().linkToDeath(new IBinder.DeathRecipient() {
132                    @Override
133                    public void binderDied() {
134                        mCbs.remove(cb);
135                    }
136                }, 0);
137            }
138            try {
139                cb.onSessionChanged(getSessionToken());
140            } catch (RemoteException e) {
141                mCbs.remove(cb);
142                throw e;
143            }
144        }
145
146        @Override
147        public void unregisterCallback(IPlayerCallback cb) throws RemoteException {
148            mCbs.remove(cb);
149        }
150
151        @Override
152        public MediaSessionToken getSessionToken() throws RemoteException {
153            return mSession.getSessionToken();
154        }
155    }
156
157}
158