aboutsummaryrefslogtreecommitdiff
path: root/TestMediaApp/src/com/android/car/media/testmediaapp/TmaMediaEvent.java
blob: 8fe30efe1d84321a3dbb3b6383f099bc3170b9f1 (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
/*
 * Copyright 2019 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.car.media.testmediaapp;

import static android.support.v4.media.session.PlaybackStateCompat.ERROR_CODE_ACTION_ABORTED;
import static android.support.v4.media.session.PlaybackStateCompat.ERROR_CODE_APP_ERROR;
import static android.support.v4.media.session.PlaybackStateCompat.ERROR_CODE_AUTHENTICATION_EXPIRED;
import static android.support.v4.media.session.PlaybackStateCompat.ERROR_CODE_CONCURRENT_STREAM_LIMIT;
import static android.support.v4.media.session.PlaybackStateCompat.ERROR_CODE_CONTENT_ALREADY_PLAYING;
import static android.support.v4.media.session.PlaybackStateCompat.ERROR_CODE_END_OF_QUEUE;
import static android.support.v4.media.session.PlaybackStateCompat.ERROR_CODE_NOT_AVAILABLE_IN_REGION;
import static android.support.v4.media.session.PlaybackStateCompat.ERROR_CODE_NOT_SUPPORTED;
import static android.support.v4.media.session.PlaybackStateCompat.ERROR_CODE_PARENTAL_CONTROL_RESTRICTED;
import static android.support.v4.media.session.PlaybackStateCompat.ERROR_CODE_PREMIUM_ACCOUNT_REQUIRED;
import static android.support.v4.media.session.PlaybackStateCompat.ERROR_CODE_SKIP_LIMIT_REACHED;
import static android.support.v4.media.session.PlaybackStateCompat.ERROR_CODE_UNKNOWN_ERROR;
import static android.support.v4.media.session.PlaybackStateCompat.STATE_BUFFERING;
import static android.support.v4.media.session.PlaybackStateCompat.STATE_CONNECTING;
import static android.support.v4.media.session.PlaybackStateCompat.STATE_ERROR;
import static android.support.v4.media.session.PlaybackStateCompat.STATE_FAST_FORWARDING;
import static android.support.v4.media.session.PlaybackStateCompat.STATE_NONE;
import static android.support.v4.media.session.PlaybackStateCompat.STATE_PAUSED;
import static android.support.v4.media.session.PlaybackStateCompat.STATE_PLAYING;
import static android.support.v4.media.session.PlaybackStateCompat.STATE_REWINDING;
import static android.support.v4.media.session.PlaybackStateCompat.STATE_SKIPPING_TO_NEXT;
import static android.support.v4.media.session.PlaybackStateCompat.STATE_SKIPPING_TO_PREVIOUS;
import static android.support.v4.media.session.PlaybackStateCompat.STATE_SKIPPING_TO_QUEUE_ITEM;
import static android.support.v4.media.session.PlaybackStateCompat.STATE_STOPPED;

import android.support.v4.media.session.PlaybackStateCompat.State;
import android.util.Log;

/**
 * Contains the info needed to generate a new playback state.
 */
public class TmaMediaEvent {

    private static final String TAG = "TmaMediaEvent";

    public static final TmaMediaEvent INSTANT_PLAYBACK =
            new TmaMediaEvent(EventState.PLAYING, StateErrorCode.UNKNOWN_ERROR, null, null,
                    ResolutionIntent.NONE, Action.NONE, 0, null, null);

    /** The name of each entry is the value used in the json file. */
    public enum EventState {
        NONE                    (STATE_NONE),
        STOPPED                 (STATE_STOPPED),
        PAUSED                  (STATE_PAUSED),
        PLAYING                 (STATE_PLAYING),
        FAST_FORWARDING         (STATE_FAST_FORWARDING),
        REWINDING               (STATE_REWINDING),
        BUFFERING               (STATE_BUFFERING),
        ERROR                   (STATE_ERROR),
        CONNECTING              (STATE_CONNECTING),
        SKIPPING_TO_PREVIOUS    (STATE_SKIPPING_TO_PREVIOUS),
        SKIPPING_TO_NEXT        (STATE_SKIPPING_TO_NEXT),
        SKIPPING_TO_QUEUE_ITEM  (STATE_SKIPPING_TO_QUEUE_ITEM);

        @State final int mValue;

        EventState(@State int value) {
            mValue = value;
        }
    }

    /** The name of each entry is the value used in the json file. */
    public enum StateErrorCode {
        UNKNOWN_ERROR                   (ERROR_CODE_UNKNOWN_ERROR),
        APP_ERROR                       (ERROR_CODE_APP_ERROR),
        NOT_SUPPORTED                   (ERROR_CODE_NOT_SUPPORTED),
        AUTHENTICATION_EXPIRED          (ERROR_CODE_AUTHENTICATION_EXPIRED),
        PREMIUM_ACCOUNT_REQUIRED        (ERROR_CODE_PREMIUM_ACCOUNT_REQUIRED),
        CONCURRENT_STREAM_LIMIT         (ERROR_CODE_CONCURRENT_STREAM_LIMIT),
        PARENTAL_CONTROL_RESTRICTED     (ERROR_CODE_PARENTAL_CONTROL_RESTRICTED),
        NOT_AVAILABLE_IN_REGION         (ERROR_CODE_NOT_AVAILABLE_IN_REGION),
        CONTENT_ALREADY_PLAYING         (ERROR_CODE_CONTENT_ALREADY_PLAYING),
        SKIP_LIMIT_REACHED              (ERROR_CODE_SKIP_LIMIT_REACHED),
        ACTION_ABORTED                  (ERROR_CODE_ACTION_ABORTED),
        END_OF_QUEUE                    (ERROR_CODE_END_OF_QUEUE);

        @State final int mValue;

        StateErrorCode(@State int value) {
            mValue = value;
        }
    }

    /** The name of each entry is the value used in the json file. */
    public enum ResolutionIntent {
        NONE,
        PREFS
    }

    /** The name of each entry is the value used in the json file. */
    public enum Action {
        NONE,
        RESET_METADATA
    }

    final EventState mState;
    final StateErrorCode mErrorCode;
    final String mErrorMessage;
    final String mActionLabel;
    final ResolutionIntent mResolutionIntent;
    final Action mAction;
    /** How long to wait before sending the event to the app. */
    final int mPostDelayMs;
    private final String mExceptionClass;
    final String mMediaItemIdToToggle;

    public TmaMediaEvent(EventState state, StateErrorCode errorCode, String errorMessage,
            String actionLabel, ResolutionIntent resolutionIntent, Action action, int postDelayMs,
            String exceptionClass, String mediaItemIdToToggle) {
        mState = state;
        mErrorCode = errorCode;
        mErrorMessage = errorMessage;
        mActionLabel = actionLabel;
        mResolutionIntent = resolutionIntent;
        mAction = action;
        mPostDelayMs = postDelayMs;
        mExceptionClass = exceptionClass;
        mMediaItemIdToToggle = mediaItemIdToToggle;
    }

    boolean premiumAccountRequired() {
        return mState == EventState.ERROR && mErrorCode == StateErrorCode.PREMIUM_ACCOUNT_REQUIRED;
    }

    void maybeThrow() {
        if (mExceptionClass != null) {
            RuntimeException exception = null;
            try {
                Class aClass = Class.forName(mExceptionClass);
                exception = (RuntimeException) aClass.newInstance();
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
                Log.e(TAG, "Class error for " + mExceptionClass + " : " + e);
            }

            if (exception != null) throw exception;
        }
    }

    @Override
    public String toString() {
        return "TmaMediaEvent{" +
                "mState=" + mState +
                ", mErrorCode=" + mErrorCode +
                ", mErrorMessage='" + mErrorMessage + '\'' +
                ", mActionLabel='" + mActionLabel + '\'' +
                ", mResolutionIntent=" + mResolutionIntent +
                ", mAction=" + mAction +
                ", mPostDelayMs=" + mPostDelayMs +
                ", mExceptionClass=" + mExceptionClass +
                '}';
    }
}