1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.tv.data;
18
19import android.support.annotation.Nullable;
20import android.text.TextUtils;
21import android.util.Log;
22
23import com.android.tv.data.Program.CriticScore;
24import com.android.tv.dvr.data.RecordedProgram;
25
26import java.io.ByteArrayInputStream;
27import java.io.ByteArrayOutputStream;
28import java.io.IOException;
29import java.io.ObjectInputStream;
30import java.io.ObjectOutputStream;
31import java.util.List;
32
33/**
34 * A utility class to parse and store data from the
35 * {@link android.media.tv.TvContract.Programs#COLUMN_INTERNAL_PROVIDER_DATA} field in the
36 * {@link android.media.tv.TvContract.Programs}.
37 */
38public final class InternalDataUtils {
39    private static final boolean DEBUG = false;
40    private static final String TAG = "InternalDataUtils";
41
42    private InternalDataUtils() {
43        //do nothing
44    }
45
46    /**
47     * Deserializes a byte array into objects to be stored in the Program class.
48     *
49     * <p> Series ID and critic scores are loaded from the bytes.
50     *
51     * @param bytes the bytes to be deserialized
52     * @param builder the builder for the Program class
53     */
54    public static void deserializeInternalProviderData(byte[] bytes, Program.Builder builder) {
55        if (bytes == null || bytes.length == 0) {
56            return;
57        }
58        try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
59            builder.setSeriesId((String) in.readObject());
60            builder.setCriticScores((List<CriticScore>) in.readObject());
61        } catch (NullPointerException e) {
62            Log.e(TAG, "no bytes to deserialize");
63        } catch (IOException e) {
64            Log.e(TAG, "Could not deserialize internal provider contents");
65        } catch (ClassNotFoundException e) {
66            Log.e(TAG, "class not found in internal provider contents");
67        }
68    }
69
70    /**
71     * Convenience method for converting relevant data in Program class to a serialized blob type
72     * for storage in internal_provider_data field.
73     * @param program the program which contains the objects to be serialized
74     * @return serialized blob-type data
75     */
76    @Nullable
77    public static byte[] serializeInternalProviderData(Program program) {
78        ByteArrayOutputStream bos = new ByteArrayOutputStream();
79        try (ObjectOutputStream out = new ObjectOutputStream(bos)) {
80            if (!TextUtils.isEmpty(program.getSeriesId()) || program.getCriticScores() != null) {
81                out.writeObject(program.getSeriesId());
82                out.writeObject(program.getCriticScores());
83                return bos.toByteArray();
84            }
85        } catch (IOException e) {
86            Log.e(TAG, "Could not serialize internal provider contents for program: "
87                    + program.getTitle());
88        }
89        return null;
90    }
91
92    /**
93     * Deserializes a byte array into objects to be stored in the RecordedProgram class.
94     *
95     * <p> Series ID is loaded from the bytes.
96     *
97     * @param bytes the bytes to be deserialized
98     * @param builder the builder for the RecordedProgram class
99     */
100    public static void deserializeInternalProviderData(byte[] bytes,
101            RecordedProgram.Builder builder) {
102        if (bytes == null || bytes.length == 0) {
103            return;
104        }
105        try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
106            builder.setSeriesId((String) in.readObject());
107        } catch (NullPointerException e) {
108            Log.e(TAG, "no bytes to deserialize");
109        } catch (IOException e) {
110            Log.e(TAG, "Could not deserialize internal provider contents");
111        } catch (ClassNotFoundException e) {
112            Log.e(TAG, "class not found in internal provider contents");
113        }
114    }
115
116    /**
117     * Serializes relevant objects in {@link android.media.tv.TvContract.Programs} to byte array.
118     * @return the serialized byte array
119     */
120    public static byte[] serializeInternalProviderData(RecordedProgram program) {
121        ByteArrayOutputStream bos = new ByteArrayOutputStream();
122        try (ObjectOutputStream out = new ObjectOutputStream(bos)) {
123            if (!TextUtils.isEmpty(program.getSeriesId())) {
124                out.writeObject(program.getSeriesId());
125                return bos.toByteArray();
126            }
127        } catch (IOException e) {
128            Log.e(TAG, "Could not serialize internal provider contents for program: "
129                    + program.getTitle());
130        }
131        return null;
132    }
133}