aboutsummaryrefslogtreecommitdiff
path: root/hwsample/src/com/example/android/fakehardwaretvinput/FakeHdmiService.java
blob: 2690a8704adfeb858aebad3b8490c6a39f08bbc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
 * Copyright (C) 2014 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.example.android.fakehardwaretvinput;

import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.hdmi.HdmiCecDeviceInfo;
import android.hardware.hdmi.IHdmiControlService;
import android.media.tv.ITvInputManager;
import android.media.tv.TvContentRating;
import android.media.tv.TvInputHardwareInfo;
import android.media.tv.TvInputInfo;
import android.media.tv.TvInputService;
import android.net.Uri;
import android.os.Handler;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;
import android.util.SparseArray;
import android.view.Surface;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.xmlpull.v1.XmlPullParserException;

/**
 * Demonstrates a simple HDMI input.
 *
 * Note that the service doesn't need to communicate with HdmiControlService directly unless it is
 * dealing with custom CEC or MHL signals.
 */
public class FakeHdmiService extends TvInputService {
    private static final boolean DEBUG = true;
    private static final String TAG = FakeHdmiService.class.getSimpleName();
    private static final int[] COLORS = {
          0xFF888888, 0xFF999999, 0xFFAAAAAA, 0xFFBBBBBB, 0xFFAAAAAA, 0xFF999999};
    private static final int[] ICONS = { R.drawable.fake_icon0, R.drawable.fake_icon1,
          R.drawable.fake_icon2, R.drawable.fake_icon3 };

    private ITvInputManager mManager = null;
    private IHdmiControlService mHdmiControlService = null;

    private final SparseArray<String> mHardwareInputIdMap = new SparseArray<String>();
    private final SparseArray<String> mCecInputIdMap = new SparseArray<String>();
    private final Map<String, TvInputInfo> mInputMap = new HashMap<String, TvInputInfo>();
    private ResolveInfo mResolveInfo;
    private final Random mRandom = new Random();

    private static class PortInfo {
        private final int mPortId;
        private final int mHardwareDeviceId;
        private final int mCecLogicalAddress;

        PortInfo(int portId, int hardwareDeviceId, int cecLogicalAddress) {
            this.mPortId = portId;
            this.mHardwareDeviceId = hardwareDeviceId;
            this.mCecLogicalAddress = cecLogicalAddress;
        }
    }
    private final SparseArray<PortInfo> mPortInfos = new SparseArray<PortInfo>();

    @Override
    public void onCreate() {
        super.onCreate();
        mResolveInfo = getPackageManager().resolveService(
                new Intent(SERVICE_INTERFACE).setClass(this, getClass()),
                PackageManager.GET_SERVICES | PackageManager.GET_META_DATA);
        mManager = ITvInputManager.Stub.asInterface(
                ServiceManager.getService(Context.TV_INPUT_SERVICE));
        mHdmiControlService = IHdmiControlService.Stub.asInterface(
                ServiceManager.getService(Context.HDMI_CONTROL_SERVICE));
    }

    @Override
    public Session onCreateSession(String inputId) {
        TvInputInfo info = mInputMap.get(inputId);
        if (info == null) {
            throw new IllegalArgumentException("Unknown inputId: " + inputId
                    + " ; this should not happen.");
        }
        return new HdmiInputSessionImpl(info, this);
    }

    @Override
    public TvInputInfo onHardwareAdded(TvInputHardwareInfo hardwareInfo) {
        if (hardwareInfo.getType() != TvInputHardwareInfo.TV_INPUT_TYPE_HDMI) {
            return null;
        }
        int deviceId = hardwareInfo.getDeviceId();
        if (mHardwareInputIdMap.indexOfKey(deviceId) >= 0) {
            Log.e(TAG, "Already created TvInputInfo for deviceId=" + deviceId);
            return null;
        }
        int portId = hardwareInfo.getHdmiPortId();
        if (portId < 0) {
            Log.e(TAG, "Failed to get HDMI port for deviceId=" + deviceId);
            return null;
        }
        if (mPortInfos.get(portId) != null) {
            Log.e(TAG, "Already have port " + portId + " for deviceId=" + deviceId);
            return null;
        }
        TvInputInfo info = null;
        try {
            info = TvInputInfo.createTvInputInfo(this, mResolveInfo, hardwareInfo,
                    "HDMI " + hardwareInfo.getHdmiPortId(), null);
        } catch (XmlPullParserException | IOException e) {
            Log.e(TAG, "Error while creating TvInputInfo", e);
            return null;
        }
        mHardwareInputIdMap.put(deviceId, info.getId());
        mInputMap.put(info.getId(), info);
        mPortInfos.put(portId, new PortInfo(portId, deviceId, -1));
        if (DEBUG) Log.d(TAG, "onHardwareAdded returns " + info);
        return info;
    }

