aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/dvr/DvrWatchedPositionManager.java
blob: 4eada74247c1ca4bfe6f9d989d3a0f829bbd69ef (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
/*
 * Copyright (C) 2016 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.android.tv.dvr;

import android.content.Context;
import android.content.SharedPreferences;
import android.media.tv.TvInputManager;
import android.support.annotation.IntDef;

import com.android.tv.common.SharedPreferencesUtils;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * A class to manage DVR watched state.
 * It will remember and provides previous watched position of DVR playback.
 */
public class DvrWatchedPositionManager {
    private final static String TAG = "DvrWatchedPositionManager";
    private final boolean DEBUG = false;

    private SharedPreferences mWatchedPositions;
    private final Map<Long, Set> mListeners = new HashMap<>();

    /**
     * The minimum percentage of recorded program being watched that will be considered as being
     * completely watched.
     */
    public static final float DVR_WATCHED_THRESHOLD_RATE = 0.98f;

    @Retention(RetentionPolicy.SOURCE)
    @IntDef({DVR_WATCHED_STATUS_NEW, DVR_WATCHED_STATUS_WATCHING, DVR_WATCHED_STATUS_WATCHED})
    public @interface DvrWatchedStatus {}
    /**
     * The status indicates the recorded program has not been watched at all.
     */
    public static final int DVR_WATCHED_STATUS_NEW = 0;
    /**
     * The status indicates the recorded program is being watched.
     */
    public static final int DVR_WATCHED_STATUS_WATCHING = 1;
    /**
     * The status indicates the recorded program was completely watched.
     */
    public static final int DVR_WATCHED_STATUS_WATCHED = 2;

    public DvrWatchedPositionManager(Context context) {
        mWatchedPositions = context.getSharedPreferences(
                SharedPreferencesUtils.SHARED_PREF_DVR_WATCHED_POSITION, Context.MODE_PRIVATE);
    }

    /**
     * Sets the watched position of the give program.
     */
    public void setWatchedPosition(long recordedProgramId, long positionMs) {
        mWatchedPositions.edit().putLong(Long.toString(recordedProgramId), positionMs).apply();
        notifyWatchedPositionChanged(recordedProgramId, positionMs);
    }

    /**
     * Gets the watched position of the give program.
     */
    public long getWatchedPosition(long recordedProgramId) {
        return mWatchedPositions.getLong(Long.toString(recordedProgramId),
                TvInputManager.TIME_SHIFT_INVALID_TIME);
    }

    @DvrWatchedStatus public int getWatchedStatus(RecordedProgram recordedProgram) {
        long watchedPosition = getWatchedPosition(recordedProgram.getId());
        if (watchedPosition == TvInputManager.TIME_SHIFT_INVALID_TIME) {
            return DVR_WATCHED_STATUS_NEW;
        } else if (watchedPosition > recordedProgram
                .getDurationMillis() * DVR_WATCHED_THRESHOLD_RATE) {
            return DVR_WATCHED_STATUS_WATCHED;
        } else {
            return DVR_WATCHED_STATUS_WATCHING;
        }
    }

    /**
     * Adds {@link WatchedPositionChangedListener}.
     */
    public void addListener(WatchedPositionChangedListener listener, long recordedProgramId) {
        if (recordedProgramId == RecordedProgram.ID_NOT_SET) {
            return;
        }
        Set<WatchedPositionChangedListener> listenerSet = mListeners.get(recordedProgramId);
        if (listenerSet == null) {
            listenerSet = new CopyOnWriteArraySet<>();
            mListeners.put(recordedProgramId, listenerSet);
        }
        listenerSet.add(listener);
    }

    /**
     * Removes {@link WatchedPositionChangedListener}.
     */
    public void removeListener(WatchedPositionChangedListener listener) {
        for (long recordedProgramId : new ArrayList<>(mListeners.keySet())) {
            removeListener(listener, recordedProgramId);
        }
    }

    /**
     * Removes {@link WatchedPositionChangedListener}.
     */
    public void removeListener(WatchedPositionChangedListener listener, long recordedProgramId) {
        Set<WatchedPositionChangedListener> listenerSet = mListeners.get(recordedProgramId);
        if (listenerSet == null) {
            return;
        }
        listenerSet.remove(listener);
        if (listenerSet.isEmpty()) {
            mListeners.remove(recordedProgramId);
        }
    }

    private void notifyWatchedPositionChanged(long recordedProgramId, long positionMs) {
        Set<WatchedPositionChangedListener> listenerSet = mListeners.get(recordedProgramId);
        if (listenerSet == null) {
            return;
        }
        for (WatchedPositionChangedListener listener : listenerSet) {
            listener.onWatchedPositionChanged(recordedProgramId, positionMs);
        }
    }

    public interface WatchedPositionChangedListener {
        /**
         * Called when the watched position of some program is changed.
         */
        void onWatchedPositionChanged(long recordedProgramId, long positionMs);
    }
}