From 965be8d98c75011067978a74e7be692c5c3fdec3 Mon Sep 17 00:00:00 2001 From: Nick Date: Mon, 24 Apr 2017 23:15:09 -0400 Subject: [PATCH] Reimplements AdvancedOptions as Parcelable --- .../activities/AdvancedShortcutActivity.java | 3 +- .../tvapprepo/model/AdvancedOptions.java | 53 +++++++++++- .../ui/ShortcutGeneratorDialogs.java | 4 + .../utils/GenerateShortcutHelper.java | 82 +------------------ 4 files changed, 58 insertions(+), 84 deletions(-) diff --git a/app/src/main/java/news/androidtv/tvapprepo/activities/AdvancedShortcutActivity.java b/app/src/main/java/news/androidtv/tvapprepo/activities/AdvancedShortcutActivity.java index c1ef026..9b87316 100644 --- a/app/src/main/java/news/androidtv/tvapprepo/activities/AdvancedShortcutActivity.java +++ b/app/src/main/java/news/androidtv/tvapprepo/activities/AdvancedShortcutActivity.java @@ -79,8 +79,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { resolveInfo = getIntent().getParcelableExtra(EXTRA_RESOLVE_INFO); } if (getIntent().hasExtra(EXTRA_ADVANCED_OPTIONS)) { - advancedOptions = AdvancedOptions.deserialize(this, - getIntent().getStringExtra(EXTRA_ADVANCED_OPTIONS)); + advancedOptions = getIntent().getParcelableExtra(EXTRA_ADVANCED_OPTIONS); } if (advancedOptions == null) { diff --git a/app/src/main/java/news/androidtv/tvapprepo/model/AdvancedOptions.java b/app/src/main/java/news/androidtv/tvapprepo/model/AdvancedOptions.java index 9924af1..50e935f 100644 --- a/app/src/main/java/news/androidtv/tvapprepo/model/AdvancedOptions.java +++ b/app/src/main/java/news/androidtv/tvapprepo/model/AdvancedOptions.java @@ -5,6 +5,8 @@ import android.graphics.Bitmap; import android.os.Handler; import android.os.Looper; +import android.os.Parcel; +import android.os.Parcelable; import com.bumptech.glide.Glide; @@ -19,7 +21,7 @@ /** * Created by Nick on 3/20/2017. A model for storing advanced options in generating shortcuts. */ -public class AdvancedOptions { +public class AdvancedOptions implements Parcelable { private volatile int mReady = 0; private String mCategory = ""; private String mIconUrl = ""; @@ -206,6 +208,55 @@ public static AdvancedOptions deserialize(Activity activity, String serializatio 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 CREATOR + = new Parcelable.Creator() { + 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); } diff --git a/app/src/main/java/news/androidtv/tvapprepo/ui/ShortcutGeneratorDialogs.java b/app/src/main/java/news/androidtv/tvapprepo/ui/ShortcutGeneratorDialogs.java index e5d164e..5f915cd 100644 --- a/app/src/main/java/news/androidtv/tvapprepo/ui/ShortcutGeneratorDialogs.java +++ b/app/src/main/java/news/androidtv/tvapprepo/ui/ShortcutGeneratorDialogs.java @@ -3,6 +3,7 @@ import android.app.Activity; import android.content.Intent; import android.support.annotation.NonNull; +import android.util.Log; import android.view.ContextThemeWrapper; import android.widget.EditText; import android.widget.Toast; @@ -20,6 +21,8 @@ */ public class ShortcutGeneratorDialogs { + private static final String TAG = ShortcutGeneratorDialogs.class.getSimpleName(); + public static void initWebBookmarkDialog(final Activity activity) { new MaterialDialog.Builder(new ContextThemeWrapper(activity, R.style.dialog_theme)) .title(R.string.generate_web_bookmark) @@ -32,6 +35,7 @@ public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) tag = "http://" + tag; } String label = tag.replaceAll("(http://)|(https://)", ""); + Log.d(TAG, IntentUriGenerator.generateWebBookmark(tag)); AdvancedOptions options = new AdvancedOptions(activity) .setIntentUri(IntentUriGenerator.generateWebBookmark(tag)) .setIconUrl("https://raw.githubusercontent.com/ITVlab/TvAppRepo/master/promo/graphics/icon.png") // TODO Replace icon url diff --git a/app/src/main/java/news/androidtv/tvapprepo/utils/GenerateShortcutHelper.java b/app/src/main/java/news/androidtv/tvapprepo/utils/GenerateShortcutHelper.java index 3004848..b71c367 100644 --- a/app/src/main/java/news/androidtv/tvapprepo/utils/GenerateShortcutHelper.java +++ b/app/src/main/java/news/androidtv/tvapprepo/utils/GenerateShortcutHelper.java @@ -97,90 +97,10 @@ private static void openAdvancedOptions(final Activity activity, final ResolveIn Intent editorPanel = new Intent(activity, AdvancedShortcutActivity.class); editorPanel.putExtra(AdvancedShortcutActivity.EXTRA_RESOLVE_INFO, resolveInfo); if (options != null) { - editorPanel.putExtra(AdvancedShortcutActivity.EXTRA_ADVANCED_OPTIONS, options.serialize()); + editorPanel.putExtra(AdvancedShortcutActivity.EXTRA_ADVANCED_OPTIONS, options); } activity.startActivity(editorPanel); } - private static void openAdvancedOptions2(final Activity activity, final ResolveInfo resolveInfo, AdvancedOptions options) { - final AlertDialog dialog = new AlertDialog.Builder(new ContextThemeWrapper(activity, R.style.dialog_theme)) - .setTitle(R.string.advanced_options) - .setView(R.layout.dialog_app_shortcut_editor) - .setNegativeButton(R.string.cancel, null) - .create(); - - if (options == null) { - options = new AdvancedOptions(activity); - } - final AdvancedOptions[] finalOptions = {options}; - dialog.setButton(Dialog.BUTTON_POSITIVE, - activity.getString(R.string.create_shortcut), - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int which) { - View editor = dialog.getWindow().getDecorView(); - boolean isGame = ((Switch) editor.findViewById(R.id.switch_isgame)).isChecked(); - finalOptions[0].setIsGame(isGame); - generateShortcut(activity, resolveInfo, finalOptions[0]); - } - }); - dialog.setOnShowListener(new DialogInterface.OnShowListener() { - @Override - public void onShow(DialogInterface dialogInterface) { - View layout = dialog.getWindow().getDecorView(); - layout.findViewById(R.id.change_banner).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - // Lookup for the app icon - if (resolveInfo != null) { - IconsTask.getIconsForComponentName(activity, - new ComponentName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name), - new IconsTask.IconsReceivedCallback() { - @Override - public void onIcons(PackedIcon[] icons) { - Log.d(TAG, icons.length + "<<<"); - // Show dialog of all icons for the user to select (or let them do their own) - final AlertDialog iconDialog = new AlertDialog.Builder(new ContextThemeWrapper(activity, R.style.dialog_theme)) - .setTitle("Select Custom Iconography") - .setView(R.layout.dialog_custom_iconography) - .create(); - iconDialog.setButton(AlertDialog.BUTTON_POSITIVE, activity.getString(R.string.ok), new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - String bannerUrl = - ((EditText) iconDialog.getWindow().getDecorView().findViewById(R.id.edit_banner)).getText().toString(); - finalOptions[0].setBannerUrl(bannerUrl); - } - }); - iconDialog.show(); - LinearLayout iconDialogLayout = (LinearLayout) iconDialog.getWindow().getDecorView().findViewById(R.id.icon_list); - for (final PackedIcon icon : icons) { - ImageButton imageButton = new ImageButton(activity); - imageButton.setImageDrawable(icon.icon); - imageButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - if (icon.isBanner) { - finalOptions[0].setBannerBitmap(icon.getBitmap()); - } else { - finalOptions[0].setIconBitmap(icon.getBitmap()); - } - Log.d(TAG, finalOptions[0].toString()); - iconDialog.dismiss(); - } - }); - iconDialogLayout.addView(imageButton); - } - } - }); - } else { - Toast.makeText(activity, "Cannot set banner of non-app yet", Toast.LENGTH_SHORT).show(); - } - } - }); - } - }); - dialog.show(); - } private static void downloadShortcutApk(Activity activity, NetworkResponse response, Object item) { JSONObject data = null;