• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.tv.data;
18 
19 import android.support.annotation.Nullable;
20 import android.text.TextUtils;
21 import android.util.Log;
22 import com.android.tv.data.Program.CriticScore;
23 import com.android.tv.dvr.data.RecordedProgram;
24 import java.io.ByteArrayInputStream;
25 import java.io.ByteArrayOutputStream;
26 import java.io.IOException;
27 import java.io.ObjectInputStream;
28 import java.io.ObjectOutputStream;
29 import 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 public final class InternalDataUtils {
37     private static final boolean DEBUG = false;
38     private static final String TAG = "InternalDataUtils";
39 
InternalDataUtils()40     private InternalDataUtils() {
41         // do nothing
42     }
43 
44     /**
45      * Deserializes a byte array into objects to be stored in the Program class.
46      *
47      * <p>Series ID and critic scores are loaded from the bytes.
48      *
49      * @param bytes the bytes to be deserialized
50      * @param builder the builder for the Program class
51      */
deserializeInternalProviderData(byte[] bytes, Program.Builder builder)52     public static void deserializeInternalProviderData(byte[] bytes, Program.Builder builder) {
53         if (bytes == null || bytes.length == 0) {
54             return;
55         }
56         try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
57             builder.setSeriesId((String) in.readObject());
58             builder.setCriticScores((List<CriticScore>) in.readObject());
59         } catch (NullPointerException e) {
60             Log.e(TAG, "no bytes to deserialize");
61         } catch (IOException e) {
62             Log.e(TAG, "Could not deserialize internal provider contents");
63         } catch (ClassNotFoundException e) {
64             Log.e(TAG, "class not found in internal provider contents");
65         }
66     }
67 
68     /**
69      * Convenience method for converting relevant data in Program class to a serialized blob type
70      * for storage in internal_provider_data field.
71      *
72      * @param program the program which contains the objects to be serialized
73      * @return serialized blob-type data
74      */
75     @Nullable
serializeInternalProviderData(Program program)76     public static byte[] serializeInternalProviderData(Program program) {
77         ByteArrayOutputStream bos = new ByteArrayOutputStream();
78         try (ObjectOutputStream out = new ObjectOutputStream(bos)) {
79             if (!TextUtils.isEmpty(program.getSeriesId()) || program.getCriticScores() != null) {
80                 out.writeObject(program.getSeriesId());
81                 out.writeObject(program.getCriticScores());
82                 return bos.toByteArray();
83             }
84         } catch (IOException e) {
85             Log.e(
86                     TAG,
87                     "Could not serialize internal provider contents for program: "
88                             + program.getTitle());
89         }
90         return null;
91     }
92 
93     /**
94      * Deserializes a byte array into objects to be stored in the RecordedProgram class.
95      *
96      * <p>Series ID is loaded from the bytes.
97      *
98      * @param bytes the bytes to be deserialized
99      * @param builder the builder for the RecordedProgram class
100      */
deserializeInternalProviderData( byte[] bytes, RecordedProgram.Builder builder)101     public static void deserializeInternalProviderData(
102             byte[] bytes, RecordedProgram.Builder builder) {
103         if (bytes == null || bytes.length == 0) {
104             return;
105         }
106         try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
107             builder.setSeriesId((String) in.readObject());
108         } catch (NullPointerException e) {
109             Log.e(TAG, "no bytes to deserialize");
110         } catch (IOException e) {
111             Log.e(TAG, "Could not deserialize internal provider contents");
112         } catch (ClassNotFoundException e) {
113             Log.e(TAG, "class not found in internal provider contents");
114         }
115     }
116 
117     /**
118      * Serializes relevant objects in {@link android.media.tv.TvContract.Programs} to byte array.
119      *
120      * @return the serialized byte array
121      */
serializeInternalProviderData(RecordedProgram program)122     public static byte[] serializeInternalProviderData(RecordedProgram program) {
123         ByteArrayOutputStream bos = new ByteArrayOutputStream();
124         try (ObjectOutputStream out = new ObjectOutputStream(bos)) {
125             if (!TextUtils.isEmpty(program.getSeriesId())) {
126                 out.writeObject(program.getSeriesId());
127                 return bos.toByteArray();
128             }
129         } catch (IOException e) {
130             Log.e(
131                     TAG,
132                     "Could not serialize internal provider contents for program: "
133                             + program.getTitle());
134         }
135         return null;
136     }
137 }
138