    @Override
    public String onHardwareRemoved(TvInputHardwareInfo hardwareInfo) {
        int deviceId = hardwareInfo.getDeviceId();
        String inputId = mHardwareInputIdMap.get(deviceId);
        if (inputId == null) {
            if (DEBUG) Log.d(TAG, "TvInputInfo for deviceId=" + deviceId + " does not exist.");
            return null;
        }
        int portId = getPortInfoForDeviceId(deviceId);
        if (portId == -1) {
            Log.w(TAG, "Port not exists for deviceId=" + deviceId);
        }
        mPortInfos.remove(portId);
        mHardwareInputIdMap.remove(deviceId);
        mInputMap.remove(inputId);
        if (DEBUG) Log.d(TAG, "onHardwareRemoved returns " + inputId);
        return inputId;
    }

    private int getPortInfoForDeviceId(int deviceId) {
        for (int i = 0; i < mPortInfos.size(); i++) {
            PortInfo portInfo = mPortInfos.valueAt(i);
            if (portInfo.mHardwareDeviceId == deviceId) {
                return portInfo.mPortId;
            }
        }
        return -1;
    }

    @Override
    public TvInputInfo onHdmiCecDeviceAdded(HdmiCecDeviceInfo cecDeviceInfo) {
        int logicalAddress = cecDeviceInfo.getLogicalAddress();
        if (mCecInputIdMap.indexOfKey(logicalAddress) >= 0) {
            Log.e(TAG, "Already created TvInputInfo for logicalAddress=" + logicalAddress);
            return null;
        }
        int portId = cecDeviceInfo.getPortId();
        if (portId < 0) {
            Log.e(TAG, "Failed to get HDMI port for logicalAddress=" + logicalAddress);
            return null;
        }
        PortInfo portInfo = mPortInfos.get(portId);
        if (portInfo == null) {
            Log.e(TAG, "Unknown HDMI port " + portId + " for logicalAddress=" + logicalAddress);
            return null;
        }
        TvInputInfo parentInfo =
                mInputMap.get(mHardwareInputIdMap.get(portInfo.mHardwareDeviceId));
        TvInputInfo info = null;
        try {
            info = TvInputInfo.createTvInputInfo(this, mResolveInfo, cecDeviceInfo,
                    parentInfo.getId(), cecDeviceInfo.getDisplayName(),
                    Uri.parse("android.resource://" + getPackageName() + "/"
                          + ICONS[mRandom.nextInt(ICONS.length)]));
        } catch (XmlPullParserException | IOException e) {
            Log.e(TAG, "Error while creating TvInputInfo", e);
            return null;
        }
        mCecInputIdMap.put(logicalAddress, info.getId());
        mInputMap.put(info.getId(), info);
        if (DEBUG) Log.d(TAG, "onHdmiCecDeviceAdded returns " + info);
        return info;
    }

    @Override
    public String onHdmiCecDeviceRemoved(HdmiCecDeviceInfo cecDeviceInfo) {
        int logicalAddress = cecDeviceInfo.getLogicalAddress();
        String inputId = mCecInputIdMap.get(logicalAddress);
        if (inputId == null) {
            if (DEBUG) {
                Log.d(TAG, "TvInputInfo for logicalAddress=" + logicalAddress + " does not exist.");
            }
            return null;
        }
        mCecInputIdMap.remove(logicalAddress);
        mInputMap.remove(inputId);
        if (DEBUG) Log.d(TAG, "onHdmiCecDeviceRemoved returns " + inputId);
        return inputId;
    }

    private class HdmiInputSessionImpl extends Session {
        private final TvInputInfo mInfo;
        private final Object mImplLock = new Object();
        private Surface mSurface = null;
        private final Paint mTextPaint = new Paint();
        private final Handler mHandler = new Handler();
        private final String mLabel;

        private final Runnable mDrawTask = new Runnable() {
            private int mIndex = 0;

            @Override
            public void run() {
                synchronized (mImplLock) {
                    if (mSurface != null) {
                        Canvas c = mSurface.lockCanvas(null);
                        c.drawColor(COLORS[mIndex]);
                        c.drawText(mLabel, 100f, 200f, mTextPaint);
                        mSurface.unlockCanvasAndPost(c);
                    }
                }
                ++mIndex;
                if (mIndex >= COLORS.length) {
                    mIndex = 0;
                }
                mHandler.postDelayed(this, 1000);
            }
        };

        HdmiInputSessionImpl(TvInputInfo info, Context context) {
            super(context);
            mInfo = info;
            mLabel = info.loadLabel(FakeHdmiService.this).toString();
            mTextPaint.setColor(Color.BLACK);
            mTextPaint.setTextSize(200);
            mHandler.post(mDrawTask);
        }

        @Override
        public void onRelease() {
            mHandler.removeCallbacks(mDrawTask);
        }

        @Override
        public boolean onSetSurface(Surface surface) {
            synchronized (mImplLock) {
                mSurface = surface;
                return true;
            }
        }

        @Override
        public void onSetStreamVolume(float volume) {
            // No-op
        }

        @Override
        public boolean onTune(Uri channelUri) {
            return true;
        }

        @Override
        public void onSetCaptionEnabled(boolean enabled) {
            // No-op
        }
    }
}