/
AdvancedOptions.java
283 lines (246 loc) · 8.58 KB
/
AdvancedOptions.java
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package news.androidtv.tvapprepo.model;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Looper;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
import com.bumptech.glide.Glide;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.util.concurrent.ExecutionException;
import news.androidtv.tvapprepo.utils.ShortcutPostTask;
/**
* Created by Nick on 3/20/2017. A model for storing advanced options in generating shortcuts.
*/
public class AdvancedOptions implements Parcelable {
private static final String TAG = AdvancedOptions.class.getSimpleName();
private volatile int mReady = 0;
private String mCategory = "";
private String mIconUrl = "";
private String mBannerUrl = "";
private String mIntentUri = "";
private String mCustomLabel = "";
private boolean mUnique = false;
private byte[] mIconData = null;
private byte[] mBannerData = null;
private Context mContext = null;
public AdvancedOptions(Context context) {
if (context == null) {
throw new NullPointerException("Context cannot be null!");
}
mContext = context;
}
public AdvancedOptions setBannerUrl(String bannerUrl) {
if (bannerUrl == null || bannerUrl.isEmpty()) {
// Exit early.
return this;
}
mReady++;
mBannerUrl = bannerUrl;
// Download from Glide.
downloadBanner(mContext, bannerUrl, new GlideCallback() {
@Override
public void onDone(byte[] binaryData) {
mBannerData = binaryData;
mReady--;
}
});
return this;
}
public AdvancedOptions setIsGame(boolean isGame) {
mCategory = (isGame) ? ShortcutPostTask.CATEGORY_GAMES : ShortcutPostTask.CATEGORY_APPS;
return this;
}
public AdvancedOptions setIntentUri(String intentUri) {
if (intentUri.length() < 20 || intentUri.length() > 300) {
throw new StringLengthException(intentUri);
}
mIntentUri = intentUri;
return this;
}
public AdvancedOptions setUniquePackageName(boolean isUnique) {
mUnique = isUnique;
return this;
}
public AdvancedOptions setCustomLabel(String label) {
mCustomLabel = label;
return this;
}
public AdvancedOptions setIconUrl(String iconUrl) {
if (iconUrl == null || iconUrl.isEmpty()) {
// Exit early.
return this;
}
mReady++;
mIconUrl = iconUrl;
// Download from Glide.
downloadBanner(mContext, iconUrl, new GlideCallback() {
@Override
public void onDone(byte[] binaryData) {
mIconData = binaryData;
mReady--;
}
});
return this;
}
public AdvancedOptions setBannerBitmap(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] results = stream.toByteArray();
mBannerData = results;
return this;
}
public AdvancedOptions setIconBitmap(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] results = stream.toByteArray();
mIconData = results;
return this;
}
public void updateContext(Context context) {
// Swap out contexts.
this.mContext = context;
}
public boolean isReady() {
return mReady == 0;
}
public byte[] getIcon() {
return mIconData;
}
public byte[] getBanner() {
return mBannerData;
}
public String getBannerUrl() {
return mBannerUrl;
}
public String getCategory() {
return mCategory;
}
public String getIntentUri() {
return mIntentUri;
}
public String getCustomLabel() {
return mCustomLabel;
}
public boolean isUnique() {
return mUnique;
}
public String getIconUrl() {
return mIconUrl;
}
private void downloadBanner(final Context context, final String url, final GlideCallback callback) {
new Thread(new Runnable() {
@Override
public void run() {
try {
if (context == null) {
throw new NullPointerException("Context is null");
}
Bitmap bitmap = Glide.with(context).load(url).asBitmap().into(320, 180).get();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
final byte[] results = stream.toByteArray();
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
callback.onDone(results);
}
});
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}).start();
}
@Override
public String toString() {
return "Name=" + mCustomLabel + ", category=" + mCategory + ", iconUrl=" + mIconUrl +
", bannerUrl=" + mBannerUrl + ", iconData=" + (mIconData != null) +
", bannerData=" + (mBannerData != null);
}
public String serialize() {
JSONObject object = new JSONObject();
try {
object.put("customLabel", mCustomLabel);
object.put("category", mCategory);
object.put("iconUrl", mIconUrl);
object.put("bannerUrl", mBannerUrl);
object.put("isUnique", mUnique);
object.put("intentUri", mIntentUri);
} catch (JSONException e) {
e.printStackTrace();
}
return object.toString();
}
public static AdvancedOptions deserialize(Activity activity, String serialization) {
AdvancedOptions options = new AdvancedOptions(activity);
try {
JSONObject object = new JSONObject(serialization);
options.setCustomLabel(object.optString("customLabel"));
options.mCategory = object.optString("category");
options.setIconUrl(object.optString("iconUrl"));
options.setBannerUrl(object.optString("bannerUrl"));
options.setUniquePackageName(object.optBoolean("isUnique"));
options.setIntentUri(object.optString("intentUri"));
} catch (JSONException e) {
e.printStackTrace();
}
return options;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mCustomLabel);
dest.writeString(mCategory);
dest.writeString(mIconUrl);
dest.writeString(mBannerUrl);
dest.writeByte((byte) (mUnique ? 1 : 0));
dest.writeString(mIntentUri);
if (mIconData == null) {
mIconData = new byte[0];
}
if (mBannerData == null) {
mBannerData = new byte[0];
}
dest.writeInt(mIconData.length);
dest.writeByteArray(mIconData);
dest.writeInt(mBannerData.length);
dest.writeByteArray(mBannerData);
}
public static final Parcelable.Creator<AdvancedOptions> CREATOR
= new Parcelable.Creator<AdvancedOptions>() {
public AdvancedOptions createFromParcel(Parcel in) {
return new AdvancedOptions(in);
}
public AdvancedOptions[] newArray(int size) {
return new AdvancedOptions[size];
}
};
private AdvancedOptions(Parcel in) {
mCustomLabel = in.readString();
mCategory = in.readString();
mIconUrl = in.readString();
mBannerUrl = in.readString();
mUnique = in.readByte() == 1;
mIntentUri = in.readString();
mIconData = new byte[in.readInt()];
in.readByteArray(mIconData);
mBannerData = new byte[in.readInt()];
in.readByteArray(mBannerData);
}
private interface GlideCallback {
void onDone(byte[] binaryData);
}
public class StringLengthException extends RuntimeException {
public StringLengthException(String string) {
super("Intent URI length must be between 20 and 300 characters: " + string);
}
}
}