/*
Swisscom Safe Connect
Copyright (C) 2014 Swisscom
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.swisscom.safeconnect.view;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.swisscom.safeconnect.R;
import com.swisscom.safeconnect.model.Subscription;
import com.swisscom.safeconnect.utils.Fonts;
import java.text.DateFormat;
import java.util.Date;
/**
* Created by vadim on 19.09.14.
*/
@SuppressWarnings("ResourceType")
public class SubscriptionStatusView extends RelativeLayout {
private TextView tvSubscrName;
private TextView tvSubscrTitle;
private ImageView imgSubscr;
private RelativeLayout container;
private Subscription subscription;
public SubscriptionStatusView(Context context, AttributeSet attrs) {
super(context, attrs);
setBackgroundResource(R.drawable.btn_filled_no_corners_bg_selector);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SubscriptionStatusView);
boolean centerHoriz = a.getBoolean(R.styleable.SubscriptionStatusView_centerHorizontally, false);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
if (centerHoriz) {
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
}
container = new RelativeLayout(context);
container.setLayoutParams(params);
a.recycle();
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
if (!centerHoriz) {
params.setMargins(18, 0, 0, 0);
}
imgSubscr = new ImageView(context, attrs);
imgSubscr.setLayoutParams(params);
imgSubscr.setId(100);
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(20, 0, 0, 0);
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
params.addRule(RelativeLayout.RIGHT_OF, imgSubscr.getId());
LinearLayout textLayout = new LinearLayout(context, attrs);
textLayout.setOrientation(LinearLayout.VERTICAL);
textLayout.setLayoutParams(params);
textLayout.setId(200);
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
tvSubscrName = new PipeTextView(context, attrs);
if (!isInEditMode()) {
tvSubscrName.setTextAppearance(context, R.style.SubscriptionName);
}
tvSubscrName.setLayoutParams(params);
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
params.setMargins(0, 0, (int) (getResources().getDisplayMetrics().density*10), 0);
TextView tvArrow = new PipeTextView(context, attrs);
tvArrow.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
tvArrow.setText(">");
tvArrow.setLayoutParams(params);
tvArrow.setId(440);
tvArrow.setTextColor(getResources().getColor(R.color.white));
tvArrow.setTypeface(Fonts.FONT_BOLD);
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
tvSubscrTitle = new PipeTextView(context, attrs);
if (!isInEditMode()) {
tvSubscrTitle.setTextAppearance(context, R.style.SubscriptionTitle);
}
tvSubscrTitle.setLayoutParams(params);
tvSubscrTitle.setText(R.string.lab_subscription_title);
tvSubscrTitle.setAllCaps(true);
textLayout.addView(tvSubscrTitle);
textLayout.addView(tvSubscrName);
container.addView(imgSubscr);
container.addView(textLayout);
addView(container);
addView(tvArrow);
tvSubscrName.setId(300);
tvSubscrTitle.setId(301);
container.setId(303);
setSubscription(null);
}
static class SavedState extends BaseSavedState {
Subscription subscription;
SavedState(Parcelable superState) {
super(superState);
}
public SavedState(Parcel source) {
super(source);
subscription = (Subscription) source.readValue(Subscription.class.getClassLoader());
}
@Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeValue(subscription);
}
public static final Parcelable.Creator<SavedState> CREATOR
= new Parcelable.Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}
@Override
protected Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
SavedState ss = new SavedState(superState);
ss.subscription = subscription;
return ss;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
if(!(state instanceof SavedState)) {
super.onRestoreInstanceState(state);
return;
}
SavedState ss = (SavedState)state;
super.onRestoreInstanceState(ss.getSuperState());
setSubscription(ss.subscription);
}
public void setSubscription(Subscription subscr) {
if (subscr == null) {
tvSubscrName.setText(R.string.lab_getting_stats);
tvSubscrName.setTextSize(17);
} else if (subscription == null || subscr.getSku() != null &&
!subscr.getSku().equals(subscription.getSku())
|| subscr.getValidTill() != subscription.getValidTill()) {
subscription = subscr;
long valid_till = subscription.getValidTill()*1000;
if (valid_till <= System.currentTimeMillis()) {
imgSubscr.setImageResource(R.drawable.ic_subscription_shoppingcart);
tvSubscrTitle.setText(R.string.lab_subscription_title);
tvSubscrName.setText(R.string.lab_no_abo);
} else {
Date date = new Date(subscr.getValidTill()*1000);
tvSubscrName.setText(getContext().getString(R.string.lab_subscription_expires_on,
DateFormat.getDateInstance().format(date)));
tvSubscrTitle.setText(R.string.lab_subscription_title);
imgSubscr.setImageResource(R.drawable.ic_subscription_network);
}
}
}
public boolean isSubscriptionValid() {
if (subscription == null) return false;
long valid_till = subscription.getValidTill()*1000;
return valid_till > System.currentTimeMillis();
}
public Subscription getCurrentSubscription() {
return subscription;
}
}