Java tutorial
/* * Copyright [2017] [zhi] * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.zhi.android.modules.welcome; import android.content.Context; import android.content.res.TypedArray; import android.os.Parcel; import android.os.Parcelable; import android.support.annotation.NonNull; import com.google.common.collect.Lists; import java.util.List; @SuppressWarnings("WeakerAccess") public class WelcomeOption implements Parcelable { public final boolean quitEnabled; public final boolean swipeEnabled; public final List<WelcomePageOption> pageOptions; public WelcomeOption(Context context) { final TypedArray a = context.obtainStyledAttributes(R.styleable.WelcomeOption); quitEnabled = a.getBoolean(R.styleable.WelcomeOption_welcomeQuitEnabled, true); swipeEnabled = a.getBoolean(R.styleable.WelcomeOption_welcomeSwipeEnabled, true); { final int pages = a.getResourceId(R.styleable.WelcomeOption_welcomePages, 0); final TypedArray aa = context.getResources().obtainTypedArray(pages); final int pageCount = aa.length(); pageOptions = Lists.newArrayListWithCapacity(pageCount); for (int i = 0; i < pageCount; i++) { pageOptions.add(new WelcomePageOption(context, aa.getResourceId(i, 0))); } aa.recycle(); } if (swipeEnabled) { pageOptions.add(WelcomePageOption.NOE); } a.recycle(); } private WelcomeOption(Builder builder) { quitEnabled = builder.quitEnabled; swipeEnabled = builder.swipeEnabled; pageOptions = builder.pageOptions; if (swipeEnabled) { pageOptions.add(WelcomePageOption.NOE); } } private WelcomeOption(Parcel in) { quitEnabled = in.readInt() != 0; swipeEnabled = in.readInt() != 0; pageOptions = in.createTypedArrayList(WelcomePageOption.CREATOR); if (swipeEnabled) { pageOptions.add(WelcomePageOption.NOE); } } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeInt(quitEnabled ? 1 : 0); dest.writeInt(swipeEnabled ? 1 : 0); dest.writeTypedList(pageOptions); } @Override public int describeContents() { return 0; } public static final Creator<WelcomeOption> CREATOR = new Creator<WelcomeOption>() { @Override public WelcomeOption createFromParcel(Parcel in) { return new WelcomeOption(in); } @Override public WelcomeOption[] newArray(int size) { return new WelcomeOption[size]; } }; public static Builder newBuilder() { return new Builder(); } public static final class Builder { private boolean quitEnabled; private boolean swipeEnabled; private final List<WelcomePageOption> pageOptions; private Builder() { pageOptions = Lists.newArrayList(); } public Builder quitEnabled(boolean quitEnabled) { this.quitEnabled = quitEnabled; return this; } public Builder swipeEnabled(boolean swipeEnabled) { this.swipeEnabled = swipeEnabled; return this; } public Builder pageOption(@NonNull WelcomePageOption pageOption) { pageOptions.add(pageOption); return this; } public WelcomeOption build() { return new WelcomeOption(this); } } }