InternalDataUtils.java revision 944779887775bd950cf1abf348d2df461593f6ab
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;
22import com.android.tv.data.Program.CriticScore;
23import com.android.tv.dvr.data.RecordedProgram;
24import java.io.ByteArrayInputStream;
25import java.io.ByteArrayOutputStream;
26import java.io.IOException;
27import java.io.ObjectInputStream;
28import java.io.ObjectOutputStream;
29import java.util.List;
30
31/**
32 * A utility class to parse and store data from the {@link
33 * android.media.tv.TvContract.Programs#COLUMN_INTERNAL_PROVIDER_DATA} field in the {@link
34 * android.media.tv.TvContract.Programs}.
35 */
36@SuppressWarnings("TryWithResources") // TODO(b/62143348): remove when error prone check fixed
37public final class InternalDataUtils {
38    private static final boolean DEBUG = false;
39    private static final String TAG = "InternalDataUtils";
40
41    private InternalDataUtils() {
42        // do nothing
43    }
44
45    /**
46     * Deserializes a byte array into objects to be stored in the Program class.
47     *
48     * <p>Series ID and critic scores are loaded from the bytes.
49     *
50     * @param bytes the bytes to be deserialized
51     * @param builder the builder for the Program class
52     */
53    public static void deserializeInternalProviderData(byte[] bytes, Program.Builder builder) {
54        if (bytes == null || bytes.length == 0) {
55            return;
56        }
57        try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
58            builder.setSeriesId((String) in.readObject());
59            builder.setCriticScores((List<CriticScore>) in.readObject());
60        } catch (NullPointerException e) {
61            Log.e(TAG, "no bytes to deserialize");
62        } catch (IOException e) {
63            Log.e(TAG, "Could not deserialize internal provider contents");
64        } catch (ClassNotFoundException e) {
65            Log.e(TAG, "class not found in internal provider contents");
66        }
67    }
68
69    /**
70     * Convenience method for converting relevant data in Program class to a serialized blob type
71     * for storage in internal_provider_data field.
72     *
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(
87                    TAG,
88                    "Could not serialize internal provider contents for program: "
89                            + program.getTitle());
90        }
91        return null;
92    }
93
94    /**
95     * Deserializes a byte array into objects to be stored in the RecordedProgram class.
96     *
97     * <p>Series ID is loaded from the bytes.
98     *
99     * @param bytes the bytes to be deserialized
100     * @param builder the builder for the RecordedProgram class
101     */
102    public static void deserializeInternalProviderData(
103            byte[] bytes, RecordedProgram.Builder builder) {
104        if (bytes == null || bytes.length == 0) {
105            return;
106        }
107        try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
108            builder.setSeriesId((String) in.readObject());
109        } catch (NullPointerException e) {
110            Log.e(TAG, "no bytes to deserialize");
111        } catch (IOException e) {
112            Log.e(TAG, "Could not deserialize internal provider contents");
113        } catch (ClassNotFoundException e) {
114            Log.e(TAG, "class not found in internal provider contents");
115        }
116    }
117
118    /**
119     * Serializes relevant objects in {@link android.media.tv.TvContract.Programs} to byte array.
120     *
121     * @return the serialized byte array
122     */
123    public static byte[] serializeInternalProviderData(RecordedProgram program) {
124        ByteArrayOutputStream bos = new ByteArrayOutputStream();
125        try (ObjectOutputStream out = new ObjectOutputStream(bos)) {
126            if (!TextUtils.isEmpty(program.getSeriesId())) {
127                out.writeObject(program.getSeriesId());
128                return bos.toByteArray();
129            }
130        } catch (IOException e) {
131            Log.e(
132                    TAG,
133                    "Could not serialize internal provider contents for program: "
134                            + program.getTitle());
135        }
136        return null;
137    }
138}
139