summaryrefslogtreecommitdiffstats
path: root/tvout/src/com/teamhacksung/tvout/TvOutService.java
blob: b633b3dd28083ca999563f727fb895bd9b223193 (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
package com.teamhacksung.tvout;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.Tvout;
import android.nfc.Tag;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;

public class TvOutService extends Service {

    public static final String TAG = "TvOutService_java";

    private Tvout mTvOut;
    private boolean mWasOn = false; // For enabling on screen on

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (Intent.ACTION_HDMI_AUDIO_PLUG.equals(action)) {
                getTvoutInstance();
                int state = intent.getIntExtra("state", 0);
                if (state == 1 && !mTvOut.getStatus()) {
                    // Enable when cable is plugged
                    Log.i(TAG, "HDMI plugged");
                    mWasOn = false;
                    enable();
                } else if (mTvOut.getStatus()) {
                    // Disable when cable is unplugged
                    Log.i(TAG, "HDMI unplugged");
                    mWasOn = false;
                    disable();
                    releaseTvout();
                }
            } else if (Intent.ACTION_SCREEN_ON.equals(action)) {
                if (mTvOut != null && mWasOn) {
                    Log.i(TAG, "Screen On - Resume TvOut stream");
                    mWasOn = false;
                    mTvOut.setSuspendStatus(false);
                }
            } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
                if (mTvOut != null && mTvOut.getStatus()) {
                    Log.i(TAG, "Screen Off - Pausing TvOut stream");
                    mWasOn = true;
                    mTvOut.setSuspendStatus(true);
                }
            }
        }

    };

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        IntentFilter filter = new IntentFilter(Intent.ACTION_HDMI_AUDIO_PLUG);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_SCREEN_ON);
        registerReceiver(mReceiver, filter);
        Log.i(TAG, "Registered Receiver");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    private boolean getTvoutInstance() {
        if (mTvOut != null) return true;

        try {
            mTvOut = new Tvout();
        } catch (Exception e) {
            return false;
        }

        return true;
    }

    private void releaseTvout() {
        if (mTvOut != null) {
            mTvOut.release();
            mTvOut = null;
        }
    }

    @Override
    public void onDestroy() {
        unregisterReceiver(mReceiver);
        releaseTvout();
        super.onDestroy();
    }

    private void enable() {
        if (mTvOut == null) return;
        mTvOut.setStatus(true);
        mTvOut.setCableStatus(true);
        mTvOut.setSuspendStatus(false);
    }

    private void disable() {
        if (mTvOut == null) return;
        mTvOut.setStatus(false);
        mTvOut.setCableStatus(false);
    }

}