Skip to content

Commit

Permalink
Reimplements AdvancedOptions as Parcelable
Browse files Browse the repository at this point in the history
  • Loading branch information
Fleker committed Apr 25, 2017
1 parent 7278c46 commit 965be8d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 = "";
Expand Down Expand Up @@ -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<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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 965be8d

Please sign in to comment.