aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/data/InternalDataUtils.java
blob: e33ca18fc2a704c5c700901e4e80c1d4b166463d (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
/*
 * 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.data;

import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;

import com.android.tv.data.Program.CriticScore;
import com.android.tv.dvr.data.RecordedProgram;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;

/**
 * A utility class to parse and store data from the
 * {@link android.media.tv.TvContract.Programs#COLUMN_INTERNAL_PROVIDER_DATA} field in the
 * {@link android.media.tv.TvContract.Programs}.
 */
public final class InternalDataUtils {
    private static final boolean DEBUG = false;
    private static final String TAG = "InternalDataUtils";

    private InternalDataUtils() {
        //do nothing
    }

    /**
     * Deserializes a byte array into objects to be stored in the Program class.
     *
     * <p> Series ID and critic scores are loaded from the bytes.
     *
     * @param bytes the bytes to be deserialized
     * @param builder the builder for the Program class
     */
    public static void deserializeInternalProviderData(byte[] bytes, Program.Builder builder) {
        if (bytes == null || bytes.length == 0) {
            return;
        }
        try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
            builder.setSeriesId((String) in.readObject());
            builder.setCriticScores((List<CriticScore>) in.readObject());
        } catch (NullPointerException e) {
            Log.e(TAG, "no bytes to deserialize");
        } catch (IOException e) {
            Log.e(TAG, "Could not deserialize internal provider contents");
        } catch (ClassNotFoundException e) {
            Log.e(TAG, "class not found in internal provider contents");
        }
    }

    /**
     * Convenience method for converting relevant data in Program class to a serialized blob type
     * for storage in internal_provider_data field.
     * @param program the program which contains the objects to be serialized
     * @return serialized blob-type data
     */
    @Nullable
    public static byte[] serializeInternalProviderData(Program program) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try (ObjectOutputStream out = new ObjectOutputStream(bos)) {
            if (!TextUtils.isEmpty(program.getSeriesId()) || program.getCriticScores() != null) {
                out.writeObject(program.getSeriesId());
                out.writeObject(program.getCriticScores());
                return bos.toByteArray();
            }
        } catch (IOException e) {
            Log.e(TAG, "Could not serialize internal provider contents for program: "
                    + program.getTitle());
        }
        return null;
    }

    /**
     * Deserializes a byte array into objects to be stored in the RecordedProgram class.
     *
     * <p> Series ID is loaded from the bytes.
     *
     * @param bytes the bytes to be deserialized
     * @param builder the builder for the RecordedProgram class
     */
    public static void deserializeInternalProviderData(byte[] bytes,
            RecordedProgram.Builder builder) {
        if (bytes == null || bytes.length == 0) {
            return;
        }
        try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
            builder.setSeriesId((String) in.readObject());
        } catch (NullPointerException e) {
            Log.e(TAG, "no bytes to deserialize");
        } catch (IOException e) {
            Log.e(TAG, "Could not deserialize internal provider contents");
        } catch (ClassNotFoundException e) {
            Log.e(TAG, "class not found in internal provider contents");
        }
    }

    /**
     * Serializes relevant objects in {@link android.media.tv.TvContract.Programs} to byte array.
     * @return the serialized byte array
     */
    public static byte[] serializeInternalProviderData(RecordedProgram program) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try (ObjectOutputStream out = new ObjectOutputStream(bos)) {
            if (!TextUtils.isEmpty(program.getSeriesId())) {
                out.writeObject(program.getSeriesId());
                return bos.toByteArray();
            }
        } catch (IOException e) {
            Log.e(TAG, "Could not serialize internal provider contents for program: "
                    + program.getTitle());
        }
        return null;
    }
}